-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
webpack.config.js
245 lines (229 loc) 路 6.9 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
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
'use strict';
const path = require('path');
const fse = require('fs-extra');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { ESBuildMinifyPlugin } = require('esbuild-loader');
const WebpackBar = require('webpackbar');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const alias = require('./webpack.alias');
const getClientEnvironment = require('./env');
const createPluginsExcludePath = require('./utils/create-plugins-exclude-path');
const EE_REGEX = /from.* ['"]ee_else_ce\//;
module.exports = ({
cacheDir,
dest,
entry,
env,
optimize,
pluginsPath,
options = {
backend: 'http://localhost:1337',
adminPath: '/admin/',
features: [],
},
roots = {
eeRoot: './ee/admin',
ceRoot: './admin/src',
},
tsConfigFilePath,
}) => {
const isProduction = env === 'production';
const envVariables = getClientEnvironment({ ...options, env });
const webpackPlugins = isProduction
? [
new MiniCssExtractPlugin({
filename: '[name].[chunkhash].css',
chunkFilename: '[name].[chunkhash].chunkhash.css',
ignoreOrder: true,
}),
new WebpackBar(),
]
: [];
const excludeRegex = createPluginsExcludePath(pluginsPath);
return {
mode: isProduction ? 'production' : 'development',
bail: !!isProduction,
devtool: isProduction ? false : 'eval-source-map',
experiments: {
topLevelAwait: true,
},
entry: [entry],
output: {
path: dest,
publicPath: options.adminPath,
// Utilize long-term caching by adding content hashes (not compilation hashes)
// to compiled assets for production
filename: isProduction ? '[name].[contenthash:8].js' : '[name].bundle.js',
chunkFilename: isProduction ? '[name].[contenthash:8].chunk.js' : '[name].chunk.js',
},
optimization: {
minimize: optimize,
minimizer: [
new ESBuildMinifyPlugin({
target: 'es2015',
css: true, // Apply minification to CSS assets
}),
],
moduleIds: 'deterministic',
runtimeChunk: true,
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: require.resolve('esbuild-loader'),
include: [cacheDir, ...pluginsPath],
exclude: excludeRegex,
options: {
loader: 'tsx',
target: 'es2015',
},
},
{
test: /\.m?jsx?$/,
include: cacheDir,
oneOf: [
// Use babel-loader for files that distinct the ee and ce code
// These files have an import Something from 'ee_else_ce/
{
test(filePath) {
if (!filePath) {
return false;
}
try {
const fileContent = fse.readFileSync(filePath).toString();
if (fileContent.match(/from.* ['"]ee_else_ce\//)) {
return true;
}
return EE_REGEX.test(fileContent);
} catch (e) {
return false;
}
},
use: {
loader: require.resolve('babel-loader'),
options: {
cacheDirectory: true,
cacheCompression: isProduction,
compact: isProduction,
presets: [
require.resolve('@babel/preset-env'),
require.resolve('@babel/preset-react'),
],
plugins: [
[
require.resolve('@strapi/babel-plugin-switch-ee-ce'),
{
// Imported this tells the custom plugin where to look for the ee folder
roots,
},
],
[
require.resolve('@babel/plugin-transform-runtime'),
{
helpers: true,
regenerator: true,
},
],
[require.resolve('babel-plugin-styled-components'), { pure: true }],
],
},
},
},
// Use esbuild-loader for the other files
{
use: {
loader: require.resolve('esbuild-loader'),
options: {
loader: 'jsx',
target: 'es2015',
},
},
},
],
},
{
test: /\.m?jsx?$/,
include: pluginsPath,
use: {
loader: require.resolve('esbuild-loader'),
options: {
loader: 'jsx',
target: 'es2015',
},
},
},
/**
* This is used to avoid webpack import errors where
* the origin is strict EcmaScript Module.
*
* e. g. a module with javascript mimetype, a '.mjs' file,
* or a '.js' file where the package.json contains '"type": "module"'
*/
{
test: /\.m?jsx?$/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(svg|eot|otf|ttf|woff|woff2)$/,
type: 'asset/resource',
},
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.ico$/],
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 1000,
},
},
},
{
test: /\.html$/,
include: [path.join(__dirname, 'src')],
use: require.resolve('html-loader'),
},
{
test: /\.(mp4|webm)$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 10000,
},
},
},
],
},
resolve: {
alias,
symlinks: false,
extensions: ['.js', '.jsx', '.react.js', '.ts', '.tsx'],
mainFields: ['browser', 'jsnext:main', 'main'],
modules: ['node_modules', path.resolve(__dirname, 'node_modules')],
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
template: path.resolve(__dirname, 'index.html'),
}),
new webpack.DefinePlugin(envVariables),
new NodePolyfillPlugin(),
new ForkTsCheckerPlugin({
typescript: {
configFile: tsConfigFilePath,
},
}),
!isProduction && process.env.REACT_REFRESH !== 'false' && new ReactRefreshWebpackPlugin(),
...webpackPlugins,
].filter(Boolean),
};
};