-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.base.config.js
151 lines (148 loc) · 4.77 KB
/
webpack.base.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
'use strict'
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const devMode = process.env.NODE_ENV !== 'production';
console.log(devMode ? '开发环境' : '生产环境');
module.exports = {
entry: ['babel-polyfill', './src/index.tsx'],
output: {
filename: 'js/[name].bundle.[hash].js',
path: path.resolve(__dirname, '../dist'),
publicPath: "/"
},
module: {
rules: [{
enforce: 'pre',
test: /\.(js|jsx)$/,
loader: 'eslint-loader',
exclude: /node_modules/,
},
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
},
{
test: /\.less$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'less-loader',
],
},
{
test: /\.(tsx|ts)$/,
exclude: /node_modules/,
use: [
'babel-loader',
],
},
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
cacheDirectory: true,
}
},
{
test: /\.(png|jpg|gif)$/i,
use: [{
loader: 'url-loader',
options: {
limit: 8192,
name: '[name][contenthash].[ext]',
publicPath: '/images',
outputPath: 'images',
},
}, ],
},
{
test: /\.(eot|svg|ttf|otf|woff|woff2|mp4|3gp)$/i,
use: [{
loader: 'file-loader',
options: {
// 设置生成字体文件的路径名字信息 [path]相对context,outputPath输出的路径,publicPath相应引用的主路径
name: '[name][contenthash].[ext]',
publicPath: '/media/',
outputPath: 'media',
// 使用文件的相对路径,这里先不用这种方式
// useRelativePath: isProduction
}
}],
}
]
},
optimization: {
/**
* 提取webpack运行时的代码
*/
runtimeChunk: {
name: 'manifest',
},
splitChunks: {
chunks: 'all',
minSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
/**
* 核心库单独打包
*/
lib_core: {
test: /[\\/]node_modules[\\/](react|react-dom|react-dom-router|runtime)[\\/]/,
name: 'lib/lib-core',
priority: 100,
minSize: 0,
chunks: 'all',
reuseExistingChunk: true,
minChunks: 1,
},
/**
* 其他一步加载的代码
*/
async_commons: {
name: 'lib/async',
minChunks: 2,
chunks: 'async',
priority: 90,
},
/**
* 引用超过两次的同步代码分割
*/
commons: {
chunks: 'all',
minChunks: 2,
name: 'lib/common',
priority: 80,
},
},
},
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html'
}),
new MiniCssExtractPlugin({
filename: devMode ? 'css/[name].css' : 'css/[name][hash].css',
chunkFilename: devMode ? '[id].css' : 'css/[name][id].[hash].css',
}),
],
resolve: {
alias: {
components: path.resolve(__dirname, '../src/components/'),
public: path.resolve(__dirname, '../src/public/')
},
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json']
}
};