-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
159 lines (151 loc) · 3.91 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin');
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const html_title = process.env.HTML_TITLE;
const isProd = process.env.NODE_ENV.indexOf('local') === -1;
const cdn = process.env.CDN || '';
function resolve(dir) {
return path.join(__dirname, dir);
}
let config = {
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
publicPath: cdn || process.env.ASSET_PATH || '',
},
module: {
rules: [
{
test: /\.(scss)$/,
include: [
path.resolve(__dirname, './src/styles/')
],
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
},
{
test: /\.(css|scss)$/,
exclude: [
path.resolve(__dirname, './src/styles/')
],
use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'],
},
// {
// test: /\.less$/,
// exclude: [
// path.resolve(__dirname, './src/styles/')
// ],
// use: ['style-loader', 'css-loader', 'postcss-loader', {
// loader: 'less-loader',
// options: {
// lessOptions: {
// javascriptEnabled: true
// }
// }
// }],
// },
{
test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
type: 'asset/resource',
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
type: 'asset/inline',
},
{
test: /\.js$/,
include: [/(src|demo)/],
use: ['babel-loader'],
},
{
test: /\.tsx?$/,
include: [/(src|demo)/],
use: [{ loader: 'babel-loader' }, { loader: 'ts-loader' }]
},
],
},
plugins: [
new webpack.DefinePlugin({
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
ASSET_PATH: JSON.stringify(process.env.ASSET_PATH),
}),
new AntdDayjsWebpackPlugin(),
// new BundleAnalyzerPlugin()
new MiniCssExtractPlugin({
filename: '[name].css'
})
],
resolve: {
modules: ['node_modules', path.join(__dirname, './node_modules')],
extensions: ['.js', '.tsx', '.ts'],
// alias: {
// '@': resolve('src'),
// },
},
};
if (isProd) {
config = {
...config,
mode: 'production',
entry: {
index: path.resolve(__dirname, './src/index.ts'),
utils: path.resolve(__dirname, './src/utils/index.ts'),
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
publicPath: cdn || process.env.ASSET_PATH || '',
},
externals: {
react: {
commonjs: 'react',
commonjs2: 'react',
amd: 'react',
},
'react-dom': {
commonjs: 'react-dom',
commonjs2: 'react-dom',
amd: 'react-dom',
},
antd: {
commonjs: 'antd',
commonjs2: 'antd',
amd: 'antd',
}
}
};
} else {
// config.target = web 可以解决web里面HMR不工作的情况
// config.target = 'web';
config = {
...config,
entry: {
index: path.resolve(__dirname, './demo/demoEntry.tsx')
},
mode: 'development',
devtool: 'inline-source-map',
plugins: [
...config.plugins,
new HtmlWebpackPlugin({
// favicon: './src/assets/' + assets_dir + '/favicon.ico',
template: './index.html',
hash: true,
inject: true,
title: `${html_title}`
}),
new webpack.HotModuleReplacementPlugin()
],
target: 'web',
devServer: {
historyApiFallback: true,
// contentBase: path.resolve(__dirname, './dist'),
open: true,
compress: true,
port: 8101,
host: '0.0.0.0',
}
};
}
module.exports = config;