Skip to content

Instantly share code, notes, and snippets.

@edivados
Created November 3, 2023 10:21
Show Gist options
  • Save edivados/f58477f7e6a9398ce18033a207b9b9b0 to your computer and use it in GitHub Desktop.
Save edivados/f58477f7e6a9398ce18033a207b9b9b0 to your computer and use it in GitHub Desktop.

Using absolute paths and vite.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment