From edad226cd803ce11570071a65c7161c7c58073d3 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 10:44:20 +0900 Subject: [PATCH] fix(bun-plugin): compile the .js files of included packages The source onLoad filter accepted .js only under @devup-ui/, and the plugin had no include option, so a library that ships Devup UI components as uncompiled .js reached the runtime stubs. DevupUI and register take include, and the filter also accepts .js of each listed package. A package is the directory right under the innermost node_modules, so pnpm and Bun stores match, and names are escaped and matched whole, the same rules as createNodeModulesExcludeRegex in #914: pkg is not pkg-extra or @scope/pkg, foo.bar is not fooXbar, and the dependencies in an included package's own node_modules stay out. Refs #686 Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_bun_include_js.json | 7 ++ packages/bun-plugin/README.md | 16 +++++ .../bun-plugin/src/__tests__/plugin.test.ts | 72 +++++++++++++++++++ packages/bun-plugin/src/plugin.ts | 32 ++++++++- 4 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 .changepacks/changepack_log_bun_include_js.json 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 03b5d0223..c5eac6b7b 100644 --- a/packages/bun-plugin/README.md +++ b/packages/bun-plugin/README.md @@ -60,6 +60,22 @@ module in the bundle. Imports of the packages Devup UI takes the place of Class names are short in `Bun.build` and readable under the runtime; pass `debug` to choose. +## Libraries + +TypeScript and JSX sources are compiled wherever they are. Published `.js` +files are compiled only for Devup UI itself and the packages you list in +`include`, as in the other plugins: + +```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`. + ## 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 6e2ea54a2..49eacbe33 100644 --- a/packages/bun-plugin/src/__tests__/plugin.test.ts +++ b/packages/bun-plugin/src/__tests__/plugin.test.ts @@ -344,6 +344,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 284772623..2fdedbc47 100644 --- a/packages/bun-plugin/src/plugin.ts +++ b/packages/bun-plugin/src/plugin.ts @@ -50,10 +50,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$`, + ) +} + async function writeDataFiles(compiler: DevupCompiler): Promise { let theme = {} let tailwindFiles: string[] = [] @@ -262,9 +290,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()