forked from nikhilnayyar002/react-min
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
136 lines (134 loc) · 4.67 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
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const wmConfig = require("./wm-config");
const CircularDependencyPlugin = require("circular-dependency-plugin");
const { InterpolateHtmlPlugin } = require("./wm-helper");
const ESLintPlugin = require("eslint-webpack-plugin");
/********************************************************************* */
module.exports = (/** @type {"development"|"production"} */ env) => ({
entry: path.resolve(__dirname, wmConfig.sourceDir, wmConfig.webpack.entryFilename),
experiments: {
outputModule: wmConfig.webpack.outputESModule, // https://webpack.js.org/configuration/output/#outputmodule
},
output: {
path: path.resolve(__dirname, wmConfig.outputDir),
publicPath: wmConfig.webpack.publicPath,
// clean the output dir before generating new output
// we are manually cleaning in custom build script
clean: false,
module: wmConfig.webpack.outputESModule, // https://webpack.js.org/configuration/output/#outputmodule
pathinfo: false, // https://webpack.js.org/guides/build-performance/#output-without-path-info
},
resolve: wmConfig.webpack.resolve,
module: {
rules: [
{
test: wmConfig.babel.testFilesRegex,
exclude: /node_modules/,
include: path.resolve(__dirname, wmConfig.sourceDir),
use: [
{
loader: "babel-loader",
options: wmConfig.babel.getBabelLoaderDefaultOptions(),
},
],
},
{
test: /\.(eot|woff|woff2|ttf)$/i,
type: "asset/resource",
},
{
test: /\.(png|jpe?g|gif)$/i,
type: "asset",
parser: {
dataUrlCondition: {
maxSize: wmConfig.webpack.inlineAssetMaxSize,
},
},
},
/** svg
* https://react-svgr.com/docs/webpack/#handle-svg-in-css-sass-or-less
* https://github.com/gregberge/svgr/issues/551#issuecomment-883073902
* https://webpack.js.org/configuration/module/#ruleoneof
* https://github.com/gregberge/svgr/pull/650
*/
{
test: /\.svg$/,
oneOf: [
{
issuer: /\.[jt]sx?$/,
resourceQuery: /react/, // *.svg?react
use: ["@svgr/webpack"],
},
{
type: "asset",
parser: {
dataUrlCondition: wmConfig.webpack.inlineSvgWhenAssetMaxSizeReached
? {
maxSize: wmConfig.webpack.inlineAssetMaxSize,
}
: () => false,
},
},
],
},
/** */
{
test: /\.css$/,
use: [{ loader: "css-loader" }],
},
{
test: /\.txt$/,
type: "asset/source",
},
{
test: /\.s[ac]ss$/i,
use: [{ loader: "css-loader" }, { loader: "sass-loader" }],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, wmConfig.publicDir, wmConfig.publicDirHtmlFileName),
filename: path.resolve(__dirname, wmConfig.outputDir, wmConfig.publicDirHtmlFileName),
inject: "body", // inject bundle's inside body tag at the end
scriptLoading: wmConfig.webpack.outputESModule ? "module" : "defer",
}),
new InterpolateHtmlPlugin(wmConfig.specialEnvVariables),
new CircularDependencyPlugin({
// exclude detection of files based on a RegExp
exclude: /node_modules/,
// include specific files based on a RegExp
include: new RegExp(wmConfig.sourceDir),
// add errors to webpack instead of warnings
failOnError: true,
// allow import cycles that include an asyncronous import,
// e.g. via import(/* webpackMode: "weak" */ './file.js')
allowAsyncCycles: false,
// set the current working directory for displaying module paths
cwd: process.cwd(),
}),
new ESLintPlugin({
exclude: path.resolve(__dirname, "node_modules"),
files: path.resolve(__dirname, wmConfig.sourceDir),
extensions: wmConfig.webpack.resolve.extensions,
}),
wmConfig.typescript &&
(env === "production"
? wmConfig.typescriptErrorCheckInProd
: env === "development" && wmConfig.typescriptErrorCheckInDev) &&
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
mode: "write-references",
},
}),
wmConfig.webpack.environmentVariablesInApp.length &&
new webpack.EnvironmentPlugin(wmConfig.webpack.environmentVariablesInApp),
].filter(Boolean),
});