@@ -694,21 +694,50 @@ gulp.task(nodeServerOutFile, false, [servicesFile], () => {
694694 . pipe ( gulp . dest ( path . dirname ( nodeServerOutFile ) ) ) ;
695695} ) ;
696696
697+ import convertMap = require( "convert-source-map" ) ;
698+ import sorcery = require( "sorcery" ) ;
699+ declare module "convert-source-map" {
700+ export function fromSource ( source : string , largeSource ?: boolean ) : SourceMapConverter ;
701+ }
702+
697703gulp . task ( "browserify" , "Runs browserify on run.js to produce a file suitable for running tests in the browser" , [ servicesFile ] , ( done ) => {
698704 const testProject = tsc . createProject ( "src/harness/tsconfig.json" , getCompilerSettings ( { outFile : "built/local/bundle.js" } , /*useBuiltCompiler*/ true ) ) ;
699705 return testProject . src ( )
700706 . pipe ( newer ( "built/local/bundle.js" ) )
701707 . pipe ( sourcemaps . init ( ) )
702708 . pipe ( tsc ( testProject ) )
703709 . pipe ( through2 . obj ( ( file , enc , next ) => {
704- browserify ( intoStream ( file . contents ) )
710+ const originalMap = file . sourceMap ;
711+ const prebundledContent = file . contents . toString ( ) ;
712+ // Make paths absolute to help sorcery deal with all the terrible paths being thrown around
713+ originalMap . sources = originalMap . sources . map ( s => path . resolve ( s ) ) ;
714+ // intoStream (below) makes browserify think the input file is named this, so this is what it puts in the sourcemap
715+ originalMap . file = "built/local/_stream_0.js" ;
716+
717+ browserify ( intoStream ( file . contents ) , { debug : true } )
705718 . bundle ( ( err , res ) => {
706719 // assumes file.contents is a Buffer
707- file . contents = res ;
720+ const maps = JSON . parse ( convertMap . fromSource ( res . toString ( ) , /*largeSource*/ true ) . toJSON ( ) ) ;
721+ delete maps . sourceRoot ;
722+ maps . sources = maps . sources . map ( s => path . resolve ( s === "_stream_0.js" ? "built/local/_stream_0.js" : s ) ) ;
723+ // Strip browserify's inline comments away (could probably just let sorcery do this, but then we couldn't fix the paths)
724+ file . contents = new Buffer ( convertMap . removeComments ( res . toString ( ) ) ) ;
725+ const chain = sorcery . loadSync ( "built/local/bundle.js" , {
726+ content : {
727+ "built/local/_stream_0.js" : prebundledContent ,
728+ "built/local/bundle.js" : file . contents . toString ( )
729+ } ,
730+ sourcemaps : {
731+ "built/local/_stream_0.js" : originalMap ,
732+ "built/local/bundle.js" : maps ,
733+ }
734+ } ) ;
735+ const finalMap = chain . apply ( ) ;
736+ file . sourceMap = finalMap ;
708737 next ( undefined , file ) ;
709738 } ) ;
710739 } ) )
711- . pipe ( sourcemaps . write ( "." , { includeContent : false , sourceRoot : "../../" } ) )
740+ . pipe ( sourcemaps . write ( "." , { includeContent : false } ) )
712741 . pipe ( gulp . dest ( "." ) ) ;
713742} ) ;
714743
0 commit comments