包装WASM文件中的Angular自定义元素捆绑包

发布于 2025-02-13 19:51:36 字数 1560 浏览 0 评论 0原文

我正在使用使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2025-02-20 19:51:37

我找到了一个有效的肮脏解决方案,但我不喜欢它。

我正在使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文