mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-18 11:44:05 +08:00
fix(@angular-devkit/build-optimizer): improve quality of sourcemaps
This commit is contained in:
parent
b989e4583e
commit
a1dede16ee
@ -27,6 +27,7 @@ ts_library(
|
|||||||
deps = [
|
deps = [
|
||||||
"@npm//@types/node",
|
"@npm//@types/node",
|
||||||
"@npm//@types/webpack",
|
"@npm//@types/webpack",
|
||||||
|
"@npm//@types/webpack-sources",
|
||||||
"@npm//source-map",
|
"@npm//source-map",
|
||||||
"@npm//tslib",
|
"@npm//tslib",
|
||||||
"@npm//typescript",
|
"@npm//typescript",
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
* Use of this source code is governed by an MIT-style license that can be
|
* Use of this source code is governed by an MIT-style license that can be
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map';
|
import { RawSourceMap } from 'source-map';
|
||||||
import * as webpack from 'webpack'; // tslint:disable-line:no-implicit-dependencies
|
import * as webpack from 'webpack'; // tslint:disable-line:no-implicit-dependencies
|
||||||
|
import { SourceMapSource } from 'webpack-sources';
|
||||||
const loaderUtils = require('loader-utils');
|
const loaderUtils = require('loader-utils');
|
||||||
|
|
||||||
import { buildOptimizer } from './build-optimizer';
|
import { buildOptimizer } from './build-optimizer';
|
||||||
@ -53,15 +53,11 @@ export default function buildOptimizerLoader(
|
|||||||
|
|
||||||
const options: BuildOptimizerLoaderOptions = loaderUtils.getOptions(this) || {};
|
const options: BuildOptimizerLoaderOptions = loaderUtils.getOptions(this) || {};
|
||||||
|
|
||||||
// Make up names of the intermediate files so we can chain the sourcemaps.
|
|
||||||
const inputFilePath = this.resourcePath + '.pre-build-optimizer.js';
|
|
||||||
const outputFilePath = this.resourcePath + '.post-build-optimizer.js';
|
|
||||||
|
|
||||||
const boOutput = buildOptimizer({
|
const boOutput = buildOptimizer({
|
||||||
content,
|
content,
|
||||||
originalFilePath: this.resourcePath,
|
originalFilePath: this.resourcePath,
|
||||||
inputFilePath,
|
inputFilePath: this.resourcePath,
|
||||||
outputFilePath,
|
outputFilePath: this.resourcePath,
|
||||||
emitSourceMap: options.sourceMap,
|
emitSourceMap: options.sourceMap,
|
||||||
isSideEffectFree:
|
isSideEffectFree:
|
||||||
this._module && this._module.factoryMeta && this._module.factoryMeta.sideEffectFree,
|
this._module && this._module.factoryMeta && this._module.factoryMeta.sideEffectFree,
|
||||||
@ -84,28 +80,18 @@ export default function buildOptimizerLoader(
|
|||||||
newContent = newContent.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
|
newContent = newContent.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
|
||||||
|
|
||||||
if (previousSourceMap) {
|
if (previousSourceMap) {
|
||||||
// If there's a previous sourcemap, we have to chain them.
|
|
||||||
// See https://github.com/mozilla/source-map/issues/216#issuecomment-150839869 for a simple
|
|
||||||
// source map chaining example.
|
|
||||||
// Use http://sokra.github.io/source-map-visualization/ to validate sourcemaps make sense.
|
// Use http://sokra.github.io/source-map-visualization/ to validate sourcemaps make sense.
|
||||||
|
|
||||||
// Force the previous sourcemap to use the filename we made up for it.
|
// The last argument is not yet in the typings
|
||||||
// In order for source maps to be chained, the consumed source map `file` needs to be in the
|
|
||||||
// consumers source map `sources` array.
|
|
||||||
previousSourceMap.file = inputFilePath;
|
|
||||||
|
|
||||||
// Chain the sourcemaps.
|
|
||||||
SourceMapConsumer.with(intermediateSourceMap, null, intermediate => {
|
|
||||||
return SourceMapConsumer.with(previousSourceMap, null, previous => {
|
|
||||||
const generator = SourceMapGenerator.fromSourceMap(intermediate);
|
|
||||||
generator.applySourceMap(previous);
|
|
||||||
|
|
||||||
return generator.toJSON();
|
|
||||||
});
|
|
||||||
// tslint:disable-next-line: no-any
|
// tslint:disable-next-line: no-any
|
||||||
}).then(map => callback(null, newContent, map as any), error => callback(error));
|
newSourceMap = new (SourceMapSource as any)(
|
||||||
|
newContent,
|
||||||
return;
|
this.resourcePath,
|
||||||
|
intermediateSourceMap,
|
||||||
|
content,
|
||||||
|
previousSourceMap,
|
||||||
|
true,
|
||||||
|
).map();
|
||||||
} else {
|
} else {
|
||||||
// Otherwise just return our generated sourcemap.
|
// Otherwise just return our generated sourcemap.
|
||||||
newSourceMap = intermediateSourceMap;
|
newSourceMap = intermediateSourceMap;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user