From 82f52b67e4acbf4f2f9761177169b9d901ef60fb Mon Sep 17 00:00:00 2001 From: VladimirAmiorkov Date: Mon, 11 Mar 2019 13:40:23 +0200 Subject: [PATCH 1/2] feat: support Angular Ivy modules --- transformers/ns-replace-bootstrap.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/transformers/ns-replace-bootstrap.ts b/transformers/ns-replace-bootstrap.ts index 3ee06420..fabf9f1f 100644 --- a/transformers/ns-replace-bootstrap.ts +++ b/transformers/ns-replace-bootstrap.ts @@ -15,14 +15,16 @@ import { import { AngularCompilerPlugin } from '@ngtools/webpack'; import { getResolvedEntryModule } from "../utils/transformers-utils"; - export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin): ts.TransformerFactory { const shouldTransform = (fileName) => !fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts'); const getTypeChecker = () => getNgCompiler().typeChecker; const standardTransform: StandardTransform = function (sourceFile: ts.SourceFile) { const ops: TransformOperation[] = []; - const entryModule = getResolvedEntryModule(getNgCompiler()); + const ngCompiler = getNgCompiler(); + // TODO: use something public when available + const enableIvy = (ngCompiler)._compilerOptions && (ngCompiler)._compilerOptions.enableIvy; + const entryModule = getResolvedEntryModule(ngCompiler); if (!shouldTransform(sourceFile.fileName) || !entryModule) { return ops; @@ -73,16 +75,15 @@ export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin): const firstNode = getFirstNode(sourceFile); - // Add the transform operations. - const factoryClassName = entryModule.className + 'NgFactory'; - const factoryModulePath = normalizedEntryModulePath + '.ngfactory'; - + const factoryClassName = enableIvy ? entryModule.className : entryModule.className + 'NgFactory'; + const factoryModulePath = enableIvy ? normalizedEntryModulePath : normalizedEntryModulePath + '.ngfactory'; const newBootstrapPropAccessExpr = ts.getMutableClone(bootstrapPropAccessExpr); const newNsPlatformCallExpr = ts.getMutableClone(bootstrapPropAccessExpr.expression) as ts.CallExpression; newNsPlatformCallExpr.expression = ts.createPropertyAccess(idPlatformNativeScript, 'platformNativeScript'); newBootstrapPropAccessExpr.expression = newNsPlatformCallExpr; - newBootstrapPropAccessExpr.name = ts.createIdentifier("bootstrapModuleFactory"); + newBootstrapPropAccessExpr.name = + enableIvy ? ts.createIdentifier("bootstrapModule") : ts.createIdentifier("bootstrapModuleFactory"); const newBootstrapCallExpr = ts.getMutableClone(bootstrapCallExpr); newBootstrapCallExpr.expression = newBootstrapPropAccessExpr; From 862241409c1dfe398b14cbf03aecd3b6102bde8a Mon Sep 17 00:00:00 2001 From: DimitarTachev Date: Wed, 13 Mar 2019 10:38:28 +0200 Subject: [PATCH 2/2] test: add test for AOT transformation of Ivy compatible modules --- transformers/ns-replace-bootstrap.spec.ts | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/transformers/ns-replace-bootstrap.spec.ts b/transformers/ns-replace-bootstrap.spec.ts index 0e7d3bf6..756a7e02 100644 --- a/transformers/ns-replace-bootstrap.spec.ts +++ b/transformers/ns-replace-bootstrap.spec.ts @@ -34,6 +34,38 @@ describe('@ngtools/webpack transformers', () => { expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); }); + it('should replace bootstrap and don`t use factories when Ivy is enabled', () => { + const input = tags.stripIndent` + import { platformNativeScriptDynamic } from "nativescript-angular/platform"; + import { AppModule } from "./app/app.module"; + + platformNativeScriptDynamic().bootstrapModule(AppModule); + `; + + const output = tags.stripIndent` + import * as __NgCli_bootstrap_1_1 from "nativescript-angular/platform-static"; + import * as __NgCli_bootstrap_2_1 from "./app/app.module"; + + __NgCli_bootstrap_1_1.platformNativeScript().bootstrapModule(__NgCli_bootstrap_2_1.AppModule); + `; + + const { program, compilerHost } = createTypescriptContext(input); + const ngCompiler: any = { + _compilerOptions: { + enableIvy: true + }, + typeChecker: program.getTypeChecker(), + entryModule: { + path: '/project/src/app/app.module', + className: 'AppModule', + }, + }; + const transformer = nsReplaceBootstrap(() => ngCompiler); + const result = transformTypescript(undefined, [transformer], program, compilerHost); + + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); + }); + it('should replace bootstrap when barrel files are used', () => { const input = tags.stripIndent` import { platformNativeScriptDynamic } from "nativescript-angular/platform";