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

Commit 7fa1786

Browse files
committed
fix-next(transformer): insert imports at the beginning of the file
It's very important for one of the two imports that we're inserting (`nativescript-angular/platform-static`) to be the first executed line of the app. fixes #639
1 parent 3b2af22 commit 7fa1786

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ transformers/ns-replace-bootstrap.d.ts
1111
transformers/ns-replace-bootstrap.js
1212
transformers/ns-replace-bootstrap.js.map
1313

14+
transformers/ast-utils.d.ts
15+
transformers/ast-utils.js
16+
transformers/ast-utils.js.map
17+
1418
plugins/PlatformFSPlugin.d.ts
1519
plugins/PlatformFSPlugin.js
1620
plugins/PlatformFSPlugin.js.map

transformers/ast-utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as ts from 'typescript';
2+
3+
export function getFirstNode(sourceFile: ts.SourceFile): ts.Node {
4+
if (sourceFile.statements.length > 0) {
5+
return sourceFile.statements[0];
6+
}
7+
return sourceFile.getChildAt(0);
8+
}

transformers/ns-replace-bootstrap.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
import { workaroundResolve } from '@ngtools/webpack/src/compiler_host';
1212
import { AngularCompilerPlugin } from '@ngtools/webpack';
1313

14+
import { getFirstNode } from './ast-utils';
15+
1416
export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin): ts.TransformerFactory<ts.SourceFile> {
1517
const shouldTransform = (fileName) => !fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
1618
const getTypeChecker = () => getNgCompiler().typeChecker;
@@ -77,23 +79,37 @@ export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin):
7779
const idPlatformBrowser = ts.createUniqueName('__NgCli_bootstrap_');
7880
const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_');
7981

82+
const firstNode = getFirstNode(sourceFile);
83+
8084
// Add the transform operations.
8185
const factoryClassName = entryModule.className + 'NgFactory';
8286
const factoryModulePath = normalizedEntryModulePath + '.ngfactory';
8387
ops.push(
84-
// Insert an import of the module factory:
88+
// Insert an import of the {N} Angular static bootstrap module in the beginning of the file:
89+
// import * as __NgCli_bootstrap_2 from "nativescript-angular/platform-static";
90+
...insertStarImport(
91+
sourceFile,
92+
idPlatformBrowser,
93+
'nativescript-angular/platform-static',
94+
firstNode,
95+
true,
96+
),
97+
98+
// Insert an import of the module factory in the beginning of the file:
8599
// import * as __NgCli_bootstrap_1 from "./app.module.ngfactory";
86-
...insertStarImport(sourceFile, idNgFactory, factoryModulePath),
100+
...insertStarImport(
101+
sourceFile,
102+
idNgFactory,
103+
factoryModulePath,
104+
firstNode,
105+
true,
106+
),
87107

88108
// Replace the NgModule nodes with NgModuleFactory nodes
89109
// from 'AppModule' to 'AppModuleNgFactory'
90110
new ReplaceNodeOperation(sourceFile, entryModuleIdentifier,
91111
ts.createPropertyAccess(idNgFactory, ts.createIdentifier(factoryClassName))),
92112

93-
// Insert an import of the {N} Angular static bootstrap module:
94-
// import * as __NgCli_bootstrap_2 from "nativescript-angular/platform-static";
95-
...insertStarImport(sourceFile, idPlatformBrowser, 'nativescript-angular/platform-static'),
96-
97113
// Replace 'platformNativeScriptDynamic' with 'platformNativeScript'
98114
// and elide all imports of 'platformNativeScriptDynamic'
99115
new ReplaceNodeOperation(sourceFile, platformBrowserDynamicIdentifier,

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"plugins/PlatformFSPlugin.ts",
1212
"plugins/WatchStateLoggerPlugin.ts",
1313
"transformers/ns-replace-bootstrap.ts",
14+
"transformers/ast-utils.ts",
1415
"host/resolver.ts"
1516
]
1617
}

0 commit comments

Comments
 (0)