-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack.config.js
191 lines (188 loc) · 5.58 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const path = require('path')
const { spawn } = require('child_process')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const WebpackAssetsManifest = require('webpack-assets-manifest')
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin')
const config = require('./src/config')
/**
* A webpack plugin that starts a node.js server after the assets are compiled.
* This is a required step, because the node server is parsing manifest.json
* when booting up.
*/
const StartServerAfterBuild = () => {
let server = false
return {
apply: (compiler) => {
compiler.hooks.done.tap('StartServerAfterBuild', () => {
if (server) {
server.stdin.write('rs\n')
} else {
server = spawn(
"npx nodemon --inspect --ignore 'src/**/__test__/**/*'",
{ stdio: ['pipe', 'inherit', 'inherit'], shell: true }
)
}
})
},
}
}
module.exports = (env) => ({
devtool: config.isProd ? false : 'source-map',
mode: config.isProd ? 'production' : 'development',
entry: {
styles: './assets/stylesheets/application.scss',
app: [
'./assets/javascripts/govuk-frontend-all.js',
'./assets/javascripts/app.js',
],
'react-app': [
'react-app-polyfill/stable',
'details-element-polyfill',
'./src/client/index.jsx',
],
'export-win-review': ['./src/client/export-win-review.jsx'],
},
output: {
path: config.buildDir,
publicPath: '/',
filename: config.isProd ? 'js/[name].[chunkhash:8].js' : 'js/[name].js',
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]',
},
optimization: { sideEffects: false },
plugins: [
new MiniCssExtractPlugin({
filename: config.isProd
? 'css/[name].[contenthash:8].css'
: 'css/[name].css',
chunkFilename: 'css/[name].[id].css',
}),
new WebpackAssetsManifest({ output: 'assets-manifest.json' }),
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.sharpMinify,
options: {
encodeOptions: {
jpeg: {
// https://sharp.pixelplumbing.com/api-output#jpeg
quality: 100,
},
webp: {
// https://sharp.pixelplumbing.com/api-output#webp
lossless: true,
},
avif: {
// https://sharp.pixelplumbing.com/api-output#avif
lossless: true,
},
// png by default sets the quality to 100%, which is same as lossless
// https://sharp.pixelplumbing.com/api-output#png
png: {},
// gif does not support lossless compression at all
// https://sharp.pixelplumbing.com/api-output#gif
gif: {},
},
},
},
}),
env && env.development ? StartServerAfterBuild() : null,
].filter(Boolean),
resolve: {
modules: [
'node_modules',
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'src', 'client', 'components'),
],
fallback: {
path: false,
fs: false,
child_process: false,
module: false,
net: false,
tls: false,
process: false,
os: false,
http: false,
https: false,
stream: false,
zlib: false,
url: require.resolve('url/'),
},
extensions: ['.*', '.js', '.jsx', '.json'],
alias: {
'govuk-colours': path.resolve(__dirname, 'src/client/utils/colours.js'),
},
},
module: {
rules: [
{
test: /\.(js|jsx|mjs)$/,
// Some packages need transpiling to es6 so below we exclude all node_modules but include specific modules.
exclude:
/node_modules\/(?!(.*(hex-rgb|set-harmonic-interval|rafz|react-spring|internmap|unified|is-plain-obj|d3-.*))\/).*/,
loader: 'babel-loader',
options: {
cacheDirectory: './babel_cache',
},
},
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.(eot|ttf|woff|woff2)$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name].[hash:8].[ext]',
},
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
type: 'asset',
generator: {
filename: 'images/[name].[hash:8][ext]',
},
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
// the below loaders load in reverse order
// 3. turns css into commonjs
{
loader: 'css-loader',
options: {
sourceMap: !config.isProd,
importLoaders: 3,
url: {
filter: (url) => {
const files = [
'/assets/images/icon-pointer.png',
'/assets/images/icon-pointer-2x.png',
'/assets/images/govuk-crest.png',
'/assets/images/govuk-crest-2x.png',
]
return !files.some((file) => url.includes(file))
},
},
},
},
{
loader: 'postcss-loader',
},
// 2. rewrites relative paths in url() statements based on the original source file
'resolve-url-loader',
// 1. turns sass into css
{
loader: 'sass-loader',
options: { sourceMap: true }, // needed for resolve-url-loader
},
],
},
],
},
})