Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changepacks/changepack_log_bun_include_js.json
Original file line number Diff line number Diff line change
@@ -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"
}
20 changes: 18 additions & 2 deletions packages/bun-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand Down
72 changes: 72 additions & 0 deletions packages/bun-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
32 changes: 29 additions & 3 deletions packages/bun-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Loading