Skip to content

Commit 5e25fd4

Browse files
filipesilvahansl
authored andcommitted
fix(@angular-devkit/build-optimizer): fix sourcemap chaining
1 parent bef1b58 commit 5e25fd4

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

packages/angular_devkit/build_optimizer/src/build-optimizer/build-optimizer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function isKnownSideEffectFree(filePath: string) {
6262

6363
export interface BuildOptimizerOptions {
6464
content?: string;
65+
originalFilePath?: string;
6566
inputFilePath?: string;
6667
outputFilePath?: string;
6768
emitSourceMap?: boolean;
@@ -72,7 +73,11 @@ export interface BuildOptimizerOptions {
7273
export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascriptOutput {
7374

7475
const { inputFilePath } = options;
75-
let { content } = options;
76+
let { originalFilePath, content } = options;
77+
78+
if (!originalFilePath && inputFilePath) {
79+
originalFilePath = inputFilePath;
80+
}
7681

7782
if (!inputFilePath && content === undefined) {
7883
throw new Error('Either filePath or content must be specified in options.');
@@ -97,7 +102,7 @@ export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascr
97102
getTransforms.push(getPrefixClassesTransformer);
98103
}
99104

100-
if (options.isSideEffectFree || inputFilePath && isKnownSideEffectFree(inputFilePath)) {
105+
if (options.isSideEffectFree || originalFilePath && isKnownSideEffectFree(originalFilePath)) {
101106
getTransforms.push(
102107
// getPrefixFunctionsTransformer is rather dangerous, apply only to known pure es5 modules.
103108
// It will mark both `require()` calls and `console.log(stuff)` as pure.

packages/angular_devkit/build_optimizer/src/build-optimizer/webpack-loader.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ export default function buildOptimizerLoader
2121
this.cacheable();
2222
const options: BuildOptimizerLoaderOptions = loaderUtils.getOptions(this) || {};
2323

24+
// Make up names of the intermediate files so we can chain the sourcemaps.
25+
const inputFilePath = this.resourcePath + '.pre-build-optimizer.js';
26+
const outputFilePath = this.resourcePath + '.post-build-optimizer.js';
27+
2428
const boOutput = buildOptimizer({
2529
content,
26-
inputFilePath: this.resourcePath,
27-
// Add a name to the build optimizer output.
28-
// Without a name the sourcemaps cannot be properly chained.
29-
outputFilePath: this.resourcePath + '.build-optimizer.js',
30+
originalFilePath: this.resourcePath,
31+
inputFilePath,
32+
outputFilePath,
3033
emitSourceMap: options.sourceMap,
3134
});
3235

@@ -53,10 +56,10 @@ export default function buildOptimizerLoader
5356
// source map chaining example.
5457
// Use http://sokra.github.io/source-map-visualization/ to validate sourcemaps make sense.
5558

56-
// Fill in the intermediate sourcemap sources as the previous sourcemap sources.
57-
if (previousSourceMap.sources) {
58-
intermediateSourceMap.sources = previousSourceMap.sources;
59-
}
59+
// Force the previous sourcemap to use the filename we made up for it.
60+
// In order for source maps to be chained, the consumed source map `file` needs to be in the
61+
// consumers source map `sources` array.
62+
previousSourceMap.file = inputFilePath;
6063

6164
// Chain the sourcemaps.
6265
const consumer = new SourceMapConsumer(intermediateSourceMap);

0 commit comments

Comments
 (0)