2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-04-11 19:07:00 +02:00
|
|
|
const path = require('path');
|
2022-03-18 15:21:20 +01:00
|
|
|
const fse = require('fs-extra');
|
2019-04-11 19:07:00 +02:00
|
|
|
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');
|
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');
|
2021-05-03 12:35:31 +02:00
|
|
|
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
|
2022-03-12 19:07:54 +07:00
|
|
|
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
|
|
|
|
|
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');
|
2022-10-24 10:08:36 +01:00
|
|
|
const createPluginsExcludePath = require('./utils/create-plugins-exclude-path');
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2022-04-08 15:31:29 +02:00
|
|
|
const EE_REGEX = /from.* ['"]ee_else_ce\//;
|
|
|
|
|
2019-04-29 19:04:23 +02:00
|
|
|
module.exports = ({
|
2021-09-09 10:56:57 +02:00
|
|
|
cacheDir,
|
2019-04-29 19:04:23 +02:00
|
|
|
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,
|
2022-03-09 17:18:27 +01:00
|
|
|
pluginsPath,
|
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
|
|
|
},
|
2021-07-01 14:19:50 +02:00
|
|
|
roots = {
|
|
|
|
eeRoot: './ee/admin',
|
|
|
|
ceRoot: './admin/src',
|
|
|
|
},
|
2022-04-14 10:59:42 +02:00
|
|
|
tsConfigFilePath,
|
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
|
|
|
|
2022-10-24 10:08:36 +01:00
|
|
|
const excludeRegex = createPluginsExcludePath(pluginsPath);
|
2022-10-19 15:15:42 +01:00
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
return {
|
|
|
|
mode: isProduction ? 'production' : 'development',
|
2022-08-08 15:50:34 +02:00
|
|
|
bail: !!isProduction,
|
2022-07-19 14:26:57 +02:00
|
|
|
devtool: isProduction ? false : 'eval-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({
|
|
|
|
target: 'es2015',
|
|
|
|
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
|
|
|
{
|
|
|
|
test: /\.tsx?$/,
|
|
|
|
loader: require.resolve('esbuild-loader'),
|
|
|
|
include: [cacheDir, ...pluginsPath],
|
2022-10-19 15:15:42 +01:00
|
|
|
exclude: excludeRegex,
|
2022-04-14 10:59:42 +02:00
|
|
|
options: {
|
|
|
|
loader: 'tsx',
|
|
|
|
target: 'es2015',
|
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
{
|
2022-04-25 10:28:14 +02:00
|
|
|
test: /\.m?jsx?$/,
|
2022-03-18 15:21:20 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-08 15:31:29 +02:00
|
|
|
return EE_REGEX.test(fileContent);
|
2022-03-18 15:21:20 +01:00
|
|
|
} 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,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
2022-03-23 14:51:41 +01:00
|
|
|
[
|
|
|
|
require.resolve('@babel/plugin-transform-runtime'),
|
|
|
|
{
|
|
|
|
helpers: true,
|
|
|
|
regenerator: true,
|
|
|
|
},
|
|
|
|
],
|
2022-03-18 15:21:20 +01:00
|
|
|
[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',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2022-04-25 10:28:14 +02:00
|
|
|
test: /\.m?jsx?$/,
|
2022-03-18 15:21:20 +01:00
|
|
|
include: pluginsPath,
|
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',
|
|
|
|
target: 'es2015',
|
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,
|
|
|
|
symlinks: false,
|
2022-03-01 12:28:48 +01:00
|
|
|
extensions: ['.js', '.jsx', '.react.js', '.ts', '.tsx'],
|
2019-04-29 15:48:16 +02:00
|
|
|
mainFields: ['browser', 'jsnext:main', 'main'],
|
2022-01-04 15:30:17 +01:00
|
|
|
modules: ['node_modules', path.resolve(__dirname, 'node_modules')],
|
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
|
|
|
|
2021-05-03 12:35:31 +02:00
|
|
|
new NodePolyfillPlugin(),
|
2022-04-14 11:09:12 +02:00
|
|
|
|
|
|
|
new ForkTsCheckerPlugin({
|
|
|
|
typescript: {
|
|
|
|
configFile: tsConfigFilePath,
|
|
|
|
},
|
|
|
|
}),
|
2022-06-30 16:08:51 +02:00
|
|
|
|
2022-03-12 19:07:54 +07:00
|
|
|
!isProduction && process.env.REACT_REFRESH !== 'false' && new ReactRefreshWebpackPlugin(),
|
2022-06-30 16:08:51 +02:00
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|