Replies: 1 comment 2 replies
-
Unfortunately this is not statically analyzable, there's no way to determine whether or not that module exists at build time as it relies on a runtime variable ( You can build your own logic out pretty easily though with a simple plugin: function myPlugin() {
return {
name: 'my-plugin',
transform(code, id) {
const dynamicImportRegex = /import\(`([^`]+)`\)/g;
const matches = code.matchAll(dynamicImportRegex);
for (const match of matches || []) {
// Check if `match[1]` exists on disk
// ...
}
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How do I ensure that dynamic imports exist at runtime?
Case in point, I'm dynamically importing SQL migrations in a for loop.
When they exist, I see lines like:
However, if they don't exist, I see lines like:
Now, since this is super critical at startup, a missing file breaks the app. How can I detect this missing at build time? Vite must be able to do this, as it's checking for the file's existance, as seen from the lines. But it's failing silently, without any warning, not even with --debug mode.
What can I do to simply fail the build process, instead of silently accepting that missing file?
This is my dynamic import part:
Beta Was this translation helpful? Give feedback.
All reactions