-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwebpack.config.production.js
170 lines (163 loc) · 4.41 KB
/
webpack.config.production.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
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 单独打包css 目前还没有webpack4版本 需安装 extract-text-webpack-plugin@next解决
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const autoprefixer = require('autoprefixer'); // 自动加前缀的插件
module.exports = {
devtool: false,
mode: 'production', // 会将 process.env.NODE_ENV 的值设为 production。
// 启用 FlagDependencyUsagePlugin, FlagIncludedChunksPlugin, ModuleConcatenationPlugin,
// NoEmitOnErrorsPlugin, OccurrenceOrderPlugin, SideEffectsFlagPlugin 和 UglifyJsPlugin.
entry: {
index: [
'./index.jsx',
],
},
output: {
filename: '[name].[chunkhash].js',
// 输出的打包文件
path: `${__dirname}/dist/assets/`,
// 项目输出路径
publicPath: '/assets/',
// 对于热替换(HMR)是必须的,让 webpack 知道在哪里载入热更新的模块(chunk)
chunkFilename: '[name].[chunkhash].js',
},
context: `${__dirname}/src`,
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: [
'babel-loader',
],
exclude: /node_modules/,
},
{
// 匹配.css文件
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader',
{
loader: 'postcss-loader',
options: {
plugins() {
return [
autoprefixer,
];
},
},
},
],
}),
exclude: /^node_modules$/,
},
{
// 匹配.less文件
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader',
{
loader: 'postcss-loader',
options: {
plugins() {
return [
autoprefixer,
];
},
},
},
{ loader: 'less-loader', options: { javascriptEnabled: true } },
],
}),
include: /node_modules/,
},
{
// 匹配.less文件
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?modules&localIdentName=[path][name]---[local]---[hash:base64:5]',
{
loader: 'postcss-loader',
options: {
plugins() {
return [
autoprefixer,
];
},
},
},
{ loader: 'less-loader', options: { javascriptEnabled: true } },
],
}),
exclude: /node_modules/,
},
{
// 匹配.html文件
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
attrs: ['img:src', 'link:href'],
},
},
],
exclude: /^node_modules$/,
},
{
test: /favicon\.png$/,
use: [
{
// 使用file-loader
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
exclude: /^node_modules$/,
},
{
// 处理静态资源
test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
exclude: /favicon\.png$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
},
},
],
},
],
},
resolve: {
extensions: ['.jsx', '.js', '.less', '.json'],
alias: {
'~': `${__dirname}/src`,
},
},
externals: {
axios: 'axios',
'prop-types': 'PropTypes',
'babel-polyfill': 'window',
},
optimization: {
runtimeChunk: true,
splitChunks: {
chunks: 'all', // 进行模块拆分 提取公共代码 相当于webpack2 的 CommonsChunkPlugin
},
// minimizer: true, // 美化代码 相当于webpack2 的 UglifyJsPlugin mode 为 production 自动 true
},
plugins: [
// 自动生成所需要的html模板
new HtmlWebpackPlugin({
template: './Template/index.html',
filename: '../index.html', // 生成的html存放路径,相对于 path
}),
new ExtractTextPlugin({ filename: '[name].[chunkhash].css', allChunks: true, disable: false }),
],
};