Closed Kerryliu closed 1 week ago
Start a new pull request in StackBlitz Codeflow.
import("./dynamic-HASH.js");
is relative to the file containing that statement. As long as you are serving the file importing the other file and the imported file on the same origin, it won't be an issue.
The problem is the imported file is not on the same origin.
For example, I am hosting my library at www.someWebsite.com/main.js
, and the imported file is at www.someWebsite.com/dynamic-HASH.js
.
If the library is consumed via www.anotherWebsite.com
, this is a different origin, so the import("./dynamic-HASH.js");
would fail, as it would try to grab it from www.anotherWebsite.com/dynamic-HASH.js
.
Do you have the file importing dynamic-HASH.js
on a different origin from dynamic-HASH.js
? For example, www.someWebsite.com/main.js
imports www.anotherWebsite.com/dynamic-HASH.js
.
In that case, you should make dynamic-HASH.js
an entry point instead of a implicit chunk generated by dynamic import, otherwise the entry signature can be changed by rollup (some export might be removed if main.js
doesn't use it).
If you have main.js
and dynamic-HASH.js
served on the same origin and importing main.js
from a different origin, then import
can work with relative path. It will be resolved from main.js
and not the location.href
/document.baseURI
.
If you have main.js and dynamic-HASH.js served on the same origin and importing main.js from a different origin, then import can work with relative path. It will be resolved from main.js and not the location.href/document.baseURI.
Interesting, you are correct, I tested this and this does seem to be the case. I vaguely remember I had problems with webpack around a similar issue (maybe it handles dynamic imports differently?), so that might be why I assumed the latter to be true.
Thanks for the help/clarification!
Describe the bug
I'm working on a project where we will be building a library to be hosted on a CDN. Currently, I'm struggling with an issue where dynamic imports are not working as expected with the
base
public path option.What I'm expecting to happen is that when this option is set in my config: (E.g.:
base: www.somepath.com
), all dynamic imports should also have this path appended to them after being built -import('./dynamic.js');
would be comeimport('www.somepath.com/dynamic.js');
However, this does not seem to currently be the case and the dynamic imports are still using relative paths. If this is not a bug, and I'm misusing this option and/or is there an alternative approach that I should be taking?
Reproduction
https://stackblitz.com/edit/vitejs-vite-bdc46m?file=dist%2Fmain.js
Steps to reproduce
npm run build
main.js
in thedist
folder.import("./dynamic-HASH.js");
does not contain thebase
path specified invite.config.ts
System Info