diff --git a/.changepacks/changepack_log_bun_include_js.json b/.changepacks/changepack_log_bun_include_js.json new file mode 100644 index 000000000..7af1c96e5 --- /dev/null +++ b/.changepacks/changepack_log_bun_include_js.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/bun-plugin/package.json": "Patch" + }, + "note": "The Bun plugin takes an `include` option, as the other plugins do, and compiles the published `.js` files of the listed packages. Before, only `.js` under `@devup-ui/` was loaded, so an uncompiled library's components threw `Cannot run on the runtime`. Each name matches that whole package, also in pnpm and Bun stores, and not the dependencies in its own `node_modules`", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/packages/bun-plugin/README.md b/packages/bun-plugin/README.md index 3fb45ddf9..6e175f256 100644 --- a/packages/bun-plugin/README.md +++ b/packages/bun-plugin/README.md @@ -63,8 +63,9 @@ Class names are short in `Bun.build` and readable under the runtime; pass ## Compiled Files `.ts`, `.tsx`, `.jsx` and `.mjs` files are compiled wherever they are, and -`.js` files only inside `@devup-ui` packages. Devup UI code in a `.js` file of -your own project is not compiled and reaches the runtime placeholders +`.js` files only inside `@devup-ui` packages and the packages you list in +`include`, as in the other plugins. Devup UI code in a `.js` file of your own +project is not compiled and reaches the runtime placeholders (`Cannot run on the runtime`). Give such files a `.jsx`, `.tsx` or `.mjs` extension. @@ -73,6 +74,21 @@ source of every file its filter matches, it cannot hand a file back to Bun, and Bun reads returned source as an ES module, so a CommonJS `.js` file (`require`, `module.exports`) it matched would stop loading. +## Libraries + +List a library that ships Devup UI components as compiled `.js` in `include`: + +```ts +DevupUI({ include: ['my-design-system'] }) +// or, under the runtime +await register({ include: ['my-design-system'] }) +``` + +Each name matches that whole package (`my-lib` is not `my-lib-extra` or +`@scope/my-lib`), including in pnpm and Bun stores, but not the dependencies +in its own `node_modules`. Under the runtime its `.js` files are read as ES +modules, as above, so list only packages that publish ES modules there. + ## Compiler Engine The native compiler is selected by default, including the zero-config preload. diff --git a/packages/bun-plugin/src/__tests__/plugin.test.ts b/packages/bun-plugin/src/__tests__/plugin.test.ts index 9d656825a..22290f809 100644 --- a/packages/bun-plugin/src/__tests__/plugin.test.ts +++ b/packages/bun-plugin/src/__tests__/plugin.test.ts @@ -437,6 +437,78 @@ describe('registered Bun hooks', () => { }) } + describe('included packages', () => { + function sourceFilter() { + const hook = loads.find(({ options }) => options.namespace === undefined) + if (!hook) throw new Error('No source load hook registered') + return hook.options.filter + } + + it('compiles the published .js of an included package', async () => { + const path = join(root, 'node_modules', 'my-lib', 'dist', 'index.js') + await Bun.write( + path, + "import { css } from '@devup-ui/react'; export const className = css({ color: 'navy' })", + ) + await register({ engine: 'wasm', include: ['my-lib'] }) + const result = await load(path) + if (!result || !('contents' in result)) + throw new Error('Expected transformed source contents') + expect(result.loader).toBe('js') + expect(result.contents).not.toContain('css(') + expect(writeSync).toHaveBeenCalledWith( + expect.stringContaining('devup-ui.css'), + expect.stringContaining('navy'), + 'utf-8', + ) + }) + + it.each([ + ['my-lib', '/app/node_modules/my-lib/index.js'], + ['my-lib', 'C:\\app\\node_modules\\my-lib\\dist\\index.js'], + [ + 'my-lib', + '/app/node_modules/.bun/my-lib@1.0.0/node_modules/my-lib/index.js', + ], + [ + 'my-lib', + '/app/node_modules/.pnpm/my-lib@1.0.0/node_modules/my-lib/index.js', + ], + ['@scope/ui', '/app/node_modules/@scope/ui/dist/index.js'], + ['@scope/ui', 'C:\\app\\node_modules\\@scope\\ui\\index.js'], + ['foo.bar', '/app/node_modules/foo.bar/index.js'], + ['foo+bar', '/app/node_modules/foo+bar/index.js'], + ])('loads .js of include %s at %s', async (name, path) => { + await register({ engine: 'wasm', include: [name] }) + expect(sourceFilter().test(path)).toBe(true) + }) + + it.each([ + ['my-lib', '/app/node_modules/my-lib-extra/index.js'], + ['my-lib', '/app/node_modules/@scope/my-lib/index.js'], + ['my-lib', '/app/node_modules/my-lib/node_modules/dep/index.js'], + ['my-lib', '/app/node_modules/other/my-lib/index.js'], + ['my-lib', '/app/my-lib/index.js'], + ['my-lib', '/app/node_modules/my-lib/index.json'], + ['foo.bar', '/app/node_modules/fooXbar/index.js'], + ['@scope/ui', '/app/node_modules/@scope/ui-kit/index.js'], + ])('leaves .js outside include %s at %s to Bun', async (name, path) => { + await register({ engine: 'wasm', include: [name] }) + expect(sourceFilter().test(path)).toBe(false) + }) + + it('loads no package .js but Devup UI without include', async () => { + await register({ engine: 'wasm' }) + expect(sourceFilter().test('/app/node_modules/my-lib/index.js')).toBe( + false, + ) + expect( + sourceFilter().test('/app/node_modules/@devup-ui/react/dist/index.js'), + ).toBe(true) + expect(sourceFilter().test('/app/src/App.tsx')).toBe(true) + }) + }) + it('returns source without style imports unchanged', async () => { const path = join(root, 'plain.ts') await Bun.write(path, 'export const value = 42') diff --git a/packages/bun-plugin/src/plugin.ts b/packages/bun-plugin/src/plugin.ts index 0e2b55754..d0747a04e 100644 --- a/packages/bun-plugin/src/plugin.ts +++ b/packages/bun-plugin/src/plugin.ts @@ -52,10 +52,38 @@ export interface DevupUIBunPluginOptions { * `false` in `Bun.build`. */ debug?: boolean + /** + * Packages whose published `.js` files are compiled as well, as in the + * other plugins: a library that ships Devup UI components uncompiled. + * Each name matches that whole package (`pkg` is not `pkg-extra` or + * `@scope/pkg`), not the dependencies in its own `node_modules`. + */ + include?: readonly string[] } type SourceLoader = 'tsx' | 'ts' | 'jsx' | 'js' +const SEPARATOR = '[\\/\\\\]' + +/** + * The files the plugin loads: TypeScript and JSX sources anywhere, `.js` under + * Devup UI, and `.js` of the packages in `include`. An included package is the + * directory right under the innermost `node_modules` (pnpm and Bun stores + * included), matched the way `createNodeModulesExcludeRegex` matches it. + */ +function createSourceFilter(include: readonly string[]): RegExp { + const sources = `\\.(?:tsx?|jsx|mjs)$|${SEPARATOR}@devup-ui${SEPARATOR}.*\\.js$` + if (include.length === 0) return new RegExp(sources) + const names = include + .map((name) => + name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replaceAll('/', SEPARATOR), + ) + .join('|') + return new RegExp( + `${sources}|${SEPARATOR}node_modules${SEPARATOR}(?!.*${SEPARATOR}node_modules${SEPARATOR})(?:${names})${SEPARATOR}.*\\.js$`, + ) +} + /** * Load devup.json into the compiler. The first load fails on a theme that * cannot be loaded or registered: Bun runs once, and an empty theme would @@ -280,9 +308,7 @@ function DevupUI(options: DevupUIBunPluginOptions = {}) { // Load source files from packages directory (file namespace) build.onLoad( - { - filter: /\.(?:tsx?|jsx|mjs)$|[\\/]@devup-ui[\\/].*\.js$/, - }, + { filter: createSourceFilter(options.include ?? []) }, ({ path }) => { if (reservationPending) { reserveFiles()