-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
new URL(foo, import.meta.url)
doesn't work when dependency was optimized
#8427
Comments
|
An ugly practice is move |
I updated some of my npm packages to a later version and it copied the wasm file for me too on build. 😸 |
This comment was marked as spam.
This comment was marked as spam.
new URL(foo, import.meta.url)
doesn't work when dependency was optimized
Is this the same issue on esbuild? In short: Input// file at ./foo/bar.js
export default import.meta.url; // should be /full/foo/bar.js // file at ./index.js
import url from './foo/bar.js';
console.log(url); // should still be /full/foo/bar.js Output// file at ./dist/index.js
const barDefaultExport = import.meta.url;
console.log(barDefaultExport); which prints |
Confirming that I have this problem with importing https://github.com/rive-app/rive-wasm. They also import their wasm file from unpkg which might also be another curve ball. I tried the workaround above with |
Leaving some notes about the current status of this issue.
|
Got a similar issue: const template = new URL('./some.txt', import.meta.url)
return readFileSync(template, 'utf-8') Results in const s = new URL("data:text/plain;base64,...", self.location);
return g(s, "utf-8"); With the error |
We just ran into this issue where we convert absolute paths into relative ones for consistent snapshots in Vitest. for anyone interested, you can use this hack to mimic the transform with the following setup code: // correctly resolve assets referenced in optimized dependencies
// https://github.com/vitejs/vite/issues/8427
const transformUrlBase = (base?: string): typeof base => {
const { config } = (globalThis as any).__vitest_worker__;
const cacheFileUrl = pathToFileURL(
path.join(config.root, config.deps.cacheDir),
);
if (base?.toString().startsWith(cacheFileUrl.href)) {
return new URL('/@fs/node_modules/', location.href).href;
}
return base;
};
const URL = globalThis.URL;
globalThis.URL = class VitestURL extends URL {
constructor(url: string, base?: string) {
super(url, transformUrlBase(base));
}
} as typeof URL; |
I just ran in to this myself, with a deeply transitive library trying to use WASM |
Hopefully this will be fixed soon, this issue is unique to Vite and doesn't happen with more modern bundler like Rsbuild. |
I doubt they'll fix it soon, as they're transitioning entirely to rolldown instead of using esbuild + rollup like Vite currently does. Could you share your experience with rsbuild, along with your configuration and any necessary changes? |
is that the vite 6 beta that uses rolldown? |
I think so rolldown#43 |
I mean, I assume the transition to rolldown will force this bug to be fixed :p Cuz this bug, if still happen will happen to both dev + prod. Probably not the right place for this, but the transition to Rsbuild was great, faster, no bug and sometime less plugin/setup required. |
@tien I think the solution might be to remove it from your |
### Description After some investigation and debug, I decided to follow the recommended workaround as suggested in vitejs/vite#8427. ### Motivation and Context There is a known issue with Vite 5.x when using WebAssembly package. Detail information is in vitejs/vite#8427. There are previous attempts to fix this problem (#23487). I tried various ways to make it working out of the box for Vite users but none of them worked: Some "fixes" did fix the usage of Vite but broke other use case/bundler and some introduced other issues. Eventually I figured out that there is no good way to fix this inside ONNX Runtime. Considering the root cause is inside Vite and it may be fixed in Vite v6. I think now the best way is to follow the recommended workaround.
### Description After some investigation and debug, I decided to follow the recommended workaround as suggested in vitejs/vite#8427. ### Motivation and Context There is a known issue with Vite 5.x when using WebAssembly package. Detail information is in vitejs/vite#8427. There are previous attempts to fix this problem (#23487). I tried various ways to make it working out of the box for Vite users but none of them worked: Some "fixes" did fix the usage of Vite but broke other use case/bundler and some introduced other issues. Eventually I figured out that there is no good way to fix this inside ONNX Runtime. Considering the root cause is inside Vite and it may be fixed in Vite v6. I think now the best way is to follow the recommended workaround.
Describe the bug
Now, i use wasm-pack for build WASM file and WASM with JavaScript glue layer like this。
The glue layer code contains below code
In Vite, after esbuild optimize third party,
import.meta.url
cannot be optimize succeed.Ref Vite Docs, even if set
build.target
toes2020
is still the problemAfter preprocess
node_modules/color-thief-wasm-web/color_thief_wasm.js
will be transform tonode_modules/.vite/deps/color_thief_wasm.js
which load.wasm
file byimport.meta.url
receive incorrect path isnode_modules/.vite/deps/xxx.wasm
finally, the correct path isnode_modules/color-thief-wasm-web/xxx.wasm
One of the solutions is set
optimizeDeps.exclude
to exclude third party module like thisReproduction
https://stackblitz.com/edit/vite-rkrfzu?file=src%2FApp.vue,vite.config.js
System Info
Used Package Manager
npm
Logs
Failed to construct 'URL': Invalid URL
Validations
The text was updated successfully, but these errors were encountered: