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

handle dynamic imports on windows #16

Merged
merged 9 commits into from
Nov 5, 2023
Merged

Conversation

edivados
Copy link
Contributor

@edivados edivados commented Nov 1, 2023

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 uses nodeImport 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 or rollup and node never sees this.

import "C:/users/edivados/index.js";
await import("C:/users/edivados/index.js");
resolver mode sees comment
✔️ vite dev /@fs/C:/users/edivados/index.js /@fs/ gets added by vite. vite:resolve plugin removes /@fs/ and normalizes to slash. /@fs/ skips checking if the file exists.
✔️ rollup build ? valid Windows path
✔️ node runtime bundler output

✔️ Windows style path

This is ok because the import is processed by vite or rollup and node never sees this.

import "C:\\users\\edivados\\index.js";
await import("C:\\users\\edivados\\index.js");
resolver mode sees comment
✔️ vite dev /@fs/C:/users/edivados/index.js /@fs/ gets added by vite + normalization. vite:resolve plugin removes /@fs/ and normalizes to slash. /@fs/ skips checking if the file exists.
✔️ rollup build ? valid Windows path
✔️ node runtime bundler output

✔️ Leading slash (Posix style path)

import "/C:/users/edivados/index.js";
await import("/C:/users/edivados/index.js");
resolver mode sees comment
✔️ vite dev /@fs/C:/users/edivados/index.js
✔️ rollup build ? valid Windows path
✔️ node runtime bundler output

Dynamic import

✔️ Conditional

// path = C:/users/edivados/index.js
if (import.meta.env.DEV) path = posix.resolve("/@fs", path);
else path = pathToFileURL(path).href;

await import(path)
resolver mode sees comment
✔️ vite dev /@fs/C:/users/edivados/index.js vite:resolve plugin removes /@fs/ and normalizes to slash. /@fs/ skips checking if the file exists.
✔️ rollup build noop
✔️ node runtime file:///C:/users/edivados/index.js

✔️ Leading slash (Posix style path)

// path = C:/users/edivados/index.js
path = posix.resolve("/", path);
await import(path);
resolver mode sees comment
✔️ vite dev /C:/users/edivados/index.js vite:resolve plugin removes the leading slash, calls resolve(root, path) and also checks if the file exists.
✔️ rollup build noop
✔️ node runtime file:///C:/users/edivados/index.js ok?

❌ Posix style path

// path = C:/users/edivados/index.js
await import(path)
resolver mode sees comment
vite dev C:/users/edivados/index.js uses nodeImport and fails
✔️ rollup build noop
node runtime C:/users/edivados/index.js has to be a file url

❌ Windows style path

// path = C:\\users\\edivados\\index.js
await import(path)
resolver mode sees comment
vite dev C:\users\edivados\index.js uses nodeImport and fails
✔️ rollup build noop
node runtime C:\users\edivados\index.js has to be a file url

Copy link

codesandbox bot commented Nov 1, 2023

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@nksaraf
Copy link
Owner

nksaraf commented Nov 4, 2023

Thanks for the test!! Its going to be very helpful. I have been meaning to add more tests for things like this.

Do you have a fix for this? Open to anything. I am usually out of my league with this mac/windows file path stuff.

@edivados edivados marked this pull request as ready for review November 4, 2023 15:32
@nksaraf nksaraf merged commit df053f2 into nksaraf:main Nov 5, 2023
3 checks passed
@edivados edivados deleted the dynimport-win branch November 5, 2023 09:54
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

Successfully merging this pull request may close these issues.

2 participants