-
Notifications
You must be signed in to change notification settings - Fork 615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stencil 3250 - Webpack 2 w/ Code Splitting, Tree Shaking #964
Conversation
assets/js/app.js
Outdated
@@ -1,70 +1,50 @@ | |||
import 'babel-polyfill'; | |||
import 'babel-polyfill'; // TODO: can we remove this? see https://jira.bigcommerce.com/browse/MERC-684 and https://github.com/bigcommerce/cornerstone/commit/0648a3f900e093fb3ce8d30ddb087feb8e6b3c6d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
want to see if we can actually remove this and just target es3/5 directly.
webpack.conf.js
Outdated
output: { | ||
filename: '[chunkhash].theme-bundle.[name].js', | ||
path: path.resolve(__dirname, "assets/dist"), | ||
publicPath: 'assets/dist/', // TODO: cdnify - https://github.com/bigcommerce/paper/blob/master/index.js#L154 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mcampa - i'm wondering how we can make this work here. we want the publicPath to include the CDN for the code-split files. here's some more info: https://webpack.js.org/guides/public-path/
webpack.conf.js
Outdated
cacheDirectory: true, | ||
presets: [['es2015', {loose: true, modules: false}]], | ||
plugins: [ | ||
'syntax-dynamic-import', // do we need babel-plugin-dynamic-import-webpack? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't seem to need this but want to make sure...
package.json
Outdated
"lazysizes": "3.0.0", | ||
"load-grunt-config": "0.17.1", | ||
"lodash": "^3.5.0", | ||
"nod-validate": "^2.0.12", | ||
"pace": "hubspot/pace#a03f1f1de62c9cea6c88b2267b8d7a83858b6cb6", | ||
"slick-carousel": "1.5.5", | ||
"time-grunt": "^1.2.2", | ||
"webpack": "^1.14.0" | ||
"webpack": "^2.2.1", | ||
"webpack-visualizer-plugin": "^0.1.11" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: remove this before merging
stencil.conf.js
Outdated
webpackConfig.devtool = false; | ||
webpackConfig.plugins.push(new webpack.optimize.DedupePlugin()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
templates/layout/amp-iframe.ejs
Outdated
|
||
{{#block "page"}} {{/block}} | ||
<% for (var chunk in htmlWebpackPlugin.files.chunks) { %> | ||
<script src="{{cdn '<%= htmlWebpackPlugin.files.chunks[chunk].entry %>'}}"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now vendor and main are placed here on build, with hashes
webpack.conf.js
Outdated
options: { | ||
compact: false, | ||
cacheDirectory: true, | ||
presets: [['es2015', {loose: true, modules: false}]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
modules: false needed for tree-shaking; not sure if we need loose, not sure what it was here for originally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loose mode was for ie10: f6f2ec0
@@ -1,70 +1,51 @@ | |||
import 'babel-polyfill'; | |||
__webpack_public_path__ = window.__webpack_public_path__; // eslint-disable-line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this sets the public path for webpack to the base path for the cdn
templates/layout/amp-iframe.html
Outdated
|
||
<script src="{{cdn 'assets/dist/theme-bundle.js'}}"></script> | ||
|
||
<script>window.__webpack_public_path__ = "{{cdn 'assets/dist/'}}";</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cdn base path is needed for the dynamically loaded bundles
webpack.conf.js
Outdated
bail: true, | ||
module: { | ||
loaders: [ | ||
rules: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cacheDirectory: true, | ||
presets: ['es2015-loose'], | ||
use: { | ||
loader: 'babel-loader', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stencil.conf.js
Outdated
warnings: true, | ||
}, | ||
minimize: true, | ||
sourceMap: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stencil.conf.js
Outdated
comments: false | ||
comments: false, | ||
compress: { | ||
warnings: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
webpack.conf.js
Outdated
}), | ||
new webpack.optimize.CommonsChunkPlugin({ | ||
name: 'vendor', | ||
minChunks: function (module) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stencil.conf.js
Outdated
compress: { | ||
warnings: true, | ||
}, | ||
sourceMap: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this plugin is only used to bundle for production we don't include the source maps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's off now, with a comment
webpack.conf.js
Outdated
watch: false | ||
}), | ||
new webpack.LoaderOptionsPlugin({ | ||
minimize: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
const getAccount = () => import('./theme/account'); | ||
const getLogin = () => import('./theme/auth'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two functions are shared
browserlist
Outdated
@@ -0,0 +1,8 @@ | |||
# See https://support.bigcommerce.com/articles/Public/Themes-Supported-Browsers/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps these should go back 3 versions to match the autoprefixer settings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, change it to last 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
webpackConfig.plugins.push(new webpack.optimize.DedupePlugin()); | ||
webpackConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({ comments: false })); | ||
webpackConfig.plugins.push(new webpack.LoaderOptionsPlugin({ | ||
minimize: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
webpackConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({ | ||
comments: false, | ||
compress: { | ||
warnings: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -24,7 +24,10 @@ | |||
|
|||
{{#block "page"}} {{/block}} | |||
|
|||
<script src="{{cdn 'assets/dist/theme-bundle.js'}}"></script> | |||
<script>window.__webpack_public_path__ = "{{cdn 'assets/dist/'}}";</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is used (in apps.js) to prefix the dynamically loaded chunk requests
webpack.conf.js
Outdated
return module.context && module.context.indexOf('node_modules') !== -1; | ||
}, | ||
}), | ||
new webpack.optimize.CommonsChunkPlugin({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one pulls duplicate child files out of the code split chunks into their parent
@@ -41,8 +41,16 @@ function development() { | |||
function production() { | |||
webpackConfig.watch = false; | |||
webpackConfig.devtool = false; | |||
webpackConfig.plugins.push(new webpack.optimize.DedupePlugin()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolve: { | ||
alias: { | ||
jquery: path.resolve(__dirname, 'node_modules/jquery/dist/jquery.min.js'), | ||
jstree: path.resolve(__dirname, 'node_modules/jstree/dist/jstree.min.js'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
both package.json's for jquery and jstree point to the their non-minified versions, this resolves to the minified versions instead
webpack.conf.js
Outdated
name: 'vendor', | ||
minChunks: function (module) { | ||
// this assumes your vendor imports exist in the node_modules directory | ||
return module.context && module.context.indexOf('node_modules') !== -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pull out the node_modules into the vendor bundle (https://webpack.js.org/plugins/commons-chunk-plugin/#passing-the-minchunks-property-a-function)
assets/js/app.js
Outdated
// Finds the appropriate class from the pageType. | ||
const PageClass = pageClasses[templateFile]; | ||
const templatePath = pageClasses[templateFile]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename templatePath
to something else? since is not a path anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, how about pageClassImporter
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
LGTM 💚 Ping me when it goes to integration so I can submit a PR for failing tests (https://github.com/bigcommerce/bigcommerce/pull/18253) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
What?
Webpack 2 w/ Code Splitting, Tree Shaking, and other Optimizations
Tickets / Documentation
Depends On
Notes
Stats
Before; size of bundle: 616K; size of zip: 10.3MB
After; total size of combined bundles: 612K; size of zip: 10.3MB
Note - if we get rid of code splitting (and async/await) the zip remains at 10.3MB but the bundle reduces to 560K (see the alternate diff here)
Code splitting example:
http://g.recordit.co/REeImIsk0o.gif
Appreciation
Thank you @dstaley for the incredibly useful writeup here.
@bigcommerce/stencil-team @bookernath