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

feat(@ngtools/webpack): replace bootstrap code for NativeScript apps #960

Closed
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
11 changes: 9 additions & 2 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import {
findResources,
registerLocaleData,
removeDecorators,
replaceBootstrap,
replaceBrowserBootstrap,
replaceNativeScriptBootstrap,
replaceResources,
replaceServerBootstrap,
} from './transformers';
Expand Down Expand Up @@ -95,6 +96,7 @@ export interface AngularCompilerPluginOptions {
export enum PLATFORM {
Browser,
Server,
NativeScript,
}

export class AngularCompilerPlugin {
Expand Down Expand Up @@ -768,7 +770,7 @@ export class AngularCompilerPlugin {

if (!this._JitMode) {
// Replace bootstrap in browser AOT.
this._transformers.push(replaceBootstrap(isAppPath, getEntryModule, getTypeChecker));
this._transformers.push(replaceBrowserBootstrap(isAppPath, getEntryModule, getTypeChecker));
}
} else if (this._platform === PLATFORM.Server) {
this._transformers.push(exportLazyModuleMap(isMainPath, getLazyRoutes));
Expand All @@ -777,6 +779,11 @@ export class AngularCompilerPlugin {
exportNgFactory(isMainPath, getEntryModule),
replaceServerBootstrap(isMainPath, getEntryModule, getTypeChecker));
}
} else if (this._platform === PLATFORM.NativeScript) {
if (!this._JitMode) {
this._transformers.push(
replaceNativeScriptBootstrap(isAppPath, getEntryModule, getTypeChecker));
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/ngtools/webpack/src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export * from './ast_helpers';
export * from './make_transform';
export * from './insert_import';
export * from './elide_imports';
export * from './replace_bootstrap';
export * from './replace_browser_bootstrap';
export * from './replace_server_bootstrap';
export * from './replace_nativescript_bootstrap';
export * from './export_ngfactory';
export * from './export_lazy_module_map';
export * from './register_locale_data';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createTypescriptContext, transformTypescript } from './ast_helpers';
import { exportLazyModuleMap } from './export_lazy_module_map';
import { exportNgFactory } from './export_ngfactory';
import { removeDecorators } from './remove_decorators';
import { replaceBootstrap } from './replace_bootstrap';
import { replaceBrowserBootstrap } from './replace_browser_bootstrap';


describe('@ngtools/webpack transformers', () => {
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('@ngtools/webpack transformers', () => {


const transformers = [
replaceBootstrap(shouldTransform, getEntryModule, getTypeChecker),
replaceBrowserBootstrap(shouldTransform, getEntryModule, getTypeChecker),
exportNgFactory(shouldTransform, getEntryModule),
exportLazyModuleMap(shouldTransform,
() => ({
Expand Down
102 changes: 0 additions & 102 deletions packages/ngtools/webpack/src/transformers/replace_bootstrap.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* @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 { dirname, relative } from 'path';
import * as ts from 'typescript';
import { collectDeepNodes } from './ast_helpers';
import { insertStarImport } from './insert_import';
import { ReplaceNodeOperation, StandardTransform, TransformOperation } from './interfaces';
import { makeTransform } from './make_transform';

export interface PlatformBootstrapOptions {
dynamicPlatformName: string;
staticPlatformName: string;
staticPlatformPath: string;
}

export interface BootstrapReplaceOptions extends PlatformBootstrapOptions {
factoryClassName: string;
factoryModulePath: string;
}

export type BootstrapReplaceFunction = (
identifiers: ts.Identifier[],
sourceFile: ts.SourceFile,
platformOptions: PlatformBootstrapOptions)
=> TransformOperation[];

export function replaceBootstrap(
shouldTransform: (fileName: string) => boolean,
getEntryModule: () => { path: string, className: string } | null,
getTypeChecker: () => ts.TypeChecker,
replaceFunctions: BootstrapReplaceFunction[],
platformOptions: PlatformBootstrapOptions,
): ts.TransformerFactory<ts.SourceFile> {

const standardTransform: StandardTransform = function (sourceFile: ts.SourceFile) {
const entryModule = getEntryModule();

if (!shouldTransform(sourceFile.fileName) || !entryModule) {
return [];
}

// Find all identifiers.
const entryModuleIdentifiers = collectDeepNodes<ts.Identifier>(sourceFile,
ts.SyntaxKind.Identifier)
.filter(identifier => identifier.text === entryModule.className);

const relativeEntryModulePath = relative(dirname(sourceFile.fileName), entryModule.path);
const normalizedEntryModulePath = `./${relativeEntryModulePath}`.replace(/\\/g, '/');
const factoryClassName = entryModule.className + 'NgFactory';
const factoryModulePath = normalizedEntryModulePath + '.ngfactory';

const bootstrapReplaceOptions: BootstrapReplaceOptions = {
...platformOptions,
factoryClassName,
factoryModulePath,
};

return replaceFunctions.reduce((ops, fn) =>
[
...ops,
...fn(entryModuleIdentifiers, sourceFile, bootstrapReplaceOptions),
], []);
};

return makeTransform(standardTransform, getTypeChecker);
}

// Figure out if it's a `platformDynamic().bootstrapModule(AppModule)` call.
export function replacePlatformBootstrap(
identifiers: ts.Identifier[],
sourceFile: ts.SourceFile,
bootstrapOptions: BootstrapReplaceOptions,
): TransformOperation[] {

const ops: TransformOperation[] = [];

identifiers.forEach(identifier => {

if (!(
identifier.parent
&& identifier.parent.kind === ts.SyntaxKind.CallExpression
)) {
return;
}

const callExpr = identifier.parent as ts.CallExpression;

if (callExpr.expression.kind !== ts.SyntaxKind.PropertyAccessExpression) {
return;
}

const propAccessExpr = callExpr.expression as ts.PropertyAccessExpression;

if (propAccessExpr.name.text !== 'bootstrapModule'
|| propAccessExpr.expression.kind !== ts.SyntaxKind.CallExpression) {
return;
}

const bootstrapModuleIdentifier = propAccessExpr.name;
const innerCallExpr = propAccessExpr.expression as ts.CallExpression;

if (!(
innerCallExpr.expression.kind === ts.SyntaxKind.Identifier
&& (innerCallExpr.expression as ts.Identifier).text === bootstrapOptions.dynamicPlatformName
)) {
return;
}

const dynamicPlatformIdentifier = innerCallExpr.expression as ts.Identifier;

const idPlatformStatic = ts.createUniqueName('__NgCli_bootstrap_');
const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_');

// Add the transform operations.
ops.push(
// Replace the entry module import.
...insertStarImport(sourceFile, idNgFactory, bootstrapOptions.factoryModulePath),
new ReplaceNodeOperation(sourceFile, identifier,
ts.createPropertyAccess(
idNgFactory,
ts.createIdentifier(bootstrapOptions.factoryClassName))),
// Replace the platformDynamic import.
...insertStarImport(sourceFile, idPlatformStatic, bootstrapOptions.staticPlatformPath),
new ReplaceNodeOperation(sourceFile, dynamicPlatformIdentifier,
ts.createPropertyAccess(idPlatformStatic, bootstrapOptions.staticPlatformName)),
new ReplaceNodeOperation(sourceFile, bootstrapModuleIdentifier,
ts.createIdentifier('bootstrapModuleFactory')),
);
});

return ops;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @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 {
PlatformBootstrapOptions,
replaceBootstrap,
replacePlatformBootstrap,
} from './replace_bootstrap_helpers';

export function replaceBrowserBootstrap(
shouldTransform: (fileName: string) => boolean,
getEntryModule: () => { path: string, className: string } | null,
getTypeChecker: () => ts.TypeChecker,
): ts.TransformerFactory<ts.SourceFile> {

const platformOptions: PlatformBootstrapOptions = {
dynamicPlatformName: 'platformBrowserDynamic',
staticPlatformName: 'platformBrowser',
staticPlatformPath: '@angular/platform-browser',
};

const replaceFunctions = [
replacePlatformBootstrap,
];

return replaceBootstrap(
shouldTransform,
getEntryModule,
getTypeChecker,
replaceFunctions,
platformOptions,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
import { createTypescriptContext, transformTypescript } from './ast_helpers';
import { replaceBootstrap } from './replace_bootstrap';
import { replaceBrowserBootstrap } from './replace_browser_bootstrap';

describe('@ngtools/webpack transformers', () => {
describe('replace_bootstrap', () => {
describe('replace_browser_bootstrap', () => {
it('should replace bootstrap', () => {
const input = tags.stripIndent`
import { enableProdMode } from '@angular/core';
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('@ngtools/webpack transformers', () => {
// tslint:enable:max-line-length

const { program, compilerHost } = createTypescriptContext(input);
const transformer = replaceBootstrap(
const transformer = replaceBrowserBootstrap(
() => true,
() => ({ path: '/project/src/app/app.module', className: 'AppModule' }),
() => program.getTypeChecker(),
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('@ngtools/webpack transformers', () => {
// tslint:enable:max-line-length

const { program, compilerHost } = createTypescriptContext(input);
const transformer = replaceBootstrap(
const transformer = replaceBrowserBootstrap(
() => true,
() => ({ path: '/project/src/app/app.module', className: 'AppModule' }),
() => program.getTypeChecker(),
Expand All @@ -109,7 +109,7 @@ describe('@ngtools/webpack transformers', () => {
`;

const { program, compilerHost } = createTypescriptContext(input);
const transformer = replaceBootstrap(
const transformer = replaceBrowserBootstrap(
() => true,
() => null,
() => program.getTypeChecker(),
Expand Down
Loading