/
webpack.common.js
404 lines (376 loc) · 13 KB
/
webpack.common.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
const fs = require('fs')
const path = require('path')
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const Dotenv = require('dotenv-webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const webpack = require('webpack')
const { WebpackManifestPlugin } = require('webpack-manifest-plugin')
const { merge } = require('webpack-merge')
const { RetryChunkLoadPlugin } = require('webpack-retry-chunk-load-plugin')
const { getWebSideDefaultBabelConfig } = require('@redwoodjs/babel-config')
const {
ChunkReferencesPlugin,
} = require('@redwoodjs/internal/dist/webpackPlugins/ChunkReferencesPlugin')
const { getConfig, getPaths } = require('@redwoodjs/project-config')
const redwoodConfig = getConfig()
const redwoodPaths = getPaths()
const isUsingVite = redwoodConfig.web.bundler !== 'webpack'
/** @returns {{[key: string]: string}} Env vars */
const getEnvVars = () => {
const redwoodEnvPrefix = 'REDWOOD_ENV_'
const includeEnvKeys = redwoodConfig.web.includeEnvironmentVariables
const redwoodEnvKeys = Object.keys(process.env).reduce((prev, next) => {
if (
next.startsWith(redwoodEnvPrefix) ||
(includeEnvKeys && includeEnvKeys.includes(next))
) {
prev[`process.env.${next}`] = JSON.stringify(process.env[next])
}
return prev
}, {})
return redwoodEnvKeys
}
/** @returns {import('webpack').RuleSetLoader[]} */
const getStyleLoaders = (isEnvProduction) => {
const styleOrExtractLoader = isEnvProduction
? MiniCssExtractPlugin.loader
: require.resolve('style-loader')
const cssLoader = (withModules, importLoaders) => {
// Obscured classnames in production, more expressive classnames in development.
const localIdentName = isEnvProduction
? '[contenthash:base64]'
: '[path][name]__[local]--[contenthash:base64:5]'
const loaderConfig = {
loader: require.resolve('css-loader'),
options: {
sourceMap: !isEnvProduction,
importLoaders,
},
}
// Enables CSS modules
if (withModules) {
loaderConfig.options.modules = { localIdentName }
}
return loaderConfig
}
const resolveUrlLoader = {
loader: require.resolve('resolve-url-loader'),
options: {
root: path.join(redwoodPaths.web.base, '/public'),
},
}
const paths = getPaths()
const hasPostCssConfig = fs.existsSync(paths.web.postcss)
// We only use the postcss-loader if there is a postcss config file
// at web/config/postcss.config.js
const postCssLoader = hasPostCssConfig
? {
loader: require.resolve('postcss-loader'),
options: {
postcssOptions: {
config: paths.web.postcss,
},
sourceMap: true, // required for resolve-url-loader
},
}
: null
const numImportLoadersForCSS = hasPostCssConfig ? 1 : 0
const numImportLoadersForSCSS = hasPostCssConfig ? 2 : 1
const sassLoader = {
loader: 'sass-loader',
options: {
sourceMap: true, // required for resolve-url-loader
},
}
return [
{
test: /\.module\.css$/,
use: [
styleOrExtractLoader,
cssLoader(true, numImportLoadersForCSS),
isUsingVite && resolveUrlLoader,
postCssLoader,
].filter(Boolean),
},
{
test: /\.css$/,
use: [
styleOrExtractLoader,
cssLoader(false, numImportLoadersForCSS),
isUsingVite && resolveUrlLoader,
postCssLoader,
].filter(Boolean),
sideEffects: true,
},
{
test: /\.module\.scss$/,
use: [
styleOrExtractLoader,
cssLoader(true, numImportLoadersForSCSS),
isUsingVite && resolveUrlLoader,
postCssLoader,
sassLoader,
].filter(Boolean),
},
{
test: /\.scss$/,
use: [
styleOrExtractLoader,
cssLoader(false, numImportLoadersForSCSS),
isUsingVite && resolveUrlLoader,
postCssLoader,
sassLoader,
].filter(Boolean),
sideEffects: true,
},
]
}
/** @returns {import('webpack').Plugin[]} Plugins shared with storybook, as well as the RW app */
const getSharedPlugins = (isEnvProduction) => {
const shouldIncludeFastRefresh =
redwoodConfig.web.fastRefresh !== false && !isEnvProduction
const devTimeAutoImports = isEnvProduction
? {}
: {
mockGraphQLQuery: ['@redwoodjs/testing/web', 'mockGraphQLQuery'],
mockGraphQLMutation: ['@redwoodjs/testing/web', 'mockGraphQLMutation'],
mockCurrentUser: ['@redwoodjs/testing/web', 'mockCurrentUser'],
}
return [
isEnvProduction &&
new MiniCssExtractPlugin({
filename: 'static/css/[name].[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].css',
}),
shouldIncludeFastRefresh &&
// 06-2021 bug with Webpack v5 and sockjs-client dependency conflict
// https://github.com/pmmmwh/react-refresh-webpack-plugin/issues/396
new ReactRefreshWebpackPlugin({ overlay: false }),
new webpack.ProvidePlugin({
React: 'react',
gql: 'graphql-tag',
...devTimeAutoImports,
}),
// The define plugin will replace these keys with their values during build
// time. Note that they're used in packages/web/src/config.ts, and made available in globalThis
new webpack.DefinePlugin({
['RWJS_ENV']: JSON.stringify({
RWJS_API_GRAPHQL_URL:
redwoodConfig.web.apiGraphQLUrl ??
`${redwoodConfig.web.apiUrl}/graphql`,
RWJS_API_URL: redwoodConfig.web.apiUrl,
__REDWOOD__APP_TITLE:
redwoodConfig.web.title || path.basename(redwoodPaths.base),
}),
['RWJS_DEBUG_ENV']: JSON.stringify({
RWJS_SRC_ROOT: redwoodPaths.base,
REDWOOD_ENV_EDITOR: process.env.REDWOOD_ENV_EDITOR,
}),
...getEnvVars(),
}),
new Dotenv({
path: path.resolve(redwoodPaths.base, '.env'),
silent: true,
// ignoreStub: true, // FIXME: this might not be necessary once the storybook webpack 4/5 stuff is ironed out. See also: https://github.com/mrsteele/dotenv-webpack#processenv-stubbing--replacing
}),
].filter(Boolean)
}
// I've borrowed and learnt extensively from the `create-react-app` repo:
// https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/webpack.config.js
module.exports = (webpackEnv) => {
const isEnvProduction = webpackEnv === 'production'
const shouldIncludeFastRefresh =
redwoodConfig.web.experimentalFastRefresh && !isEnvProduction
const webBabelOptions = getWebSideDefaultBabelConfig()
return {
mode: isEnvProduction ? 'production' : 'development',
...(isEnvProduction
? {
// this is so that users can debug a production build by setting sourceMap = true in redwood.toml
devtool: redwoodConfig.web.sourceMap ? 'source-map' : false,
}
: {
devtool: 'cheap-module-source-map',
}),
entry: {
/**
* Prerender requires a top-level component.
* Before we had `ReactDOM` and a top-level component in the same file (web/index.js).
* If index.js is defined in the user's project, use that, if not
* use the one provided in web/dist/entry/index.js
*/
app:
redwoodPaths.web.index ||
require.resolve('@redwoodjs/web/dist/entry/index.js'),
},
resolve: {
extensions: ['.wasm', '.mjs', '.js', '.jsx', '.ts', '.tsx', '.json'],
alias: {
// https://www.styled-components.com/docs/faqs#duplicated-module-in-node_modules
'styled-components': path.resolve(
redwoodPaths.base,
'node_modules',
'styled-components',
),
'~redwood-app-root': path.resolve(redwoodPaths.web.app),
'~redwood-app-routes': path.resolve(redwoodPaths.web.routes),
react: path.resolve(redwoodPaths.base, 'node_modules', 'react'),
'react-hook-form': path.resolve(
redwoodPaths.base,
'node_modules',
'react-hook-form',
),
},
},
plugins: [
!isEnvProduction && new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: path.resolve(redwoodPaths.base, 'web/src/index.html'),
scriptLoading: 'defer',
inject: true,
chunks: 'all',
}),
new CopyPlugin({
patterns: [
{
from: path.join(redwoodPaths.web.base, 'public'),
to: '',
globOptions: {
ignore: [
path.join(redwoodPaths.web.base, 'public/README.md'),
path.join(redwoodPaths.web.base, 'public/mockServiceWorker.js'),
],
},
},
],
}),
isEnvProduction &&
new RetryChunkLoadPlugin({
cacheBust: `function() {
return Date.now();
}`,
maxRetries: 5,
// @TODO: Add redirect to fatalErrorPage
// lastResortScript: "window.location.href='/500.html';"
}),
isEnvProduction &&
new WebpackManifestPlugin({
fileName: 'client-build-manifest.json',
}),
isEnvProduction && new ChunkReferencesPlugin(),
...getSharedPlugins(isEnvProduction),
].filter(Boolean),
module: {
rules: [
// ** NOTE ** People usually overwrite these loaders via index,
// so it's important to try and keep those indexes stable.
{
oneOf: [
// (0)
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.svg$/],
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 10_000,
},
},
generator: {
filename: 'static/media/[name].[contenthash:8][ext]',
},
},
// (1)
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
exclude: /(node_modules)/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
...webBabelOptions,
cwd: redwoodPaths.base,
plugins: [
shouldIncludeFastRefresh &&
require.resolve('react-refresh/babel'),
...webBabelOptions.plugins,
].filter(Boolean),
},
},
].filter(Boolean),
},
// (2)
// .module.css (2), .css (3), .module.scss (4), .scss (5)
...getStyleLoaders(isEnvProduction),
// (6)
isEnvProduction && {
test: require.resolve('@redwoodjs/router/dist/splash-page'),
use: require.resolve('null-loader'),
},
// (7)
{
test: /\.(svg|ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|cur|ani|pdf)(\?.*)?$/,
type: 'asset/resource',
generator: {
filename: 'static/media/[name].[contenthash:8][ext]',
},
},
// (8)
!redwoodConfig.experimental.realtime.enabled && {
test: require.resolve('@redwoodjs/web/dist/apollo/sseLink'),
use: require.resolve('null-loader'),
},
].filter(Boolean),
},
],
},
optimization: {
mergeDuplicateChunks: true,
splitChunks: {
chunks: 'all',
minChunks: 2,
},
runtimeChunk: {
name: (entrypoint) => `runtime-${entrypoint.name}`,
},
// This doesn't get used when mode !== 'production'
// Because minimize gets set to false, see https://webpack.js.org/configuration/mode/#usage
minimizer: ['...', new CssMinimizerPlugin()],
},
output: {
pathinfo: true,
filename: isEnvProduction
? 'static/js/[name].[contenthash:8].js'
: 'static/js/[name].bundle.js',
chunkFilename: isEnvProduction
? 'static/js/[name].[contenthash:8].chunk.js'
: 'static/js/[name].chunk.js',
path: path.resolve(redwoodPaths.base, 'web/dist'),
publicPath: '/',
devtoolModuleFilenameTemplate: isEnvProduction
? (info) =>
path
.relative(redwoodPaths.web.src, info.absoluteResourcePath)
.replace(/\\/g, '/')
: (info) => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
},
}
}
/** @returns {import('webpack').Configuration} */
module.exports.mergeUserWebpackConfig = (mode, baseConfig) => {
const redwoodPaths = getPaths()
const hasCustomConfig = fs.existsSync(redwoodPaths.web.webpack)
if (!hasCustomConfig) {
return baseConfig
}
const userWebpackConfig = require(redwoodPaths.web.webpack)
if (typeof userWebpackConfig === 'function') {
return userWebpackConfig(baseConfig, { mode })
}
return merge(baseConfig, userWebpackConfig)
}
module.exports.getEnvVars = getEnvVars
module.exports.getSharedPlugins = getSharedPlugins
module.exports.getStyleLoaders = getStyleLoaders