typst-community / typst.js

📦 Typst for JavaScript
https://typst.community/typst.js
Apache License 2.0
15 stars 2 forks source link

use optionalDependencies instead of postinstall script #3

Closed jcbhmr closed 9 months ago

jcbhmr commented 9 months ago

old title: publish npm package with binaries embedded in it instead of using postinstall script

basically: ``` . ├── src/ │ └── main.ts ├── dist/ │ ├── main.js │ └── main.d.ts ├── bin/ │ ├── typst.win32-x64.exe │ ├── typst.darwin-x64 │ ├── typst.darwin-arm64 │ ├── typst.linux-x64 │ └── typst.linux-arm64 └── package.json ``` then main.ts/js would run: `typst.${process.platform}-${process.arch}` exe instead of downloading it and then running it. basically skip the download step. why this might be good: - no lag for 5s while postinstall or main `ensureInstalled()` downloads and extracts the binary - could work better offline? the main win is startup/install time. that's it. HOWEVER this would add considerable size to the published npm package... ``` 28M typst-aarch64-apple-darwin/typst 28M typst-aarch64-unknown-linux-musl/typst 29M typst-armv7-unknown-linux-musleabi/typst 30M typst-x86_64-apple-darwin/typst 32M typst-x86_64-pc-windows-msvc/typst.exe 32M typst-x86_64-unknown-linux-musl/typst ``` total ~178M ish

alternative:

do what https://napi.rs/ does by default and splinter off things into optionalDependencies that only work on a single OS/CPU combo: https://napi.rs/docs/deep-dive/release#3-the-native-addon-for-different-platforms-is-distributed-through-different-npm-packages

ex:

// for Windows Intel/AMD x86-64
{
  "name": "@node-rs/bcrypt-win32-x64",
  "version": "0.5.0",
  "os": ["win32"],
  "cpu": ["x64"]
}
// for macOS ARM64
{
  "name": "@node-rs/bcrypt-darwin-arm64",
  "version": "0.5.0",
  "os": ["darwin"],
  "cpu": ["arm64"]
}

there appears to be a project that is designed to tackle this issue actually and it uses this second approach!

https://github.com/christophwitzko/npm-binary-releaser image


after writing all that text and seeing how large the binaries are when spread across 6x architectures (almost 200mb! when a single platform is only 30mb!) i think the best way to avoid postinstall and speed up install is to use npm-binary-releaser or similar