Repository: nuxt/nuxt.js
Pull request #6149 (2019-07-28 08:37:07)

feat: optionally strip non-client or non-server code
Types of changes
- [ ] Bug fix (a non-breaking change which fixes an issue)
- [x] New feature (a non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)


## Description

We already have `process.client` and `process.server` that take care of separating "client" and "server" code (if needed). However, it can sometimes be hard to control that in 3rd party dependencies.

With this change, the webpack define plugin will set the `typeof window` check to  `object` on client and `undefined` on server, so the code can (and will be) correctly removed by webpack.


## Checklist:
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly. (PR: #)
- [ ] I have added tests to cover my changes (if not applicable, please state why)
- [ ] All new and existing tests are passing.



clarkdo: What will happen to jsdom case？
codecov-io: # [Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/6149?src=pr&el=h1) Report
> Merging [#6149](https://codecov.io/gh/nuxt/nuxt.js/pull/6149?src=pr&el=desc) into [dev](https://codecov.io/gh/nuxt/nuxt.js/commit/0cd823c30078b3f097a91052cc7dfde74f289c0d?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/nuxt/nuxt.js/pull/6149/graphs/tree.svg?width=650&token=nGD1PtMB3M&height=150&src=pr)](https://codecov.io/gh/nuxt/nuxt.js/pull/6149?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##              dev    #6149   +/-   ##
=======================================
  Coverage   95.75%   95.75%           
=======================================
  Files          80       80           
  Lines        2659     2659           
  Branches      682      682           
=======================================
  Hits         2546     2546           
  Misses         97       97           
  Partials       16       16
```

| Flag | Coverage Δ | |
|---|---|---|
| #e2e | `100% <ø> (ø)` | :arrow_up: |
| #fixtures | `50.88% <ø> (ø)` | :arrow_up: |
| #unit | `92.44% <ø> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/nuxt/nuxt.js/pull/6149?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/webpack/src/config/client.js](https://codecov.io/gh/nuxt/nuxt.js/pull/6149/diff?src=pr&el=tree#diff-cGFja2FnZXMvd2VicGFjay9zcmMvY29uZmlnL2NsaWVudC5qcw==) | `98.07% <ø> (ø)` | :arrow_up: |
| [packages/webpack/src/config/server.js](https://codecov.io/gh/nuxt/nuxt.js/pull/6149/diff?src=pr&el=tree#diff-cGFja2FnZXMvd2VicGFjay9zcmMvY29uZmlnL3NlcnZlci5qcw==) | `100% <ø> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/6149?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/6149?src=pr&el=footer). Last update [0cd823c...5f81c81](https://codecov.io/gh/nuxt/nuxt.js/pull/6149?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

manniL: Made it optional but couldn't find a better name for it :see_no_evil: 	
stale[bot]: Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:

 1. Verify that you can still reproduce the issue in the latest version of nuxt-edge
 1. Comment the steps to reproduce it

Issues that are labeled as `pending` will not be automatically marked as stale.

stale[bot]: Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:

 1. Verify that you can still reproduce the issue in the latest version of nuxt-edge
 1. Comment the steps to reproduce it

Issues that are labeled as `pending` will not be automatically marked as stale.


Patch:
diff --git a/packages/config/src/config/build.js b/packages/config/src/config/build.js
index d19a7a0ce..fd085ea3d 100644
--- a/packages/config/src/config/build.js
+++ b/packages/config/src/config/build.js
@@ -59,6 +59,7 @@ export default () => ({
   plugins: [],
   terser: {},
   hardSource: false,
+  aggressiveCodeRemoval: false,
   optimizeCSS: undefined,
   optimization: {
     runtimeChunk: 'single',
diff --git a/packages/webpack/src/config/base.js b/packages/webpack/src/config/base.js
index 40c0728be..2e4771ba3 100644
--- a/packages/webpack/src/config/base.js
+++ b/packages/webpack/src/config/base.js
@@ -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)
diff --git a/packages/webpack/src/config/client.js b/packages/webpack/src/config/client.js
index 243e7dab6..13abfb6ab 100644
--- a/packages/webpack/src/config/client.js
+++ b/packages/webpack/src/config/client.js
@@ -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 () {
diff --git a/packages/webpack/src/config/server.js b/packages/webpack/src/config/server.js
index 24803dc61..e0057a894 100644
--- a/packages/webpack/src/config/server.js
+++ b/packages/webpack/src/config/server.js
@@ -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 () {


Test patch:
diff --git a/packages/config/test/__snapshots__/options.test.js.snap b/packages/config/test/__snapshots__/options.test.js.snap
index 24fbfad74..78c4c6fb8 100644
--- a/packages/config/test/__snapshots__/options.test.js.snap
+++ b/packages/config/test/__snapshots__/options.test.js.snap
@@ -20,6 +20,7 @@ Object {
   "build": Object {
     "_publicPath": "/_nuxt/",
     "additionalExtensions": Array [],
+    "aggressiveCodeRemoval": false,
     "analyze": false,
     "babel": Object {
       "babelrc": false,
diff --git a/packages/config/test/config/__snapshots__/index.test.js.snap b/packages/config/test/config/__snapshots__/index.test.js.snap
index 24ed8224f..c9f4e08a4 100644
--- a/packages/config/test/config/__snapshots__/index.test.js.snap
+++ b/packages/config/test/config/__snapshots__/index.test.js.snap
@@ -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,