-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Defining global: 'globalThis'
variable will replace global.css
as "globalThis".css
on building
#4796
Comments
Oh, it's |
global: 'globalThis'
variable will replace global.css
as globalThis.css
on buildingglobal: 'globalThis'
variable will replace global.css
as "globalThis".css
on building
try
|
@OneNail I'm trying to define It is a very common usage for a bundler. |
No, it's not common. Have you ever seen webpack recommend users to use Should use |
It's a bad practice to modify
Using
|
See also inspect-js/has-symbols#11 (comment) and browserify/node-util#62 (comment) I'm raising PRs for many related packages to use But, |
Polyfills do this all the time.
"is fine" != "recommended". Webpack implemented a dedicated plugin for the
Yeah, but not for this use case. |
Then, this feature is missing in vite. |
Feel free to write a plugin for this. Should be very straightforward. Or you can check out https://github.com/snowpackjs/rollup-plugin-polyfill-node I'm not sure if it works though. |
Yeah, I'm going to write a plugin maybe
This one won't work on vite development, at least. |
A node module bundler that does not provide node globals is broken; it's unfortunate vite won't try to function correctly. |
I thought there was a consensus that bundlers should not try to shim Node.js built-ins. It's bad for the ecosystem. Why rely on a runtime-specific global variable instead of the language built-in? |
There is no such consensus. Webpack 5 made this disruptive change, and package maintainers find it very hostile and disruptive: https://blog.sindresorhus.com/webpack-5-headache-b6ac24973bf1 CJS modules, which are the majority of npm and will remain so for the foreseeable future, are already node-specific - if you take them as input, you have already decided to treat node as special, and CJS modules can not function properly unless they run in a node-like environment, which includes all the builtins. |
From the exact article you linked:
The timing is different now. With Sindre Sorhus himself aggressively publishing ESM-only packages, and with the ESM-centric workflow that Vite is pushing forward, the decision should make sense. On the other hand, CJS, as a runtime-agnostic module specicifcatiton, exists before Node.js. Vite supports the CJS module system, not CJS Node modules. It doesn't treat Node as special. |
The timing isn't actually different now; when a package goes ESM-only, what happens is that everyone stops using it in favor of a less hostile package. Either way, an ESM node module still expects node globals and core modules, so that has no relevance to the issue at hand. CommonJS existed before node.js, but that version of it is long dead, far more obsolete than even AMD. The only CJS that matters now (and for the better part of a decade) is node's module format. Basically none of the packages on npm are CommonJS modules - virtually all of them are node CJS modules. Node is special, and will always be, and you only hurt users when you pretend it's not. |
I respectfully disagree. Those packages runs in browsers but still expects Node.js built-ins are more like "webpack (<= 4) compatible CJS modules" than Node.js CJS modules. Please note that the webpack's polyfills were neither complete (some were empty, some were impossible to implement on the browser side), nor up-to-date (some dependencies of https://github.com/webpack/node-libs-browser were several versions behind the latest), sometimes ambiguous or even incorrect (the result of Adding these polyfills into the browser bundle by default not only bloats the bundle, but also introduces potential bugs. This hurts the ecosystem more than leaving it to the users. |
I think another point is that vite does not provide a polyfill functionality like |
For the remaining shims in webpack 5:
Even if you don't feel like polluting the global namespace, a custom plugin won't take much more effort. (I would usually avoid global variables, too. But in the |
They’re not ambiguous whatsoever - they refer to the file they’re in. The filesystem is an inextricable part of file-based modules. It’s not a global namespace - node modules run in a smaller scope than that, and require is precisely the same as |
What should it be relative to? The input file? The output file? Or should it be left untouched because it might be executed in an SSR context too?
CJS spec is well documented, well tested, and widely used. Vite can support it, even if imperfectly. Because the edge cases are predictable and understandable. But Node.js APIs in browser? Not so much. |
Literally nobody uses the CommonJS spec - everyone is using "node's CJS modules", again for the better part of a decade. node APIs in a browser have been used by browserify and thus webpack (prior to v5, and perhaps v4) for also the better part of a decade, and a great many packages do rely on it. It's a reasonable expectation because that's how it's always been. Perhaps this wasn't a good choice initially! That doesn't change that it is the way it is, and what webpack 5 and friends are doing right now is trying to fight "what is" in a disruptive and hostile way in an effort to force an ecosystem they prefer. Please don't join in that disruptive effort. |
@ljharb the mainstream tooling authors have reached the consensus that Node is no longer special - with Deno and worker You may disagree with that and that's fine - but your opinion really doesn't matter here, because the ecosystem moves forward as a result of the natural selection process of its users. If not supporting Node built-ins is a mistake, surely fewer users would use tools that don't support them (including Vite and webpack 5), and those tools should die out in the long run. If users turn out to still opt for these tools, then it means to them the utility these tools provide are more important than the occasional minor inconvenience, and in turn they would request the package authors to properly ship universal packages (instead of relying on Node APIs as if it is available in all JS environments). That's how we shed the collective technical debt and move the ecosystem forward, and it's 100% an intentional decision in Vite's design to push it towards that direction. |
Describe the bug
As title
Reproduction
https://github.com/JounQin/test/tree/vite_define
System Info
Used Package Manager
yarn
Logs
Validations
The text was updated successfully, but these errors were encountered: