forked from javaLuo/react-luo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.production.config.js
147 lines (147 loc) · 4.48 KB
/
webpack.production.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
const path = require("path");
const ExtractTextPlugin = require("extract-text-webpack-plugin"); // 为了单独打包css
const HtmlWebpackPlugin = require("html-webpack-plugin"); // 生成html
const CleanWebpackPlugin = require("clean-webpack-plugin"); // 每次打包前清除旧的build文件夹
const PreloadWebpackPlugin = require("preload-webpack-plugin"); // 预加载所有chunk
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; // 打包分析插件,打包后会自动弹出tree图
module.exports = {
mode: "production",
entry: path.resolve(__dirname, "src", "index"),
output: {
path: path.resolve(__dirname, "build/dist"), // 将文件打包到此目录下
publicPath: "/dist/", // 在生成的html中,文件的引入路径会相对于此地址,生成的css中,以及各类图片的URL都会相对于此地址
filename: "[name].[hash:6].js",
chunkFilename: "[name].[hash:6].chunk.js"
},
context: __dirname,
module: {
rules: [
{
// .js .jsx用babel解析
test: /\.js?$/,
include: path.resolve(__dirname, "src"),
use: [
"babel-loader"
]
},
{
// .css 解析
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: "css-loader",
options: {
modules: true,
localIdentName: "[local]_[hash:base64:5]"
}
},
"postcss-loader"
]
})
},
{
// .less 解析
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: "css-loader",
options: {
modules: true,
localIdentName: "[local]_[hash:base64:5]"
}
},
"postcss-loader",
"less-loader"
]
}),
include: path.resolve(__dirname, "src")
},
{
// .less 解析 (用于解析antd的LESS文件)
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
"css-loader",
"postcss-loader",
"less-loader"
]
}),
include: path.resolve(__dirname, "node_modules")
},
{
// .scss 解析
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: "css-loader",
options: {
modules: true,
localIdentName: "[local]_[hash:base64:5]"
}
},
"postcss-loader",
"sass-loader"
]
}),
},
{
// 文件解析
test: /\.(eot|woff|svg|ttf|woff2|appcache|mp3|mp4|pdf)(\?|$)/,
include: path.resolve(__dirname, "src"),
use: [
"file-loader?name=assets/[name].[ext]"
]
},
{
// 图片解析
test: /\.(png|jpg|gif)$/,
include: path.resolve(__dirname, "src"),
use: [
"url-loader?limit=8192&name=assets/[name].[ext]",
]
},
{
// CSV/TSV文件解析
test: /\.(csv|tsv)$/,
use: [
'csv-loader'
]
},
{
// xml文件解析
test: /\.xml$/,
use: [
'xml-loader'
]
}
]
},
plugins: [
new CleanWebpackPlugin(["build"]),
// 配置了这个插件,再配合上面loader中的配置,将所有样式文件打包为一个单独的css文件
new ExtractTextPlugin({
filename: "[name].[hash:6].css", // 生成的文件名
allChunks: true // 从所有chunk中提取
}),
new HtmlWebpackPlugin({
//根据模板插入css/js等生成最终HTML
filename: "../index.html", //生成的html存放路径,相对于 output.path
template: "./public/index.html", //html模板路径
favicon: "./public/favicon.ico", // 自动把根目录下的favicon.ico图片加入html
inject: true // 是否将js放在body的末尾
}),
new PreloadWebpackPlugin(),
// new BundleAnalyzerPlugin() // 打包分析插件,打包后会自动弹出tree图:127.0.0.1:8888
],
// 解析器, webpack提供的各种方便的工具函数
resolve: {
extensions: [".js", ".jsx", ".less", ".css", ".scss"] //后缀名自动补全
}
};