orhun / git-cliff

A highly customizable Changelog Generator that follows Conventional Commit specifications ⛰️

Home Page:https://git-cliff.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Wasm support

d3lm opened this issue · comments

👋 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?

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

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

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.

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.

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

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.

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.

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.