Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Added two failing tests using server functions to reproduce the issue with dynamic imports on Windows.
Descirption
An absolute file path like
C:/users/edivados/index.js
does not work with dyamic imports in Vite (dev) and Node (runtime).Production
File urls should be used instead of absolute paths.
Development
If the path does not start with
.
or/
vite usesnodeImport
which fails on absolute paths.https://github.com/vitejs/vite/blob/385d580a7df67570b8014318a607f62fe15eaef9/packages/vite/src/node/ssr/ssrModuleLoader.ts#L158-L164
Based on what ssrImport does this works.
import(posix.resolve(/@fs, path))
import(posix.resolve('/', path))
import(decodeURIComponent(pathToFileURL(path).pathname))
Not using pathe because it does not add the leading slash on Windows.
Static and Dynamic import behavior on Windows
Static import
✔️ Posix style path
This is ok because the import is processed by
vite
orrollup
and node never sees this./@fs/
gets added by vite.vite:resolve
plugin removes /@fs/ and normalizes to slash. /@fs/ skips checking if the file exists.✔️ Windows style path
This is ok because the import is processed by
vite
orrollup
and node never sees this./@fs/
gets added by vite + normalization.vite:resolve plugin
removes /@fs/ and normalizes to slash. /@fs/ skips checking if the file exists.✔️ Leading slash (Posix style path)
Dynamic import
✔️ Conditional
vite:resolve
plugin removes/@fs/
and normalizes to slash./@fs/
skips checking if the file exists.✔️ Leading slash (Posix style path)
vite:resolve
plugin removes the leading slash, calls resolve(root, path) and also checks if the file exists.❌ Posix style path
nodeImport
and fails❌ Windows style path
nodeImport
and fails