Skip to content

Commit

Permalink
feat(@ngtools/webpack): add AngularCompilerPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva authored and hansl committed Sep 13, 2017
1 parent b0fd35e commit 939cd6c
Show file tree
Hide file tree
Showing 37 changed files with 1,578 additions and 215 deletions.
39 changes: 21 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@
"zone.js": "^0.8.14"
},
"devDependencies": {
"@angular/compiler": "^4.0.0",
"@angular/compiler-cli": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/compiler": "angular/compiler-builds#5.0.0-beta.6+fa6b802",
"@angular/compiler-cli": "angular/compiler-cli-builds#5.0.0-beta.6+fa6b802",
"@angular/core": "angular/core-builds#5.0.0-beta.6+fa6b802",
"@types/chalk": "^0.4.28",
"@types/common-tags": "^1.2.4",
"@types/copy-webpack-plugin": "^4.0.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CliConfig } from '../models/config';
import { BuildOptions } from '../models/build-options';
import { Version } from '../upgrade/version';
import { oneLine } from 'common-tags';
import { AngularCompilerPlugin } from '@ngtools/webpack';

const Command = require('../ember-cli/lib/models/command');

Expand Down Expand Up @@ -176,6 +177,13 @@ export const baseBuildCommandOptions: any = [
aliases: ['nc'],
description: 'Use file name for lazy loaded chunks.',
default: buildConfigDefaults['namedChunks']
},
{
name: 'experimental-angular-compiler',
type: Boolean,
aliases: ['eac'],
description: 'Use new Angular Compiler (Angular version 5 and greater only).',
default: AngularCompilerPlugin.isSupported()
}
];

Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/models/build-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export interface BuildOptions {
showCircularDependencies?: boolean;
buildOptimizer?: boolean;
namedChunks?: boolean;
experimentalAngularCompiler?: boolean;
}
5 changes: 5 additions & 0 deletions packages/@angular/cli/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getAotConfig
} from './webpack-configs';
import * as path from 'path';
import { AngularCompilerPlugin } from '@ngtools/webpack';

export interface WebpackConfigOptions {
projectRoot: string;
Expand Down Expand Up @@ -78,6 +79,10 @@ export class NgCliWebpackConfig {
&& !(buildOptions.aot || buildOptions.target === 'production')) {
throw new Error('The `--build-optimizer` option cannot be used without `--aot`.');
}

if (buildOptions.experimentalAngularCompiler && !AngularCompilerPlugin.isSupported()) {
throw new Error('You need Angular 5 and up to use --experimental-angular-compiler.');
}
}

// Fill in defaults for build targets
Expand Down
4 changes: 3 additions & 1 deletion packages/@angular/cli/models/webpack-configs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
},
module: {
rules: [
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader', exclude: [nodeModules] },
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader', exclude: [
nodeModules, /\.ngfactory\.js$/, /\.ngstyle\.js$/
] },
{ test: /\.html$/, loader: 'raw-loader' },
{ test: /\.(eot|svg|cur)$/, loader: `file-loader?name=[name]${hashFormat.file}.[ext]` },
{
Expand Down
31 changes: 19 additions & 12 deletions packages/@angular/cli/models/webpack-configs/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import { stripIndent } from 'common-tags';
import {AotPlugin} from '@ngtools/webpack';
import { AotPlugin, AngularCompilerPlugin } from '@ngtools/webpack';
import { WebpackConfigOptions } from '../webpack-config';

const SilentError = require('silent-error');
Expand Down Expand Up @@ -63,17 +63,24 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
};
}

return new AotPlugin(Object.assign({}, {
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
i18nFile: buildOptions.i18nFile,
i18nFormat: buildOptions.i18nFormat,
locale: buildOptions.locale,
replaceExport: appConfig.platform === 'server',
missingTranslation: buildOptions.missingTranslation,
hostReplacementPaths,
// If we don't explicitely list excludes, it will default to `['**/*.spec.ts']`.
exclude: []
}, options));
const pluginOptions = Object.assign({}, {
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
i18nFile: buildOptions.i18nFile,
i18nFormat: buildOptions.i18nFormat,
locale: buildOptions.locale,
replaceExport: appConfig.platform === 'server',
missingTranslation: buildOptions.missingTranslation,
hostReplacementPaths,
sourceMap: buildOptions.sourcemaps,
// If we don't explicitely list excludes, it will default to `['**/*.spec.ts']`.
exclude: []
}, options);

if (wco.buildOptions.experimentalAngularCompiler && !options.skipCodeGeneration) {
return new AngularCompilerPlugin(pluginOptions);
} else {
return new AotPlugin(pluginOptions);
}
}

export const getNonAotConfig = function(wco: WebpackConfigOptions) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@angular/cli/models/webpack-xi18n-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class XI18nWebpackConfig extends NgCliWebpackConfig {
super({
target: 'development',
verbose: extractOptions.verbose,
progress: extractOptions.progress
progress: extractOptions.progress,
experimentalAngularCompiler: false,
}, appConfig);
super.buildConfig();
}
Expand Down
6 changes: 5 additions & 1 deletion packages/@angular/cli/tasks/eject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getAppFromConfig } from '../utilities/app-utils';
import { EjectTaskOptions } from '../commands/eject';
import { NgCliWebpackConfig } from '../models/webpack-config';
import { CliConfig } from '../models/config';
import { AotPlugin } from '@ngtools/webpack';
import { AotPlugin, AngularCompilerPlugin } from '@ngtools/webpack';
import { yellow } from 'chalk';
import { LicenseWebpackPlugin } from 'license-webpack-plugin';

Expand Down Expand Up @@ -218,6 +218,10 @@ class JsonWebpackSerializer {
args = this._aotPluginSerialize(plugin);
this._addImport('@ngtools/webpack', 'AotPlugin');
break;
case AngularCompilerPlugin:
args = this._aotPluginSerialize(plugin);
this._addImport('@ngtools/webpack', 'AngularCompilerPlugin');
break;
case HtmlWebpackPlugin:
args = this._htmlWebpackPlugin(plugin);
this.variableImports['html-webpack-plugin'] = 'HtmlWebpackPlugin';
Expand Down
7 changes: 5 additions & 2 deletions packages/@ngtools/webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ exports = { /* ... */

The loader works with the webpack plugin to compile your TypeScript. It's important to include both, and to not include any other TypeScript compiler loader.

For Angular version 5 and up, import `AngularCompilerPlugin` instead of `AotPlugin`.

## Options

* `tsConfigPath`. The path to the `tsconfig.json` file. This is required. In your `tsconfig.json`, you can pass options to the Angular Compiler with `angularCompilerOptions`.
* `basePath`. Optional. The root to use by the compiler to resolve file paths. By default, use the `tsConfigPath` root.
* `entryModule`. Optional if specified in `angularCompilerOptions`. The path and classname of the main application module. This follows the format `path/to/file#ClassName`.
* `mainPath`. Optional if `entryModule` is specified. The `main.ts` file containing the bootstrap code. The plugin will use AST to determine the `entryModule`.
* `skipCodeGeneration`. Optional, defaults to false. Disable code generation and do not refactor the code to bootstrap. This replaces `templateUrl: "string"` with `template: require("string")` (and similar for styles) to allow for webpack to properly link the resources.
* `typeChecking`. Optional, defaults to true. Enable type checking through your application. This will slow down compilation, but show syntactic and semantic errors in webpack.
* `skipCodeGeneration`. Optional, defaults to false. Disable code generation and do not refactor the code to bootstrap. This replaces `templateUrl: "string"` with `template: require("string")` (and similar for styles) to allow for webpack to properly link the resources. Only available in `AotPlugin`.
* `typeChecking`. Optional, defaults to true. Enable type checking through your application. This will slow down compilation, but show syntactic and semantic errors in webpack. Only available in `AotPlugin`.
* `exclude`. Optional. Extra files to exclude from TypeScript compilation.
* `sourceMap`. Optional. Include sourcemaps.
* `compilerOptions`. Optional. Override options in `tsconfig.json`.

## Features
Expand Down
Loading

0 comments on commit 939cd6c

Please sign in to comment.