feat: build tinymist targeting web (#1102)

* feat: add web target

* dev: simple package rule

* dev: update web release

* dev: update workspace

* ci: setup wasm pack

* ci: correct path to upload

* ci: build artifact

* fix: update metadata and launch config
This commit is contained in:
Myriad-Dreamin 2025-01-03 10:30:38 +08:00 committed by GitHub
parent 2464c5b66c
commit d32f6261f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 218 additions and 29 deletions

1
crates/tinymist-core/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/pkg

View file

@ -0,0 +1,33 @@
[package]
name = "tinymist-core"
description = "Tinymist core library."
categories = ["compilers", "command-line-utilities"]
keywords = ["api", "language", "typst"]
authors.workspace = true
version.workspace = true
license.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true
rust-version.workspace = true
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["web"] # "no-content-hint"
web = ["wasm-bindgen"]
# no-content-hint = ["reflexo-typst/no-content-hint"]
[dependencies]
wasm-bindgen = { version = "0.2.92", optional = true }
[build-dependencies]
anyhow.workspace = true
vergen.workspace = true
cargo_metadata = "0.18.0"
[lints]
workspace = true

View file

@ -0,0 +1,31 @@
//! Generates project metadata.
use anyhow::Result;
use vergen::EmitBuilder;
fn main() -> Result<()> {
// Emit the instructions
EmitBuilder::builder()
.all_cargo()
.build_timestamp()
.git_sha(false)
.git_describe(true, true, None)
.all_rustc()
.emit()?;
let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap();
let typst = metadata
.packages
.iter()
.find(|package| package.name == "typst")
.expect("Typst should be a dependency");
println!("cargo:rustc-env=TYPST_VERSION={}", typst.version);
let src = typst
.source
.as_ref()
.map(|e| e.repr.as_str())
.unwrap_or_default();
println!("cargo:rustc-env=TYPST_SOURCE={src}");
Ok(())
}

View file

@ -0,0 +1,33 @@
{
"name": "tinymist-web",
"version": "0.12.16",
"description": "WASM module for running tinymist analyzers in JavaScript environment.",
"author": "Myriad-Dreamin",
"license": "Apache-2.0",
"keywords": [
"TypeScript",
"Typst"
],
"type": "module",
"module": "pkg/tinymist_core.js",
"types": "pkg/tinymist_core.d.ts",
"files": [
"pkg/tinymist_core_bg.wasm",
"pkg/tinymist_core_bg.wasm.d.ts",
"pkg/tinymist_core_bg.js",
"pkg/tinymist_core.js",
"pkg/tinymist_core.d.ts"
],
"scripts": {
"build:dev": "wasm-pack build --target web --dev -- --no-default-features --features web",
"build:node": "wasm-pack build --target nodejs -- --no-default-features --features web",
"build": "wasm-pack build --target web -- --no-default-features --features web",
"publish:dry": "npm publish --dry-run",
"publish:lib": "npm publish || exit 0",
"test:chrome": "wasm-pack test --chrome --headless --release",
"test:firefox": "wasm-pack test --firefox --headless --release"
},
"devDependencies": {
"turbo": "^2.3.3"
}
}

View file

@ -0,0 +1,30 @@
//! Tinymist Core Library
use std::sync::LazyLock;
/// The long version description of the library
pub static LONG_VERSION: LazyLock<String> = LazyLock::new(|| {
format!(
"
Build Timestamp: {}
Build Git Describe: {}
Commit SHA: {}
Commit Date: {}
Commit Branch: {}
Cargo Target Triple: {}
Typst Version: {}
Typst Source: {}
",
env!("VERGEN_BUILD_TIMESTAMP"),
env!("VERGEN_GIT_DESCRIBE"),
option_env!("VERGEN_GIT_SHA").unwrap_or("None"),
option_env!("VERGEN_GIT_COMMIT_TIMESTAMP").unwrap_or("None"),
option_env!("VERGEN_GIT_BRANCH").unwrap_or("None"),
env!("VERGEN_CARGO_TARGET_TRIPLE"),
env!("TYPST_VERSION"),
env!("TYPST_SOURCE"),
)
});
#[cfg(feature = "web")]
pub mod web;

View file

@ -0,0 +1,11 @@
//! Tinymist Web APIs.
use wasm_bindgen::prelude::*;
use crate::LONG_VERSION;
/// Gets the long version description of the library.
#[wasm_bindgen]
pub fn version() -> String {
LONG_VERSION.clone()
}

View file

@ -0,0 +1,14 @@
import tinymist_init from "../pkg/tinymist_core.js";
import * as tinymist from "../pkg/tinymist_core.js";
import fs from "fs";
const wasmData = fs.readFileSync("pkg/tinymist_core_bg.wasm");
async function main() {
await tinymist_init({
module_or_path: new Uint8Array(wasmData),
});
console.log(tinymist.version());
}
main().catch(console.error);