Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace entry and bootstrap for aot build #40

Merged
merged 1 commit into from
Nov 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/webpack-titanium-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"url": "https://github.com/appcelerator/titanium-webpack-devkit.git"
},
"devDependencies": {
"@ngtools/webpack": "^9.1.8"
"@ngtools/webpack": "~9.1.0",
"typescript": "~3.8.0"
},
"peerDependencies": {
"@ngtools/webpack": "~9.1.0"
"@ngtools/webpack": "~9.1.0",
"typescript": "~3.8.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { AngularCompilerPlugin } = require('@ngtools/webpack');
const { VirtualWatchFileSystemDecorator } = require('@ngtools/webpack/src/virtual_file_system_decorator');

const { replaceBootstrap } = require('./transformers')
const PlatformAwareFileSystem = require('./PlatformAwareFileSystem');

/**
Expand All @@ -11,6 +12,14 @@ const PlatformAwareFileSystem = require('./PlatformAwareFileSystem');
*/
class TitaniumAngularCompilerPlugin extends AngularCompilerPlugin {
constructor(options) {
if (options.skipCodeGeneration) {
const platformTransforms = [];
const getEntryModule = () => this.entryModule
const getTypeChecker = () => this._getTsProgram().getTypeChecker()
platformTransforms.push(replaceBootstrap(getEntryModule, getTypeChecker))
options.platformTransforms = platformTransforms
}

super(options);

this.targetPlatform = options.targetPlatform;
Expand Down
5 changes: 5 additions & 0 deletions packages/webpack-titanium-angular/src/transformers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const replaceBootstrap = require('./replpaceBootstrap')

module.exports = {
replaceBootstrap
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const {
collectDeepNodes,
insertStarImport,
makeTransform,
ReplaceNodeOperation
} = require('@ngtools/webpack/src/transformers')
const { forwardSlashPath } = require('@ngtools/webpack/src/utils')
const { dirname, relative } = require('path')
const ts = require('typescript')

module.exports = function replaceBootstrap(
getEntryModule,
getTypeChecker,
useFactories = true
) {
const standardTransform = function(sourceFile) {
const ops = []
const entryModule = getEntryModule()
const entryModuleIdentifiers = collectDeepNodes(
sourceFile, ts.SyntaxKind.Identifier
).filter(identifier => identifier.text === entryModule.className)

if (entryModuleIdentifiers.length === 0) {
return []
}

// Find the bootstrap calls.
entryModuleIdentifiers.forEach(entryModuleIdentifier => {
// Figure out if it's a `platformTitaniumDynamic().bootstrapModule(AppModule)` call.
if (!(
entryModuleIdentifier.parent
&& entryModuleIdentifier.parent.kind === ts.SyntaxKind.CallExpression
)) {
return
}

const callExpr = entryModuleIdentifier.parent
if (callExpr.expression.kind !== ts.SyntaxKind.PropertyAccessExpression) {
return
}

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

const bootstrapModuleIdentifier = propAccessExpr.name
const innerCallExpr = propAccessExpr.expression
if (!(
innerCallExpr.expression.kind === ts.SyntaxKind.Identifier
&& innerCallExpr.expression.text === 'platformTitaniumDynamic'
)) {
return;
}

const platformTitaniumDynamicIdentifier = innerCallExpr.expression
const idPlatformTitanium = ts.createUniqueName('__TiNg_bootstrap_');
const idNgFactory = ts.createUniqueName('__TiNg_bootstrap_');

// Add the transform operations.
const relativeEntryModulePath = relative(dirname(sourceFile.fileName), entryModule.path);
let className = entryModule.className;
let modulePath = forwardSlashPath(`./${relativeEntryModulePath}`);
let bootstrapIdentifier = 'bootstrapModule';

if (useFactories) {
className += 'NgFactory';
modulePath += '.ngfactory';
bootstrapIdentifier = 'bootstrapModuleFactory';
}

ops.push(
// Replace the entry module import.
...insertStarImport(sourceFile, idNgFactory, modulePath),
new ReplaceNodeOperation(sourceFile, entryModuleIdentifier,
ts.createPropertyAccess(idNgFactory, ts.createIdentifier(className))),
// Replace the platformTitaniumDynamic import.
...insertStarImport(sourceFile, idPlatformTitanium, 'titanium-angular'),
new ReplaceNodeOperation(sourceFile, platformTitaniumDynamicIdentifier,
ts.createPropertyAccess(idPlatformTitanium, 'platformTitanium')),
new ReplaceNodeOperation(sourceFile, bootstrapModuleIdentifier,
ts.createIdentifier(bootstrapIdentifier)),
);
})

return ops
}

return makeTransform(standardTransform, getTypeChecker)
}