Skip to content

oj build: a package section in oj.config builds a published npm package in tsdown's words - #827

Open
raphamorim wants to merge 2 commits into
mainfrom
rapha/oj-package-config
Open

raphamorim wants to merge 2 commits into
mainfrom
rapha/oj-package-config

Conversation

@raphamorim

@raphamorim raphamorim commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A package section in oj.config.{json,ts,mjs,js} builds a published npm package the way tsdown builds it, so a tsdown.config.ts ports key for key and the package needs no vite.config. First of the changes that let oj replace tsdown for the monorepo's packages; build.lib stays Vite's.

{ "package": { "entry": ["src/index.ts", "src/model.ts"], "format": ["cjs", "esm"], "fixedExtension": false } }

What the section resolves to (oj_config::package_builds), and what differs from the library build today:

  • Entries named by their path from the entries' lowest common directory (src/index.ts and src/protocols/mcp/index.ts are index and protocols/mcp/index), or by the keys of an object; default src/index.ts. The library build named array entries by stem, so two index.ts came out index and index2.
  • Dependency rule as rolldown's external function: dependencies, peerDependencies and optionalDependencies stay imports with every subpath, devDependencies are bundled, deps.alwaysBundle / deps.neverBundle / deps.onlyBundle / deps.skipNodeModulesBundle (and the older external / noExternal) adjust that; a string names a package or a subpath under it, a /regex/ string or a RegExp marker is tested as written. The library build matched external exactly (jose external, jose/sub bundled). onlyBundle fails the build over a node_modules package it does not name.
  • Defaults tsdown's: esm, dist, platform node (the cjs file always node), no minify, no lowering unless engines.node names a floor, clean, a hash in chunk names, fixedExtension on under node (.mjs/.cjs; off gives .js/.cjs for a type: module package).
  • Also read: define, sourcemap, copy (files, directories, globs; { from, to, flatten, rename }), globalName, tsconfig, treeshake, outputOptions.entryFileNames, name as a label, and a list of sections for a package that builds twice, each with its own clean.
  • A package build defines nothing of Vite's import.meta.env, copies no public/, and reads no vite.config beside it (that one configures the app); plugins come from oj.plugins.mjs or a config named with --config.
  • dts is read and warned about. Declarations are the next change.

Tests

  • oj_config unit tests: entry naming from the common directory, format normalization, the dependency rule, package names of specifiers, the extension table, engines.node as a target, and a section resolving to the library config with tsdown's defaults (merged define, no public dir, copy destinations, a list with clean: false).
  • oj build tests over a package fixture (a type: module manifest with an installed dependency and devDependency, a nested entry, a throwing vite.config beside it): the dual build's file set and contents (builtin and dependency subpath kept, devDependency inlined, names unminified, define applied); alwaysBundle / neverBundle / onlyBundle; a list that builds in order with copy, a browser-platform second build under clean: false, the TS config form, and an iife with globalName, minify and entryFileNames.

Verification

  • cargo test -p oj_config (86 pass) and the library and package build tests in oj; clippy -D warnings on both crates and rustfmt clean.
  • Built two of the monorepo's packages with the new binary: authorization-js (index and model, es+cjs, a shared chunk hashed) and lovable-vite-tanstack (three named entries; vite and the Node builtins kept as imports). Same file set tsdown writes, minus declarations.

Review round

Two review passes (the code-review skill and a read of the diff), fixes in the second commit:

  • oj build --config x in a package root ran the app build and dropped the section. The section now decides; a --config file lends its plugins.
  • copy destinations were fixed at config time, so --outDir build put the bundle in build/ and the copies in dist/. Entries are resolved when copied, against the outDir written to; an explicit to stays put. Globs are detected on the entry as written with the root escaped, glob read errors fail the copy, rename applies to globbed files.
  • A config mode is honoured when the command line names none.
  • A deps.onlyBundle failure removes what the build wrote, as tsdown fails before writing.
  • A wrong value inside a section now fails with serde's message for that key, not "did not match any variant of untagged enum".
  • Left as is: the one extra evaluation of an oj.config.ts on a non-package build (one QuickJS pass, to see whether a package section is there).

…kage in tsdown's words with tsdown's defaults, beside Vite's `build.lib`

A published package is built with tsdown today: `tsdown.config.ts` names
the entries, the formats, `fixedExtension: false`, `dts` and `target:
false`, and relies on tsdown's defaults for the rest (platform node,
dependencies and peers external with their subpaths, devDependencies
bundled, no minify, entries named by their path from the common
directory). oj's library build could already write a dual es+cjs package
from an `oj.config.json` with `build.lib`, but with Vite's decisions:
array entries named by stem (two `index.ts` came out `index` and
`index2`), `external` matched exactly (`jose` external, `jose/sub`
bundled), the bundle minified and lowered, and a vite.config beside the
package adopted as the package's.

The `package` section takes tsdown's option names so a config ports key
for key, and resolves (oj_config::package_builds) to the library build's
own config plus what tsdown decides differently: `entry` as a file, a
list named from the entries' lowest common directory, or an object;
`format` (`esm` default); `platform` (`node`, and `node` for the cjs
file whatever it says); `target` from `engines.node` or none; `minify`
off; `clean`; `fixedExtension` on under node; a hash in chunk names;
`define`, `sourcemap`, `copy`, `globalName`, `tsconfig`, `treeshake`,
`outputOptions.entryFileNames`; and `deps.alwaysBundle` /
`deps.neverBundle` / `deps.onlyBundle` / `deps.skipNodeModulesBundle`
(plus the older `external` / `noExternal`) over the manifest's
production dependencies, as rolldown's `external` function. A list of
sections builds a package twice, each with its own `clean`. A package
build defines nothing of Vite's `import.meta.env`, copies no `public/`,
and reads no vite.config of its own accord: `oj.plugins.mjs` carries
plugins, `--config` names a config on purpose. `dts` is read and warned
about; declarations are the next change.

Checked against two of the monorepo's packages (authorization-js and
lovable-vite-tanstack): the same file set as tsdown writes, with the
peers and the builtins kept as imports and the shared chunk hashed.
@raphamorim
raphamorim requested a review from a team as a code owner December 4, 2026 10:09
…es into the outDir it wrote to, and leaves no disallowed bundle behind

Review round. The package dispatch was gated on no `--config`, so
`oj build --config plugins.mjs` in a package root ran the app build and
dropped the section; the section now decides, and a `--config` file
lends the build its plugins as the docs said. A `mode` the config names
is read as load_build_config reads it (the config and `.env.<mode>`
again under it) when the command line names none. `copy` entries are
kept as written and resolved when the files are copied, against the
outDir the build wrote to, so `--outDir build` takes the copies along
(an explicit `to` stays put); a glob is detected on the entry as
written and the root is escaped in the pattern, glob read errors fail
the copy instead of thinning it, and `rename` applies to a glob's files
too. A `deps.onlyBundle` failure removes the chunks this build wrote,
as tsdown fails before writing. `PackageSection` is read by hand from
the JSON's shape, so a wrong value inside a section fails with serde's
message for that key rather than "did not match any variant". A RegExp
string with JS's escaped slash is tested, and a lone `/plain` string
stays a name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant