包装WASM文件中的Angular自定义元素捆绑包
我正在使用使用WASM库的Angular V14创建一个自定义元素捆绑包(my-Elements.js
)。我想在网络服务器上托管此捆绑包,并将其作为任何人都可以使用的库。
作为测试,我在localhost上托管了一个index.html,该链接链接到 https:// https:/// my-webserver.com/my-elements.js 。元素捆绑包可以正确下载,但随后它试图从Localhost下载WASM文件,而不是从my-webserver.com下载。
我尝试设置WebPack将所有角构建JS文件与WASM库将所有Angular Build JS文件捆绑到一个文件中,但我无法使其工作。浏览器下载my-elements.js,但甚至没有尝试下载WASM文件。
这是我的webpack配置:
const path = require("path");
module.exports = {
mode: "production",
entry: "./dist/custom-elements/main.js",
output: {
filename: "custom-elements.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.wasm$/,
use: ["wasm-loader"]
}
]
}
};
我还尝试遵循本指南,使用webpack自定义我的角构建: https://developer.okta.com/blog/2019/12/09/angular-webpack 。 这是我的自定义 - webpack-config:
module.exports = {
"module": {
"rules": [
{
"test": /\.wasm$/,
"use": ["wasm-loader"]
}
]
}
};
但这给了我一些看似无关的错误消息:
[error] Error: NOT SUPPORTED: keyword "id", use "$id" for schema ID
at Object.code (/home/user/custom-elements/node_modules/ajv/dist/vocabularies/core/id.js:6:15)
如何使它工作?
I'm creating a custom elements bundle (my-elements.js
) with angular v14 that use a wasm library. I want to host this bundle on a web-server and provide it as a library that anyone can use.
As a test, I hosted an index.html on localhost, that has a link to https://my-webserver.com/my-elements.js. The elements bundle gets downloaded correctly but then it tries to download the wasm file from localhost and not from my-webserver.com.
I've tried setting up webpack to bundle all the angular build js files in dist together with the wasm library into a single file but I could not get this to work. The browser downloads my-elements.js but doesn't even try to download the wasm file.
Here's my webpack config:
const path = require("path");
module.exports = {
mode: "production",
entry: "./dist/custom-elements/main.js",
output: {
filename: "custom-elements.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.wasm$/,
use: ["wasm-loader"]
}
]
}
};
I also tried following this guide to customise my angular build with webpack: https://developer.okta.com/blog/2019/12/09/angular-webpack.
Here's my custom-webpack-config:
module.exports = {
"module": {
"rules": [
{
"test": /\.wasm$/,
"use": ["wasm-loader"]
}
]
}
};
but this gave me some seemingly unrelated error messages:
[error] Error: NOT SUPPORTED: keyword "id", use "$id" for schema ID
at Object.code (/home/user/custom-elements/node_modules/ajv/dist/vocabularies/core/id.js:6:15)
How can I get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个有效的肮脏解决方案,但我不喜欢它。
我正在使用
ng build-Output-hashing = none
来构建我的项目。然后,我在
dist/runtime.js
中找到了WASM库的位置:c = fetch(S.P+“”+A+“+”。module.wasm”)
i手动以我的网站的域前缀:
c = fetch(“ https://my-webserver.com/” + s.p+“”+a+“。模块
。
cat Dist/custom-Elements/*。JS | gzip> test/elements.js.gz
这必须这样做,直到找到更好的解决方案为止。
I've found a dirty solution that works but I don't like it.
I'm building my project with
ng build --output-hashing=none
.I then located where the wasm library is being referenced in
dist/runtime.js
:c=fetch(s.p+""+a+".module.wasm")
I manually prefixed that with my web-site's domain:
c=fetch("https://my-webserver.com/" + s.p+""+a+".module.wasm")
I then bundle all of the js files that angular's build outputted into the dist folder into a single gz file:
cat dist/custom-elements/*.js | gzip > test/elements.js.gz
This will have to do until I find a better solution.