-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
98 lines (94 loc) · 2.41 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
96
97
98
const path = require('path');
const { version } = require('./package.json');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CopyPlugin = require('copy-webpack-plugin');
const ENV = process.env.NODE_ENV || 'development';
const isDev = ENV !== 'production';
const commonPlugins = [
// new BundleAnalyzerPlugin(),
new CopyPlugin({
patterns: [
{ from: path.resolve(__dirname, 'typings/react-umd-demo-component.d.ts'), to: path.resolve(__dirname, 'dist') }
]
})
];
module.exports = {
mode: isDev ? 'development' : 'production',
devtool: isDev && 'cheap-module-source-map',
context: path.resolve(__dirname, 'src'),
entry: './index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: isDev ? '/dist' : `//cdn.jsdelivr.net/npm/react-umd-demo@${version}/dist/`,
filename: 'react-umd-demo.js',
library: {
name: 'ReactUmdDemo',
type: 'umd',
export: 'default'
}
},
resolve: {
extensions: ['.tsx', '.ts', '.jsx', '.js', '.json'],
// 使用 preact 代替
alias: {
react: 'preact/compat',
'react-dom': 'preact/compat'
}
},
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.less$/,
use: [
isDev ? 'style-loader' : MiniCssExtractPlugin.loader, // 开发模式用 style-loader 才支持热更
'css-loader',
'postcss-loader',
{
loader: 'less-loader',
options: {
lessOptions: {
javascriptEnabled: true
}
}
}
]
},
{
test: /\.(svg|woff2?|ttf|eot|jpe?g|png|gif)(\?.*)?$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 1024 * 10
}
}
]
}
]
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false
})
]
},
plugins: isDev
? [...commonPlugins]
: [new MiniCssExtractPlugin({ filename: 'react-umd-demo.css' }), ...commonPlugins],
devServer: {
port: 8002,
host: '0.0.0.0',
static: {
directory: path.join(__dirname, './dev')
}
}
};