Skip to content

Commit 4524805

Browse files
committed
feat(build): add sourcemap option
Add `--no-sourcemap` option to `ng build/serve/test`. Disabling sourcemaps can help with build speeds. My tests on a medium project shoul a 11.5% improvement on initial builds, and a 37% improvement on rebuilds. Disabling sourcemaps will make debugging harder, since it makes line information very innacurate. Partially address #1980
1 parent 215e555 commit 4524805

14 files changed

+63
-20
lines changed

packages/angular-cli/commands/build.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface BuildOptions {
1111
supressSizes: boolean;
1212
baseHref?: string;
1313
aot?: boolean;
14+
sourcemap?: boolean;
1415
}
1516

1617
const BuildCommand = Command.extend({
@@ -31,7 +32,8 @@ const BuildCommand = Command.extend({
3132
{ name: 'watcher', type: String },
3233
{ name: 'suppress-sizes', type: Boolean, default: false },
3334
{ name: 'base-href', type: String, default: null, aliases: ['bh'] },
34-
{ name: 'aot', type: Boolean, default: false }
35+
{ name: 'aot', type: Boolean, default: false },
36+
{ name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] }
3537
],
3638

3739
run: function (commandOptions: BuildOptions) {

packages/angular-cli/commands/serve.ts

+3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export interface ServeTaskOptions {
2626
sslKey?: string;
2727
sslCert?: string;
2828
aot?: boolean;
29+
sourcemap?: boolean;
2930
open?: boolean;
31+
3032
}
3133

3234
const ServeCommand = Command.extend({
@@ -81,6 +83,7 @@ const ServeCommand = Command.extend({
8183
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
8284
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
8385
{ name: 'aot', type: Boolean, default: false },
86+
{ name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] },
8487
{
8588
name: 'open',
8689
type: Boolean,

packages/angular-cli/commands/test.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ const TestCommand = require('../ember-cli/lib/commands/test');
22
import TestTask from '../tasks/test';
33
import {CliConfig} from '../models/config';
44

5+
export interface TestOptions {
6+
watch?: boolean;
7+
codeCoverage?: boolean;
8+
lint?: boolean;
9+
singleRun?: boolean;
10+
browsers?: string;
11+
colors?: boolean;
12+
log?: string;
13+
port?: number;
14+
reporters?: string;
15+
build?: boolean;
16+
sourcemap?: boolean;
17+
}
18+
19+
520
const NgCliTestCommand = TestCommand.extend({
621
availableOptions: [
722
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
@@ -13,10 +28,11 @@ const NgCliTestCommand = TestCommand.extend({
1328
{ name: 'log-level', type: String },
1429
{ name: 'port', type: Number },
1530
{ name: 'reporters', type: String },
16-
{ name: 'build', type: Boolean, default: true }
31+
{ name: 'build', type: Boolean, default: true },
32+
{ name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] }
1733
],
1834

19-
run: function(commandOptions: any) {
35+
run: function(commandOptions: TestOptions) {
2036
this.project.ngConfig = this.project.ngConfig || CliConfig.fromProject();
2137

2238
const testTask = new TestTask({

packages/angular-cli/models/webpack-build-common.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export function getWebpackCommonConfig(
1111
projectRoot: string,
1212
environment: string,
1313
appConfig: any,
14-
baseHref: string
14+
baseHref: string,
15+
sourcemap: boolean
1516
) {
1617

1718
const appRoot = path.resolve(projectRoot, appConfig.root);
@@ -32,7 +33,7 @@ export function getWebpackCommonConfig(
3233
if (appConfig.scripts.length > 0) { entry['scripts'] = scripts; }
3334

3435
return {
35-
devtool: 'source-map',
36+
devtool: sourcemap ? 'source-map' : 'eval',
3637
resolve: {
3738
extensions: ['.ts', '.js'],
3839
modules: [path.resolve(projectRoot, 'node_modules')]
@@ -41,7 +42,9 @@ export function getWebpackCommonConfig(
4142
entry: entry,
4243
output: {
4344
path: path.resolve(projectRoot, appConfig.outDir),
44-
filename: '[name].bundle.js'
45+
filename: '[name].bundle.js',
46+
sourceMapFilename: '[name].bundle.map',
47+
chunkFilename: '[id].chunk.js'
4548
},
4649
module: {
4750
rules: [

packages/angular-cli/models/webpack-build-development.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const getWebpackDevConfigPartial = function(projectRoot: string, appConfi
77
: [];
88
const cssLoaders = ['style-loader', 'css-loader?sourcemap', 'postcss-loader'];
99
return {
10-
devtool: 'source-map',
1110
output: {
1211
path: path.resolve(projectRoot, appConfig.outDir),
1312
filename: '[name].bundle.js',

packages/angular-cli/models/webpack-build-production.ts

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const getWebpackProdConfigPartial = function(projectRoot: string, appConf
2121
: [];
2222
const cssLoaders = ['css-loader?sourcemap&minimize', 'postcss-loader'];
2323
return {
24-
devtool: 'source-map',
2524
output: {
2625
path: path.resolve(projectRoot, appConfig.outDir),
2726
filename: '[name].[chunkhash].bundle.js',

packages/angular-cli/models/webpack-build-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig, test
4343
}
4444

4545
return {
46-
devtool: 'inline-source-map',
46+
devtool: testConfig.sourcemap ? 'inline-source-map' : 'eval',
4747
context: path.resolve(__dirname, './'),
4848
resolve: {
4949
extensions: ['.ts', '.js'],

packages/angular-cli/models/webpack-config.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export class NgCliWebpackConfig {
2323
public environment: string,
2424
outputDir?: string,
2525
baseHref?: string,
26-
isAoT = false
26+
isAoT = false,
27+
sourcemap = true,
2728
) {
2829
const config: CliConfig = CliConfig.fromProject();
2930
const appConfig = config.config.apps[0];
@@ -34,7 +35,8 @@ export class NgCliWebpackConfig {
3435
this.ngCliProject.root,
3536
environment,
3637
appConfig,
37-
baseHref
38+
baseHref,
39+
sourcemap
3840
);
3941
let targetConfigPartial = this.getTargetConfig(this.ngCliProject.root, appConfig);
4042
const typescriptConfigPartial = isAoT

packages/angular-cli/plugins/karma.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const init = (config) => {
1212
const environment = config.angularCli.environment || 'dev';
1313
const testConfig = {
1414
codeCoverage: config.angularCli.codeCoverage || false,
15-
lint: config.angularCli.lint || false
15+
lint: config.angularCli.lint || false,
16+
sourcemap: config.angularCli.sourcemap
1617
}
1718

1819
// add webpack config

packages/angular-cli/tasks/build-webpack-watch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export default Task.extend({
2424
runTaskOptions.environment,
2525
outputDir,
2626
runTaskOptions.baseHref,
27-
runTaskOptions.aot
27+
runTaskOptions.aot,
28+
runTaskOptions.sourcemap
2829
).config;
2930
const webpackCompiler: any = webpack(config);
3031

packages/angular-cli/tasks/build-webpack.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export default <any>Task.extend({
2323
runTaskOptions.environment,
2424
outputDir,
2525
runTaskOptions.baseHref,
26-
runTaskOptions.aot
26+
runTaskOptions.aot,
27+
runTaskOptions.sourcemap
2728
).config;
2829

2930
const webpackCompiler: any = webpack(config);

packages/angular-cli/tasks/serve-webpack.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export default Task.extend({
2626
commandOptions.environment,
2727
undefined,
2828
undefined,
29-
commandOptions.aot
29+
commandOptions.aot,
30+
commandOptions.sourcemap
3031
).config;
3132

3233
// This allows for live reload of page when changes are made to repo.

packages/angular-cli/tasks/test.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Task = require('../ember-cli/lib/models/task');
2+
import { TestOptions } from '../commands/test';
23
import * as path from 'path';
34

45
// require dependencies within the target project
@@ -9,27 +10,30 @@ function requireDependency(root: string, moduleName: string) {
910
}
1011

1112
export default Task.extend({
12-
run: function (options: any) {
13+
run: function (options: TestOptions) {
1314
const projectRoot = this.project.root;
1415
return new Promise((resolve) => {
1516
const karma = requireDependency(projectRoot, 'karma');
1617
const karmaConfig = path.join(projectRoot, this.project.ngConfig.config.test.karma.config);
1718

19+
let karmaOptions: any = Object.assign({}, options);
20+
1821
// Convert browsers from a string to an array
1922
if (options.browsers) {
20-
options.browsers = options.browsers.split(',');
23+
karmaOptions.browsers = options.browsers.split(',');
2124
}
2225

23-
options.angularCli = {
26+
karmaOptions.angularCli = {
2427
codeCoverage: options.codeCoverage,
2528
lint: options.lint,
29+
sourcemap: options.sourcemap
2630
};
2731

2832
// Assign additional karmaConfig options to the local ngapp config
29-
options.configFile = karmaConfig;
33+
karmaOptions.configFile = karmaConfig;
3034

3135
// :shipit:
32-
const karmaServer = new karma.Server(options, resolve);
36+
const karmaServer = new karma.Server(karmaOptions, resolve);
3337
karmaServer.start();
3438
});
3539
}

tests/e2e/tests/build/sourcemap.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {ng} from '../../utils/process';
2+
import {expectFileToExist} from '../../utils/fs';
3+
import {expectToFail} from '../../utils/utils';
4+
5+
6+
export default function() {
7+
return ng('build')
8+
.then(() => expectFileToExist('dist/main.bundle.map'))
9+
.then(() => ng('build', '--no-sourcemap'))
10+
.then(() => expectToFail(() => expectFileToExist('dist/main.bundle.map')));
11+
}

0 commit comments

Comments
 (0)