|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies |
| 9 | +import * as ts from 'typescript'; |
| 10 | +import { createTypescriptContext, transformTypescript } from './ast_helpers'; |
| 11 | +import { removeIvyJitSupportCalls } from './remove-ivy-jit-support-calls'; |
| 12 | + |
| 13 | +function transform( |
| 14 | + input: string, |
| 15 | + transformerFactory: ( |
| 16 | + getTypeChecker: () => ts.TypeChecker, |
| 17 | + ) => ts.TransformerFactory<ts.SourceFile>, |
| 18 | +) { |
| 19 | + const { program, compilerHost } = createTypescriptContext(input); |
| 20 | + const getTypeChecker = () => program.getTypeChecker(); |
| 21 | + const transformer = transformerFactory(getTypeChecker); |
| 22 | + |
| 23 | + return transformTypescript(input, [transformer], program, compilerHost); |
| 24 | +} |
| 25 | + |
| 26 | +const input = tags.stripIndent` |
| 27 | + export class AppModule { |
| 28 | + } |
| 29 | + AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); |
| 30 | + AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ |
| 31 | + BrowserModule, |
| 32 | + AppRoutingModule |
| 33 | + ]] }); |
| 34 | + (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(AppModule, { declarations: [AppComponent, |
| 35 | + ExampleComponent], imports: [BrowserModule, |
| 36 | + AppRoutingModule] }); })(); |
| 37 | + /*@__PURE__*/ (function () { i0.ɵsetClassMetadata(AppModule, [{ |
| 38 | + type: NgModule, |
| 39 | + args: [{ |
| 40 | + declarations: [ |
| 41 | + AppComponent, |
| 42 | + ExampleComponent |
| 43 | + ], |
| 44 | + imports: [ |
| 45 | + BrowserModule, |
| 46 | + AppRoutingModule |
| 47 | + ], |
| 48 | + providers: [], |
| 49 | + bootstrap: [AppComponent] |
| 50 | + }] |
| 51 | + }], null, null); })(); |
| 52 | +`; |
| 53 | + |
| 54 | +describe('@ngtools/webpack transformers', () => { |
| 55 | + describe('remove-ivy-dev-calls', () => { |
| 56 | + it('should allow removing only set class metadata', () => { |
| 57 | + const output = tags.stripIndent` |
| 58 | + export class AppModule { |
| 59 | + } |
| 60 | + AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); |
| 61 | + AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ |
| 62 | + BrowserModule, |
| 63 | + AppRoutingModule |
| 64 | + ]] }); |
| 65 | + (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(AppModule, { declarations: [AppComponent, |
| 66 | + ExampleComponent], imports: [BrowserModule, |
| 67 | + AppRoutingModule] }); })(); |
| 68 | + `; |
| 69 | + |
| 70 | + const result = transform(input, getTypeChecker => |
| 71 | + removeIvyJitSupportCalls(true, false, getTypeChecker), |
| 72 | + ); |
| 73 | + |
| 74 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should allow removing only ng module scope', () => { |
| 78 | + const output = tags.stripIndent` |
| 79 | + export class AppModule { |
| 80 | + } |
| 81 | + AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); |
| 82 | + AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ |
| 83 | + BrowserModule, |
| 84 | + AppRoutingModule |
| 85 | + ]] }); |
| 86 | + /*@__PURE__*/ (function () { i0.ɵsetClassMetadata(AppModule, [{ |
| 87 | + type: NgModule, |
| 88 | + args: [{ |
| 89 | + declarations: [ |
| 90 | + AppComponent, |
| 91 | + ExampleComponent |
| 92 | + ], |
| 93 | + imports: [ |
| 94 | + BrowserModule, |
| 95 | + AppRoutingModule |
| 96 | + ], |
| 97 | + providers: [], |
| 98 | + bootstrap: [AppComponent] |
| 99 | + }] |
| 100 | + }], null, null); })(); |
| 101 | + `; |
| 102 | + |
| 103 | + const result = transform(input, getTypeChecker => |
| 104 | + removeIvyJitSupportCalls(false, true, getTypeChecker), |
| 105 | + ); |
| 106 | + |
| 107 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should allow removing both set class metadata and ng module scope', () => { |
| 111 | + const output = tags.stripIndent` |
| 112 | + export class AppModule { |
| 113 | + } |
| 114 | + AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); |
| 115 | + AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ |
| 116 | + BrowserModule, |
| 117 | + AppRoutingModule |
| 118 | + ]] }); |
| 119 | + `; |
| 120 | + |
| 121 | + const result = transform(input, getTypeChecker => |
| 122 | + removeIvyJitSupportCalls(true, true, getTypeChecker), |
| 123 | + ); |
| 124 | + |
| 125 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should allow removing neither set class metadata nor ng module scope', () => { |
| 129 | + const result = transform(input, getTypeChecker => |
| 130 | + removeIvyJitSupportCalls(false, false, getTypeChecker), |
| 131 | + ); |
| 132 | + |
| 133 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${input}`); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should strip unused imports when removing set class metadata and ng module scope', () => { |
| 137 | + const imports = tags.stripIndent` |
| 138 | + import { BrowserModule } from '@angular/platform-browser'; |
| 139 | + import { NgModule } from '@angular/core'; |
| 140 | + import { AppRoutingModule } from './app-routing.module'; |
| 141 | + import { AppComponent } from './app.component'; |
| 142 | + import { ExampleComponent } from './example/example.component'; |
| 143 | + import * as i0 from "@angular/core"; |
| 144 | + `; |
| 145 | + |
| 146 | + const output = tags.stripIndent` |
| 147 | + import { BrowserModule } from '@angular/platform-browser'; |
| 148 | + import { AppRoutingModule } from './app-routing.module'; |
| 149 | + import { AppComponent } from './app.component'; |
| 150 | + import * as i0 from "@angular/core"; |
| 151 | + export class AppModule { |
| 152 | + } |
| 153 | + AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] }); |
| 154 | + AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[ |
| 155 | + BrowserModule, |
| 156 | + AppRoutingModule |
| 157 | + ]] }); |
| 158 | + `; |
| 159 | + |
| 160 | + const result = transform(imports + input, getTypeChecker => |
| 161 | + removeIvyJitSupportCalls(true, true, getTypeChecker), |
| 162 | + ); |
| 163 | + |
| 164 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); |
| 165 | + }); |
| 166 | + |
| 167 | + }); |
| 168 | +}); |
0 commit comments