-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
139 lines (137 loc) · 2.88 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
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const webpack = require("webpack");
const smp = new SpeedMeasurePlugin();
const devMode = process.env.NODE_ENV === "development";
const webpackPlugins = [
new HtmlWebpackPlugin({
title: "移动端 webpack搭建",
template: "template/index.html"
}),
new webpack.ProgressPlugin(),
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: devMode ? "css/[name].css" : "css/[name].[hash:6].css",
chunkFilename: devMode ? "css/[id].css" : "css/[id].[hash:6].css"
}),
new FriendlyErrorsWebpackPlugin(),
new webpack.HotModuleReplacementPlugin()
];
if (!devMode) {
// webpackPlugins.push(new BundleAnalyzerPlugin());
}
// 使用smp.wrap 热更新会报错。
module.exports = {
entry: "./src/index.jsx",
devtool: devMode ? "inline-source-map" : "",
devServer: {
historyApiFallback: true,
contentBase: path.join(__dirname, "dist"),
port: 8080,
hot: true,
quiet: true
},
mode: devMode ? "development" : "production",
module: {
rules: [
{
test: /\.css$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true,
hmr: devMode
}
},
{
loader: "css-loader",
options: {
modules: {
localIdentName:
"[name]__[local]--[hash:base64:5]"
}
}
},
"postcss-loader"
]
},
{
test: /\.css$/,
include: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true,
hmr: devMode
}
},
"css-loader"
]
},
{
test: /\.(jsx|js)$/,
exclude: /node_modules/,
use: [
{
loader: "thread-loader",
options: {
workers: 4
}
},
{
loader: "babel-loader",
options: {
cacheDirectory: true
}
}
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: "file-loader",
options: {
outputPath: "images"
}
}
]
}
]
},
plugins: webpackPlugins,
optimization: {
runtimeChunk: true,
splitChunks: {
chunks: "all"
},
minimizer: [
// 并行压缩
new TerserPlugin({
parallel: true,
cache: true
})
]
},
stats: "errors-only",
externals: {
react: "React",
"react-dom": "ReactDOM",
swiper: "Swiper"
},
output: {
filename: "js/bundle.[hash].js",
path: path.resolve(__dirname, "dist"),
publicPath: "/"
}
};