Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 496456b

Browse files
alan-agius4hansl
authored andcommitted
feat(@angular-devkit/build-angular): add sourcemaps for external libraries
Added a new flag `vendorSourceMap` to enable consumers to have sourcemaps for vendor packages.
1 parent 65992bc commit 496456b

File tree

12 files changed

+142
-2
lines changed

12 files changed

+142
-2
lines changed

package-lock.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"semver-intersect": "^1.1.2",
136136
"source-map": "^0.5.6",
137137
"source-map-support": "^0.5.0",
138+
"source-map-loader": "^0.2.3",
138139
"stats-webpack-plugin": "^0.6.2",
139140
"style-loader": "^0.21.0",
140141
"stylus": "^0.54.5",

packages/angular_devkit/build_angular/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"rxjs": "^6.0.0",
4242
"sass-loader": "^7.0.1",
4343
"source-map-support": "^0.5.0",
44+
"source-map-loader": "^0.2.3",
4445
"stats-webpack-plugin": "^0.6.2",
4546
"style-loader": "^0.21.0",
4647
"stylus": "^0.54.5",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface BuildOptions {
1818
outputPath: string;
1919
aot?: boolean;
2020
sourceMap?: boolean;
21+
vendorSourceMap?: boolean;
2122
evalSourceMap?: boolean;
2223
vendorChunk?: boolean;
2324
commonChunk?: boolean;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
158158
extraPlugins.push(new StatsPlugin('stats.json', 'verbose'));
159159
}
160160

161+
let sourceMapUseRule;
162+
if (buildOptions.sourceMap && buildOptions.vendorSourceMap) {
163+
sourceMapUseRule = {
164+
use: [
165+
{
166+
loader: 'source-map-loader'
167+
}
168+
]
169+
}
170+
}
171+
161172
let buildOptimizerUseRule;
162173
if (buildOptions.buildOptimizer) {
163174
buildOptimizerUseRule = {
@@ -274,6 +285,12 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
274285
test: /\.js$/,
275286
...buildOptimizerUseRule,
276287
},
288+
{
289+
test: /\.js$/,
290+
exclude: /(ngfactory|ngstyle).js$/,
291+
enforce: 'pre',
292+
...sourceMapUseRule,
293+
},
277294
]
278295
},
279296
optimization: {

packages/angular_devkit/build_angular/src/browser/schema.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ export interface BrowserBuilderSchema {
6666
*/
6767
sourceMap: boolean;
6868

69+
/**
70+
* Resolve vendor packages sourcemaps.
71+
*/
72+
vendorSourceMap?: boolean;
73+
6974
/**
7075
* Output in-file eval sourcemaps.
7176
*/

packages/angular_devkit/build_angular/src/browser/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
"description": "Output sourcemaps.",
8181
"default": true
8282
},
83+
"vendorSourceMap": {
84+
"type": "boolean",
85+
"description": "Resolve vendor packages sourcemaps.",
86+
"default": false
87+
},
8388
"evalSourceMap": {
8489
"type": "boolean",
8590
"description": "Output in-file eval sourcemaps.",

packages/angular_devkit/build_angular/src/dev-server/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
"type": "boolean",
9191
"description": "Output sourcemaps."
9292
},
93+
"vendorSourceMap": {
94+
"type": "boolean",
95+
"description": "Resolve vendor packages sourcemaps.",
96+
"default": false
97+
},
9398
"evalSourceMap": {
9499
"type": "boolean",
95100
"description": "Output in-file eval sourcemaps."

packages/angular_devkit/build_angular/src/karma/schema.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,16 @@
6464
},
6565
"sourceMap": {
6666
"type": "boolean",
67-
"description": "Output sourcemaps.",
68-
"default": true
67+
"description": "Output sourcemaps."
68+
},
69+
"vendorSourceMap": {
70+
"type": "boolean",
71+
"description": "Resolve vendor packages sourcemaps.",
72+
"default": false
73+
},
74+
"evalSourceMap": {
75+
"type": "boolean",
76+
"description": "Output in-file eval sourcemaps."
6977
},
7078
"progress": {
7179
"type": "boolean",

packages/angular_devkit/build_angular/src/server/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface BuildWebpackServerSchema {
3030
* Use a separate bundle containing only vendor libraries.
3131
*/
3232
vendorChunk?: boolean;
33+
/**
34+
* Resolve vendor packages sourcemaps.
35+
*/
36+
vendorSourceMap?: boolean;
3337
/**
3438
* Output in-file eval sourcemaps.
3539
*/

packages/angular_devkit/build_angular/src/server/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
"description": "Output sourcemaps.",
5050
"default": true
5151
},
52+
"vendorSourceMap": {
53+
"type": "boolean",
54+
"description": "Resolve vendor packages sourcemaps.",
55+
"default": false
56+
},
5257
"evalSourceMap": {
5358
"type": "boolean",
5459
"description": "Output in-file eval sourcemaps.",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { join, normalize, virtualFs } from '@angular-devkit/core';
10+
import * as path from 'path';
11+
import { tap } from 'rxjs/operators';
12+
import { Timeout, browserTargetSpec, host, runTargetSpec } from '../utils';
13+
14+
describe('Browser Builder external source map', () => {
15+
const outputPath = normalize('dist');
16+
17+
beforeEach(done => host.initialize().toPromise().then(done, done.fail));
18+
afterEach(done => host.restore().toPromise().then(done, done.fail));
19+
20+
it('works', (done) => {
21+
const overrides = { sourceMap: true, vendorSourceMap: true };
22+
23+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
24+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
25+
tap(() => {
26+
const fileName = join(outputPath, 'vendor.js.map');
27+
expect(host.scopedSync().exists(fileName)).toBe(true);
28+
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
29+
// this is due the fact that some vendors like `tslib` sourcemaps to js files
30+
const sourcePath = JSON.parse(content).sources[0];
31+
expect(path.extname(sourcePath)).toBe('.ts', `${sourcePath} extention should be '.ts'`);
32+
}),
33+
).toPromise().then(done, done.fail);
34+
}, Timeout.Basic);
35+
36+
it('does not map sourcemaps from external library when disabled', (done) => {
37+
const overrides = { sourceMap: true, vendorSourceMap: false };
38+
39+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
40+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
41+
tap(() => {
42+
const fileName = join(outputPath, 'vendor.js.map');
43+
expect(host.scopedSync().exists(fileName)).toBe(true);
44+
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
45+
// this is due the fact that some vendors like `tslib` sourcemaps to js files
46+
const sourcePath = JSON.parse(content).sources[0];
47+
expect(path.extname(sourcePath)).toBe('.js', `${sourcePath} extention should be '.js'`);
48+
}),
49+
).toPromise().then(done, done.fail);
50+
}, Timeout.Basic);
51+
52+
});

0 commit comments

Comments
 (0)