-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Code which relies on checking require
at runtime to determine the env (node/browser) is not working with esbuild
#1966
Comments
@thdxr Thank you for your reply, but #1944 doesn't seem like the same issue, issue #1944 is more about cjs to esm converting in the output which I believe esbuild does't support yet. My point here is: creating a wrapper of |
This is by design because esbuild takes modules in CommonJS format as input, and the CommonJS format has the following invariants:
It doesn't matter if the code is running in the browser or node or somewhere else. Code in the CommonJS format should be able to rely on the invariants that come with the CommonJS format. I recommend testing whether code is running in the browser or not using something like |
Thanks, @evanw. Unfortunately, If I understood correctly from what you say, code like Does it make sense to only replace the |
You can still have a workaround to the name: 'fix-uniqid',
setup({ onLoad }) {
const fs = require('fs')
onLoad({ filter: /\buniqid[\\\/]index\.js$/ }, async args => {
let code = await fs.promises.readFile(args.path, 'utf8')
code = code.replace(
/if\s*\(typeof\s+__webpack_require__\b[^]+\/\/\s*Exports/m,
'/* erased */'
)
return { contents: code }
})
} By the way, the correct way to export a cross-node/browser package now is using the |
I've gotten around this by using In "browser": {
"os": "os-browserify",
"stream": "stream-browserify"
}, |
@hyrious Thanks for the tips, but actually I'm creating an ESM dev server for unbundled development with esbuild. So I don't want to go with anything too specific. For packages with I know esbuild is not designed to be compatible with all the conventions/rules in the JS ecosystem(which I really appreciate a lot). But just does not feel all right in this case which changed the code behavior at runtime. |
No, I don't think so. That would introduce inconsistency into the code and means the (function (dirtyChai) {
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
module.exports = dirtyChai;
} else if (typeof define === 'function' && define.amd) {
define(function () {
return dirtyChai;
});
} else {
chai.use(dirtyChai);
}
}(function dirtyChai(chai, util) {
...
})); Due to esbuild previously doing exactly what you described (leaving |
Ok, thanks for the context. So the old way breaks the conditional exports and the current way doesn't work well with the conditional require. I tested how webpack deals with |
I'm not sure if this is esbuild's expected behavior or not, I've checked several other similar issues, but none answered my doubts.
The issue is when I bundle this package:
uniqid
, it has code like this to conditionally require node builtin modulesWith
esbuild
:Getting the runtime error:
This is the build output, esbuild created a
__require
function for runtime:My doubt is what's the intention of creating a "__require" function for runtime? It seems it's a wrapper to output a nice error message when "require" is not exist, is it?
The text was updated successfully, but these errors were encountered: