Skip to content

Commit

Permalink
Move the source map comment to the last line, #245
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Barke authored and Erik Barke committed Jan 30, 2018
1 parent b8c8566 commit 403bfa4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
"punycode": "^1.4.1",
"querystring-es3": "^0.2.1",
"readable-stream": "^2.3.3",
"remap-istanbul": "^0.10.1",
"source-map": "0.6.1",
"remap-istanbul": "0.8.4",
"stream-browserify": "^2.0.1",
"stream-http": "^2.7.2",
"string_decoder": "^1.0.3",
Expand Down
4 changes: 3 additions & 1 deletion src/bundler/bundle-item.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as convertSourceMap from "convert-source-map";
import * as ESTree from "estree";

export class BundleItem {
Expand All @@ -7,7 +8,8 @@ export class BundleItem {
public transformedScript = false;

constructor(public moduleName: string, public filename?: string,
public source?: string, public dependencies: BundleItem[] = []) {}
public source?: string, public sourceMap?: convertSourceMap.SourceMapConverter,
public dependencies: BundleItem[] = []) {}

public isNpmModule(): boolean {
return this.moduleName.charAt(0) !== "." && this.moduleName.charAt(0) !== "/";
Expand Down
14 changes: 12 additions & 2 deletions src/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,17 @@ export class Bundler {

this.transformer.applyTsTransforms(this.bundleQueue, () => {
this.bundleQueue.forEach((queued) => {

let source = this.sourceMap.removeSourceMapComment(queued);
let map = this.sourceMap.getSourceMap(queued);

if (map) {
// used by Karma to log errors with original source code line numbers
queued.file.sourceMap = map.toObject();
}

queued.item = new BundleItem(
queued.file.path, queued.file.originalPath, this.sourceMap.createInlineSourceMap(queued));
queued.file.path, queued.file.originalPath, source, map);
});

let dependencyCount = this.dependencyWalker.collectTypescriptDependencies(this.bundleQueue);
Expand Down Expand Up @@ -176,7 +185,8 @@ export class Bundler {
"\n},'" +
PathTool.fixWindowsPath(moduleId) + "'," +
PathTool.fixWindowsPath(JSON.stringify(dependencyMap)) + "];" +
(standalone ? "})(this);" : "") + "\n";
(standalone ? "})(this);" : "") + "\n" +
(bundleItem.sourceMap ? bundleItem.sourceMap.toComment() + "\n" : "");
}

private createEntrypointFilenames() {
Expand Down
5 changes: 3 additions & 2 deletions src/bundler/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class Globals {

items.push(new BundleItem(name, name,
os.EOL + "global.process=require('_process');" +
os.EOL + "global.Buffer=require('buffer').Buffer;", [
os.EOL + "global.Buffer=require('buffer').Buffer;",
undefined, [
new BundleItem("_process"),
new BundleItem("buffer")
])
Expand All @@ -60,7 +61,7 @@ export class Globals {
});

if (source) {
items.push(new BundleItem(name, name, source, []));
items.push(new BundleItem(name, name, source, undefined, []));
}
}
}
16 changes: 10 additions & 6 deletions src/bundler/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ export class SourceMap {
this.line = this.getNumberOfNewlines(bundle);
}

public createInlineSourceMap(queued: Queued): string {
let inlined = queued.emitOutput.outputText;
public removeSourceMapComment(queued: Queued): string {
return queued.emitOutput.sourceMapText ?
combineSourceMap.removeComments(queued.emitOutput.outputText) :
queued.emitOutput.outputText;
}

public getSourceMap(queued: Queued): convertSourceMap.SourceMapConverter {
if (queued.emitOutput.sourceMapText) {

let map = convertSourceMap.fromJSON(queued.emitOutput.sourceMapText);
if (!map.getProperty("sourcesContent")) {
map.addProperty("sourcesContent", [queued.emitOutput.sourceFile.text]);
}
inlined = combineSourceMap.removeComments(queued.emitOutput.outputText) + map.toComment();

// used by Karma to log errors with original source code line numbers
queued.file.sourceMap = map.toObject();
return map;
}
return inlined;

return undefined;
}

public addFile(bundleItem: BundleItem) {
Expand Down

0 comments on commit 403bfa4

Please sign in to comment.