-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
msw not being tree shaken from client bundle (dead code elimination bug) #25607
Comments
Can confirm that this is happening with webpack 4 too. |
In the meantime, for anyone encountering this, you can manually eliminate msw from the browser bundle by adding this to your "browser": {
"./.msw": false
}, Make sure you replace See:
This is just a temporary workaround, ideally the regression would still be fixed in the next.js build process. |
Setting the env value of I initially wanted to work on a fix for this, but I think the behavior of next.js is sensible here, the only option I see would be to somehow replace all env vars which are not defined with |
Thanks for taking a look at this @lukasmoellerch. So the reason that I called this a regression is that in the linked issue here: mswjs/msw#359 (comment), the maintainer of msw verified that next was correctly removing msw. This was verified with the code from the next.js Currently it no longer does that. So what I'm wondering is: what is the intended behaviour for the next.js bundling process? My expectation would be that the way it worked when @kettanaito checked it, is the way it's supposed to work (I could not verify this in the docs though). If that is true, then even if the behaviour is sensible, it's a regression. Unless it's been announced as a breaking change, but I haven't seen that mentioned. Let me know what you think. |
Hi - in mswjs/msw#359 (comment) the maintainer mentions that the env variable was set to disabled for the test:
If this is the case the module is indeed correctly excluded from the bundle. I also couldn't reproduce it with a removed env file with several old next.js versions. Furthermore it seems like next.js intentionally doesn't default |
Ah I see, I had missed that.
Yeah in that case it seems like it's not a regression and I agree with your initial conclusion. Seems like the build process is working as intended. So maybe then we could mention this behaviour explicitly in the docs, it's a little different than what I expected (might just be me). Also, maybe we could add a note to the with-msw example, just so that it's clear to people what they should do if they want to omit msw from the production bundle. And then if replacing undefined environment vars with What do you think? |
I'd agree that a note about this behavior in the env var docs would make sense, possibly after the
In general I'd prefer the current behavior that undefined env vars aren't supported, but it might be possible to detect such misconfigurations in production builds. If you really want an environment variable to default to |
Yeah exactly, something like that seems nice 👍. Though I guess they don't have to be defined. It's just that if you want dead code elimination based on a comparison to an env var you need them to be defined, as otherwise the comparison can't be evaluated at build time.
Yeah that seems clear, given that it would also be in the next.js docs if we add something like the above.
Yeah, I don't mind the current behaviour either. And notifying the user about it seems user-friendly 👍. |
I see your point, but I am not sure whether it should be stated in the docs like that. The way I see it the use of undefined env vars is unsupported. It's not like this couldn't be implemented in next.js, but it seems like a conscious decision not to support it. Therefore I am not sure whether the docs should define the exact behavior or just the say that it isn't supported. |
Yeah I see what you mean. Yeah I think a note along the lines of what you suggested here: #25607 (comment) should be good enough. |
In this light, I'm also sharing #43284, where using a dynamic import presently results in a race condition between the import itself and any initial load requests the app may perform. I wonder why we've migrated from using |
Going to close this in favor of #43284. In short, |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What example does this report relate to?
with-msw
What version of Next.js are you using?
10.2.3
What version of Node.js are you using?
14.16.1
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
next build
isn't eliminating this require from the client bundle:It bundles msw for the client bundle, even when the require is contained in an if block that evaluates to false during build time (env var to string comparison). This is reproducible with a slight modification to the
with-msw
example.Expected Behavior
I expect
next build
to drop this code from the client bundle:and not include any of the code imported through the call to
require
.To Reproduce
First off:
https://github.com/vercel/next.js
repo, cd to thewith-msw
example..env.production
file (we don't want to mock in production and want to drop the../mocks
require)npm install
Reproduce the bug
This build will (erroneously) include msw in the client bundle:
next build
Build expected bundle
This build is what I would actually expect
next
to build. It does not include msw in the client bundle. We're simulating the dead code elimination here by completely removing therequire
call.with-msw
example's./pages/_app.js
(so remove the conditional msw require)next build
So as you see, the bundle is a lot smaller. The only change was us removing the conditional msw require. So it seems that
next build
isn't eliminating the conditional require from the client bundle. This applies to more than just thewith-msw
example, but that example is an easy way to reproduce this.I would expect
next build
(and webpack 5) to drop msw from the client bundle when the require is contained in an if block that evaluates to false during build time.The text was updated successfully, but these errors were encountered: