@@ -103,7 +103,7 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
103103 output . outputFiles . forEach ( o => {
104104 let destDirPath = path . dirname ( o . name ) ;
105105 fse . mkdirsSync ( destDirPath ) ;
106- fs . writeFileSync ( o . name , o . text , FS_OPTS ) ;
106+ fs . writeFileSync ( o . name , this . fixSourceMapSources ( o . text ) , FS_OPTS ) ;
107107 } ) ;
108108 }
109109 } ) ;
@@ -145,9 +145,9 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
145145
146146 private doFullBuild ( ) {
147147 let program = this . tsService . getProgram ( ) ;
148- let emitResult = program . emit ( undefined , function ( absoluteFilePath , fileContent ) {
148+ let emitResult = program . emit ( undefined , ( absoluteFilePath , fileContent ) => {
149149 fse . mkdirsSync ( path . dirname ( absoluteFilePath ) ) ;
150- fs . writeFileSync ( absoluteFilePath , fileContent , FS_OPTS ) ;
150+ fs . writeFileSync ( absoluteFilePath , this . fixSourceMapSources ( fileContent ) , FS_OPTS ) ;
151151 } ) ;
152152
153153 if ( emitResult . emitSkipped ) {
@@ -176,6 +176,33 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
176176 }
177177 }
178178
179+ /**
180+ * There is a bug in TypeScript 1.6, where the sourceRoot and inlineSourceMap properties
181+ * are exclusive. This means that the sources property always contains relative paths
182+ * (e.g, ../../../../angular2/src/di/injector.ts).
183+ *
184+ * Here, we normalize the sources property and remove the ../../../
185+ *
186+ * This issue is fixed in https://github.com/Microsoft/TypeScript/pull/5620.
187+ * Once we switch to TypeScript 1.8, we can remove this method.
188+ */
189+ private fixSourceMapSources ( content : string ) : string {
190+ try {
191+ const marker = "//# sourceMappingURL=data:application/json;base64," ;
192+ const index = content . indexOf ( marker ) ;
193+ if ( index == - 1 ) return content ;
194+
195+ const base = content . substring ( 0 , index + marker . length ) ;
196+ const sourceMapBit =
197+ new Buffer ( content . substring ( index + marker . length ) , 'base64' ) . toString ( "utf8" ) ;
198+ const sourceMaps = JSON . parse ( sourceMapBit ) ;
199+ const source = sourceMaps . sources [ 0 ] ;
200+ sourceMaps . sources = [ source . substring ( source . lastIndexOf ( "../" ) + 3 ) ] ;
201+ return `${ base } ${ new Buffer ( JSON . stringify ( sourceMaps ) ) . toString ( 'base64' ) } ` ;
202+ } catch ( e ) {
203+ return content ;
204+ }
205+ }
179206
180207 private removeOutputFor ( tsFilePath : string ) {
181208 let absoluteJsFilePath = path . join ( this . cachePath , tsFilePath . replace ( / \. t s $ / , '.js' ) ) ;
0 commit comments