2024-08-10 01:58:44 +08:00
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
|
|
const path = require('path');
|
2024-08-29 01:47:57 +08:00
|
|
|
const webpack = require('webpack');
|
2024-08-10 01:58:44 +08:00
|
|
|
const CopyPlugin = require('copy-webpack-plugin');
|
|
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
|
|
|
|
2024-08-13 02:41:38 +08:00
|
|
|
const env = process.env.NODE_ENV || 'development';
|
|
|
|
|
|
|
|
const prodEnv = 'production';
|
|
|
|
const devEnv = 'development';
|
|
|
|
|
|
|
|
const config = {
|
2024-08-10 01:58:44 +08:00
|
|
|
entry: './client/main.tsx',
|
|
|
|
output: {
|
|
|
|
filename: 'bundle.[fullhash].js',
|
|
|
|
path: path.resolve(__dirname, 'dist'),
|
|
|
|
publicPath: '',
|
|
|
|
},
|
2024-08-13 02:41:38 +08:00
|
|
|
cache: {
|
|
|
|
type: 'filesystem',
|
|
|
|
allowCollectingMemory: true,
|
2024-09-17 02:30:10 +08:00
|
|
|
maxAge: 86400000,
|
2024-08-10 01:58:44 +08:00
|
|
|
},
|
|
|
|
devtool: 'source-map',
|
|
|
|
plugins: [
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
template: './client/main.html',
|
|
|
|
}),
|
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
filename: 'styles.css',
|
|
|
|
}),
|
|
|
|
new CopyPlugin({
|
|
|
|
patterns: [
|
|
|
|
{ from: 'public', to: '.' },
|
|
|
|
{ from: 'private', to: 'private' },
|
|
|
|
],
|
|
|
|
}),
|
2024-08-29 01:47:57 +08:00
|
|
|
new webpack.DefinePlugin({
|
|
|
|
'process.env.NODE_ENV': JSON.stringify(env),
|
|
|
|
}),
|
2024-08-10 01:58:44 +08:00
|
|
|
],
|
|
|
|
resolve: {
|
|
|
|
modules: ['node_modules', 'src'],
|
|
|
|
enforceExtension: false,
|
|
|
|
fullySpecified: false,
|
|
|
|
extensions: ['.mjs', '.js', '.jsx', '.tsx', '.ts', '...'],
|
|
|
|
alias: {
|
|
|
|
'/client': path.resolve(__dirname, 'client/'),
|
|
|
|
'/imports': path.resolve(__dirname, '/imports/'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.(js|jsx|ts|tsx)$/,
|
|
|
|
resolve: {
|
|
|
|
fullySpecified: false,
|
|
|
|
enforceExtension: false,
|
|
|
|
},
|
|
|
|
exclude: /node_modules/,
|
|
|
|
use: ['babel-loader'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
use: [
|
|
|
|
'style-loader',
|
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
|
|
|
postcssOptions: {
|
|
|
|
plugins: [
|
|
|
|
[
|
|
|
|
'autoprefixer',
|
|
|
|
{
|
|
|
|
overrideBrowserslist: ['last 2 versions', '>1%'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(png|svg|jpg|gif)$/,
|
|
|
|
exclude: /node_modules/,
|
|
|
|
use: ['file-loader'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
2024-08-13 02:41:38 +08:00
|
|
|
|
|
|
|
if (env === prodEnv) {
|
|
|
|
config.mode = prodEnv;
|
|
|
|
config.optimization = {
|
|
|
|
minimize: true,
|
|
|
|
minimizer: [new TerserPlugin()],
|
|
|
|
};
|
|
|
|
config.performance = {
|
|
|
|
hints: 'warning',
|
|
|
|
maxAssetSize: 6000000,
|
|
|
|
maxEntrypointSize: 6000000,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
config.mode = devEnv;
|
|
|
|
config.devServer = {
|
|
|
|
port: 3000,
|
|
|
|
hot: true,
|
|
|
|
allowedHosts: 'all',
|
|
|
|
client: {
|
|
|
|
overlay: false,
|
|
|
|
webSocketURL: 'auto://0.0.0.0:0/html5client/ws',
|
|
|
|
},
|
2024-08-22 21:44:08 +08:00
|
|
|
setupMiddlewares: (middlewares, devServer) => {
|
|
|
|
if (!devServer) {
|
|
|
|
throw new Error('webpack-dev-server is not defined');
|
|
|
|
}
|
|
|
|
|
|
|
|
devServer.app.use((req, res, next) => {
|
|
|
|
// the server crashes when it receives HEAD requests, so we need to prevent it
|
|
|
|
if (req.method === 'HEAD') {
|
|
|
|
// console.log(`Request received: ${req.method} ${req.url}`);
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.setHeader('Content-Length', '0');
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return middlewares;
|
|
|
|
},
|
2024-08-13 02:41:38 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = config;
|