Skip to content

Commit 48e884a

Browse files
kixelatedclaude
andauthored
Fix building the @moq/publish and @moq/watch packages (#957)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c35f406 commit 48e884a

30 files changed

Lines changed: 197 additions & 295 deletions

bun.lock

Lines changed: 45 additions & 200 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,59 @@
33
// Split from release.ts to allow building packages without publishing
44

55
import { copyFileSync, readFileSync, writeFileSync } from "node:fs";
6-
import { join } from "node:path";
6+
import { join, resolve } from "node:path";
7+
import { publint } from "publint";
8+
import { formatMessage } from "publint/utils";
79

810
console.log("✍️ Rewriting package.json...");
911
const pkg = JSON.parse(readFileSync("package.json", "utf8"));
1012

11-
function rewritePath(p: string): string {
12-
return p.replace(/^\.\/src/, ".").replace(/\.ts(x)?$/, ".js");
13+
function rewritePath(p: string, ext: string): string {
14+
return p.replace(/^\.\/src/, ".").replace(/\.ts(x)?$/, `.${ext}`);
1315
}
1416

15-
pkg.main &&= rewritePath(pkg.main);
16-
pkg.types &&= rewritePath(pkg.types);
17+
pkg.main &&= rewritePath(pkg.main, "js");
18+
pkg.types &&= rewritePath(pkg.types, "d.ts");
1719

1820
if (pkg.exports) {
1921
for (const key in pkg.exports) {
2022
const val = pkg.exports[key];
2123
if (typeof val === "string") {
22-
pkg.exports[key] = rewritePath(val);
24+
if (val.endsWith(".css")) {
25+
// CSS exports are only needed for dev-time resolution;
26+
// consumers inline them at build time via @import.
27+
// We purposely do not copy them to the dist to help catch bugs.
28+
delete pkg.exports[key];
29+
} else {
30+
pkg.exports[key] = {
31+
types: rewritePath(val, "d.ts"),
32+
default: rewritePath(val, "js"),
33+
};
34+
}
2335
} else if (typeof val === "object") {
2436
for (const sub in val) {
2537
if (typeof val[sub] === "string") {
26-
val[sub] = rewritePath(val[sub]);
38+
val[sub] = rewritePath(val[sub], sub === "types" ? "d.ts" : "js");
2739
}
2840
}
2941
}
3042
}
3143
}
3244

3345
if (pkg.sideEffects) {
34-
pkg.sideEffects = pkg.sideEffects.map(rewritePath);
46+
pkg.sideEffects = pkg.sideEffects.map((p: string) => rewritePath(p, "js"));
3547
}
3648

3749
if (pkg.files) {
38-
pkg.files = pkg.files.map(rewritePath);
50+
pkg.files = pkg.files.map((p: string) => rewritePath(p, "js"));
3951
}
4052

4153
if (pkg.bin) {
4254
if (typeof pkg.bin === "string") {
43-
pkg.bin = rewritePath(pkg.bin);
55+
pkg.bin = rewritePath(pkg.bin, "js");
4456
} else if (typeof pkg.bin === "object") {
4557
for (const key in pkg.bin) {
46-
pkg.bin[key] = rewritePath(pkg.bin[key]);
58+
pkg.bin[key] = rewritePath(pkg.bin[key], "js");
4759
}
4860
}
4961
}
@@ -78,4 +90,19 @@ writeFileSync("dist/package.json", JSON.stringify(pkg, null, 2));
7890
console.log("📄 Copying README.md...");
7991
copyFileSync("README.md", join("dist", "README.md"));
8092

93+
// Lint the package to catch publishing issues
94+
console.log("🔍 Running publint...");
95+
const { messages, pkg: lintPkg } = await publint({
96+
pkgDir: resolve("dist"),
97+
level: "warning",
98+
pack: false,
99+
});
100+
101+
if (messages.length > 0) {
102+
for (const message of messages) {
103+
console.error(formatMessage(message, lintPkg));
104+
}
105+
process.exit(1);
106+
}
107+
81108
console.log("📦 Package built successfully in dist/");

js/common/vite-plugin-worklet.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { build } from "esbuild";
2+
import type { Plugin } from "vite";
3+
4+
const SUFFIX = "?worklet";
5+
6+
/**
7+
* A Vite plugin that compiles AudioWorklet files and inlines them as blob URLs.
8+
*
9+
* Usage: import workletUrl from "./my-worklet.ts?worklet"
10+
*
11+
* The worklet file is compiled to JS with all dependencies bundled via esbuild,
12+
* then inlined as a string. At runtime, a blob URL is created and exported.
13+
* Pass the URL to audioWorklet.addModule().
14+
*/
15+
export function workletInline(): Plugin {
16+
return {
17+
name: "worklet-inline",
18+
enforce: "pre",
19+
20+
async resolveId(source, importer) {
21+
if (!source.endsWith(SUFFIX)) return;
22+
23+
const cleanSource = source.slice(0, -SUFFIX.length);
24+
const resolved = await this.resolve(cleanSource, importer, { skipSelf: true });
25+
if (!resolved) return;
26+
27+
return { id: resolved.id + SUFFIX, moduleSideEffects: false };
28+
},
29+
30+
async load(id) {
31+
if (!id.endsWith(SUFFIX)) return;
32+
33+
const filePath = id.slice(0, -SUFFIX.length);
34+
35+
if (this.addWatchFile) {
36+
this.addWatchFile(filePath);
37+
}
38+
39+
const result = await build({
40+
entryPoints: [filePath],
41+
bundle: true,
42+
write: false,
43+
format: "esm",
44+
target: "esnext",
45+
});
46+
47+
const compiled = result.outputFiles[0].text;
48+
49+
return [
50+
`const code = ${JSON.stringify(compiled)};`,
51+
`const blob = new Blob([code], { type: "application/javascript" });`,
52+
`export default URL.createObjectURL(blob);`,
53+
].join("\n");
54+
},
55+
};
56+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare module "*-worklet.ts?worker&url" {
1+
declare module "*?worklet" {
22
const url: string;
33
export default url;
44
}

js/hang-demo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
"devDependencies": {
2020
"@tailwindcss/typography": "^0.5.16",
2121
"@tailwindcss/vite": "^4.1.13",
22+
"esbuild": "^0.27.0",
2223
"highlight.js": "^11.11.1",
2324
"tailwindcss": "^4.1.13",
2425
"typescript": "^5.9.2",
25-
"vite": "^6.3.6",
26+
"vite": "^7.3.1",
2627
"vite-plugin-solid": "^2.11.10"
2728
}
2829
}

js/hang-demo/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"compilerOptions": {
44
"outDir": "dist"
55
},
6-
"include": ["src"]
6+
"include": ["src", "../common/worklet.d.ts"]
77
}

js/hang-demo/vite.config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import tailwindcss from "@tailwindcss/vite";
2+
import { resolve } from "path";
23
import { defineConfig } from "vite";
34
import solidPlugin from "vite-plugin-solid";
5+
import { workletInline } from "../common/vite-plugin-worklet";
46

57
export default defineConfig({
68
root: "src",
7-
plugins: [tailwindcss(), solidPlugin()],
9+
plugins: [tailwindcss(), solidPlugin(), workletInline()],
810
build: {
911
target: "esnext",
1012
sourcemap: process.env.NODE_ENV === "production" ? false : "inline",
1113
rollupOptions: {
1214
input: {
13-
watch: "index.html",
14-
publish: "publish.html",
15-
support: "support.html",
16-
mse: "mse.html",
15+
watch: resolve(__dirname, "src/index.html"),
16+
publish: resolve(__dirname, "src/publish.html"),
17+
support: resolve(__dirname, "src/support.html"),
18+
mse: resolve(__dirname, "src/mse.html"),
1719
},
1820
},
1921
},

js/hang/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"./src/support/element.ts"
1818
],
1919
"scripts": {
20-
"build": "rimraf dist && tsc -b && bun ../scripts/package.ts",
20+
"build": "rimraf dist && tsc -b && bun ../common/package.ts",
2121
"check": "tsc --noEmit",
22-
"release": "bun ../scripts/release.ts"
22+
"release": "bun ../common/release.ts"
2323
},
2424
"dependencies": {
2525
"@kixelated/libavjs-webcodecs-polyfill": "^0.5.5",

js/lite/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"./zod": "./src/zod.ts"
1111
},
1212
"scripts": {
13-
"build": "rimraf dist && tsc -b && bun ../scripts/package.ts",
13+
"build": "rimraf dist && tsc -b && bun ../common/package.ts",
1414
"check": "tsc --noEmit && madge --circular --extensions ts,tsx src && tsc -b examples/tsconfig.json",
1515
"test": "bun test --only-failures",
16-
"release": "bun ../scripts/release.ts"
16+
"release": "bun ../common/release.ts"
1717
},
1818
"dependencies": {
1919
"async-mutex": "^0.5.0",
@@ -25,7 +25,6 @@
2525
},
2626
"devDependencies": {
2727
"@typescript/lib-dom": "npm:@types/web@^0.0.241",
28-
"vite": "^6.3.6",
2928
"@types/node": "^24.3.1",
3029
"vite-plugin-html": "^3.2.2",
3130
"rimraf": "^6.0.1",

0 commit comments

Comments
 (0)