Daniel de la Cruz Calvo
Software Engineer and professional mentor for developers.
from zero to hero
Extends node.js require()
var module = require("./modules.js"); // CommonJSdefine(["amd-module", "../file"], function(amdModule, file) {
require(["big-module/big/file"], function(big) {
var stuff = require("../my/stuff");
});
}); // AMDimport {Module} from './modules'; // ES2015require("coffee!./cup.coffee"); // directlyrequire('./cup'); // or, by configurationhtml, scss, images...
module.exports = {
context: __dirname + "/src",
entry: "./index.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
}
};webpack.config.js
Example: https://goo.gl/4UhBLZ
{
"name": "webpack-html5spain",
"version": "0.0.1",
"description": "An example repository for webpack newbies",
"main": "src/index.js",
"scripts": {
"start": "webpack"
},
"devDependencies": {
"webpack": "^1.12.2"
}
}package.json
Example: https://goo.gl/4UhBLZ
{
context: __dirname + "/src",
entry: {
home: "./home",
user: ["./user", "./account"]
},
output: {
path: __dirname + "/dist",
filename: "[name].bundle.[hash].js"
}
}
Example: https://goo.gl/zAGnVX
webpack.config.js
module: {
loaders: [{
test: /\.js$/,
include: [
__dirname + "/src"
],
loader: "babel-loader"
}]
},Example: https://goo.gl/NL79vp
webpack.config.js
module: {
loaders: [{
test: /\.css$/,
include: [
__dirname + "/src"
],
loader: "style-loader!css-loader"
}]
}Example: https://goo.gl/7BP7cT
webpack.config.js
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
...
plugins: [
new ExtractTextPlugin("[name].css"),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}
})
]
};Example: https://goo.gl/7BP7cT
webpack.config.js
module.exports = {
context: __dirname + "/src",
entry: {
A: "./a",
B: "./b",
C: "./c",
},
...
plugins: [
new CommonsChunkPlugin("commons", "commons.js", ["A", "B"])
]
};Example: https://goo.gl/mDjnHL
webpack.config.js
var webpack = require("webpack");
module.exports = {
entry: {
app: "./app.js",
vendor: ["jquery", "underscore", ...],
},
output: {
filename: "bundle.js"
},
plugins: [
new webpack.optimize.CommonsChunkPlugin(
/* chunkName= */"vendor",
/* filename= */"vendor.bundle.js"
)
]
};<script src="vendor.bundle.js"></script>
<script src="bundle.js"></script>Example: https://goo.gl/Mo9voS
webpack.config.js
index.html
var TARGET = process.env.TARGET;
var mergeConfig = merge.bind(null, {
entry: [path.join(ROOT_PATH, 'app/index.js')]
...
});
if(TARGET === 'build') {
module.exports = mergeConfig({
...
, plugins: [
new webpack.optimize.UglifyJsPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production'),
}
})
});
}
if(TARGET === 'dev') {
module.exports = mergeConfig({
...
, plugins: [
new webpack.HotModuleReplacementPlugin()
]
});
}webpack.config.js
"scripts": {
"build": "TARGET=build webpack",
"start": "TARGET=dev node dev-server/server.js"
}package.json (conditional config, same file)
"scripts": {
"build": "webpack --config webpack.pro.config.js",
"start": "webpack --config webpack.dev.config.js"
}package.json conditional config, different files)
*using the appropriate loaders
module.exports = {
...
devServer: {
port: 3000,
stats: { colors: true },
inline: true,
publicPath: "/dist/"
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
};webpack.config.js
Example: https://goo.gl/dC6B2h
Example: https://goo.gl/uyurxJ
By Daniel de la Cruz Calvo
A brief overview of webpack features for web designers and frontend developers.
Software Engineer and professional mentor for developers.