Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .github/workflows/release-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ on:
paths:
- 'js/**'
- '.github/workflows/release-js.yml'
# Dry-run: builds, packs, and runs `deno publish --dry-run`, which catches
# JSR-only failures before they break a release on `release`. Only a push
# publishes, so nightly.yml calls this on main in place of main PRs.
# Dry-run: builds and packs, which catches packaging failures before they
# break a release on `release`. Only a push publishes, so nightly.yml calls
# this on main in place of main PRs.
workflow_call:

# Only publishing runs share the serialized group. GitHub keeps a single pending
Expand All @@ -28,9 +28,7 @@ jobs:
runs-on: ubuntu-latest
if: github.repository_owner == 'moq-dev'
# npm trusted publishing can require this Environment, which only `release`
# can deploy to, so a branch's OIDC identity cannot publish. It also holds
# the JSR token, since JSR's OIDC trust cannot require an Environment.
# Dry-runs skip it.
# can deploy to, so a branch's OIDC identity cannot publish. Dry-runs skip it.
environment: ${{ github.event_name == 'push' && 'npm' || '' }}

steps:
Expand All @@ -53,7 +51,4 @@ jobs:
- name: Release packages
env:
DRY_RUN: ${{ github.event_name != 'push' }}
# Only the `npm` Environment holds it, so dry-runs get none.
# release.ts hands it to `deno publish` alone.
JSR_TOKEN: ${{ secrets.JSR_TOKEN }}
run: nix develop --command bun --filter '*' release
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,5 @@ __pycache__/
# installs of the py/moq-ffi package.
/py/moq-ffi/moq_ffi/_uniffi/

# Generated JSR manifest (derived from package.json by js/common/package.ts)
jsr.json

# msys/cygwin crash dumps, from running the shell scripts on Windows.
*.stackdump
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
2 changes: 1 addition & 1 deletion doc/.vitepress/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function newest(dirs: string[]): number {
found = Math.max(found, stat.mtimeMs);
if (!stat.isDirectory()) return;
for (const entry of readdirSync(path)) {
if (!["node_modules", "dist", "result", ".vite", "jsr.json"].includes(entry)) walk(join(path, entry));
if (!["node_modules", "dist", "result", ".vite"].includes(entry)) walk(join(path, entry));
}
};
for (const dir of dirs) walk(dir);
Expand Down
3 changes: 0 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@
bun
# Only for NPM publishing
nodejs_24
# JSR publishing. We call `deno publish` directly instead of `bunx jsr`
# so the release doesn't race on a runtime binary download.
deno
# One browser build for local and CI Playwright checks.
playwright-driver.browsers
];
Expand Down
6 changes: 2 additions & 4 deletions infra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ names the Environment, and a branch that is not on the list fails to start it.
| `publish` | `release`, `moq-ffi-v*`, `moq-relay-v*`, `moq-cli-v*` | Registry, signing, and deploy credentials, and the moq-bot key for jobs that also need one of those |
| `alert` | `main` | `DISCORD_WEBHOOK`, for `alert.yml`'s failure posts |
| `cache-write` | `main`, its merge queue, binary release tags | The mbx and Nix cache write keys ([below](#bootstrapping-the-rust-build-cache)) |
| `npm` | `release` | `JSR_TOKEN`; npm's trusted publisher names it |
| `npm` | `release` | Nothing; npm's trusted publisher names it |
| `pypi` | `main`, `release`, `moq-ffi-v*` | Nothing; PyPI's trusted publisher names it |
| `pub.dev` | `moq-dart-v*`, `moq-ffi-v*` | Nothing; pub.dev's automated publishing names it |
| `github-pages` | `main` | Nothing |
Expand All @@ -78,9 +78,7 @@ A tag policy only helps if the tag itself is protected, so the `release tags`
ruleset ([below](#bootstrapping-the-nix-cache)) also covers `moq-ffi-v*`
and `moq-dart-v*`. A `workflow_run` job that checks out the triggering commit
first requires its `head_branch` to be such a tag at that commit, since a
branch can share a release's name. JSR's GitHub trust checks only the repository, not an
Environment, so `release-js` publishes to JSR with `JSR_TOKEN` instead and the
JSR packages do not trust the repository.
branch can share a release's name.

## Bootstrapping a new package repository

Expand Down
2 changes: 1 addition & 1 deletion js/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ The `/js` Bun workspace, published as `@moq/<package>`. Run recipes via `just js
# Conventions

- Only browser performance counts; accept a small regression that shows only on node or bun.
- Document every export and give each entrypoint a `@module` block (JSR builds docs from them). Hide deprecated exports with `@internal` or drop them; never annotate "use X".
- Document every export and give each entrypoint a `@module` block. Hide deprecated exports with `@internal` or drop them; never annotate "use X".
- `just js check` includes the build, since declaration emit catches errors `--noEmit` misses, and rejects importing a `@moq/*` package the manifest doesn't declare.
- Test UI or playback changes with `just dev` in a real browser. `<moq-watch>` renders black in a background tab unless `visible="always"`.
127 changes: 1 addition & 126 deletions js/common/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Split from release.ts to allow building packages without publishing

import { copyFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
import { basename, join, resolve } from "node:path";
import { join, resolve } from "node:path";
import { publint } from "publint";
import { formatMessage } from "publint/utils";
import { checkPackageConstructors } from "./constructors.ts";
Expand All @@ -13,19 +13,6 @@ checkPackageConstructors(process.cwd());
console.log("✍️ Rewriting package.json...");
const pkg = JSON.parse(readFileSync("package.json", "utf8"));

// Capture the source exports before the npm rewrite below mutates them, so the
// JSR config can map them to the built (.js) entrypoints.
const srcExports: Record<string, unknown> = structuredClone(pkg.exports ?? {});

// A package publishes at all when it has a release script. Captured before pkg.scripts is
// cleared below.
const published = Boolean(pkg.scripts?.release);

// Publish to JSR alongside npm for every published package, unless it opts out with
// "jsr": false. The web-component packages opt out: JSR forbids the HTMLElementTagNameMap
// global augmentation every custom element needs.
const publishJsr = published && pkg.jsr !== false;

function rewritePath(p: string, ext: string): string {
return p.replace(/^\.\/src/, ".").replace(/\.ts(x)?$/, `.${ext}`);
}
Expand Down Expand Up @@ -107,7 +94,6 @@ rewriteWorkspaceDependency(pkg.peerDependencies);

pkg.devDependencies = undefined;
pkg.scripts = undefined;
pkg.jsr = undefined; // JSR opt-out flag, not part of the npm package

// Write the rewritten package.json
writeFileSync("dist/package.json", JSON.stringify(pkg, null, 2));
Expand Down Expand Up @@ -146,114 +132,3 @@ if (messages.length > 0) {
}

console.log("📦 Package built successfully in dist/");

// Optionally emit a jsr.json so the package can also publish to JSR (jsr.io).
// Generated from package.json so version/exports never drift. We publish the
// built dist (.js + .d.ts) rather than source: tsc emits explicit types into
// the .d.ts, which sidesteps JSR's "slow types" and ships real declarations,
// while JSR still builds the API docs from the .d.ts (JSDoc is preserved).
if (publishJsr) {
writeJsrConfig();
}

function writeJsrConfig() {
console.log("✍️ Generating jsr.json...");

const exports: Record<string, string> = {};
for (const [key, val] of Object.entries(srcExports)) {
if (typeof val !== "string") continue;
// CSS exports are dev-only and not published, same as the npm package.
if (val.endsWith(".css")) continue;
// rewritePath turns "./src/index.ts" into "./index.js".
exports[key] = `./dist/${rewritePath(val, "js").slice(2)}`;
}

// Self-contained import map so we don't rely on JSR's package.json merge
// behavior. Deps resolve via npm, so packages can publish to JSR in any
// order. Flip @moq/* entries to "jsr:" once the whole graph is on JSR if you
// want JSR-native cross-links between the docs.
const imports: Record<string, string> = {};
const deps = { ...(pkg.dependencies ?? {}), ...(pkg.peerDependencies ?? {}) };
for (const [name, range] of Object.entries(deps) as [string, string][]) {
if (name.startsWith("@types/")) continue; // type-only, never imported at runtime
imports[name] = `npm:${name}@${range}`;
// Trailing-slash subpath mapping (e.g. @moq/signals/dom). The leading slash
// in "npm:/" is required: jsr.json's imports is a standalone import map, so
// the value must parse as a base URL for relative resolution. The
// "npm:name@range/" form (no slash) fails to URL-parse the appended subpath.
imports[`${name}/`] = `npm:/${name}@${range}/`;
}

injectSelfTypes();
rewriteDtsImports();

// JSR validates the license field as a single recognized SPDX identifier, not
// an expression: it rejects "(MIT OR Apache-2.0)". Take the first identifier
// from a dual-license expression so the package keeps a valid license field.
const license =
typeof pkg.license === "string"
? pkg.license
.replace(/[()]/g, "")
.split(/\s+(?:OR|AND)\s+/i)[0]
.trim()
: undefined;

const jsr = {
name: pkg.name,
version: pkg.version,
// JSR reads this for the package description and the "has a description"
// score check; it isn't pulled from the npm package.json automatically.
...(pkg.description ? { description: pkg.description } : {}),
...(license ? { license } : {}),
exports,
...(Object.keys(imports).length ? { imports } : {}),
// A sibling @moq package is published to npm minutes before its dependents
// in the same release, and Deno otherwise refuses npm versions under 24h old.
minimumDependencyAge: 0,
// dist is gitignored, so un-ignore it with a "!" negation; JSR honors
// .gitignore otherwise and would drop the whole build from the graph.
publish: { include: ["dist", "README.md", "LICENSE*"], exclude: ["!dist"] },
};

writeFileSync("jsr.json", JSON.stringify(jsr, null, 2));
console.log("📦 jsr.json written");
}

function rewriteDtsImports() {
// JSR's doc generator resolves the .d.ts import graph and can't handle a
// ".ts" extension (the dist ships .js/.d.ts, not .ts) or a bare "." specifier.
// Normalize those in the declarations so doc generation resolves. Only the
// .d.ts needs this; the .js keeps its own specifiers for npm consumers.
const glob = new Bun.Glob("**/*.d.ts");
for (const rel of glob.scanSync("dist")) {
const file = join("dist", rel);
const body = readFileSync(file, "utf8");
const next = body
.replace(/(from\s+"\.[^"]*?)\.ts"/g, '$1"') // "./x.ts" -> "./x"
.replace(/(from\s+")\.(")/g, "$1./index$2"); // "." -> "./index"
if (next !== body) writeFileSync(file, next);
}
}

function injectSelfTypes() {
// JSR ignores a sibling .d.ts unless the .js references it explicitly;
// without this it infers types from the JS and reports "slow type" warnings.
const glob = new Bun.Glob("**/*.js");
for (const rel of glob.scanSync("dist")) {
const js = join("dist", rel);
const dts = js.replace(/\.js$/, ".d.ts");
if (!existsSync(dts)) continue;
const body = readFileSync(js, "utf8");
if (body.includes("@ts-self-types")) continue;
// Sibling .d.ts (same directory), so just its basename.
const directive = `/* @ts-self-types="./${basename(dts)}" */\n`;
// A shebang (bin entrypoints) must stay on line 1, so insert after it;
// otherwise the directive goes first.
if (body.startsWith("#!")) {
const nl = body.indexOf("\n") + 1;
writeFileSync(js, body.slice(0, nl) + directive + body.slice(nl));
} else {
writeFileSync(js, directive + body);
}
}
}
Loading