-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.config.js
151 lines (147 loc) · 5.25 KB
/
webpack.common.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
const path = require('path');
const webpack = require('webpack');
const precss = require('precss');
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const STATIC_PATH = 'static';
const extractVendor = new ExtractTextPlugin(`${STATIC_PATH}/css/[hash].vendor.css`);
const extractStyle = new ExtractTextPlugin(`${STATIC_PATH}/css/[hash].style.css`);
const devMode = process.env.NODE_ENV === 'production';
const webpackCommonConfig = {
entry: {
main: path.join(__dirname, 'src/index.jsx'),
vendor: ['react', 'react-router-dom', 'react-dom']
},
output: {
path: path.join(__dirname, './dist'),
filename: `${STATIC_PATH}/js/[chunkhash].[name].js`,
publicPath: '/'
},
resolve: {
alias: {
images: path.join(__dirname, 'src/images'),
components: path.join(__dirname, 'src/components'),
router: path.join(__dirname, 'src/router')
},
extensions: ['.js', '.jsx', '.css', '.scss', '.less']
},
module: {
rules: [{
test: /\.(js|jsx)$/,
include: path.join(__dirname, 'src'),
// exclude: path.join(__dirname, 'src/fonts'),
exclude: /(node_modules|bower_components)/,
use: ['babel-loader?cacheDirectory=true']
/**
* 第三方组件的css, scss抽离为独立文件vendor.css
*/
}, {
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? MiniCssExtractPlugin.loader
: 'style-loader',
'css-loader',
'postcss-loader',
'sass-loader'
]
}, {
/**
* 字体加载器
*/
test: /\.(woff|eot|ttf|js|svg)$/,
include: path.join(__dirname, 'src/fonts'),
use: [
{
loader: 'url-loader',
options: {
limit: 10,
name: `${STATIC_PATH}/fonts/[hash].[ext]`
}
}
]
/**
* 图片加载器
*/
}, {
test: /\.(png|jpg|jpeg|gif|svg)$/,
include: path.join(__dirname, 'src/images'),
use: [
{
loader: 'url-loader',
options: {
limit: 10,
name: `${STATIC_PATH}/images/[hash].[ext]`
}
}
]
}, {
test: /\.ico$/,
include: path.join(__dirname, 'src/images'),
use: [
{
loader: 'url-loader',
options: {
limit: 10,
name: `${STATIC_PATH}/images/[name].[ext]`
}
}
]
}]
},
optimization: { // 提取主页面和魔盒页面共享的公共模块
splitChunks: {
// chunks: 'initial', 表示显示块的范围,有三个可选值:initial(初始块)、async(按需加载块)、all(全部块),默认为all;
cacheGroups: {
// vendor: { // split `node_modules`目录下被打包的代码到 `page/vendor.js && .css` 没找到可打包文件的话,则没有。css需要依赖 `ExtractTextPlugin`
// test: /node_modules\//,
// name: 'page/vendor',
// priority: 10,
// enforce: true
// },
commons: { // split `common`和`components`目录下被打包的代码到`page/commons.js && .css`
test: /common\/|components\//,
name: 'commons',
priority: 10,
minChunks: 2, // 表示被引用次数,默认为1;
enforce: true
},
// css: {
// name: `${STATIC_PATH}/common/css`,
// test: /\.css$/,
// chunks: 'all',
// enforce: true
// } // 会生成css文件
}
},
runtimeChunk: {
name: STATIC_PATH
}
},
plugins: [
// 提取css
extractVendor,
extractStyle,
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/), // 指定moment加载中文
new CleanWebpackPlugin(['dist']), // 清除编译目录
new MiniCssExtractPlugin({
filename: `${STATIC_PATH}/css/[chunkhash].css` //放到dist/css/下
}),
// 主页面入口index.html
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html'
}),
new webpack.LoaderOptionsPlugin({
minimize: true, // 压缩loader读取的文件
options: {
postcss: function () {
return [precss, autoprefixer];
}
}
})
]
};
module.exports = webpackCommonConfig;