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

是否支持 new URL('xxx.wasm',import.meta.url) 方法,目前测试是无效 #83

Closed
chenli1989 opened this issue Apr 24, 2024 · 15 comments
Assignees

Comments

@chenli1989
Copy link

chenli1989 commented Apr 24, 2024

vite.config.ts 文件配置是以下:编译结果还是转为 base64 内联,是否支持wasm文件?还是哪里配置不正确?

libAssetsPlugin({
  include: ['**/*.wasm' **/*.svg],
}),

代码是

// 无效
new URL('demo.wasm', import.meta.url) 

// 无效
new URL('demo.svg', import.meta.url)

编译结果还是 base64

// wasm
new URL('data:application/wasm;base64,xxxxxxx')

// svg
new URL("data:image/svg+xml,%3csvg%20width=xx')
@chenli1989 chenli1989 changed the title 是否支持 wasm 后缀的静态资源文件?目前测试是无效 是否支持 wasm 后缀的静态资源文件?new URL('xxx.wasm',import.meta.url) 测试无效 Apr 24, 2024
@chenli1989 chenli1989 changed the title 是否支持 wasm 后缀的静态资源文件?new URL('xxx.wasm',import.meta.url) 测试无效 是否支持 new URL('xxx.wasm',import.meta.url) 方法,目前测试是无效 Apr 24, 2024
@coder-layne
Copy link
Contributor

之前没有遇到过 new URL 的情况,我需要了解下

@coder-layne coder-layne self-assigned this Apr 28, 2024
@coder-layne
Copy link
Contributor

@chenli1989 new URL('xxx.wasm', import.meta.url) 形式的资源引用是由 Vite 内置插件处理的,lib-assets 也可以采用相同的方式获取到被引入的资源,然后将 Vite 生成的 base64 内容替换回资源的地址,但我还需要对这种形式使用 URL 进行一些调研,避免破坏已有的代码。

@DenizUgur
Copy link

I would like to chime in here as well. I hope translation was correct and I didn't miss anything but using the following syntax is a good workaround:

instead of

new URL("./demo.wasm", import.meta.url).href

use

import demoURL from "./demo.wasm?url"

This solved the base64 encoding issue but I get the following warning(s) during build

No name was provided for external module "./assets/demo-991e9643.wasm" in "output.globals" – guessing "demoURL".

Not sure if this is something to worry about though. Any comments @coder-layne?

@coder-layne
Copy link
Contributor

@DenizUgur I'm not quite sure what the difference is between URL and import in terms of usage scenarios, and I also welcome solving @chenli1989 's issues in this way if it fits his scenario.

This warning occurs because the plugin emits assets to a specified directory and then marks them as external. If your output is in umd format, you need to provide variable names to access these assets from the globalThis. If you want your package users to access these assets through addresses provided by you, you can offer a publicUrl option. Otherwise, you should provide umd or cjs format outputs.

@DenizUgur
Copy link

Thank you @coder-layne for your reply. I get these warnings for a es build though. What would happen in that case? Do I need to do anything specific?

@coder-layne
Copy link
Contributor

@DenizUgur I am using the build configuration in the project playground, and I did not encounter the warning you mentioned when outputting the es format outputs. Can you provide a minimal reproducible version?

@DenizUgur
Copy link

DenizUgur commented May 7, 2024

It was difficult to reproduce but here you go: codesandbox link. Run nx build test-library after you open the devbox.

@coder-layne
Copy link
Contributor

@DenizUgur The plugin will externalize files that meet the inclusion criteria, and Rollup will attempt to acquire the global variable names of external modules when outputting in the iife and umd formats. If this fails, a warning will be output. So, will this warning appear when the output format is set to es? This is because we used worker mode, where the default format is iife.

@coder-layne
Copy link
Contributor

@DenizUgur Additionally, the plugin is used to handle asset files. It treats the content of files as strings, which may cause problems when dealing with scripts.

@DenizUgur
Copy link

So, will this warning appear when the output format is set to es?

No warnings, that was it. Thank you for pointing that out.

It treats the content of files as strings, which may cause problems when dealing with scripts.

I'm using the syntax I've mentioned above to get the asset's public URL so I'm not interested with the content as well.

@coder-layne
Copy link
Contributor

@DenizUgur Glad that the plugin fits your scenario :)

@P422L
Copy link

P422L commented Jan 21, 2025

@coder-layne Does the plugin work for this syntax?

new URL('demo.wasm', import.meta.url) 

In my case I'm not getting it to work by configuring the plugin and I don't have the option to change the syntax to the suggested format

import demoURL from "./demo.wasm?url"

because it's in an npn package I'm using and don't have control over.

@coder-layne coder-layne reopened this Jan 22, 2025
@coder-layne
Copy link
Contributor

@P422L Thank you for your feedback. It seems necessary to reconsider the initial plan.

@chenli1989 new URL('xxx.wasm', import.meta.url) 形式的资源引用是由 Vite 内置插件处理的,lib-assets 也可以采用相同的方式获取到被引入的资源,然后将 Vite 生成的 base64 内容替换回资源的地址,但我还需要对这种形式使用 URL 进行一些调研,避免破坏已有的代码。

@P422L
Copy link

P422L commented Jan 22, 2025

It would be awesome having the plugin supporting those imports as they've become vites recommended way of dynamically importing.

@coder-layne
Copy link
Contributor

@P422L Just released v1.1.0 under the next tag. Passed basic local tests—let me know if it fixes your issue :)

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

No branches or pull requests

4 participants