@buzola/bun-plugin 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/plugin.test.d.ts +2 -0
- package/dist/__tests__/plugin.test.d.ts.map +1 -0
- package/dist/__tests__/plugin.test.js +127 -0
- package/dist/__tests__/plugin.test.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +13 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +29 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +32 -0
- package/src/__tests__/plugin.test.ts +148 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +42 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/plugin.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as os from 'node:os';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import { buzolaPlugin } from '../plugin.js';
|
|
6
|
+
async function runSetup(plugin) {
|
|
7
|
+
const captured = {};
|
|
8
|
+
const mockBuild = {
|
|
9
|
+
onResolve: (_opts, fn) => {
|
|
10
|
+
captured.resolve = fn;
|
|
11
|
+
},
|
|
12
|
+
onLoad: (_opts, fn) => {
|
|
13
|
+
captured.load = fn;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
await plugin.setup(mockBuild);
|
|
17
|
+
return captured;
|
|
18
|
+
}
|
|
19
|
+
function makeEmptyRoutesFixture() {
|
|
20
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'buzola-bun-test-'));
|
|
21
|
+
fs.mkdirSync(path.join(root, 'routes'));
|
|
22
|
+
return {
|
|
23
|
+
root,
|
|
24
|
+
cleanup: () => fs.rmSync(root, { recursive: true, force: true }),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
describe('buzolaPlugin (Bun)', () => {
|
|
28
|
+
it('uses default plugin name and virtual module ID', async () => {
|
|
29
|
+
const { root, cleanup } = makeEmptyRoutesFixture();
|
|
30
|
+
try {
|
|
31
|
+
const plugin = buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' });
|
|
32
|
+
expect(plugin.name).toBe('buzola');
|
|
33
|
+
const handlers = await runSetup(plugin);
|
|
34
|
+
expect(handlers.resolve({ path: 'virtual:buzola/routes' })).toEqual({
|
|
35
|
+
path: 'virtual:buzola/routes',
|
|
36
|
+
namespace: 'buzola',
|
|
37
|
+
});
|
|
38
|
+
expect(handlers.resolve({ path: 'virtual:buzola/other' })).toBeUndefined();
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
cleanup();
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
it('uses named plugin and virtual module ID when name is set', async () => {
|
|
45
|
+
const { root, cleanup } = makeEmptyRoutesFixture();
|
|
46
|
+
try {
|
|
47
|
+
const plugin = buzolaPlugin({
|
|
48
|
+
root,
|
|
49
|
+
routesDir: 'routes',
|
|
50
|
+
output: 'buzola.gen.ts',
|
|
51
|
+
name: 'admin',
|
|
52
|
+
});
|
|
53
|
+
expect(plugin.name).toBe('buzola:admin');
|
|
54
|
+
const handlers = await runSetup(plugin);
|
|
55
|
+
expect(handlers.resolve({ path: 'virtual:buzola/admin/routes' })).toEqual({
|
|
56
|
+
path: 'virtual:buzola/admin/routes',
|
|
57
|
+
namespace: 'buzola',
|
|
58
|
+
});
|
|
59
|
+
expect(handlers.resolve({ path: 'virtual:buzola/routes' })).toBeUndefined();
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
cleanup();
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
it('onLoad returns a ts module re-exporting from the generated file', async () => {
|
|
66
|
+
const { root, cleanup } = makeEmptyRoutesFixture();
|
|
67
|
+
try {
|
|
68
|
+
const plugin = buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' });
|
|
69
|
+
const handlers = await runSetup(plugin);
|
|
70
|
+
const result = handlers.load({});
|
|
71
|
+
expect(result.loader).toBe('ts');
|
|
72
|
+
expect(result.contents).toContain('export { routes, pageRegistry }');
|
|
73
|
+
expect(result.contents).toContain(JSON.stringify(path.join(root, 'buzola.gen')));
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
cleanup();
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
it('writes buzola.gen.ts during setup when routes dir exists', async () => {
|
|
80
|
+
const { root, cleanup } = makeEmptyRoutesFixture();
|
|
81
|
+
try {
|
|
82
|
+
const outputPath = path.join(root, 'buzola.gen.ts');
|
|
83
|
+
const plugin = buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' });
|
|
84
|
+
expect(fs.existsSync(outputPath)).toBe(false);
|
|
85
|
+
await runSetup(plugin);
|
|
86
|
+
expect(fs.existsSync(outputPath)).toBe(true);
|
|
87
|
+
expect(fs.readFileSync(outputPath, 'utf-8')).toContain('pageRegistry');
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
cleanup();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
it('skips generation when routes dir does not exist', async () => {
|
|
94
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'buzola-bun-test-'));
|
|
95
|
+
try {
|
|
96
|
+
const outputPath = path.join(root, 'buzola.gen.ts');
|
|
97
|
+
const plugin = buzolaPlugin({ root, routesDir: 'nonexistent', output: 'buzola.gen.ts' });
|
|
98
|
+
await runSetup(plugin);
|
|
99
|
+
expect(fs.existsSync(outputPath)).toBe(false);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
it('Bun.build resolves virtual:buzola/routes through the plugin', async () => {
|
|
106
|
+
const { root, cleanup } = makeEmptyRoutesFixture();
|
|
107
|
+
try {
|
|
108
|
+
const entryPath = path.join(root, 'entry.ts');
|
|
109
|
+
fs.writeFileSync(entryPath, `import { routes, pageRegistry } from 'virtual:buzola/routes'\nexport { routes, pageRegistry }\n`);
|
|
110
|
+
const result = await Bun.build({
|
|
111
|
+
entrypoints: [entryPath],
|
|
112
|
+
plugins: [buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' })],
|
|
113
|
+
target: 'browser',
|
|
114
|
+
external: ['react', '@buzola/router'],
|
|
115
|
+
});
|
|
116
|
+
expect(result.success).toBe(true);
|
|
117
|
+
expect(result.outputs.length).toBeGreaterThan(0);
|
|
118
|
+
const bundle = await result.outputs[0].text();
|
|
119
|
+
expect(bundle).toContain('pageRegistry');
|
|
120
|
+
expect(bundle).toContain('buildRouteTree');
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
cleanup();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
//# sourceMappingURL=plugin.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.test.js","sourceRoot":"","sources":["../../src/__tests__/plugin.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,UAAU,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,SAAS,CAAA;AAC7B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAA;AAC7B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAU3C,KAAK,UAAU,QAAQ,CAAC,MAAyD;IAChF,MAAM,QAAQ,GAAqB,EAAE,CAAA;IACrC,MAAM,SAAS,GAAG;QACjB,SAAS,EAAE,CAAC,KAAc,EAAE,EAAkB,EAAE,EAAE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,YAAY,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC,CAAC;gBAC/E,MAAM,EAAE,SAAS;gBACjB,QAAQ,EAAE,CAAC,OAAO,EAAE,gBAAgB,CAAC;aACrC,CAAC,CAAA;YAEF,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACjC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAA;YAEhD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,IAAI,EAAE,CAAA;YAC9C,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,CAAA;YACxC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAA;QAC3C,CAAC;gBAAS,CAAC;YACV,OAAO,EAAE,CAAA;QACV,CAAC;IACF,CAAC,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,YAAY,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type BasePluginOptions } from '@buzola/codegen';
|
|
2
|
+
import type { BunPlugin } from 'bun';
|
|
3
|
+
export interface BuzolaPluginOptions extends BasePluginOptions {
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Bun plugin for Buzola page-centric routing. Scans the routes directory,
|
|
7
|
+
* generates `buzola.gen.ts`, and exposes the `virtual:buzola/routes` module.
|
|
8
|
+
*
|
|
9
|
+
* Options can also be provided via `buzola.config.ts` in the project root;
|
|
10
|
+
* plugin options take precedence. See `examples/bun` for a `Bun.build` setup.
|
|
11
|
+
*/
|
|
12
|
+
export declare function buzolaPlugin(options?: BuzolaPluginOptions): BunPlugin;
|
|
13
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,iBAAiB,EAOtB,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AAEpC,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB;CAAG;AAEjE;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,SAAS,CAqBzE"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { buildVirtualSource, BUZOLA_NAMESPACE, generate, resolveOptions, resolveVirtualModuleId, VIRTUAL_ID_PATTERN, } from '@buzola/codegen';
|
|
2
|
+
/**
|
|
3
|
+
* Bun plugin for Buzola page-centric routing. Scans the routes directory,
|
|
4
|
+
* generates `buzola.gen.ts`, and exposes the `virtual:buzola/routes` module.
|
|
5
|
+
*
|
|
6
|
+
* Options can also be provided via `buzola.config.ts` in the project root;
|
|
7
|
+
* plugin options take precedence. See `examples/bun` for a `Bun.build` setup.
|
|
8
|
+
*/
|
|
9
|
+
export function buzolaPlugin(options = {}) {
|
|
10
|
+
const { pluginName, id: virtualId } = resolveVirtualModuleId(options.name);
|
|
11
|
+
return {
|
|
12
|
+
name: pluginName,
|
|
13
|
+
async setup(build) {
|
|
14
|
+
const generateOptions = await resolveOptions(options);
|
|
15
|
+
await generate({ ...generateOptions, optional: true });
|
|
16
|
+
const virtualSource = buildVirtualSource(generateOptions.outputPath);
|
|
17
|
+
build.onResolve({ filter: VIRTUAL_ID_PATTERN }, args => {
|
|
18
|
+
if (args.path !== virtualId)
|
|
19
|
+
return;
|
|
20
|
+
return { path: args.path, namespace: BUZOLA_NAMESPACE };
|
|
21
|
+
});
|
|
22
|
+
build.onLoad({ filter: /.*/, namespace: BUZOLA_NAMESPACE }, () => ({
|
|
23
|
+
contents: virtualSource,
|
|
24
|
+
loader: 'ts',
|
|
25
|
+
}));
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,cAAc,EACd,sBAAsB,EACtB,kBAAkB,GAClB,MAAM,iBAAiB,CAAA;AAKxB;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,UAA+B,EAAE;IAC7D,MAAM,EAAE,UAAU,EAAE,EAAE,EAAE,SAAS,EAAE,GAAG,sBAAsB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAE1E,OAAO;QACN,IAAI,EAAE,UAAU;QAChB,KAAK,CAAC,KAAK,CAAC,KAAK;YAChB,MAAM,eAAe,GAAG,MAAM,cAAc,CAAC,OAAO,CAAC,CAAA;YACrD,MAAM,QAAQ,CAAC,EAAE,GAAG,eAAe,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;YACtD,MAAM,aAAa,GAAG,kBAAkB,CAAC,eAAe,CAAC,UAAU,CAAC,CAAA;YAEpE,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,kBAAkB,EAAE,EAAE,IAAI,CAAC,EAAE;gBACtD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS;oBAAE,OAAM;gBACnC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAAA;YACxD,CAAC,CAAC,CAAA;YAEF,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,gBAAgB,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;gBAClE,QAAQ,EAAE,aAAa;gBACvB,MAAM,EAAE,IAAI;aACZ,CAAC,CAAC,CAAA;QACJ,CAAC;KACD,CAAA;AACF,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@buzola/bun-plugin",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"bun": "./src/index.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/contember/buzola",
|
|
21
|
+
"directory": "packages/bun-plugin"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@buzola/codegen": "^0.0.1",
|
|
25
|
+
"@buzola/router": "^0.0.11"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/bun": "latest",
|
|
29
|
+
"@types/node": "^25.3.3",
|
|
30
|
+
"typescript": "^5.7.0"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import * as fs from 'node:fs'
|
|
3
|
+
import * as os from 'node:os'
|
|
4
|
+
import * as path from 'node:path'
|
|
5
|
+
import { buzolaPlugin } from '../plugin.js'
|
|
6
|
+
|
|
7
|
+
type ResolveHandler = (args: { path: string }) => unknown
|
|
8
|
+
type LoadHandler = (args: unknown) => { contents: string; loader: string }
|
|
9
|
+
|
|
10
|
+
interface CapturedHandlers {
|
|
11
|
+
resolve?: ResolveHandler
|
|
12
|
+
load?: LoadHandler
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function runSetup(plugin: { setup: (build: never) => void | Promise<void> }): Promise<CapturedHandlers> {
|
|
16
|
+
const captured: CapturedHandlers = {}
|
|
17
|
+
const mockBuild = {
|
|
18
|
+
onResolve: (_opts: unknown, fn: ResolveHandler) => {
|
|
19
|
+
captured.resolve = fn
|
|
20
|
+
},
|
|
21
|
+
onLoad: (_opts: unknown, fn: LoadHandler) => {
|
|
22
|
+
captured.load = fn
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
await plugin.setup(mockBuild as never)
|
|
26
|
+
return captured
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function makeEmptyRoutesFixture(): { root: string; cleanup: () => void } {
|
|
30
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'buzola-bun-test-'))
|
|
31
|
+
fs.mkdirSync(path.join(root, 'routes'))
|
|
32
|
+
return {
|
|
33
|
+
root,
|
|
34
|
+
cleanup: () => fs.rmSync(root, { recursive: true, force: true }),
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('buzolaPlugin (Bun)', () => {
|
|
39
|
+
it('uses default plugin name and virtual module ID', async () => {
|
|
40
|
+
const { root, cleanup } = makeEmptyRoutesFixture()
|
|
41
|
+
try {
|
|
42
|
+
const plugin = buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' })
|
|
43
|
+
expect(plugin.name).toBe('buzola')
|
|
44
|
+
|
|
45
|
+
const handlers = await runSetup(plugin)
|
|
46
|
+
expect(handlers.resolve!({ path: 'virtual:buzola/routes' })).toEqual({
|
|
47
|
+
path: 'virtual:buzola/routes',
|
|
48
|
+
namespace: 'buzola',
|
|
49
|
+
})
|
|
50
|
+
expect(handlers.resolve!({ path: 'virtual:buzola/other' })).toBeUndefined()
|
|
51
|
+
} finally {
|
|
52
|
+
cleanup()
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('uses named plugin and virtual module ID when name is set', async () => {
|
|
57
|
+
const { root, cleanup } = makeEmptyRoutesFixture()
|
|
58
|
+
try {
|
|
59
|
+
const plugin = buzolaPlugin({
|
|
60
|
+
root,
|
|
61
|
+
routesDir: 'routes',
|
|
62
|
+
output: 'buzola.gen.ts',
|
|
63
|
+
name: 'admin',
|
|
64
|
+
})
|
|
65
|
+
expect(plugin.name).toBe('buzola:admin')
|
|
66
|
+
|
|
67
|
+
const handlers = await runSetup(plugin)
|
|
68
|
+
expect(handlers.resolve!({ path: 'virtual:buzola/admin/routes' })).toEqual({
|
|
69
|
+
path: 'virtual:buzola/admin/routes',
|
|
70
|
+
namespace: 'buzola',
|
|
71
|
+
})
|
|
72
|
+
expect(handlers.resolve!({ path: 'virtual:buzola/routes' })).toBeUndefined()
|
|
73
|
+
} finally {
|
|
74
|
+
cleanup()
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('onLoad returns a ts module re-exporting from the generated file', async () => {
|
|
79
|
+
const { root, cleanup } = makeEmptyRoutesFixture()
|
|
80
|
+
try {
|
|
81
|
+
const plugin = buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' })
|
|
82
|
+
const handlers = await runSetup(plugin)
|
|
83
|
+
const result = handlers.load!({})
|
|
84
|
+
expect(result.loader).toBe('ts')
|
|
85
|
+
expect(result.contents).toContain('export { routes, pageRegistry }')
|
|
86
|
+
expect(result.contents).toContain(JSON.stringify(path.join(root, 'buzola.gen')))
|
|
87
|
+
} finally {
|
|
88
|
+
cleanup()
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('writes buzola.gen.ts during setup when routes dir exists', async () => {
|
|
93
|
+
const { root, cleanup } = makeEmptyRoutesFixture()
|
|
94
|
+
try {
|
|
95
|
+
const outputPath = path.join(root, 'buzola.gen.ts')
|
|
96
|
+
const plugin = buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' })
|
|
97
|
+
expect(fs.existsSync(outputPath)).toBe(false)
|
|
98
|
+
|
|
99
|
+
await runSetup(plugin)
|
|
100
|
+
|
|
101
|
+
expect(fs.existsSync(outputPath)).toBe(true)
|
|
102
|
+
expect(fs.readFileSync(outputPath, 'utf-8')).toContain('pageRegistry')
|
|
103
|
+
} finally {
|
|
104
|
+
cleanup()
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('skips generation when routes dir does not exist', async () => {
|
|
109
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'buzola-bun-test-'))
|
|
110
|
+
try {
|
|
111
|
+
const outputPath = path.join(root, 'buzola.gen.ts')
|
|
112
|
+
const plugin = buzolaPlugin({ root, routesDir: 'nonexistent', output: 'buzola.gen.ts' })
|
|
113
|
+
|
|
114
|
+
await runSetup(plugin)
|
|
115
|
+
|
|
116
|
+
expect(fs.existsSync(outputPath)).toBe(false)
|
|
117
|
+
} finally {
|
|
118
|
+
fs.rmSync(root, { recursive: true, force: true })
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('Bun.build resolves virtual:buzola/routes through the plugin', async () => {
|
|
123
|
+
const { root, cleanup } = makeEmptyRoutesFixture()
|
|
124
|
+
try {
|
|
125
|
+
const entryPath = path.join(root, 'entry.ts')
|
|
126
|
+
fs.writeFileSync(
|
|
127
|
+
entryPath,
|
|
128
|
+
`import { routes, pageRegistry } from 'virtual:buzola/routes'\nexport { routes, pageRegistry }\n`,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
const result = await Bun.build({
|
|
132
|
+
entrypoints: [entryPath],
|
|
133
|
+
plugins: [buzolaPlugin({ root, routesDir: 'routes', output: 'buzola.gen.ts' })],
|
|
134
|
+
target: 'browser',
|
|
135
|
+
external: ['react', '@buzola/router'],
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
expect(result.success).toBe(true)
|
|
139
|
+
expect(result.outputs.length).toBeGreaterThan(0)
|
|
140
|
+
|
|
141
|
+
const bundle = await result.outputs[0]!.text()
|
|
142
|
+
expect(bundle).toContain('pageRegistry')
|
|
143
|
+
expect(bundle).toContain('buildRouteTree')
|
|
144
|
+
} finally {
|
|
145
|
+
cleanup()
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
})
|
package/src/index.ts
ADDED
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BasePluginOptions,
|
|
3
|
+
buildVirtualSource,
|
|
4
|
+
BUZOLA_NAMESPACE,
|
|
5
|
+
generate,
|
|
6
|
+
resolveOptions,
|
|
7
|
+
resolveVirtualModuleId,
|
|
8
|
+
VIRTUAL_ID_PATTERN,
|
|
9
|
+
} from '@buzola/codegen'
|
|
10
|
+
import type { BunPlugin } from 'bun'
|
|
11
|
+
|
|
12
|
+
export interface BuzolaPluginOptions extends BasePluginOptions {}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Bun plugin for Buzola page-centric routing. Scans the routes directory,
|
|
16
|
+
* generates `buzola.gen.ts`, and exposes the `virtual:buzola/routes` module.
|
|
17
|
+
*
|
|
18
|
+
* Options can also be provided via `buzola.config.ts` in the project root;
|
|
19
|
+
* plugin options take precedence. See `examples/bun` for a `Bun.build` setup.
|
|
20
|
+
*/
|
|
21
|
+
export function buzolaPlugin(options: BuzolaPluginOptions = {}): BunPlugin {
|
|
22
|
+
const { pluginName, id: virtualId } = resolveVirtualModuleId(options.name)
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
name: pluginName,
|
|
26
|
+
async setup(build) {
|
|
27
|
+
const generateOptions = await resolveOptions(options)
|
|
28
|
+
await generate({ ...generateOptions, optional: true })
|
|
29
|
+
const virtualSource = buildVirtualSource(generateOptions.outputPath)
|
|
30
|
+
|
|
31
|
+
build.onResolve({ filter: VIRTUAL_ID_PATTERN }, args => {
|
|
32
|
+
if (args.path !== virtualId) return
|
|
33
|
+
return { path: args.path, namespace: BUZOLA_NAMESPACE }
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
build.onLoad({ filter: /.*/, namespace: BUZOLA_NAMESPACE }, () => ({
|
|
37
|
+
contents: virtualSource,
|
|
38
|
+
loader: 'ts',
|
|
39
|
+
}))
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
}
|