Skip to content

Commit

Permalink
feat(@ngtools/webpack): add an option to redirect files
Browse files Browse the repository at this point in the history
It takes multiple paths and not the content, which is cleaner in a configuration.
  • Loading branch information
hansl committed Feb 17, 2017
1 parent 351f6b1 commit e0edccf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
14 changes: 9 additions & 5 deletions packages/@angular/cli/models/webpack-configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
const { appConfig, projectRoot, buildOptions } = wco;

// Read the environment, and set it in the compiler host.
let hostOverrideFileSystem: any = {};
let hostReplacementPaths: any = {};
// process environment file replacement
if (appConfig.environments) {
if (!appConfig.environmentSource) {
Expand Down Expand Up @@ -58,9 +58,10 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
const appRoot = path.resolve(projectRoot, appConfig.root);
const sourcePath = appConfig.environmentSource;
const envFile = appConfig.environments[buildOptions.environment];
const environmentContent = fs.readFileSync(path.join(appRoot, envFile)).toString();

hostOverrideFileSystem = { [path.join(appRoot, sourcePath)]: environmentContent };
hostReplacementPaths = {
[path.join(appRoot, sourcePath)]: path.join(appRoot, envFile)
};
}

return new AotPlugin(Object.assign({}, {
Expand All @@ -69,15 +70,18 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
i18nFile: buildOptions.i18nFile,
i18nFormat: buildOptions.i18nFormat,
locale: buildOptions.locale,
hostOverrideFileSystem
hostReplacementPaths
}, options));
}


export const getNonAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
let exclude = [ '**/*.spec.ts' ];
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
if (appConfig.test) {
exclude.push(path.join(projectRoot, appConfig.root, appConfig.test));
}

return {
module: {
rules: [
Expand Down
19 changes: 16 additions & 3 deletions packages/@ngtools/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface AotPluginOptions {
typeChecking?: boolean;
skipCodeGeneration?: boolean;
hostOverrideFileSystem?: { [path: string]: string };
hostReplacementPaths?: { [path: string]: string };
i18nFile?: string;
i18nFormat?: string;
locale?: string;
Expand All @@ -35,6 +36,8 @@ export interface AotPluginOptions {


export class AotPlugin implements Tapable {
private _options: AotPluginOptions;

private _compilerOptions: ts.CompilerOptions;
private _angularCompilerOptions: AngularCompilerOptions;
private _program: ts.Program;
Expand Down Expand Up @@ -62,9 +65,11 @@ export class AotPlugin implements Tapable {
private _firstRun = true;

constructor(options: AotPluginOptions) {
this._setupOptions(options);
this._options = Object.assign({}, options);
this._setupOptions(this._options);
}

get options() { return this._options; }
get basePath() { return this._basePath; }
get compilation() { return this._compilation; }
get compilerHost() { return this._compilerHost; }
Expand Down Expand Up @@ -175,6 +180,14 @@ export class AotPlugin implements Tapable {
this._compilerHost.writeFile(filePath, options.hostOverrideFileSystem[filePath], false);
}
}
// Override some files in the FileSystem with paths from the actual file system.
if (options.hasOwnProperty('hostReplacementPaths')) {
for (const filePath of Object.keys(options.hostReplacementPaths)) {
const replacementFilePath = options.hostReplacementPaths[filePath];
const content = this._compilerHost.readFile(replacementFilePath);
this._compilerHost.writeFile(filePath, content, false);
}
}

this._program = ts.createProgram(
this._rootFilePath, this._compilerOptions, this._compilerHost);
Expand All @@ -193,8 +206,8 @@ export class AotPlugin implements Tapable {

// still no _entryModule? => try to resolve from mainPath
if (!this._entryModule && options.mainPath) {
this._entryModule = resolveEntryModuleFromMain(options.mainPath, this._compilerHost,
this._program);
const mainPath = path.resolve(basePath, options.mainPath);
this._entryModule = resolveEntryModuleFromMain(mainPath, this._compilerHost, this._program);
}

if (options.hasOwnProperty('i18nFile')) {
Expand Down

0 comments on commit e0edccf

Please sign in to comment.