Skip to content

Commit a1dede1

Browse files
clydinmgechev
authored andcommitted
fix(@angular-devkit/build-optimizer): improve quality of sourcemaps
1 parent b989e45 commit a1dede1

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

packages/angular_devkit/build_optimizer/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ts_library(
2727
deps = [
2828
"@npm//@types/node",
2929
"@npm//@types/webpack",
30+
"@npm//@types/webpack-sources",
3031
"@npm//source-map",
3132
"@npm//tslib",
3233
"@npm//typescript",

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

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map';
8+
import { RawSourceMap } from 'source-map';
99
import * as webpack from 'webpack'; // tslint:disable-line:no-implicit-dependencies
10-
10+
import { SourceMapSource } from 'webpack-sources';
1111
const loaderUtils = require('loader-utils');
1212

1313
import { buildOptimizer } from './build-optimizer';
@@ -53,15 +53,11 @@ export default function buildOptimizerLoader(
5353

5454
const options: BuildOptimizerLoaderOptions = loaderUtils.getOptions(this) || {};
5555

56-
// Make up names of the intermediate files so we can chain the sourcemaps.
57-
const inputFilePath = this.resourcePath + '.pre-build-optimizer.js';
58-
const outputFilePath = this.resourcePath + '.post-build-optimizer.js';
59-
6056
const boOutput = buildOptimizer({
6157
content,
6258
originalFilePath: this.resourcePath,
63-
inputFilePath,
64-
outputFilePath,
59+
inputFilePath: this.resourcePath,
60+
outputFilePath: this.resourcePath,
6561
emitSourceMap: options.sourceMap,
6662
isSideEffectFree:
6763
this._module && this._module.factoryMeta && this._module.factoryMeta.sideEffectFree,
@@ -84,28 +80,18 @@ export default function buildOptimizerLoader(
8480
newContent = newContent.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
8581

8682
if (previousSourceMap) {
87-
// If there's a previous sourcemap, we have to chain them.
88-
// See https://github.com/mozilla/source-map/issues/216#issuecomment-150839869 for a simple
89-
// source map chaining example.
9083
// Use http://sokra.github.io/source-map-visualization/ to validate sourcemaps make sense.
9184

92-
// Force the previous sourcemap to use the filename we made up for it.
93-
// In order for source maps to be chained, the consumed source map `file` needs to be in the
94-
// consumers source map `sources` array.
95-
previousSourceMap.file = inputFilePath;
96-
97-
// Chain the sourcemaps.
98-
SourceMapConsumer.with(intermediateSourceMap, null, intermediate => {
99-
return SourceMapConsumer.with(previousSourceMap, null, previous => {
100-
const generator = SourceMapGenerator.fromSourceMap(intermediate);
101-
generator.applySourceMap(previous);
102-
103-
return generator.toJSON();
104-
});
105-
// tslint:disable-next-line: no-any
106-
}).then(map => callback(null, newContent, map as any), error => callback(error));
107-
108-
return;
85+
// The last argument is not yet in the typings
86+
// tslint:disable-next-line: no-any
87+
newSourceMap = new (SourceMapSource as any)(
88+
newContent,
89+
this.resourcePath,
90+
intermediateSourceMap,
91+
content,
92+
previousSourceMap,
93+
true,
94+
).map();
10995
} else {
11096
// Otherwise just return our generated sourcemap.
11197
newSourceMap = intermediateSourceMap;

0 commit comments

Comments
 (0)