feat: optionally strip non-client or non-server code (#6149)

This commit is contained in:
Alexander Lichter 2019-11-24 16:39:45 +01:00 committed by Pooya Parsa
parent e9945b0896
commit 75bb0884cb
6 changed files with 31 additions and 15 deletions

View File

@ -59,6 +59,7 @@ export default () => ({
plugins: [],
terser: {},
hardSource: false,
aggressiveCodeRemoval: false,
optimizeCSS: undefined,
optimization: {
runtimeChunk: 'single',

View File

@ -20,6 +20,7 @@ Object {
"build": Object {
"_publicPath": "/_nuxt/",
"additionalExtensions": Array [],
"aggressiveCodeRemoval": false,
"analyze": false,
"babel": Object {
"babelrc": false,

View File

@ -7,6 +7,7 @@ Object {
"alias": Object {},
"build": Object {
"additionalExtensions": Array [],
"aggressiveCodeRemoval": false,
"analyze": false,
"babel": Object {
"babelrc": false,
@ -360,6 +361,7 @@ Object {
"alias": Object {},
"build": Object {
"additionalExtensions": Array [],
"aggressiveCodeRemoval": false,
"analyze": false,
"babel": Object {
"babelrc": false,

View File

@ -115,7 +115,7 @@ export default class WebpackBaseConfig {
}
if (!options.babelrc && !options.presets) {
options.presets = [ defaultPreset ]
options.presets = [defaultPreset]
}
return options
@ -141,6 +141,12 @@ export default class WebpackBaseConfig {
'process.mode': JSON.stringify(this.mode),
'process.static': this.buildContext.isStatic
}
if (this.buildContext.buildOptions.aggressiveCodeRemoval) {
env['typeof process'] = JSON.stringify(this.isServer ? 'object' : 'undefined')
env['typeof window'] = JSON.stringify(!this.isServer ? 'object' : 'undefined')
env['typeof document'] = JSON.stringify(!this.isServer ? 'object' : 'undefined')
}
Object.entries(this.buildContext.options.env).forEach(([key, value]) => {
env['process.env.' + key] =
['boolean', 'number'].includes(typeof value)

View File

@ -34,13 +34,16 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
}
env () {
return Object.assign(super.env(), {
'process.env.VUE_ENV': JSON.stringify('client'),
'process.browser': true,
'process.client': true,
'process.server': false,
'process.modern': false
})
return Object.assign(
super.env(),
{
'process.env.VUE_ENV': JSON.stringify('client'),
'process.browser': true,
'process.client': true,
'process.server': false,
'process.modern': false
}
)
}
optimization () {

View File

@ -26,13 +26,16 @@ export default class WebpackServerConfig extends WebpackBaseConfig {
}
env () {
return Object.assign(super.env(), {
'process.env.VUE_ENV': JSON.stringify('server'),
'process.browser': false,
'process.client': false,
'process.server': true,
'process.modern': false
})
return Object.assign(
super.env(),
{
'process.env.VUE_ENV': JSON.stringify('server'),
'process.browser': false,
'process.client': false,
'process.server': true,
'process.modern': false
}
)
}
optimization () {