Skip to content

Commit 1ec8790

Browse files
filipesilvaBrocco
authored andcommitted
feat(@angular/cli): use eval-source-map for serve on dev targets
1 parent ca22960 commit 1ec8790

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

packages/@angular/cli/commands/serve.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ const ServeCommand = Command.extend({
135135
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
136136
}
137137

138+
// Default evalSourcemaps to true when sourcemaps are true.
139+
// This makes rebuilds faster.
140+
if (commandOptions.sourcemaps === true) {
141+
commandOptions.evalSourcemaps = true;
142+
}
143+
138144
return checkPort(commandOptions.port, commandOptions.host, defaultPort)
139145
.then(port => {
140146
commandOptions.port = port;

packages/@angular/cli/models/build-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface BuildOptions {
44
outputPath?: string;
55
aot?: boolean;
66
sourcemaps?: boolean;
7+
evalSourcemaps?: boolean;
78
vendorChunk?: boolean;
89
commonChunk?: boolean;
910
baseHref?: string;

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

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

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

5666
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
@@ -178,9 +178,6 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
178178
},
179179
module: {
180180
rules: [
181-
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader', exclude: [
182-
nodeModules, /\.ngfactory\.js$/, /\.ngstyle\.js$/
183-
] },
184181
{ test: /\.html$/, loader: 'raw-loader' },
185182
{ test: /\.(eot|svg|cur)$/, loader: `file-loader?name=[name]${hashFormat.file}.[ext]` },
186183
{

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

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

225229
// 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)