-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
202 lines (190 loc) · 5.37 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer'); // 自动加前缀的插件
const pxtorem = require('postcss-pxtorem'); // 自动计算Rem
const path = require('path');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const svgDirs = [
require.resolve('antd-mobile').replace(/warn\.js$/, ''), // 1. 属于 antd-mobile 内置 svg 文件
path.resolve(__dirname, 'src/Iconfont'),
];
// 一句话理解 path和 publicPath的作用!!!!
// path 用来存放打包后文件的输出目录
// publicPath 用来定义静态资源的引用地址
module.exports = {
cache: true,
devtool: 'eval',
entry: {
index: [
'react-hot-loader/patch',
// 开启 React 代码的模块热替换(HMR)
'webpack-dev-server/client?http://localhost:3000',
// 为 webpack-dev-server 的环境打包代码
// 然后连接到指定服务器域名与端口
'webpack/hot/only-dev-server',
// 为热替换(HMR)打包好代码
// only- 意味着只有成功更新运行代码才会执行热替换(HMR)
'./index.jsx',
// 我们 app 的入口文件
],
vendor: ['bundle-loader', 'rc-form', 'react-hot-loader', 'redux-promise', 'redux-thunk'],
},
output: {
filename: '[name].js',
// 输出的打包文件
path: `${__dirname}/dist/assets/`,
// 项目输出路径
publicPath: '/assets/',
// 对于热替换(HMR)是必须的,让 webpack 知道在哪里载入热更新的模块(chunk)
chunkFilename: '[name].[chunkhash].js',
},
context: `${__dirname}/src`,
module: {
rules: [
{
exclude: [
/\.less$/,
/\.svg$/,
],
},
{
test: /\.(js|jsx)$/,
use: [
'babel-loader?cacheDirectory', 'eslint-loader',
],
exclude: /^node_modules$/,
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
],
exclude: /^node_modules$/,
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins() {
return [
autoprefixer({
browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
}),
pxtorem({ rootValue: 100, propWhiteList: [], minPixelValue: 2 }),
];
},
},
},
'less-loader',
],
include: /node_modules/,
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader?modules&localIdentName=[path][name]---[local]---[hash:base64:5]',
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins() {
return [
autoprefixer({
browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
}),
pxtorem({ rootValue: 100, propWhiteList: [], minPixelValue: 2 }),
];
},
},
},
'less-loader',
],
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]?[hash]',
},
},
],
exclude: /^node_modules$/,
},
{
test: /\.(svg)$/i,
loader: 'svg-sprite-loader',
include: svgDirs,
},
{
// 处理静态资源
test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svgz)(\?.+)?$/,
exclude: /favicon\.png$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
},
},
],
},
],
},
resolve: {
extensions: ['.web.js', '.jsx', '.js', '.less', '.json'],
alias: {
'~': `${__dirname}/src`,
},
},
externals: {
moment: 'moment',
axios: 'axios',
react: 'React',
redux: 'Redux',
'react-dom': 'ReactDOM',
'react-redux': 'ReactRedux',
'react-router': 'ReactRouter',
'react-router-dom': 'ReactRouterDOM',
'prop-types': 'PropTypes',
'babel-polyfill': 'window',
},
plugins: [
// 将第三方库单独打包
new webpack.optimize.CommonsChunkPlugin({ names: ['vendor', 'manifest'] }),
new webpack.HotModuleReplacementPlugin(),
// 开启全局的模块热替换(HMR)
new webpack.NamedModulesPlugin(),
// 当模块热替换(HMR)时在浏览器控制台输出对用户更友好的模块名字信息
new BundleAnalyzerPlugin(),
// webpack打包性能分析插件
new HtmlWebpackPlugin({
template: './Template/index.html',
filename: './index.html', // 生成的html存放路径,相对于 path
}),
// 生成html文件
],
};