-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
116 lines (108 loc) · 2.79 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
require('dotenv').config()
const { spawn } = require('child_process')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const path = require('path')
const webpack = require('webpack')
const publicPath = (resourcePath, context) =>
path.relative(path.dirname(resourcePath), context) + '/'
// 这里写cdn地址,如果静态资源有上传的话
const cdn = '/'
const GPT_API_FRONTEND = process.env.GPT_API_FRONTEND
const GPT_API_BACKEND = process.env.GPT_API_BACKEND
const GPT_TOKEN = process.env.GPT_TOKEN
console.log('GPT_API_BACKEND', GPT_API_BACKEND)
console.log('GPT_TOKEN', GPT_TOKEN)
let serverProcess = null
/**
* @type {webpack.Configuration}
*/
module.exports = {
entry: './src/app.tsx',
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
output: {
filename: '[name].[hash].min.js',
path: path.join(__dirname, 'build'),
publicPath: process.env.NODE_ENV === 'production' ? cdn : '/',
},
devServer: {
onBeforeSetupMiddleware() {
if (!serverProcess) {
console.log('🚀 启动本地 Node.js 服务器...')
serverProcess = spawn('node', ['server.js'], {
stdio: 'inherit', // 让 Node.js 服务器的日志显示在终端
shell: true,
})
serverProcess.on('close', (code) => {
console.log(`Node.js 服务器进程退出,退出码:${code}`)
serverProcess = null
})
}
},
},
module: {
rules: [
// 使用babel编译js、jsx、ts、tsx
{
test: /\.(js|jsx|ts|tsx)$/,
use: ['babel-loader'],
},
// 图片url处理
{
test: /\.(woff|svg|eot|ttf|png)$/,
use: ['url-loader'],
},
// css、less编译
{
test: /\.(css|less)$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: { publicPath },
},
'css-loader',
{
loader: 'less-loader',
options: {
javascriptEnabled: true,
},
},
],
},
],
},
plugins: [
// index.html模板设置
new HtmlWebpackPlugin({
template: path.join(__dirname, 'template.html'),
}),
new webpack.EnvironmentPlugin({
GPT_TOKEN,
GPT_API_FRONTEND,
}),
new MiniCssExtractPlugin({
filename: '[name].[hash].css',
chunkFilename: 'style.[id][hash].css',
}),
// webpack编译进度
new webpack.ProgressPlugin(),
],
watchOptions: {
aggregateTimeout: 1000,
ignored: /node_modules|lib/,
},
// 代码分割
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
name: 'vendors',
test: /[\\/]node_modules[\\/]/,
},
},
},
},
}