Skip to content

Commit 761482c

Browse files
authored
Use sorcery to merge sourcemaps between browserify and gulp-typescript (microsoft#9439)
* Use sorcery to merge sourcemaps between browserify and gulp-typescript * Use shorthand * Fix nit * move comments, change loop into map * Ahahaha, we should run code before pushing it * Move conditional into call
1 parent 859bd1b commit 761482c

5 files changed

Lines changed: 40 additions & 6 deletions

File tree

Gulpfile.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
697703
gulp.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

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"devDependencies": {
3232
"@types/browserify": "latest",
33+
"@types/convert-source-map": "latest",
3334
"@types/chai": "latest",
3435
"@types/del": "latest",
3536
"@types/glob": "latest",
@@ -50,6 +51,7 @@
5051
"@types/through2": "latest",
5152
"browserify": "latest",
5253
"chai": "latest",
54+
"convert-source-map": "latest",
5355
"del": "latest",
5456
"gulp": "latest",
5557
"gulp-clone": "latest",
@@ -68,6 +70,7 @@
6870
"mocha": "latest",
6971
"mocha-fivemat-progress-reporter": "latest",
7072
"run-sequence": "latest",
73+
"sorcery": "latest",
7174
"through2": "latest",
7275
"ts-node": "latest",
7376
"tsd": "latest",

scripts/types/ambient.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ declare module "into-stream" {
1919
export function obj(content: any): NodeJS.ReadableStream
2020
}
2121
export = IntoStream;
22-
}
22+
}
23+
24+
declare module "sorcery";

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
614614

615615
const sourceMappingURL = sourceMap.getSourceMappingURL();
616616
if (sourceMappingURL) {
617-
write(`//# sourceMappingURL=${sourceMappingURL}`);
617+
write(`//# ${"sourceMappingURL"}=${sourceMappingURL}`); // Sometimes tools can sometimes see this line as a source mapping url comment
618618
}
619619

620620
writeEmittedFiles(writer.getText(), jsFilePath, sourceMapFilePath, /*writeByteOrderMark*/ compilerOptions.emitBOM, sourceFiles);

src/harness/projectsRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class ProjectRunner extends RunnerBase {
328328

329329
if (Harness.Compiler.isJS(fileName)) {
330330
// Make sure if there is URl we have it cleaned up
331-
const indexOfSourceMapUrl = data.lastIndexOf("//# sourceMappingURL=");
331+
const indexOfSourceMapUrl = data.lastIndexOf(`//# ${"sourceMappingURL"}=`); // This line can be seen as a sourceMappingURL comment
332332
if (indexOfSourceMapUrl !== -1) {
333333
data = data.substring(0, indexOfSourceMapUrl + 21) + cleanProjectUrl(data.substring(indexOfSourceMapUrl + 21));
334334
}

0 commit comments

Comments
 (0)