Permalink
Browse files

feat(@ngtools/webpack): allow ts to be transformed before loading

  • Loading branch information...
connor4312 committed Oct 27, 2017
1 parent e7ef67b commit 6f8967fb9b69d80d516f9cccc43b91ea48929f43
Showing with 20 additions and 4 deletions.
  1. +17 −2 packages/@ngtools/webpack/src/compiler_host.ts
  2. +3 −2 packages/@ngtools/webpack/src/plugin.ts
@@ -90,6 +90,13 @@ export class VirtualFileStats extends VirtualStats {
get size() { return this._content.length; }
}

/**
* FileTransform can be passed into the AoT plugin to transform loaded
* TypeScript files, before they're parsed.
*/
export interface FileTransform {
(contents: string, filename: string): string;
}

export class WebpackCompilerHost implements ts.CompilerHost {
private _delegate: ts.CompilerHost;
@@ -104,7 +111,11 @@ export class WebpackCompilerHost implements ts.CompilerHost {

private _cache = false;

constructor(private _options: ts.CompilerOptions, basePath: string) {
constructor(
private _options: ts.CompilerOptions,
basePath: string,
private _transform?: FileTransform,
) {
this._setParentNodes = true;
this._delegate = ts.createCompilerHost(this._options, this._setParentNodes);
this._basePath = this._normalizePath(basePath);
@@ -172,7 +183,11 @@ export class WebpackCompilerHost implements ts.CompilerHost {

const stats = this._files[fileName];
if (stats == null) {
const result = this._delegate.readFile(fileName);
let result = this._delegate.readFile(fileName);
if (this._transform && result !== undefined) {
result = this._transform(result, fileName);
}

if (result !== undefined && this._cache) {
this._setFileContent(fileName, result);
return result;
@@ -9,7 +9,7 @@ const ContextElementDependency = require('webpack/lib/dependencies/ContextElemen
const NodeWatchFileSystem = require('webpack/lib/node/NodeWatchFileSystem');

import {WebpackResourceLoader} from './resource_loader';
import {WebpackCompilerHost} from './compiler_host';
import {FileTransform, WebpackCompilerHost} from './compiler_host';
import {resolveEntryModuleFromMain} from './entry_resolver';
import {Tapable} from './webpack';
import {PathsPlugin} from './paths-plugin';
@@ -34,6 +34,7 @@ export interface AotPluginOptions {
i18nFormat?: string;
locale?: string;
missingTranslation?: string;
transform?: FileTransform;

// Use tsconfig to include path globs.
exclude?: string | string[];
@@ -205,7 +206,7 @@ export class AotPlugin implements Tapable {
this._skipCodeGeneration = options.skipCodeGeneration;
}

this._compilerHost = new WebpackCompilerHost(this._compilerOptions, this._basePath);
this._compilerHost = new WebpackCompilerHost(this._compilerOptions, this._basePath, this._options.transform);

// Override some files in the FileSystem.
if (options.hostOverrideFileSystem) {

0 comments on commit 6f8967f

Please sign in to comment.