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

Reduce JS bundle size and add webpack bundle analyzer #86

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
57 changes: 34 additions & 23 deletions gulp/js/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var taskName = 'js',
srcBase: './source/assets/js/',
dest: './build/assets/js/',
destBase: './build/',
destStats: './build/_stats/webpack-report.html',
destAsyncSuffix: 'async/',
watch: [
'source/assets/js/**/*.js',
Expand All @@ -43,7 +44,8 @@ var taskName = 'js',
_ = require('lodash'),
path = require('path'),
webpack = require('webpack'),
livereload = require('gulp-livereload');
livereload = require('gulp-livereload'),
BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

var src = config.src,
compiler;
Expand All @@ -57,32 +59,44 @@ var taskName = 'js',
// Create a map of entries, i.e. {'assets/js/main': './source/assets/js/main.js'}
entry: helpers.webpack.getEntries(src, config.srcBase),
module: {
loaders: [
rules: [
{
test: /\.hbs$/,
loader: 'handlebars-loader'
loader: 'handlebars-loader',
options: {
partialResolver: function(partial, callback) {
var resolvedPath = path.resolve('./source/', partial + '.hbs');

callback(null, resolvedPath);
}
}
},
{
test: /jquery\.js$/,
loader: 'expose?$!expose?jQuery'
loader: 'expose-loader?$!expose-loader?jQuery'
},
{
test: /handlebars\.js$/,
loader: 'expose?Handlebars'
loader: 'expose-loader?Handlebars'
},
{
test: /(\.js|\.jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react'],
plugins: [
// Work around some issues in IE
'transform-class-properties',
'transform-proto-to-assign',
['transform-es2015-classes', {
options: {
presets: [
['env', {
debug: util.env.dev,
useBuiltIns: 'entry',
Copy link
Collaborator Author

@backflip backflip Nov 6, 2017

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note: Dependencies need to be updated accordingly:

    "@babel/core": "^7.0.0-beta.34",
    "@babel/polyfill": "^7.0.0-beta.34",
    "@babel/preset-env": "^7.0.0-beta.34",
    "babel-loader": "^8.0.0-beta.0",

targets: {
browsers: ['last 2 versions']
},
loose: true
}]
}],
'react'
],
plugins: [
'transform-proto-to-assign'
]
}
}
Expand All @@ -91,7 +105,11 @@ var taskName = 'js',

// Minifiy in prod mode
plugins: [

new BundleAnalyzerPlugin({
analyzerMode: 'static',
reportFilename: path.relative(config.dest, config.destStats), // Relative to bundles output directory
openAnalyzer: false
})
].concat(util.env.dev ? [] : [
new webpack.DefinePlugin({
'process.env': {
Expand All @@ -105,7 +123,7 @@ var taskName = 'js',
})
]),
output: {
path: config.dest,
path: path.resolve(config.dest),
filename: util.env.dev ? '[name].js' : '[name].min.js',

// Save async loaded files (using require.ensurce) in special dir
Expand All @@ -114,14 +132,7 @@ var taskName = 'js',
// Tell webpack about the asset path structure in the browser to be able to load async files
publicPath: path.join('/', path.relative(config.destBase, config.dest), '/')
},
devtool: util.env.dev ? 'eval-cheap-module-source-map' : null,
handlebarsLoader: {
partialResolver: function(partial, callback) {
var resolvedPath = path.resolve('./source/', partial + '.hbs');

callback(null, resolvedPath);
}
}
devtool: util.env.dev ? 'eval-cheap-module-source-map' : false
});

if (util.env.watch && !util.env.skipWebpackWatch) {
Expand Down
4 changes: 2 additions & 2 deletions gulp/js/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ gulp.task(taskName, function(cb) {
loaders: [
{
test: /qunit\.js$/,
loader: 'expose?QUnit'
loader: 'expose-loader?QUnit'
},
{
test: /\.css$/,
Expand All @@ -168,7 +168,7 @@ gulp.task(taskName, function(cb) {
},

output: {
path: taskConfig.destTests,
path: path.resolve(taskConfig.destTests),
filename: '[name].js'
}
});
Expand Down