-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.config.js
178 lines (176 loc) · 5.11 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
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const WebpackAssetsManifest = require('webpack-assets-manifest')
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin')
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
const webpack = require('webpack')
const config = require('./src/config')
module.exports = {
devServer: {
hot: true,
static: './.build/',
},
devtool: config.isProd ? false : 'source-map',
mode: config.isDev ? 'development' : 'production',
entry: {
shared: [
...(config.isDev ? ['webpack-hot-middleware/client'] : []),
'./assets/stylesheets/application.scss',
],
datahub: [
'./assets/javascripts/govuk-frontend-all.js',
'./assets/javascripts/app.js',
'./src/client/DataHub/index.jsx',
],
exportWinReview: './src/client/ExportWinReview/index.jsx',
},
output: {
path: config.buildDir,
publicPath: path.join('/', config.assetPath, '/'),
filename: config.isProd ? '[name].[chunkhash:8].js' : '[name].js',
},
optimization: {
sideEffects: false,
runtimeChunk: {
// This is needed for HMR to work with multiple entry points
name: 'runtime',
},
},
plugins: [
new webpack.EnvironmentPlugin({
NODE_ENV: 'production',
}),
new MiniCssExtractPlugin({
filename: config.isProd ? '[name].[contenthash:8].css' : '[name].css',
chunkFilename: '[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: {},
},
},
},
}),
...(config.isDev
? [new webpack.HotModuleReplacementPlugin({}), new ReactRefreshPlugin()]
: []),
].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: '[name].[hash:8].[ext]',
},
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
type: 'asset',
generator: {
filename: '[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.svg',
]
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
},
],
},
],
},
}