2018-10-20 20:32:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
|
|
|
const { rules, plugins } = require('./webpack.common');
|
2018-10-30 16:36:51 +08:00
|
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
2019-01-05 23:03:41 +08:00
|
|
|
const CopyPlugin = require('copy-webpack-plugin');
|
|
|
|
const CleanPlugin = require('clean-webpack-plugin');
|
2018-10-20 20:32:02 +08:00
|
|
|
const isDev = process.env.NODE_ENV !== 'production';
|
|
|
|
|
|
|
|
const resolveDir = dir => path.resolve(__dirname, dir);
|
|
|
|
const HTMLPlugin = require('html-webpack-plugin');
|
|
|
|
const html = new HTMLPlugin({
|
|
|
|
title: 'yacd - Yet Another Clash Dashboard',
|
|
|
|
template: 'src/index.template.ejs',
|
|
|
|
inject: false,
|
|
|
|
filename: 'index.html'
|
|
|
|
});
|
|
|
|
|
|
|
|
const svgSpriteRule = {
|
|
|
|
test: /\.svg$/,
|
|
|
|
use: ['svg-sprite-loader']
|
|
|
|
};
|
|
|
|
|
|
|
|
// ---- entry
|
|
|
|
|
|
|
|
const entry = {
|
2018-10-21 15:40:18 +08:00
|
|
|
// app: ['whatwg-fetch', '@babel/polyfill', './src/app.js']
|
|
|
|
app: ['whatwg-fetch', './src/app.js']
|
2018-10-20 20:32:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---- output
|
|
|
|
|
|
|
|
const output = {
|
|
|
|
path: path.resolve(__dirname, 'public'),
|
2018-10-24 00:05:51 +08:00
|
|
|
// use contenthash instead of chunkhash to take advantage of caching
|
|
|
|
filename: isDev ? '[name].bundle.js' : '[name].[contenthash].js',
|
2018-10-20 20:32:02 +08:00
|
|
|
publicPath: ''
|
|
|
|
};
|
|
|
|
|
2018-10-21 15:24:38 +08:00
|
|
|
// const vendor = ['redux', 'react', 'react-dom', 'react-router-dom'];
|
2018-10-20 20:32:02 +08:00
|
|
|
|
|
|
|
// if (!isDev) entry.vendor = vendor; // generate common vendor bundle in prod
|
|
|
|
|
|
|
|
// if (isDev) {
|
|
|
|
// const dllRefPlugin = new webpack.DllReferencePlugin({
|
|
|
|
// context: '.',
|
|
|
|
// manifest: require('./public/vendor-manifest.json')
|
|
|
|
// });
|
|
|
|
// plugins.push(dllRefPlugin);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// since we don't use dll plugin for now - we still get vendor's bundled in a separate bundle
|
|
|
|
// entry.vendor = vendor;
|
|
|
|
// entry.react = react;
|
|
|
|
|
|
|
|
const mode = isDev ? 'development' : 'production';
|
|
|
|
|
|
|
|
const definePlugin = new webpack.DefinePlugin({
|
|
|
|
__DEV__: JSON.stringify(isDev),
|
|
|
|
'process.env': {
|
|
|
|
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
plugins.push(html);
|
|
|
|
plugins.push(definePlugin);
|
2019-01-05 23:03:41 +08:00
|
|
|
plugins.push(new CopyPlugin([{ from: 'assets/*', flatten: true }]));
|
|
|
|
|
|
|
|
if (!isDev) {
|
|
|
|
plugins.push(
|
|
|
|
new CleanPlugin(['**/*'], {
|
|
|
|
root: path.join(__dirname, 'public'),
|
|
|
|
verbose: false,
|
|
|
|
beforeEmit: true
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2018-10-20 20:32:02 +08:00
|
|
|
|
|
|
|
let devtool;
|
|
|
|
if (isDev) {
|
|
|
|
devtool = 'eval-source-map';
|
|
|
|
} else {
|
|
|
|
// devtool = 'source-map';
|
|
|
|
devtool = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
devtool,
|
|
|
|
entry,
|
|
|
|
output,
|
|
|
|
mode,
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
|
|
|
a: resolveDir('src/api'),
|
|
|
|
s: resolveDir('src/svg'),
|
|
|
|
m: resolveDir('src/misc'),
|
|
|
|
d: resolveDir('src/ducks'),
|
|
|
|
c: resolveDir('src/components')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
svgSpriteRule,
|
|
|
|
rules.js,
|
|
|
|
rules.file,
|
|
|
|
rules.css,
|
|
|
|
rules.cssModules,
|
|
|
|
rules.sass,
|
|
|
|
rules.sassCssModules
|
|
|
|
]
|
|
|
|
},
|
|
|
|
optimization: {
|
|
|
|
splitChunks: {
|
|
|
|
chunks: 'all',
|
|
|
|
// see https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693#optimizationruntimechunk
|
|
|
|
cacheGroups: {
|
2018-10-21 15:24:38 +08:00
|
|
|
// corejs: {
|
|
|
|
// test: /[\\/]node_modules[\\/](core-js)[\\/]/,
|
|
|
|
// chunks: 'all'
|
|
|
|
// },
|
2018-10-30 23:37:42 +08:00
|
|
|
// chartjs: {
|
|
|
|
// test: /[\\/]node_modules[\\/]chart\.js[\\/]/,
|
|
|
|
// // name: 'chartjs',
|
|
|
|
// chunks: 'all'
|
|
|
|
// },
|
2018-10-21 15:24:38 +08:00
|
|
|
react: {
|
|
|
|
test: /[\\/]node_modules[\\/](react-dom|react|redux|react-router|react-router-dom|schedule|react-redux|react-modal)[\\/]/,
|
2018-10-20 20:32:02 +08:00
|
|
|
chunks: 'all'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-10-30 16:36:51 +08:00
|
|
|
runtimeChunk: true,
|
|
|
|
minimizer: [
|
|
|
|
// the current uglifyjs-webpack-plugin has problems workin with React Hooks
|
|
|
|
// see also:
|
|
|
|
// https://github.com/webpack-contrib/uglifyjs-webpack-plugin/issues/374
|
|
|
|
new TerserPlugin({
|
|
|
|
cache: true,
|
|
|
|
parallel: true
|
|
|
|
})
|
|
|
|
]
|
2018-10-20 20:32:02 +08:00
|
|
|
},
|
|
|
|
plugins
|
|
|
|
};
|