-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Alias from an absolute path to package name on Windows #17897
Comments
plugin-alias only matches Even if we changed that to match I'm not sure if there's a way that would benefit generally. |
I see. Thanks for the explanation. I understand the complication, however, do you think it's possible to have a unified intermediate format inside Vite for alias matching or other id handling processes? such like if
That would be very helpful in cases Vite accepts absolute paths on different OSs. Thanks again. |
Would you explain why you think this feature should be builtin? (as I guess it's possible by putting multiple alias entries and the usecase feels like an edge case) Also why do you need to alias IDs containing input id --(resolve 1)--> resolved id --(transform to url safe id)--> IDs with It would be helpful too if you can explain when each IDs happen as I don't know when it happens. Especially, the |
Sure. In short the format To explain the whole original program on posix-based system first, some keypoints:
So far it works very well on posix-based ones like macOS. (I can simplify it into another reproduction if necessary.) Then when things come to Windows, we've found in Vite dev server:
That's all about it. I agree with you that Thanks so much. |
A reproduction would help me understand the problem.
This code is incorrect. It needs to be |
I see. Back to the first issue, I tried e.g. I understand according to the existing docs and issues And I will prepare another reproduction for the whole program later. 🙏 |
I think this is a bug (made an issue: #17910) |
@sapphi-red here is another reproduction for the minimized program: https://github.com/Jinjiang/reproductions/tree/vite-windows-path-20240821 You can consider |
I took a look at your repro. This change worked for me. diff --git a/debug/init.mjs b/debug/init.mjs
index 5c3c78b..bc49c26 100644
--- a/debug/init.mjs
+++ b/debug/init.mjs
@@ -1,8 +1,9 @@
import { writeFileSync } from 'fs';
+import path from 'path';
import { files } from './data.mjs';
-writeFileSync(files.index, `<script type="module" src="${files.main}"></script>`)
-writeFileSync(files.main, `import p from '${files.preview}'\nimport l from '${files.local}'\n\nconsole.log(p)\nconsole.log(l)`)
-writeFileSync(files.local, `import foo from '${files.foo}'\n\nconsole.log(foo)\n\nexport default 'local'`)
+writeFileSync(files.index, `<script type="module" src="${path.basename(files.main)}"></script>`) // workaround https://github.com/vitejs/vite/issues/17910
+writeFileSync(files.main, `import p from '${files.preview.replace(/\\/g, '/')}'\nimport l from '${files.local.replace(/\\/g, '/')}'\n\nconsole.log(p)\nconsole.log(l)`)
+writeFileSync(files.local, `import foo from '${files.foo.replace(/\\/g, '/')}'\n\nconsole.log(foo)\n\nexport default 'local'`)
writeFileSync(files.preview, `module.exports = 'preview'`)
writeFileSync(files.foo, `module.exports = 'foo'`)
diff --git a/debug/run.mjs b/debug/run.mjs
index c6c075e..08271a4 100644
--- a/debug/run.mjs
+++ b/debug/run.mjs
@@ -5,8 +5,8 @@ import { createServer } from 'vite'
import { __dirname1, files } from './data.mjs';
const alias = [
- { find: dirname(files.preview), replacement: 'preview' },
- { find: dirname(files.local), replacement: 'local' },
+ { find: dirname(files.preview).replace(/\\/g, '/'), replacement: 'preview' },
+ { find: dirname(files.local).replace(/\\/g, '/'), replacement: 'local' },
]
// console.log(alias)
|
I see. It works. Thanks. So may I understand the point is always converting |
Yes. I guess you can make it work with |
Understood. Thanks so much. Case closed to me. |
Describe the bug
The background is we are handling some entries or imports in absolute paths. Some of the paths are actually a package in node_modules. So we'd like to set alias to convert those absolute path imports into package imports.
It works perfectly on posix-based systems. However, we've found on Windows, some of the cases not working.
The following files are all CommonJS. So they won't work they couldn't be found or couldn't be recognized as a package to optimize.
/C:/xxx/node_modules/foo
with{ find: "/C:/xxx/node_modules/foo" }
works/@fs/C:/xxx/node_modules/foo
with{ find: "/@fs/C:/xxx/node_modules/foo" }
worksC:\xxx\node_modules\foo
with{ find: "C:\xxx\node_modules\foo" }
doesn't work/@fs/C:\xxx\node_modules\foo
with{ find: "/@fs/C:\xxx\node_modules\foo" }
doesn't workUnfortunately, we have a lot existing imports in format 3. On Windows they are all failed to run.
Is there any chances we can support it?
Thanks.
Reproduction
https://github.com/Jinjiang/reproductions/tree/vite-windows-path-20240819
Steps to reproduce
Then you will find:
System Info
Used Package Manager
pnpm
Logs
Validations
The text was updated successfully, but these errors were encountered: