From cbb2681d33c34d0bcd3b3b746a0e48618b04f3dc Mon Sep 17 00:00:00 2001
From: Vanilagy <1696106+Vanilagy@users.noreply.github.com>
Date: Sun, 10 Aug 2025 00:45:30 +0200
Subject: [PATCH] Create @mediabunny/mp3-encoder, restructure and extend build
system
---
.gitignore | 6 +-
.vscode/settings.json | 5 +
api-extractor.json | 2 +-
build.mjs | 76 --
build.sh | 34 +
dev/convert.html | 7 +-
dev/demux.html | 2 +
eslint.config.mjs | 7 +-
package-lock.json | 219 +++-
package.json | 19 +-
packages/mp3-encoder/.gitattributes | 1 +
packages/mp3-encoder/LICENSE | 373 ++++++
packages/mp3-encoder/README.md | 32 +
packages/mp3-encoder/api-extractor.json | 37 +
packages/mp3-encoder/build/lame.js | 2 +
packages/mp3-encoder/build/libmp3lame.a | Bin 0 -> 344180 bytes
packages/mp3-encoder/lib/lame.h | 1342 +++++++++++++++++++++
packages/mp3-encoder/package.json | 26 +
packages/mp3-encoder/src/encode.worker.ts | 212 ++++
packages/mp3-encoder/src/index.ts | 211 ++++
packages/mp3-encoder/src/lame-bridge.c | 40 +
packages/mp3-encoder/src/shared.ts | 42 +
packages/mp3-encoder/src/tsconfig.json | 23 +
scripts/bundle.ts | 125 ++
scripts/ensure-license-headers.ts | 5 +-
scripts/esbuild/inlined-workers.ts | 98 ++
{src/mp3 => shared}/mp3-misc.ts | 0
shared/tsconfig.json | 10 +
src/custom-coder.ts | 36 +-
src/input-format.ts | 2 +-
src/media-source.ts | 4 +-
src/mp3/mp3-demuxer.ts | 2 +-
src/mp3/mp3-muxer.ts | 2 +-
src/mp3/mp3-reader.ts | 2 +-
src/mp3/mp3-writer.ts | 2 +-
src/tsconfig.json | 7 +-
todo.txt | 3 +
tsconfig.vite.json | 9 +-
38 files changed, 2913 insertions(+), 112 deletions(-)
create mode 100644 .vscode/settings.json
delete mode 100644 build.mjs
create mode 100755 build.sh
create mode 100644 packages/mp3-encoder/.gitattributes
create mode 100644 packages/mp3-encoder/LICENSE
create mode 100644 packages/mp3-encoder/README.md
create mode 100644 packages/mp3-encoder/api-extractor.json
create mode 100644 packages/mp3-encoder/build/lame.js
create mode 100644 packages/mp3-encoder/build/libmp3lame.a
create mode 100755 packages/mp3-encoder/lib/lame.h
create mode 100644 packages/mp3-encoder/package.json
create mode 100644 packages/mp3-encoder/src/encode.worker.ts
create mode 100644 packages/mp3-encoder/src/index.ts
create mode 100644 packages/mp3-encoder/src/lame-bridge.c
create mode 100644 packages/mp3-encoder/src/shared.ts
create mode 100644 packages/mp3-encoder/src/tsconfig.json
create mode 100644 scripts/bundle.ts
create mode 100644 scripts/esbuild/inlined-workers.ts
rename {src/mp3 => shared}/mp3-misc.ts (100%)
create mode 100644 shared/tsconfig.json
create mode 100644 todo.txt
diff --git a/.gitignore b/.gitignore
index 9f717d8..3c737af 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
-/node_modules
+node_modules
/dist
/dist-docs
.DS_Store
-/docs/.vitepress/cache
\ No newline at end of file
+/docs/.vitepress/cache
+
+packages/mp3-encoder/dist
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..be441af
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,5 @@
+{
+ "files.associations": {
+ "lame.h": "c"
+ }
+}
\ No newline at end of file
diff --git a/api-extractor.json b/api-extractor.json
index 4bd5a02..662a706 100644
--- a/api-extractor.json
+++ b/api-extractor.json
@@ -1,6 +1,6 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
- "mainEntryPointFilePath": "dist/modules/index.d.ts",
+ "mainEntryPointFilePath": "dist/modules/src/index.d.ts",
"bundledPackages": [],
"compiler": {},
"apiReport": {
diff --git a/build.mjs b/build.mjs
deleted file mode 100644
index 4ac1cb5..0000000
--- a/build.mjs
+++ /dev/null
@@ -1,76 +0,0 @@
-import * as esbuild from 'esbuild';
-import process from 'node:process';
-
-const baseConfig = {
- entryPoints: ['src/index.ts'],
- bundle: true,
- logLevel: 'info',
- banner: {
- js: `/*!
- * Copyright (c) 2025-present, Vanilagy and contributors
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- */`,
- },
- legalComments: 'none',
-};
-
-const umdConfig = {
- ...baseConfig,
- format: 'iife',
-
- // The following are hacks to basically make this an UMD module. No native support for that in esbuild as of today
- globalName: 'Mediabunny',
-
- footer: {
- js:
-`if (typeof module === "object" && typeof module.exports === "object") Object.assign(module.exports, Mediabunny)`,
- },
-};
-
-const esmConfig = {
- ...baseConfig,
- format: 'esm',
-};
-
-const ctxUmd = await esbuild.context({
- ...umdConfig,
- outfile: 'dist/bundles/mediabunny.cjs',
-});
-const ctxEsm = await esbuild.context({
- ...esmConfig,
- outfile: 'dist/bundles/mediabunny.mjs',
-});
-const ctxUmdMinified = await esbuild.context({
- ...umdConfig,
- outfile: 'dist/bundles/mediabunny.min.cjs',
- minify: true,
-});
-const ctxEsmMinified = await esbuild.context({
- ...esmConfig,
- outfile: 'dist/bundles/mediabunny.min.mjs',
- minify: true,
-});
-
-if (process.argv[2] === '--watch') {
- await Promise.all([
- ctxUmd.watch(),
- ctxEsm.watch(),
- ctxUmdMinified.watch(),
- ctxEsmMinified.watch(),
- ]);
-} else {
- ctxUmd.rebuild();
- ctxEsm.rebuild();
- ctxUmdMinified.rebuild();
- ctxEsmMinified.rebuild();
-
- await Promise.all([
- ctxUmd.dispose(),
- ctxEsm.dispose(),
- ctxUmdMinified.dispose(),
- ctxEsmMinified.dispose(),
- ]);
-}
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..7084915
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+set -e
+
+# This script must be executed via `npm run build`
+
+# Clear the stuff from last build
+rm -rf dist
+rm -rf packages/mp3-encoder/dist
+
+# Ensure license headers on all source files
+tsx scripts/ensure-license-headers.ts
+
+# Type check & generate .js and .d.ts files
+tsc -p src
+tsc -p packages/mp3-encoder/src
+
+# So that the resulting files use valid ESM imports with file extension. This only runs for the core Mediabunny as only
+# it ships the individual files to npm (for tree shaking, because it's large)
+npm run fix-build-import-paths
+
+# Creates bundles for all packages
+tsx scripts/bundle.ts
+
+# Declaration file rollup and checks
+api-extractor run
+api-extractor run -c packages/mp3-encoder/api-extractor.json
+
+# Checks that all symbols are documented
+tsx scripts/check-docblocks.ts dist/mediabunny.d.ts
+tsx scripts/check-docblocks.ts packages/mp3-encoder/dist/mediabunny-mp3-encoder.d.ts
+
+# Appends stuff to the declaration files to register the global variables these libraries expose
+echo 'export as namespace Mediabunny;' >> dist/mediabunny.d.ts
+echo 'export as namespace MediabunnyMp3Encoder;' >> packages/mp3-encoder/dist/mediabunny-mp3-encoder.d.ts
\ No newline at end of file
diff --git a/dev/convert.html b/dev/convert.html
index 38b9d23..790d8e5 100644
--- a/dev/convert.html
+++ b/dev/convert.html
@@ -1,8 +1,11 @@
+