-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.dev.js
More file actions
128 lines (126 loc) · 3.39 KB
/
webpack.dev.js
File metadata and controls
128 lines (126 loc) · 3.39 KB
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
const path = require("path");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); // 抽离css
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin"); // build前清理旧build文件
//编译代码的基础配置
module.exports = {
mode: "development",
entry: {
index: path.resolve(__dirname, `./src/index.js`),
},
output: {
path: path.join(__dirname, `./dist/`),
filename: "script/[name].[chunkhash:8].v1.js",
chunkFilename: "script/[name].[chunkhash:8].chunk.v1.js",
publicPath: "/",
globalObject: "this",
},
externals: {
},
//调试工具,开发环境开启eval-source-map,生产构建时不开启
devtool: 'eval-source-map',
plugins: [
new HtmlWebpackPlugin({
// favicon: "public/favicon.ico",
filename: `index.html`,
template: 'src/index.html',
chunks: 'all',
//防止各site项目一样时,不生成html文件
cache: false,
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
inject: true,
hash: true,
templateParameters: {
publicPath: `/`,
},
}),
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: "styles/[name].[fullhash:5].css",
chunkFilename: "styles/[name].[fullhash:5].css",
ignoreOrder: true
}),
],
resolve: {
//设置模块导入规则,import/require时会直接在这些目录找文件
modules: [path.resolve("/src"), "node_modules"],
//import导入时省略后缀
extensions: [".ts", ".tsx", ".js", ".jsx", ".react.js", ".css", ".json"],
},
devServer: {
proxy: {
'/api': 'http://localhost:3000'
},
static: path.join(__dirname, 'dist'),
port: 3001,
compress: true,
historyApiFallback: true,
hot: true,
https: false,
},
module: {
//设置所以编译文件的loader
rules: [
{
test: /\.css/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
{
loader: "less-loader",
options: {
lessOptions: {
modifyVars: {
'primary-color': 'sandybrown',
'link-color': 'rebeccapurple',
'border-radius-base': '2px',
},
javascriptEnabled: true
}
}
}
],
},
{
test: /\.[jt]sx?$/,
use: ["babel-loader?cacheDirectory"],
exclude: /node_modules/,
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: [
{
loader: "url-loader",
options: {
name: "imgs/[name].[ext]",
limit: 2048,
publicPath: "/",
fallback: "file-loader",
},
},
],
},
],
},
performance: {
//性能设置,文件打包过大时,不报错和警告,只做提示
hints: false,
},
};