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

REST (React): "msw" is included in the production bundle #10

Open
kettanaito opened this issue Jun 6, 2020 · 5 comments
Open

REST (React): "msw" is included in the production bundle #10

kettanaito opened this issue Jun 6, 2020 · 5 comments

Comments

@kettanaito
Copy link
Member

One of the users has reported that msw package is included in the production bundle of the rest-react example.

Expected behavior

msw must not be included in the production bundle.

@wKovacs64
Copy link
Contributor

wKovacs64 commented Jun 6, 2020

I saw Kent run into this on stream too, but possibly for different reasons. The reason I ran into it was that I assigned process.env.NODE_ENV to another variable, which doesn't work apparently (I vaguely remember knowing that once upon a time, but don't recall the reasoning behind it).

👍🏻 msw not in production bundle:

if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
  const { worker } = require('./mocks')
  worker.start()
}

👎🏻 msw in production bundle:

const isDevOrTest = process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development'
if (isDevOrTest) {
  const { worker } = require('./mocks')
  worker.start()
}

(edit: note the example works just fine unmodified, so that was an invalid report)

@thebuilder
Copy link

@wKovacs64

if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
  const { worker } = require('./mocks')
  worker.start()
}

This allows the bundler to eliminate the dead code paths, since it will be transformed to:

if (false || false) { // Will never be true
  const { worker } = require('./mocks')
  worker.start()
}

If the if statement can never be true, it will be removed from the bundle. By assigning the ENV variable to another variable first, it would require runtime analysis of the code to determine if it can be eliminated.

const isDevOrTest = false || false
if (isDevOrTest) { // Won't know the value `isDevOrTest` without running the code
  const { worker } = require('./mocks')
  worker.start()
}

@wKovacs64
Copy link
Contributor

@thebuilder There we go, that sounds familiar. Thanks for the explanation!

@kettanaito
Copy link
Member Author

We need to add an automated test that asserts the msw is not bundled when built a full example like this.
Anybody interested in this can pick this up, otherwise I will push something similar in some time. Thanks everybody for discussion on this!

@alishi973
Copy link

Hey, any update on this? if it's not, can I pick this up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants