Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

feat: Support Angular Ivy modules with AOT #828

Merged
merged 2 commits into from
Mar 13, 2019
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
32 changes: 32 additions & 0 deletions transformers/ns-replace-bootstrap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
15 changes: 8 additions & 7 deletions transformers/ns-replace-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import {
import { AngularCompilerPlugin } from '@ngtools/webpack';
import { getResolvedEntryModule } from "../utils/transformers-utils";


export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin): ts.TransformerFactory<ts.SourceFile> {
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 = (<any>ngCompiler)._compilerOptions && (<any>ngCompiler)._compilerOptions.enableIvy;
const entryModule = getResolvedEntryModule(ngCompiler);

if (!shouldTransform(sourceFile.fileName) || !entryModule) {
return ops;
Expand Down Expand Up @@ -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;
Expand Down