Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/angular_devkit/build_optimizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export interface BuildOptimizerOptions {
outputFilePath?: string;
emitSourceMap?: boolean;
strict?: boolean;
isSideEffectFree?: boolean;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
* found in the LICENSE file at https://angular.io/license
*/
import { readFileSync } from 'fs';
import { TransformJavascriptOutput, transformJavascript } from '../helpers/transform-javascript';
import {
TransformJavascriptOptions,
TransformJavascriptOutput,
transformJavascript,
} from '../helpers/transform-javascript';
import { getFoldFileTransformer } from '../transforms/class-fold';
import { getImportTslibTransformer, testImportTslib } from '../transforms/import-tslib';
import { getPrefixClassesTransformer, testPrefixClasses } from '../transforms/prefix-classes';
Expand All @@ -15,6 +19,7 @@ import { getScrubFileTransformer, testScrubFile } from '../transforms/scrub-file
import { getWrapEnumsTransformer, testWrapEnums } from '../transforms/wrap-enums';


// Angular packages are known to have no side effects.
const whitelistedAngularModules = [
/[\\/]node_modules[\\/]@angular[\\/]animations[\\/]/,
/[\\/]node_modules[\\/]@angular[\\/]common[\\/]/,
Expand All @@ -41,12 +46,27 @@ const es5AngularModules = [
/\.umd\.js$/,
];

// Factories created by AOT are known to have no side effects and contain es5 code.
// In Angular 2/4 the file path for factories can be `.ts`, but in Angular 5 it is `.js`.
const ngFactories = [
/\.ngfactory\.[jt]s/,
/\.ngstyle\.[jt]s/,
];

function isKnownSideEffectFree(filePath: string) {
return ngFactories.some((re) => re.test(filePath)) || (
whitelistedAngularModules.some((re) => re.test(filePath))
&& es5AngularModules.some((re) => re.test(filePath))
);
}

export interface BuildOptimizerOptions {
content?: string;
inputFilePath?: string;
outputFilePath?: string;
emitSourceMap?: boolean;
strict?: boolean;
isSideEffectFree?: boolean;
}

export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascriptOutput {
Expand Down Expand Up @@ -77,10 +97,7 @@ export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascr
getTransforms.push(getPrefixClassesTransformer);
}

if (inputFilePath
&& whitelistedAngularModules.some((re) => re.test(inputFilePath))
&& es5AngularModules.some((re) => re.test(inputFilePath))
) {
if (options.isSideEffectFree || inputFilePath && isKnownSideEffectFree(inputFilePath)) {
getTransforms.push(
// getPrefixFunctionsTransformer is rather dangerous, apply only to known pure es5 modules.
// It will mark both `require()` calls and `console.log(stuff)` as pure.
Expand All @@ -97,5 +114,14 @@ export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascr
);
}

return transformJavascript({ ...options, getTransforms, content });
const transformJavascriptOpts: TransformJavascriptOptions = {
content: content,
inputFilePath: options.inputFilePath,
outputFilePath: options.outputFilePath,
emitSourceMap: options.emitSourceMap,
strict: options.strict,
getTransforms,
};

return transformJavascript(transformJavascriptOpts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('build-optimizer', () => {
const decorators = 'Clazz.decorators = [ { type: Injectable } ];';

describe('basic functionality', () => {
it('applies class-fold, scrub-file and prefix-functions to whitelisted es5 modules', () => {
it('applies class-fold, scrub-file and prefix-functions to side-effect free modules', () => {
const input = tags.stripIndent`
${imports}
var __extends = (this && this.__extends) || function (d, b) {
Expand Down Expand Up @@ -51,6 +51,7 @@ describe('build-optimizer', () => {
], ComponentClazz);
return ComponentClazz;
}());
var RenderType_MdOption = ɵcrt({ encapsulation: 2, styles: styles_MdOption});
`;
// tslint:disable:max-line-length
const output = tags.oneLine`
Expand All @@ -68,6 +69,7 @@ describe('build-optimizer', () => {
function ComponentClazz() { }
return ComponentClazz;
}());
var RenderType_MdOption = /*@__PURE__*/ ɵcrt({ encapsulation: 2, styles: styles_MdOption });
`;

// Check Angular 4/5 and unix/windows paths.
Expand All @@ -76,6 +78,8 @@ describe('build-optimizer', () => {
'/node_modules/@angular/core/esm5/core.js',
'\\node_modules\\@angular\\core\\@angular\\core.es5.js',
'\\node_modules\\@angular\\core\\esm5\\core.js',
'/project/file.ngfactory.js',
'/project/file.ngstyle.js',
];

inputPaths.forEach((inputFilePath) => {
Expand Down Expand Up @@ -113,6 +117,20 @@ describe('build-optimizer', () => {
expect(tags.oneLine`${boOutput.content}`).toEqual(output);
expect(boOutput.emitSkipped).toEqual(false);
});

it('supports flagging module as side-effect free', () => {
const output = tags.oneLine`
/** PURE_IMPORTS_START PURE_IMPORTS_END */
var RenderType_MdOption = /*@__PURE__*/ ɵcrt({ encapsulation: 2, styles: styles_MdOption });
`;
const input = tags.stripIndent`
var RenderType_MdOption = ɵcrt({ encapsulation: 2, styles: styles_MdOption});
`;

const boOutput = buildOptimizer({ content: input, isSideEffectFree: true });
expect(tags.oneLine`${boOutput.content}`).toEqual(output);
expect(boOutput.emitSkipped).toEqual(false);
});
});


Expand Down
22 changes: 0 additions & 22 deletions packages/angular_devkit/build_optimizer/src/purify/purify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,6 @@ export function purifyReplacements(content: string) {
},
);

/* Prefix CCF and CMF statements */
content.replace(
/\w*__WEBPACK_IMPORTED_MODULE_\d+__angular_core__\["\w+" \/\* (ɵccf|ɵcmf|ɵcrt) \*\/\]\(/mg,
(match, _p1, offset) => {
const newContent = `/*@__PURE__*/${match}`;
addReplacement(offset, match.length, newContent);

return newContent;
},
);

/* Prefix module statements */
content.replace(
/new \w*__WEBPACK_IMPORTED_MODULE_\d+__angular_core__\["\w+" \/\* NgModuleFactory \*\/\]/mg,
(match, offset) => {
const newContent = `/*@__PURE__*/${match}`;
addReplacement(offset, match.length, newContent);

return newContent;
},
);

return replacements;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,4 @@ describe('purify', () => {

expect(tags.oneLine`${purify(input)}`).toEqual(tags.oneLine`${output}`);
});

it('prefix createComponentFactory and createNgModuleFactory function calls', () => {
const input = tags.stripIndent`
var AppComponentNgFactory = __WEBPACK_IMPORTED_MODULE_1__angular_core__["I" /* ɵccf */]('app-root');
var AppModuleNgFactory = __WEBPACK_IMPORTED_MODULE_1__angular_core__["I" /* ɵcmf */]('app-root');
var SelectComponentNgFactory = select_component_ngfactory___WEBPACK_IMPORTED_MODULE_0__angular_core__["U" /* ɵccf */]('aio-select');
`;
const output = tags.stripIndent`
var AppComponentNgFactory = /*@__PURE__*/__WEBPACK_IMPORTED_MODULE_1__angular_core__["I" /* ɵccf */]('app-root');
var AppModuleNgFactory = /*@__PURE__*/__WEBPACK_IMPORTED_MODULE_1__angular_core__["I" /* ɵcmf */]('app-root');
var SelectComponentNgFactory = /*@__PURE__*/select_component_ngfactory___WEBPACK_IMPORTED_MODULE_0__angular_core__["U" /* ɵccf */]('aio-select');
`;

expect(tags.oneLine`${purify(input)}`).toEqual(tags.oneLine`${output}`);
});

it('should prefix createRendererType2 function calls', () => {
const input = tags.stripIndent`
var RenderType_MdOption = index_ngfactory___WEBPACK_IMPORTED_MODULE_0__angular_core__["W" /* ɵcrt */]({ encapsulation: 2, styles: styles_MdOption});
`;
const output = tags.stripIndent`
var RenderType_MdOption = /*@__PURE__*/index_ngfactory___WEBPACK_IMPORTED_MODULE_0__angular_core__["W" /* ɵcrt */]({ encapsulation: 2, styles: styles_MdOption});
`;

expect(tags.oneLine`${purify(input)}`).toEqual(tags.oneLine`${output}`);
});

it('prefix module statements', () => {
const input = tags.stripIndent`
var AppModuleNgFactory = new __WEBPACK_IMPORTED_MODULE_1__angular_core__["I" /* NgModuleFactory */];
`;
const output = tags.stripIndent`
var AppModuleNgFactory = /*@__PURE__*/new __WEBPACK_IMPORTED_MODULE_1__angular_core__["I" /* NgModuleFactory */];
`;

expect(tags.oneLine`${purify(input)}`).toEqual(tags.oneLine`${output}`);
});
});