2019-04-11 19:07:00 +02:00
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
|
|
|
|
|
|
|
// Webpack plugins
|
|
|
|
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
|
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
|
|
const DuplicatePckgChecker = require('duplicate-package-checker-webpack-plugin');
|
2019-04-29 15:48:16 +02:00
|
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
2019-05-13 16:01:16 +02:00
|
|
|
const WebpackBar = require('webpackbar');
|
2019-04-29 15:48:16 +02:00
|
|
|
const isWsl = require('is-wsl');
|
2019-04-16 12:54:16 +02:00
|
|
|
const alias = require('./webpack.alias.js');
|
2020-05-25 14:04:09 +02:00
|
|
|
const IS_EE = require('./is_ee_env');
|
2019-04-29 15:48:16 +02:00
|
|
|
// TODO: parametrize
|
2019-04-11 19:07:00 +02:00
|
|
|
const URLs = {
|
|
|
|
mode: 'host',
|
|
|
|
};
|
|
|
|
|
2019-04-29 19:04:23 +02:00
|
|
|
module.exports = ({
|
|
|
|
entry,
|
|
|
|
dest,
|
|
|
|
env,
|
2019-10-10 07:08:04 +02:00
|
|
|
optimize,
|
2019-04-29 19:04:23 +02:00
|
|
|
options = {
|
|
|
|
backend: 'http://localhost:1337',
|
|
|
|
publicPath: '/admin/',
|
|
|
|
},
|
|
|
|
}) => {
|
2019-04-29 15:48:16 +02:00
|
|
|
const isProduction = env === 'production';
|
|
|
|
const webpackPlugins = isProduction
|
2019-04-25 12:34:55 +02:00
|
|
|
? [
|
|
|
|
new webpack.IgnorePlugin({
|
|
|
|
resourceRegExp: /^\.\/locale$/,
|
|
|
|
contextRegExp: /moment$/,
|
|
|
|
}),
|
|
|
|
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
|
|
|
]
|
|
|
|
: [
|
|
|
|
new DuplicatePckgChecker({
|
|
|
|
verbose: true,
|
|
|
|
}),
|
2019-09-24 14:10:57 +02:00
|
|
|
new FriendlyErrorsWebpackPlugin({
|
|
|
|
clearConsole: false,
|
|
|
|
}),
|
2019-04-25 12:34:55 +02:00
|
|
|
];
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
return {
|
|
|
|
mode: isProduction ? 'production' : 'development',
|
|
|
|
bail: isProduction ? true : false,
|
|
|
|
devtool: isProduction ? false : 'cheap-module-source-map',
|
|
|
|
entry,
|
|
|
|
output: {
|
|
|
|
path: dest,
|
2019-04-29 19:04:23 +02:00
|
|
|
publicPath: options.publicPath,
|
2019-04-29 15:48:16 +02:00
|
|
|
// Utilize long-term caching by adding content hashes (not compilation hashes)
|
|
|
|
// to compiled assets for production
|
2019-05-20 17:13:14 +02:00
|
|
|
filename: isProduction ? '[name].[contenthash:8].js' : '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: [
|
|
|
|
// Copied from react-scripts
|
|
|
|
new TerserPlugin({
|
|
|
|
terserOptions: {
|
|
|
|
parse: {
|
|
|
|
ecma: 8,
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
compress: {
|
|
|
|
ecma: 5,
|
|
|
|
warnings: false,
|
|
|
|
comparisons: false,
|
|
|
|
inline: 2,
|
|
|
|
},
|
|
|
|
mangle: {
|
|
|
|
safari10: true,
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
ecma: 5,
|
|
|
|
comments: false,
|
|
|
|
ascii_only: true,
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
parallel: !isWsl,
|
|
|
|
// Enable file caching
|
|
|
|
cache: true,
|
|
|
|
sourceMap: false,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
runtimeChunk: true,
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.m?js$/,
|
|
|
|
exclude: /node_modules/,
|
|
|
|
use: {
|
|
|
|
loader: require.resolve('babel-loader'),
|
2019-04-11 19:07:00 +02:00
|
|
|
options: {
|
2019-04-29 15:48:16 +02:00
|
|
|
cacheDirectory: true,
|
|
|
|
cacheCompression: isProduction,
|
|
|
|
compact: isProduction,
|
|
|
|
presets: [
|
|
|
|
require.resolve('@babel/preset-env'),
|
|
|
|
require.resolve('@babel/preset-react'),
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
require.resolve('@babel/plugin-proposal-class-properties'),
|
|
|
|
require.resolve('@babel/plugin-syntax-dynamic-import'),
|
2019-10-31 11:11:59 +01:00
|
|
|
require.resolve('@babel/plugin-transform-modules-commonjs'),
|
2020-05-25 14:04:09 +02:00
|
|
|
require.resolve('@babel/plugin-proposal-async-generator-functions'),
|
2019-04-29 15:48:16 +02:00
|
|
|
[
|
|
|
|
require.resolve('@babel/plugin-transform-runtime'),
|
|
|
|
{
|
|
|
|
helpers: true,
|
|
|
|
regenerator: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
2019-10-10 06:24:19 +02:00
|
|
|
// Copied from react-boilerplate https://github.com/react-boilerplate/react-boilerplate
|
2019-04-29 15:48:16 +02:00
|
|
|
{
|
2019-10-10 06:24:19 +02:00
|
|
|
// Preprocess our own .css files
|
|
|
|
// This is the place to add your own loaders (e.g. sass/less etc.)
|
|
|
|
// for a list of loaders, see https://webpack.js.org/loaders/#styling
|
2019-04-29 15:48:16 +02:00
|
|
|
test: /\.css$/,
|
2019-10-10 06:24:19 +02:00
|
|
|
exclude: /node_modules/,
|
|
|
|
use: ['style-loader', 'css-loader'],
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
2019-10-10 06:24:19 +02:00
|
|
|
// Preprocess 3rd party .css files located in node_modules
|
|
|
|
test: /\.css$/,
|
|
|
|
include: /node_modules/,
|
|
|
|
use: ['style-loader', 'css-loader'],
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(svg|eot|otf|ttf|woff|woff2)$/,
|
|
|
|
use: 'file-loader',
|
|
|
|
},
|
|
|
|
{
|
2019-12-11 11:46:08 +01:00
|
|
|
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.ico$/],
|
|
|
|
loader: require.resolve('url-loader'),
|
|
|
|
options: {
|
|
|
|
limit: 1000,
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.html$/,
|
|
|
|
include: [path.join(__dirname, 'src')],
|
|
|
|
use: require.resolve('html-loader'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(mp4|webm)$/,
|
|
|
|
loader: require.resolve('url-loader'),
|
|
|
|
options: {
|
|
|
|
limit: 10000,
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias,
|
|
|
|
symlinks: false,
|
|
|
|
extensions: ['.js', '.jsx', '.react.js'],
|
|
|
|
mainFields: ['browser', 'jsnext:main', 'main'],
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
inject: true,
|
|
|
|
template: path.resolve(__dirname, 'index.html'),
|
2019-08-23 11:11:40 +02:00
|
|
|
// favicon: path.resolve(__dirname, 'admin/src/favicon.ico'),
|
2019-04-29 15:48:16 +02:00
|
|
|
}),
|
|
|
|
new webpack.DefinePlugin({
|
2020-05-25 14:04:09 +02:00
|
|
|
'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development'),
|
2019-04-29 19:04:23 +02:00
|
|
|
NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development'),
|
|
|
|
REMOTE_URL: JSON.stringify(options.publicPath),
|
|
|
|
BACKEND_URL: JSON.stringify(options.backend),
|
2019-04-29 15:48:16 +02:00
|
|
|
MODE: JSON.stringify(URLs.mode), // Allow us to define the public path for the plugins assets.
|
2019-04-29 19:04:23 +02:00
|
|
|
PUBLIC_PATH: JSON.stringify(options.publicPath),
|
2019-04-29 15:48:16 +02:00
|
|
|
}),
|
2020-05-25 14:04:09 +02:00
|
|
|
new webpack.NormalModuleReplacementPlugin(/ee_else_ce(\.*)/, function(resource) {
|
|
|
|
// We might need to improve this if we want to make it work with components
|
|
|
|
const containerPathName = resource.context.split('/containers/');
|
2020-05-25 16:56:39 +02:00
|
|
|
const componentPathName = resource.context.split('/components/');
|
|
|
|
const wantedPath =
|
|
|
|
containerPathName.length === 1 ? componentPathName[0] : containerPathName[0];
|
|
|
|
console.log(containerPathName);
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2020-05-25 14:04:09 +02:00
|
|
|
if (IS_EE) {
|
|
|
|
resource.request = resource.request.replace(
|
|
|
|
/ee_else_ce/,
|
2020-05-25 16:56:39 +02:00
|
|
|
path.join(wantedPath[0], '..', 'ee')
|
2020-05-25 14:04:09 +02:00
|
|
|
);
|
|
|
|
} else {
|
2020-05-25 16:56:39 +02:00
|
|
|
resource.request = resource.request.replace(/ee_else_ce/, path.join(wantedPath[0]));
|
2020-05-25 14:04:09 +02:00
|
|
|
}
|
|
|
|
}),
|
2019-04-29 15:48:16 +02:00
|
|
|
...webpackPlugins,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
};
|