Skip to content

Commit

Permalink
fix(@angular/cli): add environment file to compilerHost
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Feb 7, 2017
1 parent 26def64 commit ab80faf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
43 changes: 27 additions & 16 deletions packages/@angular/cli/models/webpack-configs/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import {AotPlugin} from '@ngtools/webpack';
import {AotPlugin, AotPluginOptions} from '@ngtools/webpack';
import { WebpackConfigOptions } from '../webpack-config';


Expand All @@ -9,6 +10,28 @@ const webpackLoader: string = g['angularCliIsLocal']
: '@ngtools/webpack';


function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
const { appConfig, projectRoot, buildOptions } = wco;

// Read the environment, and set it in the compiler host.
const appRoot = path.resolve(projectRoot, appConfig.root);
const sourcePath = appConfig.environments['source'];
const envFile = appConfig.environments[buildOptions.environment];
const environmentContent = fs.readFileSync(path.join(appRoot, envFile)).toString();

return new AotPlugin(Object.assign({}, {
tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig),
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
i18nFile: buildOptions.i18nFile,
i18nFormat: buildOptions.i18nFormat,
locale: buildOptions.locale,
hostOverrideFileSystem: {
[path.join(appRoot, sourcePath)]: environmentContent
}
}, options));
}


export const getNonAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
let exclude = [ '**/*.spec.ts' ];
Expand All @@ -24,18 +47,13 @@ export const getNonAotConfig = function(wco: WebpackConfigOptions) {
]
},
plugins: [
new AotPlugin({
tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig),
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
exclude: exclude,
skipCodeGeneration: true
}),
_createAotPlugin(wco, { exclude, skipCodeGeneration: true }),
]
};
};

export const getAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, buildOptions, appConfig } = wco;
const { projectRoot, appConfig } = wco;
let exclude = [ '**/*.spec.ts' ];
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
return {
Expand All @@ -49,14 +67,7 @@ export const getAotConfig = function(wco: WebpackConfigOptions) {
]
},
plugins: [
new AotPlugin({
tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig),
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
i18nFile: buildOptions.i18nFile,
i18nFormat: buildOptions.i18nFormat,
locale: buildOptions.locale,
exclude: exclude
})
_createAotPlugin(wco, { exclude })
]
};
};
9 changes: 9 additions & 0 deletions packages/@ngtools/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface AotPluginOptions {
mainPath?: string;
typeChecking?: boolean;
skipCodeGeneration?: boolean;
hostOverrideFileSystem?: { [path: string]: string };
i18nFile?: string;
i18nFormat?: string;
locale?: string;
Expand Down Expand Up @@ -167,6 +168,14 @@ export class AotPlugin implements Tapable {
}

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

// Override some files in the FileSystem.
if (options.hasOwnProperty('hostOverrideFileSystem')) {
for (const filePath of Object.keys(options.hostOverrideFileSystem)) {
this._compilerHost.writeFile(filePath, options.hostOverrideFileSystem[filePath], false);
}
}

this._program = ts.createProgram(
this._rootFilePath, this._compilerOptions, this._compilerHost);

Expand Down

0 comments on commit ab80faf

Please sign in to comment.