Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275import { defineConfig, type OutputOptions, type RolldownOptions,} from 'rolldown';import { dts, } from 'rolldown-plugin-dts';
import { binShebangPlugin, } from './bin-shebang.ts';import { browserslistTargets, } from './browserslist-targets.ts';import { anchoredExternal, packageExternals,} from './package-externals.ts';import { shebangExecutablePlugin, } from './shebang-executable.ts';
/** Resolved Browserslist targets shared by Node builds, via {@link browserslistTargets}. */const target = await browserslistTargets({ runtime: 'node', },);
/** Bundle-inclusion patterns for Node builds: workspace source plus the ESM-only utility historically inlined so committed plugin bundles stay dependency-free. */export const NODE_ALWAYS_BUNDLE: readonly string[] = [ '@monochromatic-dev/**', 'nano-spawn',];
/** Bare names kept external even when undeclared: pi extension peers are provided by the pi runtime at load time, and bundling them duplicates the pi API causing CJS/ESM "exports is not defined" errors. */export const NODE_ALWAYS_EXTERNAL: readonly string[] = [ '@earendil-works/pi-coding-agent', 'typebox', '@earendil-works/pi-ai',];
/** Build the Node flavor external list for a custom bundle-inclusion set. Combines manifest-derived externals with the pi runtime peers kept external even when undeclared. @param alwaysBundle - Patterns whose matching dependencies must stay inline. @returns Regex list for rolldown's `external` input option. @example ```ts // TODO: deprecate Optique external: await nodeExternal({ alwaysBundle: ['\@monochromatic-dev/**', '\@optique/**',], },), ``` */export async function nodeExternal({ alwaysBundle, }: { readonly alwaysBundle: readonly string[];},): Promise<RegExp[]> { return [ ...await packageExternals({ alwaysBundle, },), ...NODE_ALWAYS_EXTERNAL.map(function toMatcher(name: string,): RegExp { return anchoredExternal(name,); },), ];}
/** Plugins shared by every Node build: Oxc declarations, executable bits for shebang outputs, and Node shebang injection for bin-targeted chunks. */const basePlugins = [ dts({ generator: 'oxc', },), shebangExecutablePlugin(),];
/** Shared raw-rolldown options for Node platform builds, without an input. Bundles workspace dependencies (`@monochromatic-dev/*`) into the output so built artifacts are self-contained and work outside the monorepo (e.g. Claude Code plugins installed via marketplace). Selects Oxc declaration generation explicitly; the tsgo backend cannot emit declarations for workspace source inlined from outside the entry package's tsconfig project (see `doc/troubleshooting/rolldown-plugin-dts-typescript-7-generator.md`). */const baseOptions: RolldownOptions = { platform: 'node', external: await nodeExternal({ alwaysBundle: NODE_ALWAYS_BUNDLE, },), transform: { target: [...target,], }, plugins: basePlugins,};
/** Shared output options for Node builds, without a directory. `entryFileNames`/`chunkFileNames` force `.mjs` because raw rolldown has no `fixedExtension`; `rolldown-plugin-dts` derives `.d.mts` from the template. */const baseOutput = { format: 'es' as const, entryFileNames: '[name].mjs', chunkFileNames: '[name]-[hash].mjs', minify: { compress: true, // Mangle breaks func.name and makes output difficult for users to audit. mangle: false, codegen: true, },};
/** Narrow a flavor input union to its list form. @param value - Input paths as a list or a named-entry record. @returns Whether value is the list form. @example ```ts isInputList(['./src/index.ts']); ``` */function isInputList( value: readonly string[] | Readonly<Record<string, string>>,): value is readonly string[] { return Array.isArray(value,);}
/** Build one Node flavor config with overridable input and output directory. Bin-targeted entry chunks get `#!/usr/bin/env node` injected at byte 0 through {@link binShebangPlugin}, so built CLIs cannot fall through to `/bin/sh`. @param input - Source input paths; defaults to the package index. @param outputDir - Output directory; committed Claude Code plugin bundles override this to `bundle/node` (see `doc/decision/gitignore-negations.md`). @param outputOverrides - Shallow output-option overrides for consumers needing e.g. `minify: false` or `codeSplitting: false`. @param external - Replacement external list from {@link nodeExternal} for consumers with custom bundle-inclusion patterns. @returns Node flavor rolldown config for one self-contained build. @example ```ts // rolldown.node.config.ts import { nodeConfig, } from '\@monochromatic-dev/config-rolldown/.node.ts'; export default nodeConfig({ outputDir: 'bundle/node', },); ``` */export function nodeConfig( { input = ['./src/index.ts',], outputDir = 'dist/final/node', outputOverrides = {}, external, }: { readonly input?: readonly string[] | Readonly<Record<string, string>>; readonly outputDir?: string; readonly outputOverrides?: OutputOptions; readonly external?: readonly RegExp[]; } = {},): RolldownOptions { return defineConfig({ ...baseOptions, ...external === undefined ? {} : { external: [...external,], }, input: isInputList(input,) ? [...input,] : { ...input, }, plugins: [ ...basePlugins, binShebangPlugin({ outputDir, },), ], output: { ...baseOutput, dir: outputDir, cleanDir: true, ...outputOverrides, }, },);}
/** Default single-input Node build config via {@link nodeConfig}. Single-input builds are already self-contained: with one input there is nothing to hoist into a shared chunk. Multi-entry plugins must use {@link perEntryNodeConfig} instead so each entry stays self-contained. @example ```ts // rolldown.node.config.ts export { default, } from '\@monochromatic-dev/config-rolldown/.node.ts'; ``` */const _default_1: RolldownOptions = nodeConfig();export default _default_1;
/** Build each input as its own single-input bundle. A single build with multiple inputs hoists code shared between those inputs into a separate chunk whose filename carries a content hash. Because these dist bundles are committed and published, that hash churns the filename on every content change and leaks an internal chunk into the published plugin. Building one input per config removes the shared chunk entirely: the shared code inlines into each entry, so every output file is self-contained with a stable name. `cleanDir` stays off for every per-entry config because raw rolldown's clean is per-config and not watch-safe: any config cleaning the shared `dist/final/node` would delete sibling outputs. The owning mise task pre-cleans the directory once instead. @param entries - Source input paths, one self-contained bundle emitted per path. @param outputDir - Shared output directory; committed Claude Code plugin bundles override this to `bundle/node`. @returns One single-input rolldown config per entry. @example ```ts // rolldown.node.config.ts import { perEntryNodeConfig, } from '\@monochromatic-dev/config-rolldown/.node.ts'; export default perEntryNodeConfig({ entries: ['./src/index.ts', './src/filter.ts',], },); ``` */export function perEntryNodeConfig( { entries, outputDir = 'dist/final/node', }: { readonly entries: readonly string[]; readonly outputDir?: string; },): RolldownOptions[] { return defineConfig(entries.map(function toSingleEntryConfig(entry: string,): RolldownOptions { return { ...baseOptions, input: [entry,], plugins: [ ...basePlugins, binShebangPlugin({ outputDir, },), ], output: { ...baseOutput, dir: outputDir, cleanDir: false, }, }; },),);}
/** Consumer-facing alias for Node flavor config values, so package configs can annotate their exported const without depending on rolldown directly. */export type NodeFlavorConfig = RolldownOptions;
/** Consumer-facing alias for per-entry Node flavor config arrays. */export type NodeFlavorConfigs = RolldownOptions[];