Repository navigation
Control emitting Ivy class metadata and Angular module scope information #16694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/ngtools/webpack/src/transformers/remove-ivy-jit-support-calls.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
| import * as ts from 'typescript'; | ||
| import { collectDeepNodes } from './ast_helpers'; | ||
| import { RemoveNodeOperation, StandardTransform, TransformOperation } from './interfaces'; | ||
| import { makeTransform } from './make_transform'; | ||
|
|
||
| export function removeIvyJitSupportCalls( | ||
| classMetadata: boolean, | ||
| ngModuleScope: boolean, | ||
| getTypeChecker: () => ts.TypeChecker, | ||
| ): ts.TransformerFactory<ts.SourceFile> { | ||
| const standardTransform: StandardTransform = function(sourceFile: ts.SourceFile) { | ||
| const ops: TransformOperation[] = []; | ||
|
|
||
| collectDeepNodes<ts.ExpressionStatement>(sourceFile, ts.SyntaxKind.ExpressionStatement) | ||
| .filter(statement => { | ||
| const innerStatement = getIifeStatement(statement); | ||
| if (!innerStatement) { | ||
| return false; | ||
| } | ||
|
|
||
| if (ngModuleScope && ts.isBinaryExpression(innerStatement.expression)) { | ||
| return isIvyPrivateCallExpression(innerStatement.expression.right, 'ɵɵsetNgModuleScope'); | ||
| } | ||
|
|
||
| return ( | ||
| classMetadata && | ||
| isIvyPrivateCallExpression(innerStatement.expression, 'ɵsetClassMetadata') | ||
| ); | ||
| }) | ||
| .forEach(statement => ops.push(new RemoveNodeOperation(sourceFile, statement))); | ||
|
|
||
| return ops; | ||
| }; | ||
|
|
||
| return makeTransform(standardTransform, getTypeChecker); | ||
| } | ||
|
|
||
| // Each Ivy private call expression is inside an IIFE | ||
| function getIifeStatement(exprStmt: ts.ExpressionStatement): null | ts.ExpressionStatement { | ||
| const expression = exprStmt.expression; | ||
| if (!expression || !ts.isCallExpression(expression) || expression.arguments.length !== 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const parenExpr = expression; | ||
| if (!ts.isParenthesizedExpression(parenExpr.expression)) { | ||
| return null; | ||
| } | ||
|
|
||
| const funExpr = parenExpr.expression.expression; | ||
| if (!ts.isFunctionExpression(funExpr)) { | ||
| return null; | ||
| } | ||
|
|
||
| const innerStmts = funExpr.body.statements; | ||
| if (innerStmts.length !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| const innerExprStmt = innerStmts[0]; | ||
| if (!ts.isExpressionStatement(innerExprStmt)) { | ||
| return null; | ||
| } | ||
|
|
||
| return innerExprStmt; | ||
| } | ||
|
|
||
| function isIvyPrivateCallExpression(expression: ts.Expression, name: string) { | ||
| // Now we're in the IIFE and have the inner expression statement. We can check if it matches | ||
| // a private Ivy call. | ||
| if (!ts.isCallExpression(expression)) { | ||
| return false; | ||
| } | ||
|
|
||
| const propAccExpr = expression.expression; | ||
| if (!ts.isPropertyAccessExpression(propAccExpr)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (propAccExpr.name.text != name) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } |
168 changes: 168 additions & 0 deletions
168
packages/ngtools/webpack/src/transformers/remove-ivy-jit-support-calls_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
| import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies | ||
| import * as ts from 'typescript'; | ||
| import { createTypescriptContext, transformTypescript } from './ast_helpers'; | ||
| import { removeIvyJitSupportCalls } from './remove-ivy-jit-support-calls'; | ||
|
|
||
| function transform( | ||
| input: string, | ||
| transformerFactory: ( | ||
| getTypeChecker: () => ts.TypeChecker, | ||
| ) => ts.TransformerFactory<ts.SourceFile>, | ||
| ) { | ||
| const { program, compilerHost } = createTypescriptContext(input); | ||
| const getTypeChecker = () => program.getTypeChecker(); | ||
| const transformer = transformerFactory(getTypeChecker); | ||
|
|
||
| return transformTypescript(input, [transformer], program, compilerHost); | ||
| } | ||
|
|
||
| const input = tags.stripIndent` | ||
| export class AppModule { | ||
| } | ||
| AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); | ||
| AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ | ||
| BrowserModule, | ||
| AppRoutingModule | ||
| ]] }); | ||
| (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(AppModule, { declarations: [AppComponent, | ||
| ExampleComponent], imports: [BrowserModule, | ||
| AppRoutingModule] }); })(); | ||
| /*@__PURE__*/ (function () { i0.ɵsetClassMetadata(AppModule, [{ | ||
| type: NgModule, | ||
| args: [{ | ||
| declarations: [ | ||
| AppComponent, | ||
| ExampleComponent | ||
| ], | ||
| imports: [ | ||
| BrowserModule, | ||
| AppRoutingModule | ||
| ], | ||
| providers: [], | ||
| bootstrap: [AppComponent] | ||
| }] | ||
| }], null, null); })(); | ||
| `; | ||
|
|
||
| describe('@ngtools/webpack transformers', () => { | ||
| describe('remove-ivy-dev-calls', () => { | ||
| it('should allow removing only set class metadata', () => { | ||
| const output = tags.stripIndent` | ||
| export class AppModule { | ||
| } | ||
| AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); | ||
| AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ | ||
| BrowserModule, | ||
| AppRoutingModule | ||
| ]] }); | ||
| (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(AppModule, { declarations: [AppComponent, | ||
| ExampleComponent], imports: [BrowserModule, | ||
| AppRoutingModule] }); })(); | ||
| `; | ||
|
|
||
| const result = transform(input, getTypeChecker => | ||
| removeIvyJitSupportCalls(true, false, getTypeChecker), | ||
| ); | ||
|
|
||
| expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); | ||
| }); | ||
|
|
||
| it('should allow removing only ng module scope', () => { | ||
| const output = tags.stripIndent` | ||
| export class AppModule { | ||
| } | ||
| AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); | ||
| AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ | ||
| BrowserModule, | ||
| AppRoutingModule | ||
| ]] }); | ||
| /*@__PURE__*/ (function () { i0.ɵsetClassMetadata(AppModule, [{ | ||
| type: NgModule, | ||
| args: [{ | ||
| declarations: [ | ||
| AppComponent, | ||
| ExampleComponent | ||
| ], | ||
| imports: [ | ||
| BrowserModule, | ||
| AppRoutingModule | ||
| ], | ||
| providers: [], | ||
| bootstrap: [AppComponent] | ||
| }] | ||
| }], null, null); })(); | ||
| `; | ||
|
|
||
| const result = transform(input, getTypeChecker => | ||
| removeIvyJitSupportCalls(false, true, getTypeChecker), | ||
| ); | ||
|
|
||
| expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); | ||
| }); | ||
|
|
||
| it('should allow removing both set class metadata and ng module scope', () => { | ||
| const output = tags.stripIndent` | ||
| export class AppModule { | ||
| } | ||
| AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); | ||
| AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ | ||
| BrowserModule, | ||
| AppRoutingModule | ||
| ]] }); | ||
| `; | ||
|
|
||
| const result = transform(input, getTypeChecker => | ||
| removeIvyJitSupportCalls(true, true, getTypeChecker), | ||
| ); | ||
|
|
||
| expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); | ||
| }); | ||
|
|
||
| it('should allow removing neither set class metadata nor ng module scope', () => { | ||
| const result = transform(input, getTypeChecker => | ||
| removeIvyJitSupportCalls(false, false, getTypeChecker), | ||
| ); | ||
|
|
||
| expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${input}`); | ||
| }); | ||
|
|
||
| it('should strip unused imports when removing set class metadata and ng module scope', () => { | ||
| const imports = tags.stripIndent` | ||
| import { BrowserModule } from '@angular/platform-browser'; | ||
| import { NgModule } from '@angular/core'; | ||
| import { AppRoutingModule } from './app-routing.module'; | ||
| import { AppComponent } from './app.component'; | ||
| import { ExampleComponent } from './example/example.component'; | ||
| import * as i0 from "@angular/core"; | ||
| `; | ||
|
|
||
| const output = tags.stripIndent` | ||
| import { BrowserModule } from '@angular/platform-browser'; | ||
| import { AppRoutingModule } from './app-routing.module'; | ||
| import { AppComponent } from './app.component'; | ||
| import * as i0 from "@angular/core"; | ||
| export class AppModule { | ||
| } | ||
| AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); | ||
| AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ | ||
| BrowserModule, | ||
| AppRoutingModule | ||
| ]] }); | ||
| `; | ||
|
|
||
| const result = transform(imports + input, getTypeChecker => | ||
| removeIvyJitSupportCalls(true, true, getTypeChecker), | ||
| ); | ||
|
|
||
| expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); | ||
| }); | ||
|
|
||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.