Skip to content

Commit

Permalink
feat(@angular/cli): add scope hoisting
Browse files Browse the repository at this point in the history
See the following links for details on scope hoisting:
https://medium.com/webpack/webpack-freelancing-log-book-week-5-7-4764be3266f5
https://medium.com/webpack/webpack-freelancing-log-book-week-8-e73811deb412

Node's global was also remove, since it seemed to affect optimization
gains. It's still enabled for `ng serve` with live reload, since that
functionality needs it.
  • Loading branch information
filipesilva committed Jun 21, 2017
1 parent a0db191 commit 9c1872b
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"typescript": "~2.3.1",
"url-loader": "^0.5.7",
"walk-sync": "^0.3.1",
"webpack": "~2.4.0",
"webpack": "~3.0.0",
"webpack-dev-middleware": "^1.10.2",
"webpack-dev-server": "~2.4.5",
"webpack-merge": "^2.4.0",
Expand Down
1 change: 0 additions & 1 deletion packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export const baseBuildCommandOptions: any = [
{
name: 'vendor-chunk',
type: Boolean,
default: true,
aliases: ['vc'],
description: 'Use a separate bundle containing only vendor libraries.'
},
Expand Down
4 changes: 3 additions & 1 deletion packages/@angular/cli/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ export class NgCliWebpackConfig {
environment: 'dev',
outputHashing: 'media',
sourcemaps: true,
extractCss: false
extractCss: false,
vendorChunk: true
},
production: {
environment: 'prod',
outputHashing: 'all',
sourcemaps: false,
extractCss: true,
vendorChunk: false,
aot: true
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/@angular/cli/models/webpack-configs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
].concat(extraPlugins),
node: {
fs: 'empty',
global: true,
global: false,
crypto: 'empty',
tls: 'empty',
net: 'empty',
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/models/webpack-configs/production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
'NODE_ENV': 'production'
}),
new (<any>webpack).HashedModuleIdsPlugin(),
new (webpack.optimize as any).ModuleConcatenationPlugin(),
new webpack.optimize.UglifyJsPlugin(<any>{
mangle: { screw_ie8: true },
compress: { screw_ie8: true, warnings: buildOptions.verbose },
Expand Down
2 changes: 1 addition & 1 deletion packages/@angular/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"typescript": ">=2.0.0 <2.4.0",
"url-loader": "^0.5.7",
"walk-sync": "^0.3.1",
"webpack": "~2.4.0",
"webpack": "~3.0.0",
"webpack-dev-middleware": "^1.10.2",
"webpack-dev-server": "~2.4.5",
"webpack-merge": "^2.4.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/@angular/cli/tasks/eject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ class JsonWebpackSerializer {
case webpack.optimize.UglifyJsPlugin:
this._addImport('webpack.optimize', 'UglifyJsPlugin');
break;
case (webpack.optimize as any).ModuleConcatenationPlugin:
this._addImport('webpack.optimize', 'ModuleConcatenationPlugin');
break;
case angularCliPlugins.BaseHrefWebpackPlugin:
case angularCliPlugins.GlobCopyWebpackPlugin:
case angularCliPlugins.SuppressExtractedTextChunksWebpackPlugin:
Expand Down Expand Up @@ -413,6 +416,8 @@ export default Task.extend({
}

const webpackConfig = new NgCliWebpackConfig(runTaskOptions, appConfig).buildConfig();
// Without node['global'] = true, webpack-dev-server will break at runtime due to sockjs.
webpackConfig.node['global'] = true;
const serializer = new JsonWebpackSerializer(process.cwd(), outputPath, appConfig.root);
const output = serializer.serialize(webpackConfig);
const webpackConfigStr = `${serializer.generateVariables()}\n\nmodule.exports = ${output};\n`;
Expand Down
4 changes: 4 additions & 0 deletions packages/@angular/cli/tasks/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ export default Task.extend({
`);
}
}
// Live reload needs an additional entry point.
if (!webpackConfig.entry.main) { webpackConfig.entry.main = []; }
webpackConfig.entry.main.unshift(...entryPoints);
// Live reload required the node `global` to be set to true.
if (!webpackConfig.node) { webpackConfig.node = {}; }
webpackConfig.node.global = true;
} else if (serveTaskOptions.hmr) {
ui.writeLine(yellow('Live reload is disabled. HMR option ignored.'));
}
Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/tests/build/chunk-hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export default function() {
`, '@angular/router'))
.then(() => addImportToModule(
'src/app/app.module.ts', 'ReactiveFormsModule', '@angular/forms'))
.then(() => ng('build', '--prod'))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
oldHashes = generateFileHashMap();
})
.then(() => ng('build', '--prod'))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
Expand All @@ -74,16 +74,16 @@ export default function() {
oldHashes = newHashes;
})
.then(() => writeFile('src/styles.css', 'body { background: blue; }'))
.then(() => ng('build', '--prod'))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, ['styles']);
validateHashes(oldHashes, newHashes, ['inline', 'styles']);
oldHashes = newHashes;
})
.then(() => writeFile('src/app/app.component.css', 'h1 { margin: 10px; }'))
.then(() => ng('build', '--prod'))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
Expand All @@ -93,7 +93,7 @@ export default function() {
})
.then(() => addImportToModule(
'src/app/lazy/lazy.module.ts', 'ReactiveFormsModule', '@angular/forms'))
.then(() => ng('build', '--prod'))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e/tests/misc/common-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {appendToFile} from '../../utils/fs';

export default function() {
let oldNumberOfFiles = 0;

// webpack 2.5.1+ has a bug with CommonsChunkPlugin
// https://github.com/webpack/webpack/issues/4850

return Promise.resolve()
.then(() => ng('build'))
.then(() => oldNumberOfFiles = readdirSync('dist').length)
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/tests/test/test-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function () {
// Not using `async()` in tests as it seemed to swallow `fetch()` errors
'src/app/app.component.spec.ts': stripIndent`
describe('Test Runner', () => {
const fetch = global['fetch'];
const fetch = window['fetch'];
it('should serve files in assets folder', (done) => {
fetch('/assets/file.txt')
.then(response => response.text())
Expand Down

0 comments on commit 9c1872b

Please sign in to comment.