forked from Brolly0204/webpack-optimation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.base.config.js
95 lines (92 loc) · 3.01 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
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HappyPack = require('happypack');
const os = require('os'); // 系统操作函数
const happyThreadPool = HappyPack.ThreadPool({size: os.cpus().length}); // 指定线程池个数
function resolve(dir) {
return path.join(__dirname, dir);
}
module.exports = {
entry: {
app1: './src/main1.js',
app2: './src/main2.js'
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
optimization: {
// runtimeChunk: {
// name: "manifest"
// },
splitChunks: {
cacheGroups: {
commons: {
name: 'commons',
chunks: 'initial',
minChunks: 2,
maxInitialRequests: 5,
minSize: 0
},
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
priority: 10,
enforce: true
}
}
}
},
module: {
rules: [
{
test: /\.js$/,
// use: 'babel-loader?cacheDirectory'
use: 'happypack/loader?id=babel', // 缓存loader执行结果
exclude: /node_modules/, // 排除不要加载的文件夹
include: path.resolve(__dirname, 'src') // 指定需要加载的文件夹
}
],
noParse: function(content) { // content 从入口开始解析的模块路径
return /no-parser/.test(content); // 返回true则忽略对no-parser.js的解析
}
},
resolve: {
modules: [ // 优化模块查找路径
resolve('src'),
resolve('node_modules') // 指定node_modules所在位置 当你import第三方模块式 直接从这个路径下搜寻
],
alias: {
funs$: resolve('src/util/funs.js')
},
extensions: ['.js', '.vue']
},
plugins: [
new webpack.DefinePlugin({ // 定义环境变量
"process.env": JSON.stringify(process.env.NODE_ENV)
}),
new HappyPack({
id: 'babel',
loaders: ['babel-loader?cacheDirectory'],
threadPool: happyThreadPool,
verbose: true
}),
new HtmlWebpackPlugin({
template: resolve('index.html'),
filename: 'index1.html',
title: 'hello webpack!',
chunks: ['vendor', 'commons', 'app1']
}),
new HtmlWebpackPlugin({
template: resolve('index2.html'),
filename: 'index2.html',
title: 'hello react!',
chunks: ['vendor', 'commons', 'app2']
}),
new CleanWebpackPlugin([resolve('dist')]),
new webpack.optimize.ModuleConcatenationPlugin()
]
}