2018-10-20 20:32:02 +08:00
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
2018-10-30 16:36:51 +08:00
|
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
2019-12-27 22:53:00 +08:00
|
|
|
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
|
|
|
|
const HTMLPlugin = require('html-webpack-plugin');
|
2019-01-05 23:03:41 +08:00
|
|
|
const CopyPlugin = require('copy-webpack-plugin');
|
2019-05-27 23:03:21 +08:00
|
|
|
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
|
2020-04-26 17:35:03 +08:00
|
|
|
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
|
|
|
const ForkTsCheckerNotifierWebpackPlugin = require('fork-ts-checker-notifier-webpack-plugin');
|
2020-10-11 19:31:32 +08:00
|
|
|
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
|
2019-04-12 23:34:50 +08:00
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
2020-09-03 21:11:44 +08:00
|
|
|
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
|
2020-11-06 22:36:54 +08:00
|
|
|
const { InjectManifest } = require('workbox-webpack-plugin');
|
2019-12-20 23:02:10 +08:00
|
|
|
|
2019-08-29 22:51:22 +08:00
|
|
|
const pkg = require('./package.json');
|
2019-04-12 23:34:50 +08:00
|
|
|
|
|
|
|
process.env.BABEL_ENV = process.env.NODE_ENV;
|
2018-10-20 20:32:02 +08:00
|
|
|
const isDev = process.env.NODE_ENV !== 'production';
|
|
|
|
|
2020-11-06 22:36:54 +08:00
|
|
|
const publicPath = '';
|
|
|
|
|
2018-10-20 20:32:02 +08:00
|
|
|
const html = new HTMLPlugin({
|
|
|
|
title: 'yacd - Yet Another Clash Dashboard',
|
|
|
|
template: 'src/index.template.ejs',
|
2020-04-16 15:18:28 +08:00
|
|
|
scriptLoading: 'defer',
|
|
|
|
filename: 'index.html',
|
2018-10-20 20:32:02 +08:00
|
|
|
});
|
|
|
|
|
2019-11-26 23:25:06 +08:00
|
|
|
const postcssPlugins = () =>
|
|
|
|
[
|
|
|
|
require('postcss-import')(),
|
2019-12-27 16:29:10 +08:00
|
|
|
require('postcss-simple-vars')(),
|
2019-11-26 23:25:06 +08:00
|
|
|
require('postcss-custom-media')({
|
|
|
|
importFrom: [
|
|
|
|
{
|
|
|
|
customMedia: {
|
|
|
|
'--breakpoint-not-small': 'screen and (min-width: 30em)',
|
|
|
|
'--breakpoint-medium':
|
|
|
|
'screen and (min-width: 30em) and (max-width: 60em)',
|
2020-04-16 15:18:28 +08:00
|
|
|
'--breakpoint-large': 'screen and (min-width: 60em)',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2019-11-26 23:25:06 +08:00
|
|
|
}),
|
|
|
|
require('postcss-nested')(),
|
|
|
|
require('autoprefixer')(),
|
|
|
|
require('postcss-extend-rule')(),
|
|
|
|
].filter(Boolean);
|
2019-04-12 23:34:50 +08:00
|
|
|
|
2020-09-05 00:02:53 +08:00
|
|
|
const postcssOptions = { plugins: postcssPlugins() };
|
|
|
|
|
2019-04-12 23:34:50 +08:00
|
|
|
const cssExtractPlugin = new MiniCssExtractPlugin({
|
2020-04-16 15:18:28 +08:00
|
|
|
filename: isDev ? '[name].css' : '[name].[contenthash].css',
|
2019-04-12 23:34:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const bundleAnalyzerPlugin = new BundleAnalyzerPlugin({
|
|
|
|
analyzerMode: 'static',
|
|
|
|
reportFilename: 'report.html',
|
2020-04-16 15:18:28 +08:00
|
|
|
openAnalyzer: false,
|
2019-04-12 23:34:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const plugins = [
|
|
|
|
html,
|
2020-11-06 22:36:54 +08:00
|
|
|
new webpack.DefinePlugin({
|
|
|
|
__DEV__: JSON.stringify(isDev),
|
|
|
|
__VERSION__: JSON.stringify(pkg.version),
|
|
|
|
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
|
|
|
|
'process.env.PUBLIC_URL': JSON.stringify(publicPath),
|
|
|
|
}),
|
2020-06-28 11:21:35 +08:00
|
|
|
new ForkTsCheckerWebpackPlugin(),
|
2020-04-26 17:35:03 +08:00
|
|
|
new ForkTsCheckerNotifierWebpackPlugin({
|
|
|
|
title: 'TypeScript',
|
|
|
|
excludeWarnings: false,
|
|
|
|
}),
|
2021-01-04 21:44:12 +08:00
|
|
|
new CopyPlugin({ patterns: [{ from: 'assets' }] }),
|
2019-05-27 23:03:21 +08:00
|
|
|
new CleanWebpackPlugin(),
|
2019-06-12 19:49:11 +08:00
|
|
|
// chart.js requires moment
|
|
|
|
// and we don't need locale stuff in moment
|
2020-10-11 19:31:32 +08:00
|
|
|
new webpack.IgnorePlugin({ resourceRegExp: /(^\.\/locale$)|(moment$)/ }),
|
2020-11-06 22:36:54 +08:00
|
|
|
isDev
|
|
|
|
? false
|
|
|
|
: new InjectManifest({
|
|
|
|
swSrc: './src/sw.ts',
|
|
|
|
dontCacheBustURLsMatching: /\.[0-9a-f]{20}\./,
|
|
|
|
exclude: [
|
|
|
|
/\.map$/,
|
|
|
|
/asset-manifest\.json$/,
|
|
|
|
/LICENSE/,
|
|
|
|
'CNAME',
|
|
|
|
'_headers',
|
|
|
|
],
|
|
|
|
}),
|
2019-12-20 23:02:10 +08:00
|
|
|
// https://github.com/pmmmwh/react-refresh-webpack-plugin
|
2020-09-03 21:11:44 +08:00
|
|
|
isDev
|
|
|
|
? new ReactRefreshWebpackPlugin({
|
|
|
|
overlay: { sockIntegration: 'whm' },
|
|
|
|
})
|
|
|
|
: false,
|
2019-12-27 22:53:00 +08:00
|
|
|
// isDev ? false : new webpack.HashedModuleIdsPlugin(),
|
2019-04-12 23:34:50 +08:00
|
|
|
isDev ? false : cssExtractPlugin,
|
2020-04-16 15:18:28 +08:00
|
|
|
isDev ? false : bundleAnalyzerPlugin,
|
2019-04-12 23:34:50 +08:00
|
|
|
].filter(Boolean);
|
|
|
|
|
2018-10-20 20:32:02 +08:00
|
|
|
module.exports = {
|
2020-08-08 16:14:29 +08:00
|
|
|
stats: {
|
|
|
|
children: false,
|
|
|
|
},
|
2019-11-19 23:53:25 +08:00
|
|
|
// https://webpack.js.org/configuration/devtool/
|
2020-09-05 00:02:53 +08:00
|
|
|
devtool: isDev ? 'eval-source-map' : 'source-map',
|
2019-11-19 23:53:25 +08:00
|
|
|
entry: {
|
2020-10-31 18:18:04 +08:00
|
|
|
app: { import: ['./src/app.tsx'], dependOn: 'libs' },
|
2020-10-11 19:31:32 +08:00
|
|
|
libs: { import: ['react-query', 'react-modal'], dependOn: 'vendor' },
|
|
|
|
vendor: { import: ['react', 'react-dom'], dependOn: 'corejs' },
|
|
|
|
corejs: { import: 'core-js' },
|
2019-11-19 23:53:25 +08:00
|
|
|
},
|
2020-04-26 17:35:03 +08:00
|
|
|
context: __dirname,
|
2019-11-19 23:53:25 +08:00
|
|
|
output: {
|
|
|
|
path: path.resolve(__dirname, 'public'),
|
2020-01-04 14:34:44 +08:00
|
|
|
filename: isDev ? '[name].js' : '[name].[contenthash].js',
|
2020-11-06 22:36:54 +08:00
|
|
|
publicPath,
|
2019-11-19 23:53:25 +08:00
|
|
|
},
|
2019-11-26 23:25:06 +08:00
|
|
|
mode: isDev ? 'development' : 'production',
|
2020-04-26 17:35:03 +08:00
|
|
|
resolve: {
|
|
|
|
extensions: ['.ts', '.tsx', '.js'],
|
2020-06-28 11:21:35 +08:00
|
|
|
alias: {
|
|
|
|
src: path.resolve(__dirname, 'src'),
|
|
|
|
},
|
2020-04-26 17:35:03 +08:00
|
|
|
},
|
2018-10-20 20:32:02 +08:00
|
|
|
module: {
|
|
|
|
rules: [
|
2020-10-11 19:31:32 +08:00
|
|
|
// to work around "Module not found" issue for babel runtime imports
|
|
|
|
// https://github.com/webpack/webpack/issues/11467
|
|
|
|
{
|
|
|
|
test: /\.m?js/,
|
2020-12-06 14:57:59 +08:00
|
|
|
resolve: { fullySpecified: false },
|
2020-10-11 19:31:32 +08:00
|
|
|
},
|
2020-04-26 17:35:03 +08:00
|
|
|
{
|
2020-06-03 21:44:00 +08:00
|
|
|
test: /\.[tj]sx?$/,
|
2019-04-12 23:34:50 +08:00
|
|
|
exclude: /node_modules/,
|
2020-04-16 15:18:28 +08:00
|
|
|
use: { loader: 'babel-loader', options: { cacheDirectory: true } },
|
2019-04-12 23:34:50 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(ttf|eot|woff|woff2)(\?.+)?$/,
|
2020-04-16 15:18:28 +08:00
|
|
|
use: [{ loader: 'file-loader', options: { name: '[name].[ext]' } }],
|
2019-04-12 23:34:50 +08:00
|
|
|
},
|
2019-11-26 23:25:06 +08:00
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
exclude: /\.module\.css$/,
|
|
|
|
use: [
|
2020-01-04 14:34:44 +08:00
|
|
|
isDev ? { loader: 'style-loader' } : MiniCssExtractPlugin.loader,
|
2019-11-26 23:25:06 +08:00
|
|
|
{ loader: 'css-loader' },
|
2020-09-05 00:02:53 +08:00
|
|
|
{ loader: 'postcss-loader', options: { postcssOptions } },
|
2020-08-06 16:26:01 +08:00
|
|
|
],
|
2019-11-26 23:25:06 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.module\.css$/,
|
|
|
|
use: [
|
2020-01-04 14:34:44 +08:00
|
|
|
isDev ? { loader: 'style-loader' } : MiniCssExtractPlugin.loader,
|
2019-11-26 23:25:06 +08:00
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
|
|
|
modules: {
|
|
|
|
localIdentName: isDev
|
|
|
|
? '[path]_[name]_[local]_[hash:base64:5]'
|
2020-04-16 15:18:28 +08:00
|
|
|
: '[hash:base64:10]',
|
|
|
|
},
|
|
|
|
},
|
2019-11-26 23:25:06 +08:00
|
|
|
},
|
2020-09-05 00:02:53 +08:00
|
|
|
{ loader: 'postcss-loader', options: { postcssOptions } },
|
2020-08-06 16:26:01 +08:00
|
|
|
],
|
2020-04-16 15:18:28 +08:00
|
|
|
},
|
|
|
|
],
|
2018-10-20 20:32:02 +08:00
|
|
|
},
|
|
|
|
optimization: {
|
2020-10-11 19:31:32 +08:00
|
|
|
splitChunks: { chunks: 'all' },
|
|
|
|
minimizer: [new TerserPlugin(), new CssMinimizerPlugin()],
|
2018-10-20 20:32:02 +08:00
|
|
|
},
|
2020-04-16 15:18:28 +08:00
|
|
|
plugins,
|
2020-10-11 19:31:32 +08:00
|
|
|
bail: true,
|
|
|
|
cache: {
|
|
|
|
type: 'filesystem',
|
|
|
|
buildDependencies: {
|
|
|
|
config: [__filename],
|
|
|
|
},
|
|
|
|
},
|
2018-10-20 20:32:02 +08:00
|
|
|
};
|