Webpack: Module Bundler
Webpack: Module Bundler
Module Bundler
What is Webpack?
Loaders
require(style-loader!css-loader!less-loader!./mystyles.less");
Plugins
Optimisation plugins:
OccurenceOrderPlugin,UglifyJsPlugin,
ExtractTextPlugin,CommonsChunkPlugin
http://webpack.github.io/docs/list-of-plugins.html
Sample Config
var
var
var
var
var
path = require("path");
webpack = require("webpack");
HtmlWebpackPlugin = require('html-webpack-plugin');
CleanWebpackPlugin = require('clean-webpack-plugin');
enviroment = require('./enviroment.js');
module.exports = {
entry: "./app.js",
output: {
publicPath: "minified/scripts/",
path: __dirname + "/minified/scripts/",
filename: "[name]-[chunkhash].js",
chunkname: "[name]-[chunkhash].js"
},
module: {
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, '../'),
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['es2015']
}
},
{
test: /\.less$/,
loader: "style!css!less"
},
{
test: /\.html$/,
loader: "handlebars-template-loader"
},
{
test: /\.(jpe|jpg|gif|woff|woff2|eot|ttf|svg|png)(\?.*$|$)/,
loader: 'url-loader?importLoaders=1&limit=100000'
},
{
test: /\.js/,
loader: 'imports?define=>false'
}
]
},
};
resolve: {
root: [
path.resolve(__dirname),
path.resolve('./modules'),
path.resolve('../apps/Listings'),
path.resolve('../')
],
alias: {
modules: enviroment.modules,
plugins: enviroment.plugins,
apps: enviroment.apps,
framework: enviroment.framework,
home: enviroment.home
},
extensions: ['', '.js']
},
plugins: [
new HtmlWebpackPlugin({
title: 'Brand Hub',
template: './index.ejs',
inject: 'body',
filename: '../../index.html'
}),
new CleanWebpackPlugin(['scripts'], {
root: enviroment.minified,
verbose: true,
dry: false
}),
new webpack.optimize.CommonsChunkPlugin({
name: "initializer",
minChunks: Infinity // disable automatic module selection
}),
new webpack.ProvidePlugin({
'window.jQuery': 'home/node_modules/jquery/dist/jquery.js',
'$': 'home/node_modules/jquery/dist/jquery.js' ,
'jQuery': 'home/node_modules/jquery/dist/jquery.js'
}),
new webpack.optimize.CommonsChunkPlugin({name:"main","async":true,'minChunk':2})
]
Code Splitting
You can define split points in your code base. Webpack takes care
of the dependencies, output files and runtime stuff.
require.ensure(dependencies, callback)
OPTIMIZATION
Minimize- To minimize your scripts (and your css, if you use the css-loader)
webpack supports a simple option:
--optimize-minimize resp. new webpack.optimize.UglifyJsPlugin()
Deduplication-This prevents the inclusion of duplicate code into your bundle and
instead applies a copy of the function at runtime. It doesnt affect semantics. You
can enable it with:
--optimize-dedupe resp. new webpack.optimize.DedupePlugin()
Chunks-
Limit the maximum chunk count with --optimize-max-chunks 15 new
webpack.optimize.LimitChunkCountPlugin({maxChunks: 15})
Limit the minimum chunk size with --optimize-min-chunk-size 10000 new
webpack.optimize.MinChunkSizePlugin({minChunkSize: 10000})
References
http://www.pro-react.com/materials/appendixA/
http://webpack.github.io/
https://medium.com/@matt.krick/a-productionready-realtime-saas-withwebpack-7b11ba2fa5b0#.c75jbpan8