diff --git a/lib/build/bundle.js b/lib/build/bundle.js index 62eec2a52..3bcf2ff49 100644 --- a/lib/build/bundle.js +++ b/lib/build/bundle.js @@ -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()); @@ -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; @@ -414,11 +418,11 @@ 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); } @@ -426,8 +430,14 @@ exports.Bundle = class { 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)}`); });