bun-plugin-civet-sourcemaps
v0.0.2
Published
Bun bundler plugin that maps compiled Civet/TSX back to original .civet source files
Maintainers
Readme
bun-plugin-civet-sourcemaps
Unpublished 0.0.2 release candidate (dev.0) — a Bun bundler plugin that preserves original .civet locations after Civet → TypeScript/TSX → JavaScript compilation.
The previously published 0.0.1 release is immutable. Changes and fixes in this candidate belong to 0.0.2.
This is a real Bun BunPlugin, not a wrapper around Bun.build(). It uses onLoad to compile Civet with an inline source map and onEnd to compose Bun's output source maps with each Civet input map. No separate postbuild command or source-map library is needed.
Quick start
Install the package (from the accompanying local tarball until it is published) and Civet:
bun add -d @danielx/civet ./bun-plugin-civet-sourcemaps-0.0.2.tgzCreate build.mjs:
import { civetSourceMaps } from 'bun-plugin-civet-sourcemaps';
const result = await Bun.build({
entrypoints: ['./src/main.civet'],
outdir: './dist',
target: 'browser',
sourcemap: 'linked',
plugins: [civetSourceMaps()],
});
if (!result.success) throw new AggregateError(result.logs, 'Build failed');Then run bun build.mjs. With sourcemap: 'linked', DevTools receives dist/main.js and dist/main.js.map; mapped generated positions should refer to the original .civet source. sourcemap: 'external' works too, but doesn't append a sourceMappingURL comment.
API
civetSourceMaps({
// optional; default: lazy import of @danielx/civet's `compile`
compile?: (source: string, options: object) => string | Promise<string>,
// options passed through to Civet, e.g. { comptime: false }
compileOptions?: object,
// default: /\.civet$/
filter?: RegExp,
})comptime defaults to true (matching Civet’s Bun plugin); set compileOptions: { comptime: false } to disable it. filename, outputFilename, inlineMap: true, and sourceMap: false are forced to ensure reliable map generation and a string compiler return. filter must not have global or sticky flags.
This plugin replaces Civet's existing Bun onLoad handler when used with Bun.build(). Do not install two .civet handlers for the same files. It is not a preload-time source-map solution for bun file.civet.
What it does
onStart: clear the per-build registry.onLoad: read.civet, invoke Civet, record its inline Source Map v3, return TSX source to Bun.- Bun bundles and creates its usual intermediate-TSX-based source map.
onEnd: match input files individually, compose each final map, and rewrite the emitted.js.mapfiles beforeBun.build()resolves.- Unmodified TypeScript/JavaScript input mappings remain untouched;
sourcesContentembeds original Civet text.
A missing compiler map, unsupported source map, absent output map, or disabled sourcemap causes an error rather than silently publishing incorrect debug positions.
Testing
The supplied Bun Linux x64 1.4.4 binary was used for integration tests:
bun test
bun tests/integration.test.mjsUnit tests exercise Base64 VLQ and map composition. Simulated-compiler integration tests exercise Bun's actual onLoad/onEnd, multiple .civet modules, .ts input, multiple entrypoints, minification, repeated builds, linked/external maps, and failures. The compiler used in tests is a simulated Civet compiler with legitimate Source Map v3 output.
Real-compiler integration tests also run with @danielx/[email protected], loaded from the user-supplied tarball, covering linked/external maps, minification, multiple entrypoints, sequential reuse, comptime override, JSX, and Unicode source preservation. JSX compilation in a dependency-free fixture marks react/jsx-runtime and react/jsx-dev-runtime as external; a production JSX app needs a compatible JSX runtime installed. Note: Bun 1.4.4 does not necessarily emit mappings for every generated JSX child literal. Missing downstream mapping segments are kept unmapped rather than assigned speculative Civet positions.
Current constraints
- Requires
Bun.build({ sourcemap: 'linked' | 'external' })and disk writes. Nosourcemap: 'inline'orwrite: falsesupport. - Bun
onEndis a bundler lifecycle hook, so this does not add runtimebun runsource-map support. - Source Map v3 standard (not indexed/sectioned maps); the small in-house composer supports common local filesystem sources but is not a production-hardened implementation of URL/Windows/indexed maps or source-map edge cases.
- Compiled input paths are matched against Bun's output map paths; real Civet 0.11.16 matching is verified on Linux but Windows paths and unusual source roots remain unverified.
- Watch/incremental builds must trigger a new Bun build so
onEndruns again. Bun's own watcher may need special handling for plugin-loaded files. - Other plugins with overlapping
onLoadfilters can conflict. When chaining other transforms, each intermediate stage needs its own upstream mapping.
Package contents
The npm tarball contains src/, README.md, and LICENSE, not internal handoff docs or tests. The separate development source ZIP contains tests, examples, and HANDOFF.md.
