Skip to content

Instantly share code, notes, and snippets.

Bundle Size Analysis

This document analyzes the bundle size of MoonBit JavaScript output, identifying the main contributors to code size and potential optimization opportunities.

Note: The analyzed output is after terser DCE (Dead Code Elimination) with compress enabled. Unused code has already been removed.

Analysis Target

https://github.com/mizchi/js.mbt/blob/main/src/examples/aisdk/main.mbt

Moonbit FFI Core

///|
#external
pub type JsValue

///|
pub fn[T] JsValue::cast(self : JsValue) -> T = "%identity"
Proposal Author Status Review and discussion
ME-0011
mizchi
Under review
Github issue

新しい json enum 構文を提案します。

モチベーション

/// original https://github.com/moonbit-community/jmop/blob/main/promise.mbt
///|
extern "js" fn ffi_promise_resolve(x : Value) -> Value = "(x) => Promise.resolve(x)"
///|
extern "js" fn ffi_promise_reject(e : Error) -> Value = "(x) => Promise.reject(x)"
///|
extern "js" fn ffi_promise_then(
```mbt
async fn[T, U] map_async(arr: Array[T], f: async (T) -> U raise?) -> Array[U] raise? {
let res: Array[U] = []
for item in arr {
res.push(f(item))
}
res
}
async test {
///|
extern "js" fn ffi_request_animation_frame(f : () -> Unit) -> Unit =
  #|(f) => {
  #|  if (globalThis.requestAnimationFrame) {
  #|    requestAnimationFrame(f)
  #|  } else {
  #|    setTimeout(f, 0) // fallback
  #|  }
  #|}
///|
fnalias @svg.node as svg_node
///|
fn[M] common_svg_node(
tag_name : String,
new_attrs : Array[@svg.Attribute[M]],
style~ : Array[String]?,
class~ : String?,
id~ : String?,
const OPENAI_API_KEY_PATH = "%_%=k";
const TEMP_PKCE_VERIFIER = "%_%=pk";
function base64UrlEncode(array: Uint8Array): string {
const base64 = btoa(String.fromCharCode(...array));
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
type LoginState =
| {
/**
Discord Auth for Cloudflare Access
based on https://github.com/Erisa/discord-oidc-worker
# Create discord application
- Get client id and secret
- Set redirect URL to `https://<cloudflare-name>.cloudflareaccess.com/cdn-cgi/access/callback`
///|
test "String to Bytes conversion" {
fn find_index(url : Bytes) -> Int? {
url.find("://")
}
let url = "https://www.example.ocom"
let url_bytes : Bytes = "https://example.com"
inspect(url.find("://"), content="Some(5)")
inspect(find_index("https://example.com"), content="Some(5)")