Better Programming

Advice for programmers.

Follow publication

Webpack 4 — The Complete Guide

Esakkimuthu E
Better Programming
Published in
8 min readSep 2, 2019

Webpack Setup

npm init
npm install --save-dev webpack webpack-dev-server webpack-cli
{
"name": "projectName",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"start" : "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.2.6",
"webpack-cli": "^3.2.3"
}
}

Configuring Webpack

Entry

module.exports = {
entry: ‘./src/index.js’
};

Output

const path = require(‘path’);
module.exports = {
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist’),
filename: ‘main.js’
}
};
“script” : {
"start" : "webpack --config webpack.config.js"
}

Mode

const path = require(‘path’);
module.exports = {
mode: "development",
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist’),
filename: ‘main.js’
}
};

Loaders

CSS loaders

npm install --save-dev style-loader css-loader
const path = require(‘path’);
module.exports = {
mode: "development",
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist’),
filename: ‘main.js’
},
module:{
rules:[
{
test: /\.css$/,
use: ["style-loader", "css-loader"]}]
}
};
npm install sass-loader node-sass webpack --sav-dev
const path = require(‘path’);
module.exports = {
mode: "development",
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist’),
filename: ‘main.js’
},
module:{
rules:[
{
test: /\.css$/,
use: [
"style-loader", // Injects style into DOM
"css-loader", // Turns CSS into JS
"sass-loader" // Turns SCSS into CSS
]
}
]
}
};

Cache Busting and Plugins

output : { 
filename : "main.[contentHash].js"
}

Plugins

Html webpack plugin

npm install --save-dev html-webpack-plugin
const path = require(‘path’);
module.exports = {
mode: "development",
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist’),
filename: ‘main.js’
},
plugins: [
new HtmlWebpackPlugin()
]
};
<!DOCTYPE html>
<html>
<head>
<meta
charset=”UTF-8">
<title>
Webpack App</title>
</head>
<body>
<script type=”text/javascript” src=”main.170skb99wrjcb.js”></script>
</body>
</html>
const path = require(‘path’);
module.exports = {
mode: "development",
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist’),
filename: ‘main.js’
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
]
};
npm install mini-css-extract-plugin
const path = require(‘path’);
module.exports = {
mode: "development",
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist’),
filename: ‘main.js’
},
module:{
rules:[
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"style-loader", // Injects style into DOM
"css-loader", // Turns CSS into JS
"sass-loader" // Turns SCSS into CSS
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new MiniCssExtractPlugin()
]
};

Splitting Code Dev and Production Mode

npm install --save-dev webpack-dev-server
npm install --save-dev webpack-merge
const path = require(“path”);
var HtmlWebpackPlugin = require(“html-webpack-plugin”); module.exports = {
entry: “./src/index.js”,
plugins: [
new HtmlWebpackPlugin({
template: “./src/template.html”
})
],
module: {
rules: [
{
test: /\.scss$/,
use: [
“style-loader”, // Inject styles into DOM
“css-loader”, // Turns css into commonjs
“sass-loader” // Turns sass into css
]
}
]
}
};
const path = require(“path”);
const common = require(“./webpack.common”);
const merge = require(“webpack-merge”);
module.exports = merge(common, {
mode: “development”,
output: {
filename: “main.js”,
path: path.resolve(__dirname, “dist”)
}
});
const path = require(“path”);
const common = require(“./webpack.common”);
const merge = require(“webpack-merge”);
module.exports = merge(common, {
mode: “production”,
output: {
filename: “main.[contentHash].js”,
path: path.resolve(__dirname, “dist”)
}
});
“script” : {
"start": "webpack-dev-server --config webpack.dev.js --open",
"build": "webpack --config webpack.prod.js"
}
module.exports = { 
entry: {
main: “./src/index.js”,
vendor: "./src/vendor.js"
}
}
module.exports = merge(common, { 
mode: “development”,
output: {
filename: “[name].bundle.js”,
path: path.resolve(__dirname, “dist”)
}
});
module.exports = merge(common, { 
mode: “production”,
output: {
filename: “[name].[contentHash].js”,
path: path.resolve(__dirname, “dist”)
}
});

Conclusion

Esakkimuthu E
Esakkimuthu E

Written by Esakkimuthu E

Front End Developer with over eight years of commercial experience, Passionate about front-end architecture, performance, scalable code, and thoughtful design.

Responses (2)

Write a response