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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getScrubFileTransformer } from '../transforms/scrub-file';
const hasDecorators = /decorators/;
const hasCtorParameters = /ctorParameters/;
const hasTsHelpers = /var (__extends|__decorate|__metadata|__param) = /;
const isAngularPackage = /(\\|\/)node_modules(\\|\/)@angular(\\|\/)/;

export interface BuildOptimizerOptions {
content?: string;
Expand Down Expand Up @@ -50,15 +51,22 @@ export function buildOptimizer(options: BuildOptimizerOptions):
getTransforms.push(getImportTslibTransformer);
}


if (hasDecorators.test(content) || hasCtorParameters.test(content)) {
if (inputFilePath && isAngularPackage.test(inputFilePath)) {
// Order matters, getPrefixFunctionsTransformer needs to be called before
// getFoldFileTransformer.
getTransforms.push(...[
getTransforms.push(
// getPrefixFunctionsTransformer is rather dangerous.
// It will mark both `require()` calls and `console.log(stuff)` as pure.
// We only apply it to @angular/* packages, since we know they are safe.
getPrefixFunctionsTransformer,
getScrubFileTransformer,
getFoldFileTransformer,
]);
);
} else if (hasDecorators.test(content) || hasCtorParameters.test(content)) {
getTransforms.push(
getScrubFileTransformer,
getFoldFileTransformer,
);
}

if (getTransforms.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ describe('build-optimizer', () => {
`;
// tslint:enable:max-line-length

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

it('doesn\'t process files without decorators/ctorParameters', () => {
it('doesn\'t process files without decorators/ctorParameters/outside Angular', () => {
const input = oneLine`
var Clazz = (function () { function Clazz() { } return Clazz; }());
${staticProperty}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export default function buildOptimizerLoader
this.cacheable();
const options: BuildOptimizerLoaderOptions = loaderUtils.getOptions(this) || {};

const boOutput = buildOptimizer({ content, emitSourceMap: options.sourceMap });
const boOutput = buildOptimizer({
content,
inputFilePath: this.resourcePath,
emitSourceMap: options.sourceMap,
});
const intermediateSourceMap = boOutput.sourceMap;
let newContent = boOutput.content;

Expand Down