Skip to content
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

Merged
merged 1 commit into from
Apr 25, 2017
Merged

Stencil 3250 - Webpack 2 w/ Code Splitting, Tree Shaking #964

merged 1 commit into from
Apr 25, 2017

Conversation

mjschock
Copy link
Contributor

@mjschock mjschock commented Mar 16, 2017

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

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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?
Copy link
Contributor Author

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"
Copy link
Contributor Author

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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


{{#block "page"}} {{/block}}
<% for (var chunk in htmlWebpackPlugin.files.chunks) { %>
<script src="{{cdn '<%= htmlWebpackPlugin.files.chunks[chunk].entry %>'}}"></script>
Copy link
Contributor Author

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}]],
Copy link
Contributor Author

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.

Copy link
Contributor Author

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

@mjschock mjschock mentioned this pull request Mar 17, 2017
@@ -1,70 +1,51 @@
import 'babel-polyfill';
__webpack_public_path__ = window.__webpack_public_path__; // eslint-disable-line
Copy link
Contributor Author

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


<script src="{{cdn 'assets/dist/theme-bundle.js'}}"></script>

<script>window.__webpack_public_path__ = "{{cdn 'assets/dist/'}}";</script>
Copy link
Contributor Author

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: [
Copy link
Contributor Author

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',
Copy link
Contributor Author

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,
Copy link
Contributor Author

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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjschock mjschock changed the title WIP - Stencil 3250 Stencil 3250 - Webpack 2 w/ Code Splitting Mar 20, 2017
webpack.conf.js Outdated
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
Copy link
Contributor Author

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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjschock mjschock mentioned this pull request Mar 21, 2017
@mjschock mjschock changed the title Stencil 3250 - Webpack 2 w/ Code Splitting Stencil 3250 - Webpack 2 w/ Code Splitting, Tree Shaking, Optimization Mar 21, 2017
@mjschock mjschock changed the title Stencil 3250 - Webpack 2 w/ Code Splitting, Tree Shaking, Optimization Stencil 3250 - Webpack 2 w/ Code Splitting, Tree Shaking Mar 21, 2017

const getAccount = () => import('./theme/account');
const getLogin = () => import('./theme/auth');
Copy link
Contributor Author

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/
Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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,
Copy link
Contributor Author

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,
Copy link
Contributor Author

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>
Copy link
Contributor Author

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({
Copy link
Contributor Author

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());
Copy link
Contributor Author

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'),
Copy link
Contributor Author

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assets/js/app.js Outdated
// Finds the appropriate class from the pageType.
const PageClass = pageClasses[templateFile];
const templatePath = pageClasses[templateFile];
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, how about pageClassImporter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

@mjschock
Copy link
Contributor Author

Cornerstone-1.6.2.zip

@nickdengler
Copy link
Contributor

LGTM 💚

Ping me when it goes to integration so I can submit a PR for failing tests (https://github.com/bigcommerce/bigcommerce/pull/18253)

@mjschock
Copy link
Contributor Author

Cornerstone-1.6.3.zip

Copy link
Contributor

@mcampa mcampa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mjschock mjschock merged commit ff212a0 into bigcommerce:master Apr 25, 2017
@mjschock mjschock deleted the STENCIL-3250 branch April 25, 2017 19:43
@ghost ghost mentioned this pull request Jan 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants