-
Notifications
You must be signed in to change notification settings - Fork 10
/
webpack.config.js
232 lines (231 loc) · 6.84 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin"); //生成html并注入
const CopyWebpackPlugin = require("copy-webpack-plugin"); //拷贝资源文件
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const AutoImport = require("unplugin-auto-import/webpack");
const config = {
resolve: {
extensions: ["*", ".js", ".jsx", ".json"],
alias: {
components: path.resolve(__dirname, "src/components/"),
constants: path.resolve(__dirname, "src/store/constants/"),
actions: path.resolve(__dirname, "src/store/actions/"),
reducers: path.resolve(__dirname, "src/store/reducers/"),
router: path.resolve(__dirname, "src/router/"),
style: path.resolve(__dirname, "src/style/"),
store: path.resolve(__dirname, "src/store/"),
utils: path.resolve(__dirname, "src/utils/"),
assets: path.resolve(__dirname, "src/public/assets/")
}
},
devtool: "eval", // 调试工具
mode: "development",
entry: {
app: [
path.resolve(__dirname, "src/index.jsx") // 定位客户端目标
]
},
target: "web", // 目标是web 服务器
output: {
path: path.resolve(__dirname, "dist"), // 输出编译文件目录
publicPath: "/", //根目录
filename: "js/[name]-[hash].js?v=[chunkhash]",
chunkFilename: "js/[name]-[hash].js?v=[chunkhash]",
},
devServer: {
client: { overlay: false },
static: {
directory: path.join(__dirname, "public/"),
staticOptions: {},
// Don't be confused with `devMiddleware.publicPath`, it is `publicPath` for static directory
// Can be:
// publicPath: ['/static-public-path-one/', '/static-public-path-two/'],
publicPath: "/",
// Can be:
// serveIndex: {} (options for the `serveIndex` option you can find https://github.com/expressjs/serve-index)
serveIndex: true,
// Can be:
// watch: {} (options for the `watch` option you can find https://github.com/paulmillr/chokidar)
watch: true,
},
port: 3005,
historyApiFallback: true,
// proxy: [{
// 'context' :['/api'],
// 'target': 'https://test.zhiyeinfo.com/api',
// 'changeOrigin': true
// }],
// hotOnly: true,
hot: true,
open: true,
},
cache: {
type: "filesystem",
allowCollectingMemory: true,
},
plugins: [
AutoImport.default({
imports: ["react","react-router-dom"],
}),
new ReactRefreshWebpackPlugin({overlay: false}),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify("development"), // Tells React to build in either dev or prod modes. https://facebook.github.io/react/downloads.html (See bottom)
"process.env.BUILD_TYPE": JSON.stringify("webpack"),
__DEV__: true
}),
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "src/public/assets"),
to: path.resolve(__dirname, "dist/assets"),
// ignore: ['.*']
}
]
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new HtmlWebpackPlugin({ // Create HTML file that includes references to bundled CSS and JS.
template: "src/index.html",
filename: "index.html",
favicon: "src/favicon.ico",
minify: {
removeComments: true,
collapseWhitespace: true
},
inject: true
})
].filter(Boolean),
module: {
// 编译模式
rules: [
{
test: /\.(jsx|js)?$/,
exclude: /node_modules/,
use: [{
loader: "babel-loader",
options: {
plugins: [require.resolve("react-refresh/babel")].filter(Boolean),
},
}]
},
{
test: /\.eot(\?v=\d+.\d+.\d+)?$/,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
},
{
test: /\.[ot]tf(\?v=\d+.\d+.\d+)?$/,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// loader: 'url-loader',
// options: {
// limit: 10000,
// mimetype: 'application/octet-stream'
// }
// }
// ]
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
type: "asset/inline",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// loader: 'url-loader',
// options: {
// limit: 10000,
// mimetype: 'image/svg+xml'
// }
// }
// ]
},
{
test: /\.(jpe?g|png|gif|ico)$/i,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// loader: 'file-loader',
// options: {
// name: '[name].[ext]'
// }
// }
// ]
},
{
test: /\.css|\.scss$/,
// exclude: /node_modules/, //排除这个文件夹
use: [
"style-loader",
{
loader: "css-loader",
options: {
// importLoaders: 2,
// modules: true,
// // namedExport: true, // this is invalid Options ,I find it
// camelCase: true,
// localIdentName: '[path][name]__[local]--[hash:base64:5]',
sourceMap: true,
url: false
}
}, {
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
["autoprefixer",{/*options*/}],
require("postcss-pxtorem")({
rootValue: 16,
unitPrecision: 5,
propList: ["*"],
selectorBlackList: [],
replace: true,
mediaQuery: false,
minPixelValue: 0
})
],
sourceMap: false
}
}
}, {
loader: "sass-loader",
options: {
sassOptions: {
webpackImporter: false,
indentWidth: 4,
includePaths: [path.resolve(__dirname, "src", "scss"),"node_modules"],
},
}
// loader: 'less-loader',
// options: {
// lessOptions: {
// paths: [path.resolve(__dirname, 'src'), path.resolve(__dirname, 'node_modules')],
// javascriptEnabled: true,
// sourceMap: false
// }
// }
}
]
}
]
}
};
module.exports = config;