Skip to content

Commit

Permalink
[optimize] More aggressive chunking of common/vendor code
Browse files Browse the repository at this point in the history
Previously, we were not aggressive in combining common code which resulted in duplicates included in the bundles.

As an example `node_modules/elasticsearch-browser/elasticsearch.angular.js` is present in the following chunks:

* kibana.bundle.js
* dashboardViewer.bundle.js
* apm.bundle.js
* monitoring.bundle.js
* ml.bundle.js
* timelion.bundle.js
* graph.bundle.js

Vendored code (anything inside node_modules) is placed in vendors.bundle.js while everything else with more than two references is placed in commons.bundle.js.

This has a couple positive side-effects (numbers are with x-pack & canvas):

* Decreased build time. Seeing builds go from 475.76 seconds to 274.72.
* Decreased memory overhead. Uses roughly 1/3 the memory overhead and overwriting `--max-old-space-size` is no longer necessary.
* Decreased bundle size. A 68% reduction in overall bundle size. Going from 66.16 MB to 21.13 MB.

Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>
  • Loading branch information
Tyler Smalley committed Jan 2, 2018
1 parent ee2fd5b commit 8c71e7e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/optimize/base_optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { writeFile } from 'fs';
import Boom from 'boom';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import webpack from 'webpack';
import CommonsChunkPlugin from 'webpack/lib/optimize/CommonsChunkPlugin';
import DefinePlugin from 'webpack/lib/DefinePlugin';
import UglifyJsPlugin from 'webpack/lib/optimize/UglifyJsPlugin';
import NoEmitOnErrorsPlugin from 'webpack/lib/NoEmitOnErrorsPlugin';
import Stats from 'webpack/lib/Stats';
import webpackMerge from 'webpack-merge';

Expand Down Expand Up @@ -122,12 +118,19 @@ export default class BaseOptimizer {
allChunks: true
}),

new CommonsChunkPlugin({
new webpack.optimize.CommonsChunkPlugin({
name: 'commons',
filename: 'commons.bundle.js'
filename: 'commons.bundle.js',
minChunks: 2,
}),

new NoEmitOnErrorsPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
filename: 'vendors.bundle.js',
minChunks: module => module.context && module.context.indexOf('node_modules') !== -1
}),

new webpack.NoEmitOnErrorsPlugin(),
],

module: {
Expand Down Expand Up @@ -225,12 +228,12 @@ export default class BaseOptimizer {

return webpackMerge(commonConfig, {
plugins: [
new DefinePlugin({
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': '"production"'
}
}),
new UglifyJsPlugin({
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
Expand Down
2 changes: 2 additions & 0 deletions src/ui/ui_render/views/ui_app.jade
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ block content
return anchor.href;
}
var files = [
bundleFile('vendors.style.css'),
bundleFile('commons.style.css'),
bundleFile('#{app.getId()}.style.css'),
bundleFile('vendors.bundle.js'),
bundleFile('commons.bundle.js'),
bundleFile('#{app.getId()}.bundle.js')
];
Expand Down
2 changes: 2 additions & 0 deletions tasks/config/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ module.exports = function (grunt) {

// list of files / patterns to load in the browser
files: [
'http://localhost:5610/bundles/vendors.bundle.js',
'http://localhost:5610/bundles/commons.bundle.js',
'http://localhost:5610/bundles/tests.bundle.js',
'http://localhost:5610/bundles/vendors.style.css',
'http://localhost:5610/bundles/commons.style.css',
'http://localhost:5610/bundles/tests.style.css'
],
Expand Down

0 comments on commit 8c71e7e

Please sign in to comment.