Skip to content

Commit 586181d

Browse files
committed
feat(@angular/cli): use eva source map for dev targets
1 parent d8e3d79 commit 586181d

4 files changed

Lines changed: 41 additions & 17 deletions

File tree

packages/@angular/cli/models/webpack-configs/browser.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
4444
}
4545

4646
if (buildOptions.sourcemaps) {
47-
extraPlugins.push(new webpack.SourceMapDevToolPlugin({
48-
filename: '[file].map[query]',
49-
moduleFilenameTemplate: '[resource-path]',
50-
fallbackModuleFilenameTemplate: '[resource-path]?[hash]',
51-
sourceRoot: 'webpack:///'
52-
}));
47+
// See https://webpack.js.org/configuration/devtool/ for sourcemap types.
48+
if (buildOptions.target === 'development') {
49+
// Produce eval sourcemaps for development, which are faster.
50+
extraPlugins.push(new webpack.EvalSourceMapDevToolPlugin({
51+
moduleFilenameTemplate: '[resource-path]',
52+
sourceRoot: 'webpack:///'
53+
}));
54+
} else if (buildOptions.target === 'production') {
55+
// Produce full separate sourcemaps for production.
56+
extraPlugins.push(new webpack.SourceMapDevToolPlugin({
57+
filename: '[file].map[query]',
58+
moduleFilenameTemplate: '[resource-path]',
59+
fallbackModuleFilenameTemplate: '[resource-path]?[hash]',
60+
sourceRoot: 'webpack:///'
61+
}));
62+
}
5363
}
5464

5565
if (buildOptions.commonChunk) {

packages/@angular/cli/models/webpack-configs/common.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
188188
},
189189
module: {
190190
rules: [
191-
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader', exclude: [
192-
nodeModules, /\.ngfactory\.js$/, /\.ngstyle\.js$/
193-
] },
194191
{ test: /\.html$/, loader: 'raw-loader' },
195192
{ test: /\.(eot|svg|cur)$/, loader: `file-loader?name=[name]${hashFormat.file}.[ext]` },
196193
{

packages/@ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,17 @@ export class AngularCompilerPlugin implements Tapable {
212212
this._compilerOptions.sourceMap = true;
213213
this._compilerOptions.inlineSources = true;
214214
this._compilerOptions.inlineSourceMap = false;
215-
this._compilerOptions.sourceRoot = basePath;
215+
this._compilerOptions.mapRoot = undefined;
216+
// We will set the source to the full path of the file in the loader, so we don't
217+
// need sourceRoot here.
218+
this._compilerOptions.sourceRoot = undefined;
216219
} else {
217220
this._compilerOptions.sourceMap = false;
218221
this._compilerOptions.sourceRoot = undefined;
219222
this._compilerOptions.inlineSources = undefined;
220223
this._compilerOptions.inlineSourceMap = undefined;
221224
this._compilerOptions.mapRoot = undefined;
225+
this._compilerOptions.sourceRoot = undefined;
222226
}
223227

224228
// Compose Angular Compiler Options.

packages/@ngtools/webpack/src/loader.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ interface Platform {
1414
const loaderUtils = require('loader-utils');
1515
const NormalModule = require('webpack/lib/NormalModule');
1616

17+
const sourceMappingUrlRe = /^\/\/# sourceMappingURL=[^\r\n]*/gm;
18+
1719
// This is a map of changes which need to be made
1820
const changeMap: {[key: string]: Platform} = {
1921
platformBrowserDynamic: {
@@ -544,19 +546,30 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
544546
.then(() => {
545547
timeEnd(timeLabel + '.ngcLoader.AngularCompilerPlugin');
546548
const result = plugin.getFile(sourceFileName);
549+
550+
if (result.sourceMap) {
551+
// Process sourcemaps for Webpack.
552+
// Remove the sourceMappingURL.
553+
result.outputText = result.outputText.replace(sourceMappingUrlRe, '');
554+
// Set the map source to use the full path of the file.
555+
const sourceMap = JSON.parse(result.sourceMap);
556+
sourceMap.sources[0] = sourceFileName;
557+
result.sourceMap = JSON.stringify(sourceMap);
558+
}
559+
547560
if (plugin.failedCompilation) {
548561
// Return an empty string if there is no result to prevent extra loader errors.
549562
// Plugin errors were already pushed to the compilation errors.
550563
timeEnd(timeLabel);
551564
cb(null, result.outputText || '', result.sourceMap);
552565
} else {
553566
timeEnd(timeLabel);
554-
cb(null, result.outputText, result.sourceMap);
555-
}
556-
})
557-
.catch(err => {
558-
timeEnd(timeLabel + '.ngcLoader.AngularCompilerPlugin');
559-
cb(err);
567+
cb(null, result.outputText, result.sourceMap);
568+
}
569+
})
570+
.catch(err => {
571+
timeEnd(timeLabel + '.ngcLoader.AngularCompilerPlugin');
572+
cb(err);
560573
});
561574
} else if (plugin instanceof AotPlugin) {
562575
time(timeLabel + '.ngcLoader.AotPlugin');
@@ -686,7 +699,7 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
686699

687700
const result = refactor.transpile(compilerOptions);
688701
// Webpack is going to take care of this.
689-
result.outputText = result.outputText.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
702+
result.outputText = result.outputText.replace(sourceMappingUrlRe, '');
690703
timeEnd(timeLabel);
691704
cb(null, result.outputText, result.sourceMap);
692705
}

0 commit comments

Comments
 (0)