Skip to content

Commit 42f58c9

Browse files
committed
feat(@angular/cli): use cache and parallelization for build-optimizer
1 parent 6f2c865 commit 42f58c9

File tree

5 files changed

+120
-7
lines changed

5 files changed

+120
-7
lines changed

package-lock.json

+80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@angular-devkit/schematics": "~0.0.51",
4747
"@schematics/angular": "~0.1.16",
4848
"autoprefixer": "^7.2.3",
49+
"cache-loader": "^1.2.0",
4950
"chalk": "~2.2.0",
5051
"circular-dependency-plugin": "^4.2.1",
5152
"clean-css": "^4.1.9",
@@ -61,6 +62,7 @@
6162
"file-loader": "^1.1.5",
6263
"fs-extra": "^4.0.0",
6364
"glob": "^7.0.3",
65+
"happypack": "^4.0.0",
6466
"html-webpack-plugin": "^2.29.0",
6567
"istanbul-instrumenter-loader": "^3.0.0",
6668
"karma-source-map-support": "^1.2.0",
@@ -114,8 +116,8 @@
114116
"@types/fs-extra": "^4.0.0",
115117
"@types/glob": "^5.0.29",
116118
"@types/jasmine": "2.5.45",
117-
"@types/lodash": "~4.14.50",
118119
"@types/loader-utils": "^1.1.0",
120+
"@types/lodash": "~4.14.50",
119121
"@types/minimist": "^1.2.0",
120122
"@types/mock-fs": "^3.6.30",
121123
"@types/node": "^6.0.84",

packages/@angular/cli/models/webpack-configs/common.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as webpack from 'webpack';
22
import * as path from 'path';
3+
import * as os from 'os';
34
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
45
import { NamedLazyChunksWebpackPlugin } from '../../plugins/named-lazy-chunks-webpack-plugin';
56
import { extraEntryParser, getOutputHashFormat, AssetPattern } from './utils';
@@ -11,6 +12,8 @@ import { ScriptsWebpackPlugin } from '../../plugins/scripts-webpack-plugin';
1112
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
1213
const CircularDependencyPlugin = require('circular-dependency-plugin');
1314
const SilentError = require('silent-error');
15+
const HappyPack = require('happypack');
16+
const resolve = require('resolve');
1417

1518
/**
1619
* Enumerate loaders and their dependencies from this file to let the dependency validator
@@ -20,6 +23,7 @@ const SilentError = require('silent-error');
2023
* require('raw-loader')
2124
* require('url-loader')
2225
* require('file-loader')
26+
* require('cache-loader')
2327
* require('@angular-devkit/build-optimizer')
2428
*/
2529

@@ -104,8 +108,8 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
104108

105109
if (!asset.allowOutsideOutDir) {
106110
const message = 'An asset cannot be written to a location outside of the output path. '
107-
+ 'You can override this message by setting the `allowOutsideOutDir` '
108-
+ 'property on the asset to true in the CLI configuration.';
111+
+ 'You can override this message by setting the `allowOutsideOutDir` '
112+
+ 'property on the asset to true in the CLI configuration.';
109113
throw new SilentError(message);
110114
}
111115
}
@@ -163,13 +167,29 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
163167
}
164168

165169
if (buildOptions.buildOptimizer) {
170+
// Set the cache directory to the Build Optimizer dir, so that package updates will delete it.
171+
const buildOptimizerDir = path.dirname(
172+
resolve.sync('@angular-devkit/build-optimizer', { basedir: projectRoot }));
173+
const cacheDirectory = path.resolve(buildOptimizerDir, './.cache/');
174+
166175
extraRules.push({
167176
test: /\.js$/,
168-
use: [{
169-
loader: '@angular-devkit/build-optimizer/webpack-loader',
170-
options: { sourceMap: buildOptions.sourcemaps }
171-
}]
177+
use: ['happypack/loader'],
172178
});
179+
extraPlugins.push(new HappyPack({
180+
verbose: false,
181+
threads: (os.cpus().length - 1) || 1,
182+
loaders: [
183+
{
184+
loader: 'cache-loader',
185+
options: { cacheDirectory }
186+
},
187+
{
188+
loader: '@angular-devkit/build-optimizer/webpack-loader',
189+
options: { sourceMap: buildOptions.sourcemaps }
190+
}
191+
],
192+
}));
173193
}
174194

175195
if (buildOptions.namedChunks) {

packages/@angular/cli/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@ngtools/webpack": "1.10.0-beta.1",
3535
"@schematics/angular": "~0.1.16",
3636
"autoprefixer": "^7.2.3",
37+
"cache-loader": "^1.2.0",
3738
"chalk": "~2.2.0",
3839
"circular-dependency-plugin": "^4.2.1",
3940
"clean-css": "^4.1.9",
@@ -48,6 +49,7 @@
4849
"file-loader": "^1.1.5",
4950
"fs-extra": "^4.0.0",
5051
"glob": "^7.0.3",
52+
"happypack": "^4.0.0",
5153
"html-webpack-plugin": "^2.29.0",
5254
"karma-source-map-support": "^1.2.0",
5355
"less": "^2.7.2",

packages/@angular/cli/tasks/eject.ts

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
2727
const SilentError = require('silent-error');
2828
const CircularDependencyPlugin = require('circular-dependency-plugin');
2929
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
30+
const HappyPlugin = require('happypack');
3031
const Task = require('../ember-cli/lib/models/task');
3132

3233
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
@@ -171,6 +172,10 @@ class JsonWebpackSerializer {
171172
return plugin.options;
172173
}
173174

175+
private _happyPlugin(plugin: any) {
176+
return plugin.config;
177+
}
178+
174179
private _pluginsReplacer(plugins: any[]) {
175180
return plugins.map(plugin => {
176181
let args = plugin.options || undefined;
@@ -255,6 +260,10 @@ class JsonWebpackSerializer {
255260
args = this._uglifyjsPlugin(plugin);
256261
this.variableImports['uglifyjs-webpack-plugin'] = 'UglifyJsPlugin';
257262
break;
263+
case HappyPlugin:
264+
args = this._happyPlugin(plugin);
265+
this.variableImports['happypack'] = 'HappyPlugin';
266+
break;
258267
case SubresourceIntegrityPlugin:
259268
this.variableImports['webpack-subresource-integrity'] = 'SubresourceIntegrityPlugin';
260269
break;

0 commit comments

Comments
 (0)