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

Commit 06c63f2

Browse files
committed
feat(@angular-devkit/build-optimizer): support es2015 modules
1 parent 9cf1c38 commit 06c63f2

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

packages/angular_devkit/build_optimizer/src/build-optimizer/build-optimizer.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@ import { getWrapEnumsTransformer, testWrapEnums } from '../transforms/wrap-enums
1616

1717

1818
const whitelistedAngularModules = [
19-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)animations(\\|\/)/,
20-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)common(\\|\/)/,
21-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)compiler(\\|\/)/,
22-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)core(\\|\/)/,
23-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)forms(\\|\/)/,
24-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)http(\\|\/)/,
25-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)platform-browser-dynamic(\\|\/)/,
26-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)platform-browser(\\|\/)/,
27-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)platform-webworker-dynamic(\\|\/)/,
28-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)platform-webworker(\\|\/)/,
29-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)router(\\|\/)/,
30-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)upgrade(\\|\/)/,
31-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)material(\\|\/)/,
32-
/(\\|\/)node_modules(\\|\/)@angular(\\|\/)cdk(\\|\/)/,
19+
/[\\\/]node_modules[\\\/]@angular[\\\/]animations[\\\/]/,
20+
/[\\\/]node_modules[\\\/]@angular[\\\/]common[\\\/]/,
21+
/[\\\/]node_modules[\\\/]@angular[\\\/]compiler[\\\/]/,
22+
/[\\\/]node_modules[\\\/]@angular[\\\/]core[\\\/]/,
23+
/[\\\/]node_modules[\\\/]@angular[\\\/]forms[\\\/]/,
24+
/[\\\/]node_modules[\\\/]@angular[\\\/]http[\\\/]/,
25+
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-browser-dynamic[\\\/]/,
26+
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-browser[\\\/]/,
27+
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-webworker-dynamic[\\\/]/,
28+
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-webworker[\\\/]/,
29+
/[\\\/]node_modules[\\\/]@angular[\\\/]router[\\\/]/,
30+
/[\\\/]node_modules[\\\/]@angular[\\\/]upgrade[\\\/]/,
31+
/[\\\/]node_modules[\\\/]@angular[\\\/]material[\\\/]/,
32+
/[\\\/]node_modules[\\\/]@angular[\\\/]cdk[\\\/]/,
33+
];
34+
35+
const es5AngularModules = [
36+
// Angular 4 packaging format has .es5.js as the extension.
37+
/.es5.js$/, // Angular 4
38+
// Angular 5 has esm5 folders.
39+
/[\\\/]node_modules[\\\/]@angular[\\\/][^\\/][\\\/]esm5[\\\/]/,
40+
// All Angular versions have UMD with es5.
41+
/.umd.js$/,
3342
];
3443

3544
export interface BuildOptimizerOptions {
@@ -70,9 +79,10 @@ export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascr
7079

7180
if (inputFilePath
7281
&& whitelistedAngularModules.some((re) => re.test(inputFilePath))
82+
&& es5AngularModules.some((re) => re.test(inputFilePath))
7383
) {
7484
getTransforms.push(
75-
// getPrefixFunctionsTransformer is rather dangerous, apply only to known pure modules.
85+
// getPrefixFunctionsTransformer is rather dangerous, apply only to known pure es5 modules.
7686
// It will mark both `require()` calls and `console.log(stuff)` as pure.
7787
// We only apply it to whitelisted modules, since we know they are safe.
7888
// getPrefixFunctionsTransformer needs to be before getFoldFileTransformer.

packages/angular_devkit/build_optimizer/src/build-optimizer/build-optimizer_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ describe('build-optimizer', () => {
6666
expect(boOutput.content).toBeFalsy();
6767
expect(boOutput.emitSkipped).toEqual(true);
6868
});
69+
70+
it('supports es2015 modules', () => {
71+
// prefix-functions would add PURE_IMPORTS_START and PURE to the super call.
72+
// This test ensures it isn't applied to es2015 modules.
73+
const output = oneLine`
74+
import { Injectable } from '@angular/core';
75+
class Clazz extends BaseClazz { constructor(e) { super(e); } }
76+
`;
77+
const input = stripIndent`
78+
${output}
79+
Clazz.ctorParameters = () => [ { type: Injectable } ];
80+
`;
81+
82+
const inputFilePath = '/node_modules/@angular/core/@angular/core.js';
83+
const boOutput = buildOptimizer({ content: input, inputFilePath });
84+
expect(oneLine`${boOutput.content}`).toEqual(output);
85+
expect(boOutput.emitSkipped).toEqual(false);
86+
});
6987
});
7088

7189

packages/angular_devkit/build_optimizer/src/transforms/scrub-file.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boo
226226
if (expr.operatorToken.kind !== ts.SyntaxKind.FirstAssignment) {
227227
return false;
228228
}
229-
if (expr.right.kind !== ts.SyntaxKind.FunctionExpression) {
229+
if (expr.right.kind !== ts.SyntaxKind.FunctionExpression
230+
&& expr.right.kind !== ts.SyntaxKind.ArrowFunction
231+
) {
230232
return false;
231233
}
232234

packages/angular_devkit/build_optimizer/src/transforms/scrub-file_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ describe('scrub-file', () => {
173173
expect(oneLine`${transform(input)}`).toEqual(oneLine`${output}`);
174174
});
175175

176+
177+
fit('removes top-level Angular constructor parameters in es2015', () => {
178+
const output = stripIndent`
179+
class Clazz extends BaseClazz { constructor(e) { super(e); } }
180+
`;
181+
const input = stripIndent`
182+
${output}
183+
Clazz.ctorParameters = () => [ { type: Injectable } ];
184+
`;
185+
186+
expect(testScrubFile(input)).toBeTruthy();
187+
expect(oneLine`${transform(input)}`).toEqual(oneLine`${output}`);
188+
});
189+
176190
it('removes nested constructor parameters', () => {
177191
const output = stripIndent`
178192
import { Injector } from '@angular/core';

0 commit comments

Comments
 (0)