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

Add Wasm support #510

Open
d3lm opened this issue Feb 21, 2024 · 8 comments
Open

Add Wasm support #510

d3lm opened this issue Feb 21, 2024 · 8 comments
Labels
feature/request New feature or request

Comments

@d3lm
Copy link

d3lm commented Feb 21, 2024

👋 First of all, great project!

I was wondering if we can add a wasm-wasi target so folks could use this in WebContainer as well which can't run native binaries.

I was thinking for those that install this via npm, we could add another optional dependencies for cpu=wasm32 which would be installed in WebContainer and then people could use this to generate their changelogs.

What do you think?

@d3lm d3lm added the feature/request New feature or request label Feb 21, 2024
Copy link

welcome bot commented Feb 21, 2024

Thanks for opening your first issue at git-cliff! Be sure to follow the issue template! ⛰️

@orhun
Copy link
Owner

orhun commented Feb 22, 2024

Hey!

This is definitely on the roadmap! I still couldn't get to it yet. I highly appreciate contributions regards supporting WASM.

I don't think libgit2 plays well with WASM so I think it would be also nice to support gitoxide (and make it default) as well. See #131

@d3lm
Copy link
Author

d3lm commented Feb 23, 2024

Ah yea, I think libgit2 can be compiled to Wasm but it's gonna be quite sizeable. For a Wasm target I could imagine just relying on host functionality to spawn git commands, e.g., via child_process#exec which could be either passed in as an import or handled on the host side and we just pass back some result. Or, like you said, we could use gitoxide which already has a Wasm target AFAIK.

@orhun
Copy link
Owner

orhun commented Feb 23, 2024

Oh, forgot to mention, one other thing that we can do is to disable the repo feature of git-cliff-core which removes git2 dependency. I think then it would be feasible to compile to Wasm.

@d3lm
Copy link
Author

d3lm commented Feb 23, 2024

Ohhh, that's a good idea at least to start with. We could then add more features as we go! I love it.

@orhun
Copy link
Owner

orhun commented Feb 23, 2024

Yup! Are you interested in trying that out? There might be other wasm-related issues with other crates but I think it is worth a shot.

@d3lm
Copy link
Author

d3lm commented Feb 23, 2024

I can see if I find some time but I can't promise anything. My schedule is pretty busy at the moment. But if you decide to take a stab and have questions, let me know. Happy to help.

@orhun
Copy link
Owner

orhun commented May 23, 2024

I tried to compile git-cliff-core to WASM and here are my findings so far:

Intuitively, first thing that I tried was slapping wasm32-unknown-unknown to cargo build with the default features (i.e. repo which pulls in git2 dependency) disabled:

rustup target add wasm32-unknown-unknown
cargo build -p git-cliff-core --no-default-features --target wasm32-unknown-unknown

error: the wasm*-unknown-unknown targets are not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support

It turns out tera pulls in getrandom dependency and that's why it's failing. As a workaround we can simply remove the rand feature from features array:

[dependencies.tera]
version = "1.19.1"
default-features = false
features = ["urlencode", "slug", "humansize", "chrono", "chrono-tz"]

That works! Okay, now how do we use it?

I found this tutorial and it refers to wasm-pack for quickly setting up a WASM demo:

cd git-cliff-core
wasm-pack build --target web

cargo:warning=In file included from libgit2/src/util/git2_util.h:14:

Ugh, it tries to pull in git2 due to repo feature being enabled. Not sure if there is a CLI flag for specifying the features, but I couldn't find it so I manually edited Cargo.toml:

[features]
default = []

It also made me add the following to Cargo.toml:

[lib]
crate-type = ["cdylib", "rlib"]

wasm-pack build --target web:

[INFO]: 📦 Your wasm pkg is ready to publish at /home/orhun/gh/git-cliff/git-cliff-core/pkg.

Yay!

Then I initialized the actual web project:

npm init wasm-app www

Added the git-cliff dependency to package.json:

  "dependencies": {
    "git-cliff": "file:../pkg"
  },
  "devDependencies": {
    "hello-wasm-pack": "^0.1.0",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5",
    "copy-webpack-plugin": "^5.0.0",
    "git-cliff": "file:../pkg"
  }

Update index.js:

import * as git_cliff from "git-cliff";

Run the app (I had to set export NODE_OPTIONS=--openssl-legacy-provider before this for some reason):

npm run start

ERROR in ../pkg/git_cliff_core.js 73:56
Module parse failed: Unexpected token (73:56)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| if (typeof input === 'undefined') {
input = new URL('git_cliff_core_bg.wasm', import.meta.url);
| }
| const imports = __wbg_get_imports();
@ ./index.js 1:0-39
@ ./bootstrap.js
ℹ 「wdm」: Failed to compile.

I don't know what happened there. Also, all the js and WASM files under pkg seems weird, I don't know if I should expect to see any git-cliff related functions in there.

Anyways, that was my attempt. Let me know if you find something / if I'm on the correct path.

@orhun orhun removed their assignment May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants