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

feat(@ngtools/webpack): allow custom platform transformers #973

Merged
merged 1 commit into from
May 29, 2018
Merged
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
47 changes: 29 additions & 18 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface AngularCompilerPluginOptions {
compilerOptions?: ts.CompilerOptions;

host?: virtualFs.Host<fs.Stats>;
platformTransformers?: ts.TransformerFactory<ts.SourceFile>[];
}

export enum PLATFORM {
Expand All @@ -115,6 +116,7 @@ export class AngularCompilerPlugin {
private _mainPath: string | undefined;
private _basePath: string;
private _transformers: ts.TransformerFactory<ts.SourceFile>[] = [];
private _platformTransformers: ts.TransformerFactory<ts.SourceFile>[] | null = null;
private _platform: PLATFORM;
private _JitMode = false;
private _emitSkipped = true;
Expand Down Expand Up @@ -260,6 +262,11 @@ export class AngularCompilerPlugin {
this._forkTypeChecker = options.forkTypeChecker;
}

// Add custom platform transformers.
if (options.platformTransformers !== undefined) {
this._platformTransformers = options.platformTransformers;
}

// Create the webpack compiler host.
const webpackCompilerHost = new WebpackCompilerHost(
this._compilerOptions,
Expand Down Expand Up @@ -757,25 +764,29 @@ export class AngularCompilerPlugin {
this._transformers.push(removeDecorators(isAppPath, getTypeChecker));
}

if (this._platform === PLATFORM.Browser) {
// If we have a locale, auto import the locale data file.
// This transform must go before replaceBootstrap because it looks for the entry module
// import, which will be replaced.
if (this._normalizedLocale) {
this._transformers.push(registerLocaleData(isAppPath, getEntryModule,
this._normalizedLocale));
}
if (this._platformTransformers !== null) {
this._transformers.push(...this._platformTransformers);
} else {
if (this._platform === PLATFORM.Browser) {
// If we have a locale, auto import the locale data file.
// This transform must go before replaceBootstrap because it looks for the entry module
// import, which will be replaced.
if (this._normalizedLocale) {
this._transformers.push(registerLocaleData(isAppPath, getEntryModule,
this._normalizedLocale));
}

if (!this._JitMode) {
// Replace bootstrap in browser AOT.
this._transformers.push(replaceBootstrap(isAppPath, getEntryModule, getTypeChecker));
}
} else if (this._platform === PLATFORM.Server) {
this._transformers.push(exportLazyModuleMap(isMainPath, getLazyRoutes));
if (!this._JitMode) {
this._transformers.push(
exportNgFactory(isMainPath, getEntryModule),
replaceServerBootstrap(isMainPath, getEntryModule, getTypeChecker));
if (!this._JitMode) {
// Replace bootstrap in browser AOT.
this._transformers.push(replaceBootstrap(isAppPath, getEntryModule, getTypeChecker));
}
} else if (this._platform === PLATFORM.Server) {
this._transformers.push(exportLazyModuleMap(isMainPath, getLazyRoutes));
if (!this._JitMode) {
this._transformers.push(
exportNgFactory(isMainPath, getEntryModule),
replaceServerBootstrap(isMainPath, getEntryModule, getTypeChecker));
}
}
}
}
Expand Down