-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathwebpack.js
119 lines (101 loc) · 4.09 KB
/
webpack.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
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
const VueSSRClientPlugin = require('vue-server-renderer/client-plugin')
const nodeExternals = require('webpack-node-externals')
const WebpackBar = require('webpackbar')
const config = require('./config')
const HtmlFilterPlugin = require('./plugins/HtmlFilterPlugin')
const CssContextLoader = require.resolve('./loaders/css-context')
exports.chainWebpack = (webpackConfig) => {
const target = process.env.VUE_CLI_SSR_TARGET
if (!target) return
const isClient = target === 'client'
const isProd = process.env.NODE_ENV === 'production'
// Remove unneeded plugins
webpackConfig.plugins.delete('hmr')
webpackConfig.plugins.delete('preload')
webpackConfig.plugins.delete('prefetch')
webpackConfig.plugins.delete('progress')
if (!isProd) webpackConfig.plugins.delete('no-emit-on-errors')
if (!isClient) {
webpackConfig.plugins.delete('friendly-errors')
const isExtracting = webpackConfig.plugins.has('extract-css')
if (isExtracting && config.criticalCSS) {
// Remove extract
const langs = ['css', 'postcss', 'scss', 'sass', 'less', 'stylus']
const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
for (const lang of langs) {
for (const type of types) {
const rule = webpackConfig.module.rule(lang).oneOf(type)
rule.uses.delete('extract-css-loader')
// Critical CSS
rule.use('css-context').loader(CssContextLoader).before('css-loader')
}
}
webpackConfig.plugins.delete('extract-css')
}
}
// HTML
webpackConfig.plugin('html-filter').use(HtmlFilterPlugin)
if (isProd) {
webpackConfig.plugin('html').tap(args => {
args[0].minify.removeComments = false
args[0].minify.caseSensitive = true
return args
})
}
const htmlPlugin = webpackConfig.plugins.get('html').store
webpackConfig.plugin('html-ssr').use(htmlPlugin.get('plugin'), [
{
...htmlPlugin.get('args')[0],
template: config.api.resolve('public/index.ssr.html'),
filename: 'index.ssr.html',
},
])
webpackConfig.entry('app').clear().add(config.entry(target))
webpackConfig.plugin('define').tap(args => {
return [Object.assign(args[0], { 'process.client': target === 'client', 'process.server': target === 'server' })]
})
webpackConfig.stats(isProd ? 'normal' : 'none')
webpackConfig.devServer.stats('errors-only').quiet(true).noInfo(true)
if (isClient) {
webpackConfig.plugin('ssr').use(VueSSRClientPlugin)
webpackConfig.plugin('loader').use(WebpackBar, [{ name: 'Client', color: 'green' }])
webpackConfig.devtool(!isProd ? '#cheap-module-source-map' : undefined)
webpackConfig.module.rule('vue').use('vue-loader').tap(options => {
options.optimizeSSR = false
return options
})
} else {
webpackConfig.plugin('ssr').use(VueSSRServerPlugin)
webpackConfig.plugin('loader').use(WebpackBar, [{ name: 'Server', color: 'orange' }])
webpackConfig.devtool('source-map')
webpackConfig.externals(nodeExternals({ whitelist: config.nodeExternalsWhitelist }))
webpackConfig.output.libraryTarget('commonjs2'); webpackConfig.target('node')
webpackConfig.optimization.splitChunks(false).minimize(false)
webpackConfig.node.clear()
if (!isClient) {
webpackConfig.module.rule('vue').use('cache-loader').tap(options => {
// Change cache directory for server-side
options.cacheIdentifier += '-server'
options.cacheDirectory += '-server'
return options
})
}
webpackConfig.module.rule('vue').use('vue-loader').tap(options => {
if (!isClient) {
options.cacheIdentifier += '-server'
options.cacheDirectory += '-server'
}
options.optimizeSSR = !isClient
return options
})
}
}
exports.getWebpackConfigs = (service) => {
process.env.VUE_CLI_MODE = service.mode
process.env.VUE_CLI_SSR_TARGET = 'client'
const clientConfig = service.resolveWebpackConfig()
process.env.VUE_CLI_SSR_TARGET = 'server'
const serverConfig = service.resolveWebpackConfig()
return [clientConfig, serverConfig]
}