2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-04-11 19:07:00 +02:00
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
2022-03-07 09:36:48 +01:00
|
|
|
const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
|
2023-08-22 09:37:43 +01:00
|
|
|
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
|
2019-04-11 19:07:00 +02:00
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
2022-03-18 15:21:20 +01:00
|
|
|
const { ESBuildMinifyPlugin } = require('esbuild-loader');
|
2019-05-13 16:01:16 +02:00
|
|
|
const WebpackBar = require('webpackbar');
|
2023-03-23 15:38:03 -07:00
|
|
|
const browserslist = require('browserslist');
|
2023-01-06 11:54:09 +00:00
|
|
|
const browserslistToEsbuild = require('browserslist-to-esbuild');
|
2022-03-12 19:07:54 +07:00
|
|
|
|
2021-06-23 20:18:13 +02:00
|
|
|
const alias = require('./webpack.alias');
|
2021-05-05 10:48:27 +02:00
|
|
|
const getClientEnvironment = require('./env');
|
2023-09-05 10:25:14 +01:00
|
|
|
const { createPluginsExcludePath } = require('./utils/plugins');
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2019-04-29 19:04:23 +02:00
|
|
|
module.exports = ({
|
|
|
|
dest,
|
2022-03-09 17:18:27 +01:00
|
|
|
entry,
|
2019-04-29 19:04:23 +02:00
|
|
|
env,
|
2019-10-10 07:08:04 +02:00
|
|
|
optimize,
|
2023-08-22 09:37:43 +01:00
|
|
|
plugins,
|
2019-04-29 19:04:23 +02:00
|
|
|
options = {
|
|
|
|
backend: 'http://localhost:1337',
|
2021-05-05 10:48:27 +02:00
|
|
|
adminPath: '/admin/',
|
2021-01-27 18:24:24 +01:00
|
|
|
features: [],
|
2019-04-29 19:04:23 +02:00
|
|
|
},
|
2022-04-14 10:59:42 +02:00
|
|
|
tsConfigFilePath,
|
2023-07-26 15:42:33 +02:00
|
|
|
enforceSourceMaps,
|
2019-04-29 19:04:23 +02:00
|
|
|
}) => {
|
2019-04-29 15:48:16 +02:00
|
|
|
const isProduction = env === 'production';
|
2021-05-12 10:28:20 +02:00
|
|
|
|
2021-07-01 15:50:11 +02:00
|
|
|
const envVariables = getClientEnvironment({ ...options, env });
|
2021-05-03 12:35:31 +02:00
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
const webpackPlugins = isProduction
|
2019-04-25 12:34:55 +02:00
|
|
|
? [
|
|
|
|
new MiniCssExtractPlugin({
|
2019-04-29 15:48:16 +02:00
|
|
|
filename: '[name].[chunkhash].css',
|
|
|
|
chunkFilename: '[name].[chunkhash].chunkhash.css',
|
2019-10-08 16:32:07 +02:00
|
|
|
ignoreOrder: true,
|
2019-04-25 12:34:55 +02:00
|
|
|
}),
|
2019-09-24 14:10:57 +02:00
|
|
|
new WebpackBar(),
|
2019-04-29 15:48:16 +02:00
|
|
|
]
|
2021-05-03 12:35:31 +02:00
|
|
|
: [];
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2023-08-22 09:37:43 +01:00
|
|
|
const nodeModulePluginPaths = Object.values(plugins)
|
|
|
|
.filter((plugin) => plugin.info?.packageName || plugin.info?.required)
|
|
|
|
.map((plugin) => plugin.pathToPlugin);
|
|
|
|
|
|
|
|
const excludeRegex = createPluginsExcludePath(nodeModulePluginPaths);
|
2022-10-19 15:15:42 +01:00
|
|
|
|
2023-03-23 15:38:03 -07:00
|
|
|
// Ensure we use the config in this directory, even if run with a different
|
|
|
|
// working directory
|
|
|
|
const browserslistConfig = browserslist.loadConfig({ path: __dirname });
|
|
|
|
const buildTarget = browserslistToEsbuild(browserslistConfig);
|
2023-01-11 09:42:26 +00:00
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
return {
|
|
|
|
mode: isProduction ? 'production' : 'development',
|
2022-08-08 15:50:34 +02:00
|
|
|
bail: !!isProduction,
|
2023-07-26 15:42:33 +02:00
|
|
|
devtool: isProduction && !enforceSourceMaps ? false : 'source-map',
|
2021-07-15 12:34:31 +02:00
|
|
|
experiments: {
|
|
|
|
topLevelAwait: true,
|
|
|
|
},
|
2022-08-11 10:40:09 +02:00
|
|
|
entry: [entry],
|
2019-04-29 15:48:16 +02:00
|
|
|
output: {
|
|
|
|
path: dest,
|
2021-05-05 10:48:27 +02:00
|
|
|
publicPath: options.adminPath,
|
2019-04-29 15:48:16 +02:00
|
|
|
// Utilize long-term caching by adding content hashes (not compilation hashes)
|
|
|
|
// to compiled assets for production
|
2021-05-03 12:35:31 +02:00
|
|
|
filename: isProduction ? '[name].[contenthash:8].js' : '[name].bundle.js',
|
2020-05-25 14:04:09 +02:00
|
|
|
chunkFilename: isProduction ? '[name].[contenthash:8].chunk.js' : '[name].chunk.js',
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
optimization: {
|
2019-10-10 07:08:04 +02:00
|
|
|
minimize: optimize,
|
2019-04-29 15:48:16 +02:00
|
|
|
minimizer: [
|
2022-03-18 15:21:20 +01:00
|
|
|
new ESBuildMinifyPlugin({
|
2023-01-11 09:42:26 +00:00
|
|
|
target: buildTarget,
|
2022-03-18 15:21:20 +01:00
|
|
|
css: true, // Apply minification to CSS assets
|
2019-04-29 15:48:16 +02:00
|
|
|
}),
|
|
|
|
],
|
2022-07-19 11:09:30 +02:00
|
|
|
moduleIds: 'deterministic',
|
2019-04-29 15:48:16 +02:00
|
|
|
runtimeChunk: true,
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
2022-04-14 10:59:42 +02:00
|
|
|
{
|
2023-09-05 10:25:14 +01:00
|
|
|
test: /\.(ts|tsx)$/,
|
2022-04-14 10:59:42 +02:00
|
|
|
loader: require.resolve('esbuild-loader'),
|
2022-10-19 15:15:42 +01:00
|
|
|
exclude: excludeRegex,
|
2022-04-14 10:59:42 +02:00
|
|
|
options: {
|
|
|
|
loader: 'tsx',
|
2023-01-11 09:42:26 +00:00
|
|
|
target: buildTarget,
|
2023-10-05 08:39:49 +01:00
|
|
|
jsx: 'automatic',
|
2022-04-14 10:59:42 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
{
|
2023-08-22 09:37:43 +01:00
|
|
|
test: /\.(js|jsx|mjs)$/,
|
|
|
|
exclude: excludeRegex,
|
2019-04-29 15:48:16 +02:00
|
|
|
use: {
|
2022-03-18 15:21:20 +01:00
|
|
|
loader: require.resolve('esbuild-loader'),
|
2019-04-11 19:07:00 +02:00
|
|
|
options: {
|
2022-03-18 15:21:20 +01:00
|
|
|
loader: 'jsx',
|
2023-01-11 09:42:26 +00:00
|
|
|
target: buildTarget,
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
2022-11-18 10:27:10 +00:00
|
|
|
/**
|
|
|
|
* 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"'
|
|
|
|
*/
|
2022-11-17 17:27:13 +00:00
|
|
|
{
|
|
|
|
test: /\.m?jsx?$/,
|
|
|
|
resolve: {
|
|
|
|
fullySpecified: false,
|
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
{
|
2021-05-03 10:22:52 +02:00
|
|
|
test: /\.css$/i,
|
2019-10-10 06:24:19 +02:00
|
|
|
use: ['style-loader', 'css-loader'],
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(svg|eot|otf|ttf|woff|woff2)$/,
|
2021-12-14 11:46:47 +01:00
|
|
|
type: 'asset/resource',
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
2019-12-11 11:46:08 +01:00
|
|
|
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.ico$/],
|
2021-12-14 11:46:47 +01:00
|
|
|
type: 'asset',
|
|
|
|
parser: {
|
|
|
|
dataUrlCondition: {
|
|
|
|
maxSize: 1000,
|
|
|
|
},
|
2019-12-11 11:46:08 +01:00
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.html$/,
|
|
|
|
include: [path.join(__dirname, 'src')],
|
|
|
|
use: require.resolve('html-loader'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(mp4|webm)$/,
|
2021-12-14 11:46:47 +01:00
|
|
|
type: 'asset',
|
|
|
|
parser: {
|
|
|
|
dataUrlCondition: {
|
|
|
|
maxSize: 10000,
|
|
|
|
},
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias,
|
2022-03-01 12:28:48 +01:00
|
|
|
extensions: ['.js', '.jsx', '.react.js', '.ts', '.tsx'],
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
inject: true,
|
|
|
|
template: path.resolve(__dirname, 'index.html'),
|
|
|
|
}),
|
2021-05-05 15:41:23 +02:00
|
|
|
new webpack.DefinePlugin(envVariables),
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2022-04-14 11:09:12 +02:00
|
|
|
new ForkTsCheckerPlugin({
|
|
|
|
typescript: {
|
|
|
|
configFile: tsConfigFilePath,
|
|
|
|
},
|
|
|
|
}),
|
2022-03-12 19:07:54 +07:00
|
|
|
!isProduction && process.env.REACT_REFRESH !== 'false' && new ReactRefreshWebpackPlugin(),
|
2019-04-29 15:48:16 +02:00
|
|
|
...webpackPlugins,
|
2022-03-12 19:07:54 +07:00
|
|
|
].filter(Boolean),
|
2019-04-29 15:48:16 +02:00
|
|
|
};
|
|
|
|
};
|