Skip to content

Commit d34f117

Browse files
author
Tania Rascia
committed
Upgrade to webpack 5
1 parent 6af142f commit d34f117

21 files changed

+6092
-2948
lines changed

README.md

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# 📦 Webpack Boilerplate
1+
# 📦 webpack Boilerplate
22

33
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) ![David](https://img.shields.io/david/dev/taniarascia/webpack-boilerplate)
44

5-
Sensible Webpack 4 boilerplate using Babel and PostCSS with HMR development server and an optimized production build.
5+
Sensible webpack 5 boilerplate using Babel, PostCSS and Sass with a hot dev server and an optimized production build.
66

77
## Installation
88

@@ -42,48 +42,45 @@ cd dist && http-server
4242

4343
## Features
4444

45-
- [Webpack](https://webpack.js.org/)
45+
- [webpack](https://webpack.js.org/)
4646
- [Babel](https://babeljs.io/)
4747
- [Sass](https://sass-lang.com/)
4848
- [PostCSS](https://postcss.org/)
4949

5050
## Dependencies
5151

52-
### Webpack
52+
### webpack
5353

5454
- [`webpack`](https://github.com/webpack/webpack) - Module and asset bundler.
55-
- [`webpack-cli`](https://github.com/webpack/webpack-cli) - Command line interface for Webpack.
56-
- [`webpack-dev-server`](https://github.com/webpack/webpack-dev-server) - Development server for Webpack.
55+
- [`webpack-cli`](https://github.com/webpack/webpack-cli) - Command line interface for webpack.
56+
- [`webpack-dev-server`](https://github.com/webpack/webpack-dev-server) - Development server for webpack.
5757
- [`webpack-merge`](https://github.com/survivejs/webpack-merge) - Simplify development/production configuration
5858
- [`cross-env`](https://github.com/kentcdodds/cross-env) - Cross platform configuration.
5959

6060
### Babel
6161

62-
- [`@babel/core`](https://www.npmjs.com/package/@babel/core) - Transpile ES6+ to backwards compatible JavaScript.
63-
- [`@babel/plugin-proposal-class-properties`](https://babeljs.io/docs/en/babel-plugin-proposal-class-properties) - Use properties directly on a class. (example Babel config)
64-
- [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env) - Smart defaults for Babel.
62+
- [`@babel/core`](https://www.npmjs.com/package/@babel/core) - Transpile ES6+ to backwards compatible JavaScript
63+
- [`@babel/plugin-proposal-class-properties`](https://babeljs.io/docs/en/babel-plugin-proposal-class-properties) - Use properties directly on a class (an example Babel config)
64+
- [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env) - Smart defaults for Babel
6565

6666
### Loaders
6767

68-
- [`babel-loader`](https://webpack.js.org/loaders/babel-loader/) - Transpile files with Babel and Webpack.
69-
- [`sass-loader`](https://webpack.js.org/loaders/sass-loader/) - Load SCSS and compile to CSS.
70-
- [`node-sass`](https://github.com/sass/node-sass) - Node Sass.
71-
- [`postcss-loader`](https://webpack.js.org/loaders/postcss-loader/) - Process CSS with PostCSS.
72-
- [`cssnano`](https://github.com/cssnano/cssnano) - Optimize and compress PostCSS.
73-
- [`postcss-preset-env`](https://www.npmjs.com/package/postcss-preset-env) - Sensible defaults for PostCSS.
74-
- [`css-loader`](https://webpack.js.org/loaders/css-loader/) - Resolves CSS imports into JS.
75-
- [`style-loader`](https://webpack.js.org/loaders/style-loader/) - Inject CSS into the DOM.
76-
- [`file-loader`](https://webpack.js.org/loaders/file-loader/) - Allow import of files and emit file to build folder.
77-
- [`url-loader`](https://webpack.js.org/loaders/url-loader/) - Encode and inline files. Falls back to file-loader.
68+
- [`babel-loader`](https://webpack.js.org/loaders/babel-loader/) - Transpile files with Babel and webpack
69+
- [`sass-loader`](https://webpack.js.org/loaders/sass-loader/) - Load SCSS and compile to CSS
70+
- [`node-sass`](https://github.com/sass/node-sass) - Node Sass
71+
- [`postcss-loader`](https://webpack.js.org/loaders/postcss-loader/) - Process CSS with PostCSS
72+
- [`postcss-preset-env`](https://www.npmjs.com/package/postcss-preset-env) - Sensible defaults for PostCSS
73+
- [`css-loader`](https://webpack.js.org/loaders/css-loader/) - Resolve CSS imports
74+
- [`style-loader`](https://webpack.js.org/loaders/style-loader/) - Inject CSS into the DOM
7875

7976
### Plugins
8077

81-
- [`clean-webpack-plugin`](https://github.com/johnagan/clean-webpack-plugin) - Remove/clean build folders.
82-
- [`copy-webpack-plugin`](https://github.com/webpack-contrib/copy-webpack-plugin) - Copy files to build directory.
83-
- [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin) - Generate HTML files from template.
84-
- [`mini-css-extract-plugin`](https://github.com/webpack-contrib/mini-css-extract-plugin) - Extract CSS into separate files.
85-
- [`optimize-css-assets-webpack-plugin`](https://github.com/NMFR/optimize-css-assets-webpack-plugin) - Optimize and minimize CSS assets.
86-
- [`terser-webpack-plugin`](https://github.com/webpack-contrib/terser-webpack-plugin) - Minify JavaScript.
78+
- [`clean-webpack-plugin`](https://github.com/johnagan/clean-webpack-plugin) - Remove/clean build folders
79+
- [`copy-webpack-plugin`](https://github.com/webpack-contrib/copy-webpack-plugin) - Copy files to build directory
80+
- [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin) - Generate HTML files from template
81+
- [`mini-css-extract-plugin`](https://github.com/webpack-contrib/mini-css-extract-plugin) - Extract CSS into separate files
82+
- [`optimize-css-assets-webpack-plugin`](https://github.com/NMFR/optimize-css-assets-webpack-plugin) - Optimize and minimize CSS assets
83+
- [`terser-webpack-plugin`](https://github.com/webpack-contrib/terser-webpack-plugin) - Optimize and minimize JavaScript
8784

8885
## Author
8986

config/paths.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const path = require('path')
22

33
module.exports = {
4-
src: path.resolve(__dirname, '../src'), // source files
5-
build: path.resolve(__dirname, '../dist'), // production build files
6-
static: path.resolve(__dirname, '../public'), // static files to copy to build folder
4+
// Source files
5+
src: path.resolve(__dirname, '../src'),
6+
7+
// Production build files
8+
build: path.resolve(__dirname, '../dist'),
9+
10+
// Static files that get copied to build folder
11+
public: path.resolve(__dirname, '../public'),
712
}

config/webpack.common.js

Lines changed: 23 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
11
const paths = require('./paths')
2-
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
2+
3+
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
34
const CopyWebpackPlugin = require('copy-webpack-plugin')
45
const HtmlWebpackPlugin = require('html-webpack-plugin')
56

67
module.exports = {
7-
/**
8-
* Entry
9-
*
10-
* The first place Webpack looks to start building the bundle.
11-
*/
8+
// Where weback looks to start building the bundle
129
entry: [paths.src + '/index.js'],
1310

14-
/**
15-
* Output
16-
*
17-
* Where Webpack outputs the assets and bundles.
18-
*/
11+
// Where webpack outputs the assets and bundles
1912
output: {
2013
path: paths.build,
2114
filename: '[name].bundle.js',
2215
publicPath: '/',
2316
},
2417

25-
/**
26-
* Plugins
27-
*
28-
* Customize the Webpack build process.
29-
*/
18+
// Customize the webpack build process
3019
plugins: [
31-
/**
32-
* CleanWebpackPlugin
33-
*
34-
* Removes/cleans build folders and unused assets when rebuilding.
35-
*/
20+
// Removes/cleans build folders and unused assets when rebuilding
3621
new CleanWebpackPlugin(),
3722

38-
/**
39-
* CopyWebpackPlugin
40-
*
41-
* Copies files from target to destination folder.
42-
*/
23+
// Copies files from target to destination folder
4324
new CopyWebpackPlugin({
4425
patterns: [
4526
{
46-
from: paths.static,
27+
from: paths.public,
4728
to: 'assets',
4829
globOptions: {
4930
ignore: ['*.DS_Store'],
@@ -52,80 +33,38 @@ module.exports = {
5233
],
5334
}),
5435

55-
/**
56-
* HtmlWebpackPlugin
57-
*
58-
* Generates an HTML file from a template.
59-
*/
36+
// Generates an HTML file from a template
37+
// Generates deprecation warning: https://github.com/jantimon/html-webpack-plugin/issues/1501
6038
new HtmlWebpackPlugin({
61-
title: 'Webpack Boilerplate',
62-
favicon: paths.static + '/favicon.png',
39+
title: 'webpack Boilerplate',
40+
favicon: paths.src + '/images/favicon.png',
6341
template: paths.src + '/template.html', // template file
6442
filename: 'index.html', // output file
6543
}),
6644
],
6745

68-
/**
69-
* Module
70-
*
71-
* Determine how modules within the project are treated.
72-
*/
46+
// Determine how modules within the project are treated
7347
module: {
7448
rules: [
75-
/**
76-
* JavaScript
77-
*
78-
* Use Babel to transpile JavaScript files.
79-
*/
80-
{
81-
test: /\.js$/,
82-
exclude: /node_modules/,
83-
use: ['babel-loader'],
84-
},
49+
// JavaScript: Use Babel to transpile JavaScript files
50+
{test: /\.js$/, exclude: /node_modules/, use: ['babel-loader']},
8551

86-
/**
87-
* Styles
88-
*
89-
* Inject CSS into the head with source maps.
90-
*/
52+
// Styles: Inject CSS into the head with source maps
9153
{
9254
test: /\.(scss|css)$/,
9355
use: [
9456
'style-loader',
95-
{ loader: 'css-loader', options: { sourceMap: true, importLoaders: 1 } },
96-
{ loader: 'postcss-loader', options: { sourceMap: true } },
97-
{ loader: 'sass-loader', options: { sourceMap: true } },
57+
{loader: 'css-loader', options: {sourceMap: true, importLoaders: 1}},
58+
{loader: 'postcss-loader', options: {sourceMap: true}},
59+
{loader: 'sass-loader', options: {sourceMap: true}},
9860
],
9961
},
10062

101-
/**
102-
* Images
103-
*
104-
* Copy image files to build folder.
105-
*/
106-
{
107-
test: /\.(?:ico|gif|png|jpg|jpeg|webp|svg)$/i,
108-
loader: 'file-loader',
109-
options: {
110-
name: '[path][name].[ext]',
111-
context: 'src', // prevent display of src/ in filename
112-
},
113-
},
63+
// Images: Copy image files to build folder
64+
{test: /\.(?:ico|gif|png|jpg|jpeg)$/i, type: 'asset/resource'},
11465

115-
/**
116-
* Fonts
117-
*
118-
* Inline font files.
119-
*/
120-
{
121-
test: /\.(woff(2)?|eot|ttf|otf|)$/,
122-
loader: 'url-loader',
123-
options: {
124-
limit: 8192,
125-
name: '[path][name].[ext]',
126-
context: 'src', // prevent display of src/ in filename
127-
},
128-
},
66+
// Fonts and SVGs: Inline files
67+
{test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, type: 'asset/inline'},
12968
],
13069
},
13170
}

config/webpack.dev.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
const paths = require('./paths')
2+
23
const webpack = require('webpack')
3-
const {merge} = require('webpack-merge')
4+
const { merge } = require('webpack-merge')
45
const common = require('./webpack.common.js')
56

67
module.exports = merge(common, {
7-
/**
8-
* Mode
9-
*
10-
* Set the mode to development or production.
11-
*/
8+
// Set the mode to development or production
129
mode: 'development',
1310

14-
/**
15-
* Devtool
16-
*
17-
* Control how source maps are generated.
18-
*/
11+
// Control how source maps are generated
1912
devtool: 'inline-source-map',
2013

21-
/**
22-
* DevServer
23-
*
24-
* Spin up a server for quick development.
25-
*/
14+
// Spin up a server for quick development
2615
devServer: {
2716
historyApiFallback: true,
2817
contentBase: paths.build,
@@ -33,11 +22,7 @@ module.exports = merge(common, {
3322
},
3423

3524
plugins: [
36-
/**
37-
* HotModuleReplacementPlugin
38-
*
39-
* Only update what has changed.
40-
*/
25+
// Only update what has changed on hot reload
4126
new webpack.HotModuleReplacementPlugin(),
4227
],
4328
})

config/webpack.prod.js

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
const paths = require('./paths')
2-
const {merge} = require('webpack-merge')
2+
const { merge } = require('webpack-merge')
33
const common = require('./webpack.common.js')
4+
45
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
5-
const TerserJSPlugin = require('terser-webpack-plugin')
6-
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
6+
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
7+
const TerserPlugin = require('terser-webpack-plugin')
78

89
module.exports = merge(common, {
910
mode: 'production',
1011
devtool: false,
1112
output: {
1213
path: paths.build,
1314
publicPath: '/',
14-
filename: '[name].[contenthash].bundle.js',
15+
filename: 'js/[name].[contenthash].bundle.js',
1516
},
1617
plugins: [
17-
/**
18-
* MiniCssExtractPlugin
19-
*
20-
* Extracts CSS into separate files.
21-
*
22-
* Note: style-loader is for development, MiniCssExtractPlugin is for production.
23-
* They cannot be used together in the same config.
24-
*/
18+
// Extracts CSS into separate files
19+
// Note: style-loader is for development, MiniCssExtractPlugin is for production
2520
new MiniCssExtractPlugin({
2621
filename: 'styles/[name].[contenthash].css',
2722
chunkFilename: '[id].css',
@@ -36,7 +31,8 @@ module.exports = merge(common, {
3631
{
3732
loader: 'css-loader',
3833
options: {
39-
importLoaders: 1,
34+
importLoaders: 2,
35+
sourceMap: false,
4036
},
4137
},
4238
'postcss-loader',
@@ -45,28 +41,14 @@ module.exports = merge(common, {
4541
},
4642
],
4743
},
48-
49-
/**
50-
* Optimization
51-
*
52-
* Production minimizing of JavaSvript and CSS assets.
53-
*/
5444
optimization: {
55-
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
45+
minimize: true,
46+
minimizer: [new OptimizeCssAssetsPlugin(), new TerserPlugin()],
5647
// Once your build outputs multiple chunks, this option will ensure they share the webpack runtime
5748
// instead of having their own. This also helps with long-term caching, since the chunks will only
5849
// change when actual code changes, not the webpack runtime.
59-
runtimeChunk: 'single',
60-
// This breaks apart commonly shared deps (react, semantic ui, etc) into one shared bundle. React, etc
61-
// won't change as often as the app code, so this chunk can be cached separately from app code.
62-
splitChunks: {
63-
cacheGroups: {
64-
vendor: {
65-
test: /[\\/]node_modules[\\/](react|react-dom|lodash)[\\/]/,
66-
name: 'vendors',
67-
chunks: 'all',
68-
},
69-
},
50+
runtimeChunk: {
51+
name: 'runtime',
7052
},
7153
},
7254
performance: {

0 commit comments

Comments
 (0)