-
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
Output a warning when encountering __dirname
#1874
Comments
Well, I guess this is related to #1155 |
This is by far the safest option because there are many other aspects of node's API other than If you're looking for more of a "hands on" approach, then there are various ways of doing this. One way could be to use something like Another way to do this is to try to write a plugin. One approach is shown below. It's a little hacky but you can detect whether or not an access for any given global is present or not by using the inject feature to replace it with something and then using the metafile feature to check and see if the replacement happened: const noDirnamePlugin = {
name: 'no-__dirname',
setup(build) {
const path = require('path')
const injected = path.join(require('os').tmpdir(), 'inject-__dirname.js')
require('fs').writeFileSync(injected, 'export let __dirname')
const options = build.initialOptions
options.inject ??= []
options.inject.push(injected)
options.metafile = true
build.onEnd(result => {
const metafile = result.metafile
if (metafile)
for (const output in metafile.outputs)
for (const input in metafile.outputs[output].inputs)
if (path.basename(input) === path.basename(injected))
throw new Error(`Using "__dirname" is not allowed`)
})
},
} This will fail the build if something uses
I don't want esbuild to become a full-blown linter, since that's a big step up in complexity. I agree with you that these kinds of things should be warnings (that's what warnings are for) and that you should just turn warnings off if you don't want to see them. But I've tried similar things in the past and lots of people complain about them, which is not something I want to deal with. So my solution is to move them to |
Is this something esbuild should be doing by default? E.g. a new Thanks for your "hands on" approaches. I would additionally need to check if these files are in
I'm on the other end of the spectrum: esbuild should straight up refuse to compile these things and tell you to I've seen multiple issues in this repo where you've commented things like "well actually this is totally valid code, see here is the spec" or "no we can't remove this unused import, because importing might have side effects, even if you don't use the import" so you seem to care about all the little details and doing things the correct way. That's why I'm a little confused why in this case it's the opposite. |
I'm reopening this as the fix for this broke an Amazon product that didn't pin the version of esbuild they were using. There's more context in #3819. |
I'm aware of #859, but this is not a request to add some sort of
__dirname
transformation, but to output a warning for the current behavior.I'm currently trying to bundle a large Electron app for faster startup time (less
require()
calls at runtime). I've run into multiple issues with modules using__dirname
. E.g. I already markedvm2
asexternal
because it loads files at runtime (#495). In the vm2 case I was lucky enough to get an error at runtime, but really this needs to be a warning when bundling. With__dirname
the bundling effectively changes the semantics of the code. I'm using Piscina to run worker jobs. Naturally I called my workerworker.js
. But after bundling a worker task would never finish and also not error. Turns out Piscina also ships with aworker.js
that loads my worker. After bundling with esbuild Piscina was loading my worker.js instead of it's own. This is a side effect that should definitely cause a warning. Right now I'm considering adding every single dependency asexternal
and only bundle my own code, because I cannot know for sure if something inside my dependency tree will use__dirname
in unexpected ways and causes funny runtime behavior. With a warning I could only make the packages external that use__dirname
. Assuming I could even easily figure out which package is the problem from the warning (it could be in a file that is a third level dependency of a dependency).I'm now doing this:
but I'm losing a lot of benefit of the bundling.
The text was updated successfully, but these errors were encountered: