This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
feat: NativeScript bootstrap transformer #634
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as ts from 'typescript'; | ||
import { AngularCompilerPlugin } from '@ngtools/webpack'; | ||
export declare function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin): ts.TransformerFactory<ts.SourceFile>; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { dirname, relative } from 'path'; | ||
import * as ts from 'typescript'; | ||
import { | ||
StandardTransform, | ||
TransformOperation, | ||
collectDeepNodes, | ||
insertStarImport, | ||
ReplaceNodeOperation, | ||
makeTransform | ||
} from "@ngtools/webpack/src/transformers"; | ||
import { workaroundResolve } from '@ngtools/webpack/src/compiler_host'; | ||
import { AngularCompilerPlugin } from '@ngtools/webpack'; | ||
|
||
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 ngCompiler = getNgCompiler(); | ||
|
||
const entryModule = ngCompiler.entryModule | ||
? { path: workaroundResolve(ngCompiler.entryModule.path), className: getNgCompiler().entryModule.className } | ||
: ngCompiler.entryModule; | ||
|
||
if (!shouldTransform(sourceFile.fileName) || !entryModule) { | ||
return ops; | ||
} | ||
|
||
// Find all identifiers. | ||
const entryModuleIdentifiers = collectDeepNodes<ts.Identifier>(sourceFile, | ||
ts.SyntaxKind.Identifier) | ||
.filter(identifier => identifier.text === entryModule.className); | ||
|
||
if (entryModuleIdentifiers.length === 0) { | ||
return []; | ||
} | ||
|
||
const relativeEntryModulePath = relative(dirname(sourceFile.fileName), entryModule.path); | ||
const normalizedEntryModulePath = `./${relativeEntryModulePath}`.replace(/\\/g, '/'); | ||
|
||
// Find the bootstrap calls. | ||
entryModuleIdentifiers.forEach(entryModuleIdentifier => { | ||
// Figure out if it's a `platformNativeScriptDynamic().bootstrapModule(AppModule)` call. | ||
if (!( | ||
entryModuleIdentifier.parent | ||
&& entryModuleIdentifier.parent.kind === ts.SyntaxKind.CallExpression | ||
)) { | ||
return; | ||
} | ||
|
||
const callExpr = entryModuleIdentifier.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 === 'platformNativeScriptDynamic' | ||
)) { | ||
return; | ||
} | ||
|
||
const platformBrowserDynamicIdentifier = innerCallExpr.expression as ts.Identifier; | ||
|
||
const idPlatformBrowser = ts.createUniqueName('__NgCli_bootstrap_'); | ||
const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_'); | ||
|
||
// Add the transform operations. | ||
const factoryClassName = entryModule.className + 'NgFactory'; | ||
const factoryModulePath = normalizedEntryModulePath + '.ngfactory'; | ||
ops.push( | ||
// Replace the entry module import. | ||
...insertStarImport(sourceFile, idNgFactory, factoryModulePath), | ||
new ReplaceNodeOperation(sourceFile, entryModuleIdentifier, | ||
ts.createPropertyAccess(idNgFactory, ts.createIdentifier(factoryClassName))), | ||
|
||
// Replace the platformBrowserDynamic import. | ||
...insertStarImport(sourceFile, idPlatformBrowser, 'nativescript-angular/platform-static'), | ||
new ReplaceNodeOperation(sourceFile, platformBrowserDynamicIdentifier, | ||
ts.createPropertyAccess(idPlatformBrowser, 'platformNativeScript')), | ||
|
||
new ReplaceNodeOperation(sourceFile, bootstrapModuleIdentifier, | ||
ts.createIdentifier('bootstrapModuleFactory')), | ||
); | ||
}); | ||
|
||
return ops; | ||
}; | ||
|
||
return makeTransform(standardTransform, getTypeChecker); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the
getAotEntryModule
function from index.js as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a breaking change if we remove it completely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. We discussed to introduce a
Deprecated
decorator in another PR.