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: [], plugins: [],
terser: {}, terser: {},
hardSource: false, hardSource: false,
aggressiveCodeRemoval: false,
optimizeCSS: undefined, optimizeCSS: undefined,
optimization: { optimization: {
runtimeChunk: 'single', runtimeChunk: 'single',

View File

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

View File

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

View File

@ -115,7 +115,7 @@ export default class WebpackBaseConfig {
} }
if (!options.babelrc && !options.presets) { if (!options.babelrc && !options.presets) {
options.presets = [ defaultPreset ] options.presets = [defaultPreset]
} }
return options return options
@ -141,6 +141,12 @@ export default class WebpackBaseConfig {
'process.mode': JSON.stringify(this.mode), 'process.mode': JSON.stringify(this.mode),
'process.static': this.buildContext.isStatic '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]) => { Object.entries(this.buildContext.options.env).forEach(([key, value]) => {
env['process.env.' + key] = env['process.env.' + key] =
['boolean', 'number'].includes(typeof value) ['boolean', 'number'].includes(typeof value)

View File

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

View File

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