-
Notifications
You must be signed in to change notification settings - Fork 1.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
false positive: use of require.resolve causes unexpected warning (v0.7.4) #410
Comments
I think most bundlers will statically evaluate Perhaps it should be changed to something like:
That said, perhaps a better solution would be to just implement static evaluation of |
Interesting. I'm surprised to learn that! I'd argue that from an idealistic standpoint, evaluating Personally I'd like to see only deterministic function results evaluated & inlined by a compiler. For example |
I think this behavior originated in browserify and is also present in webpack. Because But even for Node.js bundles, it is generally desirable to replace If you want At least for webpack, for runtime |
So esbuild doesn't really have a robust solution for bundling projects with dynamic import paths. Some other bundlers such as Webpack have a complex virtual file system that attempts to include files in the bundle that you may be trying to import and then resolve the paths to the bundled files at run time. In that context statically evaluating Because esbuild doesn't handle these cases, I think these warnings about unusual uses of If the warnings are an aesthetic problem, they can currently either be disabled completely via
This is an interesting proposal. I think that sounds reasonable to me. It's consistent with how |
I tried flagging the files required and resolved as external but it seems that only "named" imports (as in names of node_modules) are supported. Here's my scenario:
Snippet: product file layout package.json
dist/estrella.js <-- CLI program
dist/debug.js <-- 40kB of code loaded as needed Snippet: Loading additional code just-in-time Error.prepareStackTrace = (error, stack) => {
const debug = require(Path.join(__dirname, "debug.js"))
return debug.prepareStackTrace(error, stack)
} Snippet: Getting the esbuild version function getEsbuildVersion() {
try {
return JSON.parse(resolveModulePackageFile("esbuild")).version
} catch (_) {
return "(unknown)"
}
}
function resolveModulePackageFile(moduleSpec) {
const mainfile = require.resolve(moduleSpec)
let dir = Path.dirname(Path.resolve(mainfile))
let lastdir = Path.sep
while (dir != lastdir) {
let pfile = Path.join(dir, "package.json")
if (fs.existsSync(pfile)) {
return pfile
}
dir = Path.dirname(dir)
}
throw new Error(`package.json not found for module ${moduleSpec}`)
} An expensive work-around would be to make each product file its own separate node module, flag those as external and import them with e.g. For now I've found a good work-around that avoids the warning: const runtimeRequire = eval("require")
// replace all uses of require with runtimeRequire Thanks Evan! |
@rtsao So what are we supposed to do when require.resolveing a relative file like a web worker that isn't in node_modules? How do I mark that as external? |
I appreciate the helpful warning messages esbuild emits when it detects that I've done something janky. However it seems that the sophistication of detecting use or require could use some improvement, in particular with the
require.resolve
function.Repro:
main.js
For a program like Estrella it is sometimes useful to poke around for node modules without using their code, for example to read package.json files.
The text was updated successfully, but these errors were encountered: