Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4ef16c2

Browse files
committedAug 2, 2017
fix(@angular-devkit/build-optimizer): only prefix top-level functions in Angular packages
Fix #51 Fix #52
1 parent 4aee45d commit 4ef16c2

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed
 

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getScrubFileTransformer } from '../transforms/scrub-file';
1919
const hasDecorators = /decorators/;
2020
const hasCtorParameters = /ctorParameters/;
2121
const hasTsHelpers = /var (__extends|__decorate|__metadata|__param) = /;
22+
const isAngularPackage = /(\\|\/)node_modules(\\|\/)@angular(\\|\/)/;
2223

2324
export interface BuildOptimizerOptions {
2425
content?: string;
@@ -50,15 +51,22 @@ export function buildOptimizer(options: BuildOptimizerOptions):
5051
getTransforms.push(getImportTslibTransformer);
5152
}
5253

53-
54-
if (hasDecorators.test(content) || hasCtorParameters.test(content)) {
54+
if (inputFilePath && isAngularPackage.test(inputFilePath)) {
5555
// Order matters, getPrefixFunctionsTransformer needs to be called before
5656
// getFoldFileTransformer.
57-
getTransforms.push(...[
57+
getTransforms.push(
58+
// getPrefixFunctionsTransformer is rather dangerous.
59+
// It will mark both `require()` calls and `console.log(stuff)` as pure.
60+
// We only apply it to @angular/* packages, since we know they are safe.
5861
getPrefixFunctionsTransformer,
5962
getScrubFileTransformer,
6063
getFoldFileTransformer,
61-
]);
64+
);
65+
} else if (hasDecorators.test(content) || hasCtorParameters.test(content)) {
66+
getTransforms.push(
67+
getScrubFileTransformer,
68+
getFoldFileTransformer,
69+
);
6270
}
6371

6472
if (getTransforms.length > 0) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ describe('build-optimizer', () => {
4040
`;
4141
// tslint:enable:max-line-length
4242

43-
expect(oneLine`${buildOptimizer({ content: input }).content}`).toEqual(output);
43+
const inputFilePath = '/node_modules/@angular/some-lib';
44+
expect(oneLine`${buildOptimizer({ content: input, inputFilePath }).content}`).toEqual(output);
4445
});
4546

46-
it('doesn\'t process files without decorators/ctorParameters', () => {
47+
it('doesn\'t process files without decorators/ctorParameters/outside Angular', () => {
4748
const input = oneLine`
4849
var Clazz = (function () { function Clazz() { } return Clazz; }());
4950
${staticProperty}

‎packages/angular_devkit/build_optimizer/src/build-optimizer/webpack-loader.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export default function buildOptimizerLoader
2121
this.cacheable();
2222
const options: BuildOptimizerLoaderOptions = loaderUtils.getOptions(this) || {};
2323

24-
const boOutput = buildOptimizer({ content, emitSourceMap: options.sourceMap });
24+
const boOutput = buildOptimizer({
25+
content,
26+
inputFilePath: this.resourcePath,
27+
emitSourceMap: options.sourceMap,
28+
});
2529
const intermediateSourceMap = boOutput.sourceMap;
2630
let newContent = boOutput.content;
2731

0 commit comments

Comments
 (0)
Please sign in to comment.