-
Notifications
You must be signed in to change notification settings - Fork 0
/
production.js
103 lines (102 loc) · 2.93 KB
/
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
const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const nodeModuleDir = path.resolve(__dirname, 'node_module')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const InlineChunkManifestHtmlWebpackPlugin = require('./dev/inline/index')
const { routers, publicPath } = require('./config.json')
const webpackConfig = {
performance: { maxEntrypointSize: 400000 },
entry: {},
output: {
path: path.resolve(__dirname, 'build/assets'),
chunkFilename: '[name].[chunkhash:5].js',
publicPath: publicPath,
filename: '[name].[chunkhash:5].js'
},
module: {
rules: [{
test: /\.(js|jsx)$/,
use: ['babel-loader'],
include: [path.resolve(__dirname, 'app')],
exclude: [nodeModuleDir]
},
{
test: /\.css$/,
use: [
'style.loader',
'css-loader?modules&localIdentName=_[local]_[hash:base64:5]',
{ loader: 'postcss-loader',
options: {
ident: 'postcss',
config: { path: path.resolve(__dirname, 'dev/postcss.config.js') }
}
}],
include: [path.resolve(__dirname, 'app')],
exclude: [nodeModuleDir]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [{ loader: 'url-loader?limit=25000&name=[name].[ext]&outputPath=/images&publicPath=./images' }],
include: [path.resolve(__dirname, 'app')],
exclude: [nodeModuleDir]
}
]
},
// 4.0 之后分代码
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true,
uglifyOptions: {
compress: {
drop_console: true
},
output: {
comments: false
}
}
}),
new OptimizeCSSAssetsPlugin({})
],
runtimeChunk: { name: () => { return 'manifest' } },
splitChunks: {
cacheGroups: {
globals: {
minChunks: 2,
name: 'globals',
priority: -20,
chunks: 'all'
}
}
}
},
plugins: [new CleanWebpackPlugin(['build'])],
mode: 'production'
}
routers.map((item, index) => {
const {
name,
template
} = item
// 每个页面使用一个entry配置
const routerScript = [path.resolve(__dirname, `app/router/${template}/index.js`)]
const plugin = new HtmlWebpackPlugin({
filename: `../${template}.html`,
title: name,
template: path.resolve(__dirname, `app/router/${template}/index.html`),
inject: true,
minify: {
collapseWhitespace: true,
conservativeCollapse: true
},
chunks: ['manifest', 'globals', template]
})
webpackConfig.entry[template] = routerScript
webpackConfig.plugins.push(plugin)
})
webpackConfig.plugins.push(new InlineChunkManifestHtmlWebpackPlugin({ inlineChunks: ['manifest'] }))
module.exports = webpackConfig