Skip to content
This repository was archived by the owner on Feb 14, 2022. It is now read-only.

Commit d3ff19f

Browse files
committed
upgrade to webpack 5
1 parent 0e5df3c commit d3ff19f

File tree

6 files changed

+55
-70
lines changed

6 files changed

+55
-70
lines changed

README.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MixPack
22

3-
[![Netlify Status](https://api.netlify.com/api/v1/badges/981f8252-c7bd-4215-8b05-60aee8402a2d/deploy-status)](https://app.netlify.com/sites/mixpack/deploys) ![webpack](https://img.shields.io/badge/webpack-4.44.1-green)
3+
[![Netlify Status](https://api.netlify.com/api/v1/badges/981f8252-c7bd-4215-8b05-60aee8402a2d/deploy-status)](https://app.netlify.com/sites/mixpack/deploys) ![webpack](https://img.shields.io/badge/webpack-5.0.0-green)
44

55
A mixed TypeScript/JavaScript Webpack boilerplate with Express. [Click here to see it live on Netlify](https://mixpack.netlify.app).
66

@@ -85,9 +85,6 @@ Visualize Webpack output
8585
- webpack: module and asset bundler
8686
- webpack-cli: command line interface for Webpack
8787
- webpack-merge: simplify development/production configuration
88-
- webpack-hot-middleware: add hot-reloading capabilities to Express without webpack-dev-server
89-
- webpack-dev-middleware: serves the files emitted from Webpack over Express.
90-
- webpack-livereload-plugin: trigger reloads from Webpack's build pipeline for html includes inside EJS template
9188
- webpack-node-externals: exclude node modules in Webpack
9289

9390
### Loaders
@@ -109,18 +106,15 @@ Visualize Webpack output
109106
- PreloadWebpackPlugin
110107
- FaviconsWebpackPlugin
111108
- Minifiers
112-
- TerserPlugin
113-
- OptimizeCSSAssetsPlugin
109+
- TerserPlugin (Comes bundled with Webpack)
110+
- CssMinimizerWebpackPlugin
114111
- ImageminPlugin
115112
- Linters
116113
- ESLintPlugin
117114
- StylelintPlugin
118-
- Build Optimizers
119-
- HardSourceWebpackPlugin
120115
- Miscellaneous
121116
- Webpack Bundle Analyzer
122-
- LiveReloadPlugin (for EJS includes)
123-
- imagemin/imagemin-webp - (non-Webpack Plugins)
117+
- imagemin-webp - (separately generate webp images)
124118

125119
### Transpilers/Tools
126120
- Babel: transpile modern JavaScript into browser recognizable JavaScript
@@ -138,6 +132,7 @@ Visualize Webpack output
138132
### Back-end Tools
139133
- Express: server framework for Node.js
140134
- serverless-http: allows you to 'wrap' your API for serverless use (used for hosting on Netlify)
135+
- webpack-dev-server: development server that provides live reloading
141136

142137
### Testing
143138
- Jest - JavaScript testing framework

package.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"coverage": "jest --coverage",
99
"optimize": "node ./scripts/optimize-images.js",
1010
"analyze": "webpack --json --config ./scripts/webpack.prod.js > stats.json && webpack-bundle-analyzer --port 8080 stats.json",
11-
"dev": "rm -rf dist && webpack --mode development --config ./scripts/webpack.server.config.js && webpack --mode development --config ./scripts/webpack.dev.js && node ./dist/server.js",
12-
"build": "rm -rf dist && webpack --mode production --config ./scripts/webpack.server.config.js && webpack --mode production --config ./scripts/webpack.prod.js",
11+
"dev": "rm -rf dist && webpack serve --mode development --config ./scripts/webpack.dev.js",
12+
"build": "rm -rf dist && webpack --config ./scripts/webpack.server.config.js && webpack --mode production --config ./scripts/webpack.prod.js",
1313
"serve": "node ./dist/server.js"
1414
},
1515
"keywords": [
@@ -43,6 +43,7 @@
4343
"babel-jest": "^26.5.2",
4444
"babel-loader": "^8.1.0",
4545
"css-loader": "^4.2.1",
46+
"css-minimizer-webpack-plugin": "^1.1.5",
4647
"dart-sass": "^1.25.0",
4748
"eslint": "^7.11.0",
4849
"eslint-config-airbnb-base": "^14.2.0",
@@ -54,16 +55,14 @@
5455
"favicons-webpack-plugin": "^4.2.0",
5556
"file-loader": "^6.0.0",
5657
"font-loader": "^0.1.2",
57-
"hard-source-webpack-plugin": "^0.13.1",
5858
"html-loader": "^1.3.2",
59-
"html-webpack-plugin": "^4.3.0",
59+
"html-webpack-plugin": "^5.0.0-alpha.6",
6060
"imagemin": "^7.0.1",
6161
"imagemin-webp": "^6.0.0",
6262
"imagemin-webpack-plugin": "^2.4.2",
6363
"jest": "^26.4.1",
6464
"jest-transform-stub": "^2.0.0",
6565
"mini-css-extract-plugin": "^0.10.0",
66-
"optimize-css-assets-webpack-plugin": "^5.0.3",
6766
"postcss": "^8.1.2",
6867
"postcss-import": "^13.0.0",
6968
"postcss-loader": "^4.0.4",
@@ -80,17 +79,14 @@
8079
"stylelint-order": "^4.1.0",
8180
"stylelint-scss": "^3.18.0",
8281
"stylelint-webpack-plugin": "^2.1.1",
83-
"terser-webpack-plugin": "^3.0.0",
8482
"ts-jest": "^26.4.1",
8583
"ts-loader": "^8.0.5",
8684
"typescript": "^4.0.3",
8785
"url-loader": "^4.1.0",
88-
"webpack": "^4.44.1",
86+
"webpack": "^5.0.0-rc.6",
8987
"webpack-bundle-analyzer": "^3.9.0",
90-
"webpack-cli": "^3.3.12",
91-
"webpack-dev-middleware": "^3.7.2",
92-
"webpack-hot-middleware": "^2.25.0",
93-
"webpack-livereload-plugin": "^2.3.0",
88+
"webpack-cli": "^4.2.0",
89+
"webpack-dev-server": "^3.11.0",
9490
"webpack-merge": "^5.2.0",
9591
"webpack-node-externals": "^2.5.1"
9692
}

scripts/webpack.dev.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,65 @@
11
const path = require('path');
2+
const chokidar = require('chokidar');
23
const webpack = require('webpack');
34
const { merge } = require('webpack-merge');
45
const common = require('./webpack.common.js');
5-
const LiveReloadPlugin = require('webpack-livereload-plugin');
66
const ESLintPlugin = require('eslint-webpack-plugin');
77
const StylelintPlugin = require('stylelint-webpack-plugin');
8-
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
8+
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
99

1010
module.exports = (env, argv) => {
1111
const config = {
1212
mode: argv.mode,
1313
// Map your compiled code back to your original source code.
1414
devtool: 'inline-source-map',
1515
target: 'web',
16-
entry: {
17-
main: [
18-
'webpack/hot/dev-server',
19-
'webpack-hot-middleware/client',
20-
],
21-
},
2216
output: {
2317
filename: '[name].js',
2418
// specify chunck path for code splitted files
2519
chunkFilename: '[name].js',
2620
},
27-
plugins: [
21+
devServer: {
2822
/*
29-
Provides an intermediate caching step for modules
30-
Not using on prod builds because Netlify stalls out a while
23+
We're using chokidar to watch for html includes so reloading can occur automatically.
24+
Why use this method?
25+
- webpack-live-reload does not support Webpack 5 as of 11/2020
26+
- webpack-dev-server uses chokidar internally so we don't have to install an extra dependency
27+
28+
https://stackoverflow.com/questions/52322913/webpack-4-devserver-hmr-plus-full-reload-on-other-file-changes-like-views
3129
*/
32-
new HardSourceWebpackPlugin(),
33-
new LiveReloadPlugin({
34-
appendScriptTag: true,
35-
}),
30+
before(app, server) {
31+
chokidar.watch([
32+
'./src/pages/**/**'
33+
]).on('all', function() {
34+
server.sockWrite(server.sockets, 'content-changed');
35+
})
36+
},
37+
historyApiFallback: true,
38+
contentBase: path.resolve(__dirname, '../dist'),
39+
publicPath: '/',
40+
open: true,
41+
compress: true,
42+
hot: true,
43+
port: 8080,
44+
},
45+
plugins: [
3646
new ESLintPlugin({
3747
extensions: ['js', 'ts'],
3848
}),
3949
new StylelintPlugin(),
50+
new FaviconsWebpackPlugin({
51+
mode: 'light',
52+
}),
4053
new webpack.HotModuleReplacementPlugin(),
4154
],
4255
optimization: {
4356
// don't minimize so we can debug
4457
minimize: false,
58+
/*
59+
The value 'single' instead creates a runtime file to be shared for all generated chunks.
60+
https://github.com/webpack/webpack-dev-server/issues/2792
61+
*/
62+
runtimeChunk: "single",
4563
},
4664
};
4765
return merge(common(env, argv), config);

scripts/webpack.prod.js

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const path = require('path');
22
const { merge } = require('webpack-merge');
33
const common = require('./webpack.common.js');
44
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
5-
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
6-
const TerserPlugin = require('terser-webpack-plugin');
5+
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
76
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
87
const ImageminPlugin = require('imagemin-webpack-plugin').default;
98

@@ -20,42 +19,22 @@ module.exports = (env, argv) => {
2019
},
2120
plugins: [
2221
// generate optimized favicons for different devices
23-
new FaviconsWebpackPlugin(),
22+
new FaviconsWebpackPlugin({
23+
// remove mode to enable default 'webp' for comprehensive favicon generation
24+
mode: 'light',
25+
}),
2426
// where the compiled scss is saved to
2527
new MiniCssExtractPlugin({
2628
filename: '[name].[contenthash:8].css',
2729
}),
28-
// compress images
29-
new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i }),
3030
],
3131
optimization: {
3232
minimize: true,
3333
minimizer: [
34-
// minimize js
35-
/*
36-
Are you getting a 'TypeError: Cannot read property 'javascript' of undefined' from terser when running a build?
37-
Was it because you upgraded? If so, downgrade back to v3.0.0
38-
*/
39-
new TerserPlugin({
40-
// https://github.com/webpack-contrib/terser-webpack-plugin/#remove-comments
41-
terserOptions: {
42-
output: {
43-
comments: false,
44-
},
45-
},
46-
extractComments: false,
47-
}),
48-
// minimize css
49-
new OptimizeCSSAssetsPlugin({
50-
// remove comments
51-
cssProcessorPluginOptions: {
52-
preset: ['default', {
53-
discardComments: {
54-
removeAll: true,
55-
},
56-
}],
57-
},
58-
}),
34+
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`)
35+
'...',
36+
new CssMinimizerPlugin(),
37+
new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i }),
5938
],
6039
// Webpack will identify any code it thinks isn’t being used and mark it during the initial bundling step
6140
usedExports: true, // Set TRUE to enable tree-shaking

scripts/webpack.server.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
const nodeExternals = require('webpack-node-externals');
33

44
module.exports = (env, argv) => {
5-
const SERVER_PATH = (argv.mode === 'production') ? './scripts/server-prod.js' : './scripts/server-dev.js';
6-
75
return {
86
entry: {
9-
server: SERVER_PATH,
7+
server: './scripts/server-prod.js',
108
},
119
/*
1210
if you don't put this as, __dirname and __filename return blank or /

src/pages/template.ejs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8">
5-
<link rel="icon" href="data:;base64,=">
65
<title><%= htmlWebpackPlugin.options.title %></title>
76
<meta name="description" content="<%= htmlWebpackPlugin.options.description %>">
87
</head>

0 commit comments

Comments
 (0)