From 5f909aa7e408b5fdbac863eab2d8e93daf820f48 Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 6 Jun 2016 20:54:48 -0700 Subject: [PATCH] fix(sourcemaps): try to improve the source maps by fixing the path (#1028) --- .../ng2/files/__path__/tsconfig.json | 1 + lib/broccoli/angular-broccoli-bundle.js | 10 ++---- lib/broccoli/broccoli-typescript.js | 32 +++++++++++++++---- tests/e2e/e2e_workflow.spec.js | 8 ++--- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json b/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json index bc7e4503c6b1..8ae99db810ac 100644 --- a/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json +++ b/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json @@ -4,6 +4,7 @@ "declaration": false, "emitDecoratorMetadata": true, "experimentalDecorators": true, + "mapRoot": "/", "module": "commonjs", "moduleResolution": "node", "noEmitOnError": true, diff --git a/lib/broccoli/angular-broccoli-bundle.js b/lib/broccoli/angular-broccoli-bundle.js index 8b61d478a9cc..b00b4426ebf6 100644 --- a/lib/broccoli/angular-broccoli-bundle.js +++ b/lib/broccoli/angular-broccoli-bundle.js @@ -16,14 +16,8 @@ class BundlePlugin extends Plugin { build() { var relativeRoot = path.relative(process.cwd(), this.inputPaths[0]); var builder = new Builder(relativeRoot, `${relativeRoot}/system-config.js`); - return builder.bundle('main - [app/**/*]', - `${this.outputPath}/main.js`, { - minify: true - }) - .then(() => builder.bundle('app - (app/**/*.js - [app/**/*.js])', - `${this.outputPath}/app/index.js`, { - minify: true - })) + + return builder.bundle('main', `${this.outputPath}/main.js`, { minify: true }) .then(() => fse.copySync(`${this.inputPaths[0]}/system-config.js`, `${this.outputPath}/system-config.js`)); } diff --git a/lib/broccoli/broccoli-typescript.js b/lib/broccoli/broccoli-typescript.js index 93b3fec5a54d..5d33086a063a 100644 --- a/lib/broccoli/broccoli-typescript.js +++ b/lib/broccoli/broccoli-typescript.js @@ -195,6 +195,7 @@ class BroccoliTypeScriptCompiler extends Plugin { _outputFile(absoluteFilePath, fileContent, registry) { absoluteFilePath = path.resolve(this.cachePath, absoluteFilePath); + let inputFilePath = absoluteFilePath; // Replace the input path by the output. absoluteFilePath = absoluteFilePath.replace(this.inputPaths[0], this.cachePath); const outputFilePath = absoluteFilePath.replace(this.cachePath, this.outputPath); @@ -204,7 +205,7 @@ class BroccoliTypeScriptCompiler extends Plugin { } fse.mkdirsSync(path.dirname(absoluteFilePath)); - const content = this.fixSourceMapSources(fileContent); + const content = this.fixSourceMapSources(fileContent, inputFilePath); fs.writeFileSync(absoluteFilePath, content, FS_OPTS); fse.mkdirsSync(path.dirname(outputFilePath)); @@ -242,12 +243,31 @@ class BroccoliTypeScriptCompiler extends Plugin { * This issue is fixed in https://github.com/Microsoft/TypeScript/pull/5620. * Once we switch to TypeScript 1.8, we can remove this method. */ - fixSourceMapSources(content) { + fixSourceMapSources(content, inputFilePath) { try { - var marker = '//# sourceMappingURL=data:application/json;base64,'; - var index = content.indexOf(marker); - if (index == -1) - return content; + const marker = '//# sourceMappingURL=data:application/json;base64,'; + + let index = content.indexOf(marker); + if (index == -1) { + const pathMarker = '//# sourceMappingURL='; + index = content.indexOf(pathMarker); + if (index == -1) { + return content; + } + + // We have a regular path, make it relative to the input path. + let base = content.substring(0, index + pathMarker.length); + let mapPath = content.substring(index + pathMarker.length); + if (mapPath.startsWith(this.outputPath)) { + mapPath = mapPath.replace(this.outputPath, this.inputPaths[0]); + } else if (!mapPath.startsWith(this.inputPaths[0])) { + mapPath = path.join(this.inputPaths[0], path.dirname(this._tsConfigPath), mapPath); + } + + mapPath = path.relative(path.dirname(inputFilePath), mapPath); + return '' + base + mapPath; + } + var base = content.substring(0, index + marker.length); var sourceMapBit = new Buffer(content.substring(index + marker.length), 'base64').toString('utf8'); var sourceMaps = JSON.parse(sourceMapBit); diff --git a/tests/e2e/e2e_workflow.spec.js b/tests/e2e/e2e_workflow.spec.js index 309264802ee4..6a579fb1a7cb 100644 --- a/tests/e2e/e2e_workflow.spec.js +++ b/tests/e2e/e2e_workflow.spec.js @@ -66,10 +66,10 @@ describe('Basic end-to-end Workflow', function () { // stuck to the first build done sh.exec(`${ngBin} build -prod`); expect(existsSync(path.join(process.cwd(), 'dist'))).to.be.equal(true); - var appBundlePath = path.join(process.cwd(), 'dist', 'app', 'index.js'); - var appBundleContent = fs.readFileSync(appBundlePath, { encoding: 'utf8' }); + var mainBundlePath = path.join(process.cwd(), 'dist', 'main.js'); + var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' }); // production: true minimized turns into production:!0 - expect(appBundleContent).to.include('production:!0'); + expect(mainBundleContent).to.include('production:!0'); // Also does not create new things in GIT. expect(sh.exec('git status --porcelain').output).to.be.equal(undefined); }); @@ -415,7 +415,7 @@ describe('Basic end-to-end Workflow', function () { expect('build failed where it should have succeeded').to.equal(''); }); }); - + it('Serve and run e2e tests after all other commands', function () { this.timeout(240000);