Skip to content
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

Dynamic import(): how to ignore an import that’s passed a variable? #574

Closed
iamakulov opened this issue Dec 3, 2020 · 2 comments
Closed

Comments

@iamakulov
Copy link

In our codebase, there’re a few cases where we deliberately use a native import() with a variable, like:

for (const moduleName of moduleNames) {
    const evaluationResult = await import(/* webpackIgnore: true */ moduleName)
    this.evaluatedModules[moduleName] = evaluationResult
}

This works in webpack thanks to /* webpackIgnore: true */ (docs). But in ESBuild, this produces a warning like:

xxx/xxx.ts:241:47: warning: This dynamic import will not be bundled because the argument is not a string literal

Is there any way to silence this warning for such import? If this was a string literal, I’d add it into externals, but I can’t do that with a variable.

@evanw
Copy link
Owner

evanw commented Dec 4, 2020

This is a general problem with warnings in esbuild. Currently there isn't a built-in way to filter specific warnings. Some options:

  1. You could use --log-level=error to silence all warnings.

  2. You could filter warnings yourself using the API.

  3. I currently skip similar warnings for require() when inside a try block, since presumably that is evidence that the code is anticipating possible errors and can handle them correctly. I should probably do that for this await import() pattern too.

  4. I could just remove this warning entirely. Arguably import() returns a promise that could be rejected, so code that uses it should be handling that failure anyway. However, this is likely to cause confusion when esbuild silently succeeds but the resulting bundle doesn't work. Also anecdotally I have never seen real code that uses import() and actually handles errors, so these failures would likely be silently ignored. This doesn't seem great.

  5. I could keep the warning. Warnings exist to point out potential issues and don't cause the build to fail. Arguably this warning is working correctly.

@iamakulov
Copy link
Author

Thank you, that makes sense! I agree that this warning seems totally reasonable (and FWIW the code isn’t handling import() errors in that case properly either). I’m going to try 1 or 2, then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants