From 403bfa4caf52225a3e578bcd862fcffc153ecf71 Mon Sep 17 00:00:00 2001 From: Erik Barke Date: Tue, 30 Jan 2018 11:19:24 +0100 Subject: [PATCH] Move the source map comment to the last line, #245 --- package.json | 2 +- src/bundler/bundle-item.ts | 4 +++- src/bundler/bundler.ts | 14 ++++++++++++-- src/bundler/globals.ts | 5 +++-- src/bundler/source-map.ts | 16 ++++++++++------ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index b3d3383f..42aaa132 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/bundler/bundle-item.ts b/src/bundler/bundle-item.ts index 3bd08623..f766b705 100644 --- a/src/bundler/bundle-item.ts +++ b/src/bundler/bundle-item.ts @@ -1,3 +1,4 @@ +import * as convertSourceMap from "convert-source-map"; import * as ESTree from "estree"; export class BundleItem { @@ -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) !== "/"; diff --git a/src/bundler/bundler.ts b/src/bundler/bundler.ts index 098305aa..c67068d7 100644 --- a/src/bundler/bundler.ts +++ b/src/bundler/bundler.ts @@ -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); @@ -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() { diff --git a/src/bundler/globals.ts b/src/bundler/globals.ts index c828355e..4cb3e7fc 100644 --- a/src/bundler/globals.ts +++ b/src/bundler/globals.ts @@ -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") ]) @@ -60,7 +61,7 @@ export class Globals { }); if (source) { - items.push(new BundleItem(name, name, source, [])); + items.push(new BundleItem(name, name, source, undefined, [])); } } } diff --git a/src/bundler/source-map.ts b/src/bundler/source-map.ts index 7c8cc610..c8bdfd9b 100644 --- a/src/bundler/source-map.ts +++ b/src/bundler/source-map.ts @@ -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) {