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');
|
|
|
|
const isWsl = require('is-wsl');
|
2019-04-16 12:54:16 +02:00
|
|
|
const alias = require('./webpack.alias.js');
|
2019-04-11 19:07:00 +02:00
|
|
|
|
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,
|
|
|
|
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-04-25 12:34:55 +02:00
|
|
|
}),
|
2019-04-29 15:48:16 +02:00
|
|
|
]
|
|
|
|
: [
|
|
|
|
new DuplicatePckgChecker({
|
|
|
|
verbose: true,
|
|
|
|
}),
|
|
|
|
new FriendlyErrorsWebpackPlugin(),
|
2019-04-25 12:34:55 +02:00
|
|
|
];
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
const scssLoader = isProduction
|
|
|
|
? [
|
2019-04-25 12:34:55 +02:00
|
|
|
{
|
|
|
|
loader: MiniCssExtractPlugin.loader,
|
|
|
|
options: {
|
|
|
|
fallback: require.resolve('style-loader'),
|
2019-04-29 19:04:23 +02:00
|
|
|
publicPath: options.publicPath,
|
2019-04-25 12:34:55 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
]
|
|
|
|
: [{ loader: 'style-loader', options: {} }];
|
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
|
|
|
|
filename: isProduction ? '[name].js' : '[name].[chunkhash].js',
|
|
|
|
chunkFilename: isProduction
|
|
|
|
? '[name].chunk.js'
|
|
|
|
: '[name].[chunkhash].chunk.js',
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
optimization: {
|
|
|
|
minimize: isProduction,
|
|
|
|
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,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
// splitChunks: {
|
|
|
|
// chunks: 'all',
|
|
|
|
// name: 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'),
|
|
|
|
require.resolve(
|
|
|
|
'@babel/plugin-proposal-async-generator-functions'
|
|
|
|
),
|
|
|
|
[
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
include: /node_modules/,
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: require.resolve('style-loader'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: require.resolve('css-loader'),
|
|
|
|
options: {
|
|
|
|
sourceMap: false,
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
{
|
|
|
|
loader: require.resolve('postcss-loader'),
|
|
|
|
options: {
|
|
|
|
config: {
|
|
|
|
path: path.resolve(__dirname, 'postcss.config.js'),
|
|
|
|
},
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
use: scssLoader.concat([
|
|
|
|
{
|
|
|
|
loader: require.resolve('css-loader'),
|
|
|
|
options: {
|
|
|
|
localIdentName: '[local]__[path][name]__[hash:base64:5]',
|
|
|
|
modules: true,
|
|
|
|
importLoaders: 1,
|
|
|
|
sourceMap: false,
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: require.resolve('postcss-loader'),
|
|
|
|
options: {
|
|
|
|
config: {
|
|
|
|
path: path.resolve(__dirname, 'postcss.config.js'),
|
|
|
|
},
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
2019-04-29 15:48:16 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'sass-loader',
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(svg|eot|otf|ttf|woff|woff2)$/,
|
|
|
|
use: 'file-loader',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(jpg|png|gif)$/,
|
|
|
|
loaders: [
|
|
|
|
require.resolve('file-loader'),
|
|
|
|
{
|
|
|
|
loader: require.resolve('image-webpack-loader'),
|
|
|
|
query: {
|
|
|
|
mozjpeg: {
|
|
|
|
progressive: true,
|
|
|
|
},
|
|
|
|
gifsicle: {
|
|
|
|
interlaced: false,
|
|
|
|
},
|
|
|
|
optipng: {
|
|
|
|
optimizationLevel: 4,
|
|
|
|
},
|
|
|
|
pngquant: {
|
|
|
|
quality: '65-90',
|
|
|
|
speed: 4,
|
|
|
|
},
|
2019-04-11 19:07:00 +02:00
|
|
|
},
|
|
|
|
},
|
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'),
|
|
|
|
favicon: path.resolve(__dirname, 'admin/src/favicon.ico'),
|
|
|
|
}),
|
|
|
|
new webpack.DefinePlugin({
|
2019-04-29 19:04:23 +02:00
|
|
|
'process.env.NODE_ENV': JSON.stringify(
|
|
|
|
isProduction ? 'production' : 'development'
|
|
|
|
),
|
|
|
|
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
|
|
|
}),
|
2019-04-11 19:07:00 +02:00
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
...webpackPlugins,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
};
|