-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
132 lines (125 loc) · 4.12 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
/* eslint-env node */
/**************************
* @file: webpack配置
* @author: leinov
* @date: 2018-10-08
* @update: 2018-11-04 优化html文件
* 1.修改htmlConfig.js
* 2.在页面文件夹下添加pageinfo.json
***************************/
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");//css分离打包
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");//js压缩
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin"); //css压缩
const createHtml = require("./build/create-html");// html配置
const config = require("./config")
const getEntry = require("./build/get-entry");
const entry = getEntry(config.entries);
const htmlArr = createHtml(entry.details);
//主配置
module.exports = (env, argv) => ({
entry: entry.entries,
// node: {
// fs: "empty"
// },
output: {
path: path.join(__dirname, "dist"),
filename: "static/[name].[hash:7].js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
// 这句很重要 不然箭头函数出错
{"plugins": ["@babel/plugin-proposal-class-properties"]}
],
}
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
// exclude: /node_modules/,
},
{
//css打包 路径在plugins里
test: /\.(css|less|scss)$/,
use: [
// 是否提取
argv.mode == "development" ? {loader: "style-loader"} : MiniCssExtractPlugin.loader,
{loader: "css-loader", options: {url: false, sourceMap: true}},
{loader: "sass-loader", options: {sourceMap: true}},
{loader: "less-loader", options: {sourceMap: true, javascriptEnabled: true}},
],
// exclude: /node_modules/,
},
{
test: /\.(png|jpg)$/,
loader: 'url-loader?limit=8192&name=images/[name].[hash:7].[ext]',
options: {
publicPath: '/'
}
},
],
},
devServer: {
port: 3100,
open: false,
quiet: true,
},
resolve: {
alias: {
// src: path.resolve(__dirname, "src"),
'@': path.resolve(__dirname, "src"),
// components: path.resolve(__dirname, "src/components/"),
// store: path.resolve(__dirname, "src/store/"),
}
},
plugins: [
// html插件数组
...htmlArr,
//分离css插件
new MiniCssExtractPlugin({
filename: "static/[name].[hash:7].css",
chunkFilename: "static/[id].[hash:7].css"
}),
// 临时配置下, 别瞎输出
new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: {
// messages: [`Your application is running here: http://${config.dev.host}:${config.dev.port}`],
},
// onErrors: config.dev.notifyOnErrors
// ? utils.createNotifierCallback()
// : undefined,
clearConsole: true,
}),
],
optimization: {
minimizer: [//压缩js
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: false
}),
new OptimizeCSSAssetsPlugin({})
],
splitChunks: { //压缩css
cacheGroups: {
styles: {
name: "styles",
test: /\.css$/,
chunks: "all",
enforce: true
}
}
}
}
});