Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
Nov 24, 2019
1 change: 1 addition & 0 deletions packages/config/src/config/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default () => ({
plugins: [],
terser: {},
hardSource: false,
aggressiveCodeRemoval: false,
TheAlexLichter marked this conversation as resolved.
Show resolved Hide resolved
optimizeCSS: undefined,
optimization: {
runtimeChunk: 'single',
Expand Down
1 change: 1 addition & 0 deletions packages/config/test/__snapshots__/options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Object {
"build": Object {
"_publicPath": "/_nuxt/",
"additionalExtensions": Array [],
"aggressiveCodeRemoval": false,
"analyze": false,
"babel": Object {
"babelrc": false,
Expand Down
2 changes: 2 additions & 0 deletions packages/config/test/config/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"alias": Object {},
"build": Object {
"additionalExtensions": Array [],
"aggressiveCodeRemoval": false,
"analyze": false,
"babel": Object {
"babelrc": false,
Expand Down Expand Up @@ -334,6 +335,7 @@ Object {
"alias": Object {},
"build": Object {
"additionalExtensions": Array [],
"aggressiveCodeRemoval": false,
"analyze": false,
"babel": Object {
"babelrc": false,
Expand Down
8 changes: 7 additions & 1 deletion packages/webpack/src/config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class WebpackBaseConfig {
}

if (!options.babelrc && !options.presets) {
options.presets = [ defaultPreset ]
options.presets = [defaultPreset]
}

return options
Expand Down Expand Up @@ -131,6 +131,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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Values are the same. Can we refactor the right hand part to a const?

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)
Expand Down
17 changes: 10 additions & 7 deletions packages/webpack/src/config/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,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 () {
Expand Down
17 changes: 10 additions & 7 deletions packages/webpack/src/config/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down