Skip to content

Commit 8516d68

Browse files
alan-agius4vikerman
authored andcommitted
feat(@angular-devkit/build-angular): fine grain settings for sourceMaps (angular#13062)
* feat(@angular/cli): update schema to match new `sourceMap` * feat(@angular-devkit/build-angular): fine grain settings for sourceMaps This PR add more control over which sourceMaps you want, Now you can enable sourceMaps for scripts only, styles only or both. Also we added another functionality which are hidden sourcemaps. These are normaly used for error reporting tools. Fixes angular#7527
1 parent ddbc7f2 commit 8516d68

File tree

25 files changed

+731
-89
lines changed

25 files changed

+731
-89
lines changed

packages/angular/cli/lib/config/schema.json

+119-8
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,39 @@
633633
"default": false
634634
},
635635
"sourceMap": {
636-
"type": "boolean",
637636
"description": "Output sourcemaps.",
638-
"default": true
637+
"default": true,
638+
"oneOf": [
639+
{
640+
"type": "object",
641+
"properties": {
642+
"scripts": {
643+
"type": "boolean",
644+
"description": "Output sourcemaps for all scripts.",
645+
"default": true
646+
},
647+
"styles": {
648+
"type": "boolean",
649+
"description": "Output sourcemaps for all styles.",
650+
"default": true
651+
},
652+
"hidden": {
653+
"type": "boolean",
654+
"description": "Output sourcemaps used for error reporting tools.",
655+
"default": false
656+
},
657+
"vendor": {
658+
"type": "boolean",
659+
"description": "Resolve vendor packages sourcemaps.",
660+
"default": false
661+
}
662+
},
663+
"additionalProperties": false
664+
},
665+
{
666+
"type": "boolean"
667+
}
668+
]
639669
},
640670
"vendorSourceMap": {
641671
"type": "boolean",
@@ -1045,8 +1075,34 @@
10451075
"description": "Build using Ahead of Time compilation."
10461076
},
10471077
"sourceMap": {
1048-
"type": "boolean",
1049-
"description": "Output sourcemaps."
1078+
"description": "Output sourcemaps.",
1079+
"default": true,
1080+
"oneOf": [
1081+
{
1082+
"type": "object",
1083+
"properties": {
1084+
"scripts": {
1085+
"type": "boolean",
1086+
"description": "Output sourcemaps for all scripts.",
1087+
"default": true
1088+
},
1089+
"styles": {
1090+
"type": "boolean",
1091+
"description": "Output sourcemaps for all styles.",
1092+
"default": true
1093+
},
1094+
"vendor": {
1095+
"type": "boolean",
1096+
"description": "Resolve vendor packages sourcemaps.",
1097+
"default": false
1098+
}
1099+
},
1100+
"additionalProperties": false
1101+
},
1102+
{
1103+
"type": "boolean"
1104+
}
1105+
]
10501106
},
10511107
"vendorSourceMap": {
10521108
"type": "boolean",
@@ -1189,9 +1245,34 @@
11891245
"description": "Defines the build environment."
11901246
},
11911247
"sourceMap": {
1192-
"type": "boolean",
11931248
"description": "Output sourcemaps.",
1194-
"default": true
1249+
"default": true,
1250+
"oneOf": [
1251+
{
1252+
"type": "object",
1253+
"properties": {
1254+
"scripts": {
1255+
"type": "boolean",
1256+
"description": "Output sourcemaps for all scripts.",
1257+
"default": true
1258+
},
1259+
"styles": {
1260+
"type": "boolean",
1261+
"description": "Output sourcemaps for all styles.",
1262+
"default": true
1263+
},
1264+
"vendor": {
1265+
"type": "boolean",
1266+
"description": "Resolve vendor packages sourcemaps.",
1267+
"default": false
1268+
}
1269+
},
1270+
"additionalProperties": false
1271+
},
1272+
{
1273+
"type": "boolean"
1274+
}
1275+
]
11951276
},
11961277
"progress": {
11971278
"type": "boolean",
@@ -1455,9 +1536,39 @@
14551536
"description": "The path where style resources will be placed, relative to outputPath."
14561537
},
14571538
"sourceMap": {
1458-
"type": "boolean",
14591539
"description": "Output sourcemaps.",
1460-
"default": true
1540+
"default": true,
1541+
"oneOf": [
1542+
{
1543+
"type": "object",
1544+
"properties": {
1545+
"scripts": {
1546+
"type": "boolean",
1547+
"description": "Output sourcemaps for all scripts.",
1548+
"default": true
1549+
},
1550+
"styles": {
1551+
"type": "boolean",
1552+
"description": "Output sourcemaps for all styles.",
1553+
"default": true
1554+
},
1555+
"hidden": {
1556+
"type": "boolean",
1557+
"description": "Output sourcemaps used for error reporting tools.",
1558+
"default": false
1559+
},
1560+
"vendor": {
1561+
"type": "boolean",
1562+
"description": "Resolve vendor packages sourcemaps.",
1563+
"default": false
1564+
}
1565+
},
1566+
"additionalProperties": false
1567+
},
1568+
{
1569+
"type": "boolean"
1570+
}
1571+
]
14611572
},
14621573
"vendorSourceMap": {
14631574
"type": "boolean",

packages/angular_devkit/build_angular/src/angular-cli-files/models/build-options.ts

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export interface BuildOptions {
2424
resourcesOutputPath?: string;
2525
aot?: boolean;
2626
sourceMap?: boolean;
27+
scriptsSourceMap?: boolean;
28+
stylesSourceMap?: boolean;
29+
hiddenSourceMap?: boolean;
2730
vendorSourceMap?: boolean;
2831
evalSourceMap?: boolean;
2932
vendorChunk?: boolean;

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/browser.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as path from 'path';
1010
import { IndexHtmlWebpackPlugin } from '../../plugins/index-html-webpack-plugin';
1111
import { generateEntryPoints } from '../../utilities/package-chunk-sort';
1212
import { WebpackConfigOptions } from '../build-options';
13-
import { normalizeExtraEntryPoints } from './utils';
13+
import { getSourceMapDevTool, normalizeExtraEntryPoints } from './utils';
1414

1515
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
1616

@@ -19,16 +19,11 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
1919
const { root, buildOptions } = wco;
2020
const extraPlugins = [];
2121

22-
let sourcemaps: string | false = false;
23-
if (buildOptions.sourceMap) {
24-
// See https://webpack.js.org/configuration/devtool/ for sourcemap types.
25-
if (buildOptions.evalSourceMap && !buildOptions.optimization) {
26-
// Produce eval sourcemaps for development with serve, which are faster.
27-
sourcemaps = 'eval';
28-
} else {
29-
// Produce full separate sourcemaps for production.
30-
sourcemaps = 'source-map';
31-
}
22+
let isEval = false;
23+
// See https://webpack.js.org/configuration/devtool/ for sourcemap types.
24+
if (buildOptions.sourceMap && buildOptions.evalSourceMap && !buildOptions.optimization) {
25+
// Produce eval sourcemaps for development with serve, which are faster.
26+
isEval = true;
3227
}
3328

3429
if (buildOptions.index) {
@@ -59,11 +54,25 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
5954
}));
6055
}
6156

57+
if (!isEval && buildOptions.sourceMap) {
58+
const {
59+
scriptsSourceMap = false,
60+
stylesSourceMap = false,
61+
hiddenSourceMap = false,
62+
} = buildOptions;
63+
64+
extraPlugins.push(getSourceMapDevTool(
65+
scriptsSourceMap,
66+
stylesSourceMap,
67+
hiddenSourceMap,
68+
));
69+
}
70+
6271
const globalStylesBundleNames = normalizeExtraEntryPoints(buildOptions.styles, 'styles')
6372
.map(style => style.bundleName);
6473

6574
return {
66-
devtool: sourcemaps,
75+
devtool: isEval ? 'eval' : false,
6776
resolve: {
6877
mainFields: [
6978
...(wco.supportES2015 ? ['es2015'] : []),

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ScriptsWebpackPlugin } from '../../plugins/scripts-webpack-plugin';
1515
import { findUp } from '../../utilities/find-up';
1616
import { isDirectory } from '../../utilities/is-directory';
1717
import { requireProjectModule } from '../../utilities/require-project-module';
18-
import { WebpackConfigOptions } from '../build-options';
18+
import { BuildOptions, WebpackConfigOptions } from '../build-options';
1919
import { getOutputHashFormat, normalizeExtraEntryPoints } from './utils';
2020

2121
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
@@ -102,7 +102,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
102102

103103
extraPlugins.push(new ScriptsWebpackPlugin({
104104
name: bundleName,
105-
sourceMap: buildOptions.sourceMap,
105+
sourceMap: buildOptions.scriptsSourceMap,
106106
filename: `${path.basename(bundleName)}${hash}.js`,
107107
scripts: script.paths,
108108
basePath: projectRoot,
@@ -174,7 +174,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
174174
use: [
175175
{
176176
loader: buildOptimizerLoader,
177-
options: { sourceMap: buildOptions.sourceMap },
177+
options: { sourceMap: buildOptions.scriptsSourceMap },
178178
},
179179
],
180180
};
@@ -297,12 +297,12 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
297297
// TODO: check with Mike what this feature needs.
298298
new BundleBudgetPlugin({ budgets: buildOptions.budgets }),
299299
new CleanCssWebpackPlugin({
300-
sourceMap: buildOptions.sourceMap,
300+
sourceMap: buildOptions.stylesSourceMap,
301301
// component styles retain their original file name
302302
test: (file) => /\.(?:css|scss|sass|less|styl)$/.test(file),
303303
}),
304304
new TerserPlugin({
305-
sourceMap: buildOptions.sourceMap,
305+
sourceMap: buildOptions.scriptsSourceMap,
306306
parallel: true,
307307
cache: true,
308308
terserOptions,

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/server.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { Configuration } from 'webpack';
99
import { WebpackConfigOptions } from '../build-options';
10+
import { getSourceMapDevTool } from './utils';
1011

1112

1213
/**
@@ -15,8 +16,22 @@ import { WebpackConfigOptions } from '../build-options';
1516
*/
1617
export function getServerConfig(wco: WebpackConfigOptions) {
1718

19+
const extraPlugins = [];
20+
if (wco.buildOptions.sourceMap) {
21+
const {
22+
scriptsSourceMap = false,
23+
stylesSourceMap = false,
24+
hiddenSourceMap = false,
25+
} = wco.buildOptions;
26+
27+
extraPlugins.push(getSourceMapDevTool(
28+
scriptsSourceMap,
29+
stylesSourceMap,
30+
hiddenSourceMap,
31+
));
32+
}
33+
1834
const config: Configuration = {
19-
devtool: wco.buildOptions.sourceMap ? 'source-map' : false,
2035
resolve: {
2136
mainFields: [
2237
...(wco.supportES2015 ? ['es2015'] : []),
@@ -27,6 +42,7 @@ export function getServerConfig(wco: WebpackConfigOptions) {
2742
output: {
2843
libraryTarget: 'commonjs',
2944
},
45+
plugins: extraPlugins,
3046
node: false,
3147
};
3248

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/styles.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
4040
const entryPoints: { [key: string]: string[] } = {};
4141
const globalStylePaths: string[] = [];
4242
const extraPlugins = [];
43-
const cssSourceMap = buildOptions.sourceMap;
43+
44+
const cssSourceMap = buildOptions.stylesSourceMap;
4445

4546
// Determine hashing format.
4647
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as string);
@@ -187,7 +188,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
187188
options: {
188189
ident: 'embedded',
189190
plugins: postcssPluginCreator,
190-
sourceMap: cssSourceMap ? 'inline' : false,
191+
sourceMap: cssSourceMap && !buildOptions.hiddenSourceMap ? 'inline' : false,
191192
},
192193
},
193194
...(use as webpack.Loader[]),
@@ -208,7 +209,10 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
208209
options: {
209210
ident: buildOptions.extractCss ? 'extracted' : 'embedded',
210211
plugins: postcssPluginCreator,
211-
sourceMap: cssSourceMap && !buildOptions.extractCss ? 'inline' : cssSourceMap,
212+
sourceMap: cssSourceMap
213+
&& !buildOptions.extractCss
214+
&& !buildOptions.hiddenSourceMap
215+
? 'inline' : cssSourceMap,
212216
},
213217
},
214218
...(use as webpack.Loader[]),

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/test.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as glob from 'glob';
1010
import * as path from 'path';
1111
import * as webpack from 'webpack';
1212
import { WebpackConfigOptions, WebpackTestOptions } from '../build-options';
13+
import { getSourceMapDevTool } from './utils';
1314

1415

1516
/**
@@ -55,6 +56,20 @@ export function getTestConfig(
5556
});
5657
}
5758

59+
if (wco.buildOptions.sourceMap) {
60+
const {
61+
scriptsSourceMap = false,
62+
stylesSourceMap = false,
63+
} = wco.buildOptions;
64+
65+
extraPlugins.push(getSourceMapDevTool(
66+
scriptsSourceMap,
67+
stylesSourceMap,
68+
false,
69+
true,
70+
));
71+
}
72+
5873
return {
5974
mode: 'development',
6075
resolve: {
@@ -63,7 +78,7 @@ export function getTestConfig(
6378
'browser', 'module', 'main',
6479
],
6580
},
66-
devtool: buildOptions.sourceMap ? 'inline-source-map' : 'eval',
81+
devtool: buildOptions.sourceMap ? false : 'eval',
6782
entry: {
6883
main: path.resolve(root, buildOptions.main),
6984
},

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/typescript.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function _createAotPlugin(
7575
locale: buildOptions.i18nLocale,
7676
platform: buildOptions.platform === 'server' ? PLATFORM.Server : PLATFORM.Browser,
7777
missingTranslation: buildOptions.i18nMissingTranslation,
78-
sourceMap: buildOptions.sourceMap,
78+
sourceMap: buildOptions.scriptsSourceMap,
7979
additionalLazyModules,
8080
hostReplacementPaths,
8181
nameLazyFiles: buildOptions.namedChunks,
@@ -108,7 +108,7 @@ export function getAotConfig(
108108
if (buildOptions.buildOptimizer) {
109109
loaders.unshift({
110110
loader: buildOptimizerLoader,
111-
options: { sourceMap: buildOptions.sourceMap }
111+
options: { sourceMap: buildOptions.scriptsSourceMap }
112112
});
113113
}
114114

0 commit comments

Comments
 (0)