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

feat(@angular-devkit/build-optimizer): support es2015 #237

Closed
wants to merge 1 commit into from
Closed
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 @@ -32,15 +32,6 @@ const whitelistedAngularModules = [
/[\\/]node_modules[\\/]@angular[\\/]cdk[\\/]/,
];

const es5AngularModules = [
// Angular 4 packaging format has .es5.js as the extension.
/\.es5\.js$/, // Angular 4
// Angular 5 has esm5 folders.
/[\\/]node_modules[\\/]@angular[\\/][^\\/]+[\\/]esm5[\\/]/,
// All Angular versions have UMD with es5.
/\.umd\.js$/,
];

export interface BuildOptimizerOptions {
content?: string;
inputFilePath?: string;
Expand Down Expand Up @@ -77,9 +68,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 (inputFilePath && whitelistedAngularModules.some((re) => re.test(inputFilePath))
) {
getTransforms.push(
// getPrefixFunctionsTransformer is rather dangerous, apply only to known pure es5 modules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function findTopLevelFunctions(parentNode: ts.Node): ts.Node[] {
topLevelFunctions.push(node.parent);
} else if ((node.kind === ts.SyntaxKind.CallExpression
|| node.kind === ts.SyntaxKind.NewExpression)
&& !isSuperCall(node)
&& !hasPureComment(node)
) {
topLevelFunctions.push(node);
Expand Down Expand Up @@ -117,3 +118,9 @@ function hasPureComment(node: ts.Node) {

return leadingComment && leadingComment.some((comment) => comment.text === pureFunctionComment);
}

function isSuperCall(node: ts.Node) {
const callExpr = node as ts.CallExpression;

return callExpr.expression && callExpr.expression.kind === ts.SyntaxKind.SuperKeyword;
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('prefix-functions', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('doesn\'t adds comment when inside function declarations or expressions', () => {
it('doesn\'t add comment when inside function declarations or expressions', () => {
const input = tags.stripIndent`
function funcDecl() {
var newClazz = Clazz();
Expand All @@ -102,5 +102,21 @@ describe('prefix-functions', () => {

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

it('doesn\'t add comment to super calls', () => {
const input = tags.stripIndent`
class ExtendedClass extends BaseClass {
constructor(e) {
super(e);
}
}
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function testScrubFile(content: string) {

// Don't remove `ctorParameters` from these.
const platformWhitelist = [
'PlatformRef',
'PlatformRef_',
'TestabilityRegistry',
'Console',
Expand Down