-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
95 lines (91 loc) · 2.57 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* eslint-disable @typescript-eslint/no-var-requires */
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const distPath = `${__dirname}/dist`;
module.exports = (env) => {
const isProd = env.production;
const isDev = env.development;
const cssLoaders = (isLessLoader) =>
[
isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: isLessLoader ? 2 : 1,
sourceMap: isProd,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: isProd,
},
},
isLessLoader && 'less-loader',
].filter(Boolean);
return {
mode: isProd ? 'production' : 'development',
devtool: 'source-map',
entry: ['./src/index.tsx'],
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
modules: ['node_modules', 'src'],
fallback: {
path: require.resolve('path-browserify'),
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
os: require.resolve('os-browserify/browser'),
buffer: require.resolve('buffer/'),
fs: false,
child_process: false,
},
},
output: {
path: distPath,
pathinfo: isDev,
filename: isDev ? '[name].js' : '[name]-[contenthash].js',
chunkFilename: isDev ? '[name].chunk.js' : '[name]-[contenthash].js',
publicPath: '/',
clean: true,
},
devServer: {
port: 8000,
historyApiFallback: true,
},
optimization: {
minimize: isProd,
minimizer: [new TerserPlugin(), new CssMinimizerPlugin()],
},
plugins: [
new HtmlWebpackPlugin({ template: 'src/index.html' }),
isDev && new ReactRefreshWebpackPlugin({ overlay: false }),
isProd &&
new MiniCssExtractPlugin({
filename: '[name]-[contenthash].css',
}),
].filter(Boolean),
module: {
rules: [
{
test: /\.[jt]sx?/,
exclude: /node_modules/,
loader: 'ts-loader',
options: {
transpileOnly: isProd,
},
},
{
test: /\.css$/,
use: cssLoaders(false),
},
{
test: /\.less$/,
use: cssLoaders(true),
},
],
},
};
};