Skip to content

Commit

Permalink
fix(bundler): fix sourceMap source file path and sourceRoot
Browse files Browse the repository at this point in the history
The source path should be relative path, not the absolute disk file path. sourceRoot is removed in final bundle source map.
  • Loading branch information
3cp committed Apr 14, 2019
1 parent bd6fe8a commit d8a04c0
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,16 @@ exports.Bundle = class {
for (let i = 0; i < files.length; ++i) {
let currentFile = files[i];
let sourceMapEnabled = buildOptions.isApplicable('sourcemaps');
let sourceMap = sourceMapEnabled ? currentFile.sourceMap : undefined;

function fileIsDependency(file) {
return file
&& file.path
&& file.path.indexOf('node_modules') >= 0;
}
let sourceMap = sourceMapEnabled && currentFile.sourceMap ?
JSON.parse(JSON.stringify(currentFile.sourceMap)) :
undefined;
let parsedPath = currentFile.path && path.parse(currentFile.path);

function acquireSourceMapForDependency(file) {
if (!file || !file.path) {
return;
}

let parsedPath = path.parse(file.path);

try {
let base64SourceMap = Convert.fromSource(file.contents.toString());

Expand All @@ -307,30 +302,39 @@ exports.Bundle = class {
? converter.sourcemap
: null;

if (sourceMap !== null) {
let sourceRoot = parsedPath.dir.slice(process.cwd().length);
sourceMap.sourceRoot = sourceRoot.replace(/\\/g, '\/');
}

return sourceMap;
}

let content = currentFile.contents;

if (sourceMapEnabled) {
if (fileIsDependency(currentFile)) {
if (!sourceMap) {
sourceMap = acquireSourceMapForDependency(currentFile);
}

if (sourceMap) {
if (sourceMap && parsedPath) {
let sourceRoot = parsedPath.dir.slice(process.cwd().length + 1);
sourceMap.sourceRoot = sourceRoot.replace(/\\/g, '\/');
needsSourceMap = true;
content = Convert.removeMapFileComments(currentFile.contents);
}

if (sourceMap && !sourceMap.mappings) {
// Need a dummy identity map.
// Otherwise concat-with-sourcemaps is confused about source path.
sourceMap.mappings = 'AAAA';
}
}
concat.add(currentFile.path, content, sourceMap ? JSON.stringify(sourceMap) : undefined);


concat.add(
currentFile.path ? path.relative(process.cwd(), currentFile.path) : null,
content,
sourceMap ? JSON.stringify(sourceMap) : undefined
);
}

let mapContents;
let bundleMap;
let contents = concat.content;
let bundleFileName = this.config.name;

Expand Down Expand Up @@ -414,20 +418,26 @@ exports.Bundle = class {
if (minificationResult.error) throw minificationResult.error;

contents = minificationResult.code;
mapContents = needsSourceMap ? Convert.fromJSON(minificationResult.map).toJSON() : undefined;
bundleMap = needsSourceMap ? Convert.fromJSON(minificationResult.map).toObject() : undefined;
} else if (needsSourceMap) {
mapContents = Convert.fromJSON(concat.sourceMap)
bundleMap = Convert.fromJSON(concat.sourceMap)
.setProperty('sourceRoot', mapSourceRoot)
.toJSON();
.toObject();

contents += os.EOL + '//# sourceMappingURL=' + path.basename(mapFileName);
}

return fs.writeFile(path.posix.join(platform.output, bundleFileName), contents).then(() => {
this.requiresBuild = false;

if (mapContents) {
return fs.writeFile(path.posix.join(platform.output, mapFileName), mapContents)
if (bundleMap) {
const sourceRoot = bundleMap.sourceRoot;
if (sourceRoot) {
// Remove sourceRoot in order to be nicer to karma code coverage tool.
delete bundleMap.sourceRoot;
bundleMap.sources = bundleMap.sources.map(s => path.posix.join(sourceRoot, s));
}
return fs.writeFile(path.posix.join(platform.output, mapFileName), JSON.stringify(bundleMap))
.catch(e => {
logger.error(`Unable to write the sourcemap to ${path.posix.join(platform.output, mapFileName)}`);
});
Expand Down

0 comments on commit d8a04c0

Please sign in to comment.