-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix: avoid replacing defines and NODE_ENV in optimized deps (fix #8593) #8606
Conversation
esbuild's This might be the reason of the failing test. |
dd68152#diff-456b3c61f2f19b160b6a5016cb9a180500c4e3b3a9a41a192ca24670637e7706R475-R487 const define: Record<string, string> = {
'process.env.NODE_ENV': JSON.stringify(
process.env.NODE_ENV || config.mode
)
}
if (!isBuild) {
// We only use define for dev optimized deps. During build we let the define keys
// unchanged as the define plugin will process them
for (const key in config.define) {
const value = config.define[key]
define[key] = typeof value === 'string' ? value : JSON.stringify(value)
}
} the tests passed. |
BTW What is the purpose of using esbuild's define during dev optimization? I was curious since define plugin does not run during dev. |
The define plugins don't run during dev as an optimization, and we have the defines as global variables in the client env directly: vite/packages/vite/src/client/env.ts Line 17 in 7157b15
So, during the regular dev I agree that they aren't needed since we have the globals. The defines where added in this commit: But I don't understand why we are doing it, before optimized deps weren't used during SSR dev, and now the define plugin still does a replacement: vite/packages/vite/src/node/plugins/define.ts Line 119 in 7157b15
Maybe we should directly remove the defines during esbuild optimization? |
…emove user define handling from optimize deps
Thanks for this pointer, this was indeed the problem. It also showed that we were missing changing the platform to In lib mode, we are keeping The user defines have now been removed from optimize deps, I think they aren't actually needed at the optimize deps level. |
Because we are handling package resolution and externalization, maybe we could use
I think so, too. This will fix #8526 (It was closed but I was wondering if I could do a better handling). |
Agreed, I thought of this but wanted to avoid doing further changes to the way we optimize during dev but I think you are right here, let test this now. This is another piece where we are removing differences between dev and build. And there is no perf regression, esbuild is just bundling and not applying tree-shaking at this point. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! @sapphi-red brought up a good point about define
in optimized deps in define
, and I think it's safe that we don't do that too (for reasons above).
I think this should be good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🚀
@patak-dev @sapphi-red @bluwy Removing the esbuild defines during esbuild optimization bring problems at dynamic imports case: if (FRAMEWORK === 'preact') {
const options = require('preact').options
// ...
} For example, when user are using In Vite2, vite' define options pass to esbuild' define too.esbuild will had dead code eliminated and everything ok: // Vite2 output
if (false) {
const options = null.options
} However, after removing esbuild' define. Vite only define // Vite3 output
if (FRAMEWORK === 'preact') {
const options = require('preact').options
// ...
} Besides, Webpack's output: if (false) {} And maybe #5676 related. |
@Chen-jj Thanks for the explanation! I think that makes sense though it feels a bit off to have custom user defines intercept dependency code for treeshaking. Nonetheless I think it wouldn't hurt to re-add this, can you create a feature request so we can keep track of this? |
Description
When we use
define
for build optimized deps and there is CJS interop, the defined keys end up being used as part of the interop. In the define playground, it looks like:So if we don't skip these files in the define plugin, then the keys are re-replaced and there is an error.
We have two ways to fix this:
cons:
pro:
cons:
Pushing so we can continue the discussion here
What is the purpose of this pull request?