Skip to content

Commit 5f909aa

Browse files
committed
fix(sourcemaps): try to improve the source maps by fixing the path (#1028)
1 parent 81691a3 commit 5f909aa

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

addon/ng2/blueprints/ng2/files/__path__/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"declaration": false,
55
"emitDecoratorMetadata": true,
66
"experimentalDecorators": true,
7+
"mapRoot": "/",
78
"module": "commonjs",
89
"moduleResolution": "node",
910
"noEmitOnError": true,

lib/broccoli/angular-broccoli-bundle.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ class BundlePlugin extends Plugin {
1616
build() {
1717
var relativeRoot = path.relative(process.cwd(), this.inputPaths[0]);
1818
var builder = new Builder(relativeRoot, `${relativeRoot}/system-config.js`);
19-
return builder.bundle('main - [app/**/*]',
20-
`${this.outputPath}/main.js`, {
21-
minify: true
22-
})
23-
.then(() => builder.bundle('app - (app/**/*.js - [app/**/*.js])',
24-
`${this.outputPath}/app/index.js`, {
25-
minify: true
26-
}))
19+
20+
return builder.bundle('main', `${this.outputPath}/main.js`, { minify: true })
2721
.then(() => fse.copySync(`${this.inputPaths[0]}/system-config.js`,
2822
`${this.outputPath}/system-config.js`));
2923
}

lib/broccoli/broccoli-typescript.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class BroccoliTypeScriptCompiler extends Plugin {
195195

196196
_outputFile(absoluteFilePath, fileContent, registry) {
197197
absoluteFilePath = path.resolve(this.cachePath, absoluteFilePath);
198+
let inputFilePath = absoluteFilePath;
198199
// Replace the input path by the output.
199200
absoluteFilePath = absoluteFilePath.replace(this.inputPaths[0], this.cachePath);
200201
const outputFilePath = absoluteFilePath.replace(this.cachePath, this.outputPath);
@@ -204,7 +205,7 @@ class BroccoliTypeScriptCompiler extends Plugin {
204205
}
205206

206207
fse.mkdirsSync(path.dirname(absoluteFilePath));
207-
const content = this.fixSourceMapSources(fileContent);
208+
const content = this.fixSourceMapSources(fileContent, inputFilePath);
208209
fs.writeFileSync(absoluteFilePath, content, FS_OPTS);
209210

210211
fse.mkdirsSync(path.dirname(outputFilePath));
@@ -242,12 +243,31 @@ class BroccoliTypeScriptCompiler extends Plugin {
242243
* This issue is fixed in https://github.com/Microsoft/TypeScript/pull/5620.
243244
* Once we switch to TypeScript 1.8, we can remove this method.
244245
*/
245-
fixSourceMapSources(content) {
246+
fixSourceMapSources(content, inputFilePath) {
246247
try {
247-
var marker = '//# sourceMappingURL=data:application/json;base64,';
248-
var index = content.indexOf(marker);
249-
if (index == -1)
250-
return content;
248+
const marker = '//# sourceMappingURL=data:application/json;base64,';
249+
250+
let index = content.indexOf(marker);
251+
if (index == -1) {
252+
const pathMarker = '//# sourceMappingURL=';
253+
index = content.indexOf(pathMarker);
254+
if (index == -1) {
255+
return content;
256+
}
257+
258+
// We have a regular path, make it relative to the input path.
259+
let base = content.substring(0, index + pathMarker.length);
260+
let mapPath = content.substring(index + pathMarker.length);
261+
if (mapPath.startsWith(this.outputPath)) {
262+
mapPath = mapPath.replace(this.outputPath, this.inputPaths[0]);
263+
} else if (!mapPath.startsWith(this.inputPaths[0])) {
264+
mapPath = path.join(this.inputPaths[0], path.dirname(this._tsConfigPath), mapPath);
265+
}
266+
267+
mapPath = path.relative(path.dirname(inputFilePath), mapPath);
268+
return '' + base + mapPath;
269+
}
270+
251271
var base = content.substring(0, index + marker.length);
252272
var sourceMapBit = new Buffer(content.substring(index + marker.length), 'base64').toString('utf8');
253273
var sourceMaps = JSON.parse(sourceMapBit);

tests/e2e/e2e_workflow.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ describe('Basic end-to-end Workflow', function () {
6666
// stuck to the first build done
6767
sh.exec(`${ngBin} build -prod`);
6868
expect(existsSync(path.join(process.cwd(), 'dist'))).to.be.equal(true);
69-
var appBundlePath = path.join(process.cwd(), 'dist', 'app', 'index.js');
70-
var appBundleContent = fs.readFileSync(appBundlePath, { encoding: 'utf8' });
69+
var mainBundlePath = path.join(process.cwd(), 'dist', 'main.js');
70+
var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' });
7171
// production: true minimized turns into production:!0
72-
expect(appBundleContent).to.include('production:!0');
72+
expect(mainBundleContent).to.include('production:!0');
7373
// Also does not create new things in GIT.
7474
expect(sh.exec('git status --porcelain').output).to.be.equal(undefined);
7575
});
@@ -415,7 +415,7 @@ describe('Basic end-to-end Workflow', function () {
415415
expect('build failed where it should have succeeded').to.equal('');
416416
});
417417
});
418-
418+
419419
it('Serve and run e2e tests after all other commands', function () {
420420
this.timeout(240000);
421421

0 commit comments

Comments
 (0)