@tsrx/bun-plugin-vue 0.0.91 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +28 -5
  2. package/package.json +9 -8
  3. package/src/index.js +308 -57
package/README.md CHANGED
@@ -21,8 +21,8 @@ await Bun.build({
21
21
  });
22
22
  ```
23
23
 
24
- The plugin compiles `.tsrx` modules with `@tsrx/vue`, runs the downstream
25
- `vue-jsx-vapor` transform, strips the remaining TypeScript syntax with Bun, and
24
+ The plugin compiles `.tsrx` modules with `@tsrx/vue`, runs `vue-jsx-vapor`'s
25
+ macros and Vapor transform, strips the remaining TypeScript syntax with Bun, and
26
26
  emits sibling-scoped `<style>` blocks (each styles its siblings and everything
27
27
  below them) as virtual CSS modules.
28
28
 
@@ -34,17 +34,40 @@ import tsrxVue from '@tsrx/bun-plugin-vue';
34
34
  Bun.plugin(tsrxVue());
35
35
  ```
36
36
 
37
+ A preload compiles `.tsrx` modules and leaves every other module to Bun. It
38
+ doesn't add `vaporInteropPlugin` to `createVaporApp()` calls, as `Bun.build()`
39
+ does, so install it yourself where you need it:
40
+
41
+ ```ts
42
+ import { createVaporApp, vaporInteropPlugin } from 'vue';
43
+
44
+ createVaporApp(App).use(vaporInteropPlugin).mount('#app');
45
+ ```
46
+
47
+ Preloads cover `.tsrx` modules that export plain values. A component with a
48
+ template fails to load under `bun:test`, because Vue 3.6's `node` entry, which
49
+ Bun's runtime resolves, has no Vapor APIs such as `template`.
50
+
37
51
  ## Options
38
52
 
39
- - `vapor`: options forwarded to `vue-jsx-vapor`.
53
+ - `vapor`: options forwarded to `vue-jsx-vapor`. As in the Vite plugin, its macros
54
+ run by default (`macros: false` turns them off), their `include` and `exclude`
55
+ options see a module as `<file>.tsrx.tsx`, and the plugin serves the helper
56
+ modules they import. `defineStyle()` stylesheets must be CSS, since Bun can't
57
+ compile Sass, Less or Stylus.
40
58
  - `runtimeImports`: helper import mode (`'compiler'` by default, or `'direct'` for
41
59
  standalone runtime imports).
42
60
  - `platform`: optional override/fallback for the compile-time platform. Normally
43
61
  the plugin reads `tsrx.platform` from Bun's selected/nearest tsconfig, including
44
62
  `extends`. An override must match tsconfig. For `Bun.build`, all three exact
45
- flags are defined and conflicting definitions are rejected.
63
+ flags are defined and conflicting definitions are rejected. With `Bun.plugin()`,
64
+ including `bun:test` preloads, flags left in compiled `.tsrx` output are
65
+ replaced with their values.
46
66
  - `emitCss`: whether to emit virtual CSS imports (default: `true`).
47
- - `include`, `exclude`: regex filters for source files.
67
+ - `include`, `exclude`: regex filters for source files. Under `Bun.plugin()`, an
68
+ instance hands a `.tsrx` file it leaves out to the first registered TSRX plugin
69
+ instance that includes it. When no instance includes the file, Bun loads it as
70
+ it would without a plugin.
48
71
 
49
72
  When using `runtimeImports: 'direct'`, install the runtime as a direct production
50
73
  dependency of the package that owns the compiled modules:
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Bun plugin for @tsrx/vue (.tsrx modules)",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.91",
6
+ "version": "0.1.1",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -20,25 +20,26 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@tsrx/core": "0.5.5",
24
- "@tsrx/vue": "0.3.5"
23
+ "@tsrx/core": "0.6.1",
24
+ "@tsrx/vue": "0.4.1"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "bun": "^1.0.0",
28
- "vue": ">=3.5",
29
- "vue-jsx-vapor": ">=3.2.14"
28
+ "vue": ">=3.6.0-0",
29
+ "vue-jsx-vapor": ">=3.2.27"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/bun": "^1.3.9",
33
33
  "typescript": "^5.9.3",
34
- "vue": "3.6.0-beta.12",
35
- "vue-jsx-vapor": "^3.2.14"
34
+ "vue": "3.6.0-rc.14",
35
+ "vue-jsx-vapor": "^3.2.29"
36
36
  },
37
37
  "files": [
38
38
  "src",
39
39
  "types"
40
40
  ],
41
41
  "scripts": {
42
- "test": "pnpm -w test --project bun-plugin-vue"
42
+ "test": "pnpm -w test --project bun-plugin-vue",
43
+ "test:native": "bun test tests/native.test.ts"
43
44
  }
44
45
  }
package/src/index.js CHANGED
@@ -1,21 +1,36 @@
1
- /** @import { BunPlugin, Target, Transpiler } from 'bun' */
1
+ /** @import { BunPlugin, OnLoadResult, Target, Transpiler } from 'bun' */
2
2
  /** @import { Platform, RuntimeImportMode } from '@tsrx/vue' */
3
3
 
4
4
  import { readFile } from 'node:fs/promises';
5
+ import path from 'node:path';
5
6
  import { createRequire } from 'node:module';
6
7
  import { compile } from '@tsrx/vue';
7
- import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
8
+ import { mergePlatformDefinitions, replacePlatformFlags, validatePlatform } from '@tsrx/core';
8
9
  import { resolveBuildPlatform } from '@tsrx/core/config';
9
10
  import { addVaporInteropToCreateVaporApp } from '@tsrx/vue/interop';
10
11
 
11
12
  const require = createRequire(import.meta.url);
12
13
  const { transformVueJsxVapor } = require('vue-jsx-vapor/api');
14
+ // vue-jsx-vapor's plugin hooks as its Rollup plugin. Called outside Rollup,
15
+ // each hook applies its own `filter` first, as unplugin does for Rollup
16
+ // versions without hook filters.
17
+ const { default: create_vue_jsx_vapor_plugins } = require('vue-jsx-vapor/rollup');
13
18
 
14
19
  const DEFAULT_INCLUDE = /\.tsrx$/;
15
- const SOURCE_EXTENSION_PATTERN = /\.[cm]?[jt]sx?$/;
20
+ // JavaScript and TypeScript sources outside node_modules. Bun evaluates the
21
+ // lookahead itself, so dependencies never reach the interop hook.
22
+ const PROJECT_SOURCE_PATTERN = /^(?!.*[/\\]node_modules[/\\]).*\.[cm]?[jt]sx?$/;
16
23
  const CSS_QUERY = '?tsrx-css&lang.css';
17
24
  const CSS_QUERY_PATTERN = /\?tsrx-css&lang\.css$/;
18
- const NODE_MODULES_PATTERN = /[/\\]node_modules[/\\]/;
25
+ // The macros import helper modules by ids such as `/vue-jsx-vapor/props`. The
26
+ // plugin serves each at a path that carries the id in this query.
27
+ const VAPOR_HELPER_ID_PATTERN = /^\/vue-jsx-vapor\//;
28
+ const VAPOR_HELPER_IMPORT_PATTERN = /\b(from\s*|import\s*)"(\/vue-jsx-vapor\/[^"]+)"/g;
29
+ const VAPOR_HELPER_QUERY = '?vue-jsx-vapor=';
30
+ const VAPOR_HELPER_PATH_PATTERN = /\?vue-jsx-vapor=([^?&]+)$/;
31
+ const VUE_IMPORT_PATTERN = /\b(from\s*|import\s*)(["'])vue\2/g;
32
+ // A `defineStyle` stylesheet's language: `lang.css`, `lang.module.scss`, ...
33
+ const VAPOR_STYLESHEET_LANG_PATTERN = /&lang\.(?:module\.)?(\w+)$/;
19
34
  const DEFAULT_VAPOR_OPTIONS = {
20
35
  macros: true,
21
36
  compiler: {
@@ -78,14 +93,60 @@ function to_css_id(file_path) {
78
93
  return file_path + CSS_QUERY;
79
94
  }
80
95
 
96
+ const RUNTIME_STATE = Symbol.for('@tsrx/bun-plugin.runtime.v1');
97
+
98
+ /**
99
+ * @typedef {{
100
+ * loaders: Array<{
101
+ * compiles: (file_path: string) => boolean,
102
+ * load: (file_path: string) => Promise<OnLoadResult>,
103
+ * load_vapor_helper?: (id: string) => Promise<string | undefined>,
104
+ * }>,
105
+ * css: Map<string, string>,
106
+ * }} RuntimeState
107
+ */
108
+
109
+ /**
110
+ * State that every TSRX Bun plugin instance registered with Bun.plugin()
111
+ * shares. There Bun calls only the first onLoad hook whose filter matches and
112
+ * needs an object back, so the instance it calls serves the stylesheets any
113
+ * instance compiled, hands on the `.tsrx` files it leaves out, and serves the
114
+ * vue-jsx-vapor helper modules any Vue instance's output imports. The key has
115
+ * a version, so a later change to this shape can take a new key without
116
+ * breaking older plugins installed alongside. `Bun.plugin.clearAll()` doesn't
117
+ * tell plugins, so instances registered before it stay in the list.
118
+ *
119
+ * @returns {RuntimeState}
120
+ */
121
+ function get_runtime_state() {
122
+ const scope = /** @type {any} */ (globalThis);
123
+ return (scope[RUNTIME_STATE] ??= { loaders: [], css: new Map() });
124
+ }
125
+
81
126
  /**
127
+ * Loads a `.tsrx` file this instance leaves out under Bun.plugin(): with the
128
+ * first registered instance that compiles it, or else the way Bun loads it
129
+ * without a plugin, as a module whose default export is the file's path.
130
+ *
131
+ * @param {RuntimeState} runtime
132
+ * @param {string} file_path
133
+ * @returns {OnLoadResult | Promise<OnLoadResult>}
134
+ */
135
+ function load_elsewhere(runtime, file_path) {
136
+ const owner = runtime.loaders.find((loader) => loader.compiles(file_path));
137
+ return owner ? owner.load(file_path) : { exports: { default: file_path }, loader: 'object' };
138
+ }
139
+
140
+ /**
141
+ * Bun's default loader for the file: `.js` files may contain JSX.
142
+ *
82
143
  * @param {string} file_path
83
144
  * @returns {'js' | 'jsx' | 'ts' | 'tsx'}
84
145
  */
85
146
  function get_source_loader(file_path) {
86
147
  if (/\.[cm]?tsx$/.test(file_path)) return 'tsx';
87
148
  if (/\.[cm]?ts$/.test(file_path)) return 'ts';
88
- if (/\.[cm]?jsx$/.test(file_path)) return 'jsx';
149
+ if (/\.(?:[cm]?jsx|js)$/.test(file_path)) return 'jsx';
89
150
  return 'js';
90
151
  }
91
152
 
@@ -103,6 +164,41 @@ function create_transpiler(target) {
103
164
  });
104
165
  }
105
166
 
167
+ /**
168
+ * Directory the tsconfig lookup starts from. Without `root`, Bun builds from
169
+ * the common ancestor directory of the entrypoints, or from the working
170
+ * directory when every entrypoint is an in-memory `files` entry. The nearest
171
+ * tsconfig is searched from there. A relative `tsconfig` option resolves from
172
+ * the working directory.
173
+ *
174
+ * @param {{ root?: string, tsconfig?: unknown, entrypoints?: unknown[], files?: object }} build_config
175
+ * @returns {string}
176
+ */
177
+ function get_tsconfig_base(build_config) {
178
+ if (build_config.root) return build_config.root;
179
+ const entrypoints = (build_config.entrypoints ?? []).filter(
180
+ (entrypoint) => typeof entrypoint === 'string',
181
+ );
182
+ const virtual_files = new Set(
183
+ Object.keys(build_config.files ?? {}).map((file) => path.resolve(file)),
184
+ );
185
+ if (
186
+ typeof build_config.tsconfig === 'string' ||
187
+ entrypoints.every((entrypoint) => virtual_files.has(path.resolve(entrypoint)))
188
+ ) {
189
+ return process.cwd();
190
+ }
191
+
192
+ let base = path.dirname(path.resolve(entrypoints[0]));
193
+ for (const entrypoint of entrypoints) {
194
+ const file_path = path.resolve(entrypoint);
195
+ while (/^\.\.(?:[/\\]|$)/.test(path.relative(base, file_path))) {
196
+ base = path.dirname(base);
197
+ }
198
+ }
199
+ return base;
200
+ }
201
+
106
202
  /**
107
203
  * @param {TsrxVueBunPluginOptions['vapor']} options
108
204
  */
@@ -121,6 +217,85 @@ function resolve_vapor_options(options) {
121
217
  };
122
218
  }
123
219
 
220
+ /**
221
+ * @typedef {((...args: string[]) => unknown) | { handler: (...args: string[]) => unknown }} VueJsxVaporHook
222
+ * @typedef {{ name: string, load?: VueJsxVaporHook, transform?: VueJsxVaporHook }} VueJsxVaporPlugin
223
+ */
224
+
225
+ /**
226
+ * Calls a vue-jsx-vapor plugin hook and returns the code it produced, if any.
227
+ *
228
+ * @param {VueJsxVaporHook} hook
229
+ * @param {...string} args
230
+ * @returns {Promise<string | undefined>}
231
+ */
232
+ async function call_vapor_hook(hook, ...args) {
233
+ const result = /** @type {string | { code: string } | null | undefined} */ (
234
+ await (typeof hook === 'function' ? hook : hook.handler)(...args)
235
+ );
236
+ return typeof result === 'string' ? result : (result?.code ?? undefined);
237
+ }
238
+
239
+ /**
240
+ * vue-jsx-vapor set up as its Vite plugin sets it up: the macros, unless
241
+ * `macros` is false, expand before the Vapor transform, and the helper modules
242
+ * they import load through the same plugin hooks.
243
+ *
244
+ * @param {ReturnType<typeof resolve_vapor_options>} vapor_options
245
+ */
246
+ function create_vue_jsx_vapor(vapor_options) {
247
+ /** @type {VueJsxVaporPlugin[]} */
248
+ const plugins = [create_vue_jsx_vapor_plugins(vapor_options)].flat();
249
+ const macros = plugins.find((plugin) => plugin.name === '@vue-jsx-vapor/macros')?.transform;
250
+
251
+ return {
252
+ /**
253
+ * Expands the macros, unless their `include` and `exclude` options leave
254
+ * the module out, then runs the Vapor transform.
255
+ *
256
+ * @param {string} code
257
+ * @param {string} file_path the `.tsrx` module's path
258
+ * @returns {Promise<string>}
259
+ */
260
+ async transform(code, file_path) {
261
+ // The macros' `include` and `exclude` see the id the Vite plugin gives it.
262
+ const expanded = macros ? await call_vapor_hook(macros, code, `${file_path}.tsx`) : undefined;
263
+ const id = file_path.replace(/\.tsrx$/, '.tsx');
264
+ return transformVueJsxVapor(expanded ?? code, id, vapor_options, false, false, false).code;
265
+ },
266
+
267
+ /**
268
+ * Loads a helper module by its `/vue-jsx-vapor/…` id, then runs the
269
+ * transforms for helper modules, such as the one that compiles a
270
+ * `defineStyle` stylesheet.
271
+ *
272
+ * @param {string} id
273
+ * @returns {Promise<string | undefined>}
274
+ */
275
+ async load_helper(id) {
276
+ let code;
277
+ for (const { load } of plugins) {
278
+ code = load && (await call_vapor_hook(load, id));
279
+ if (code !== undefined) break;
280
+ }
281
+ if (code === undefined) return undefined;
282
+ for (const { transform } of plugins) {
283
+ code = (transform && (await call_vapor_hook(transform, code, id))) ?? code;
284
+ }
285
+ return code;
286
+ },
287
+ };
288
+ }
289
+
290
+ /**
291
+ * @param {string} anchor a file the helper module resolves its imports from
292
+ * @param {string} id
293
+ * @returns {string}
294
+ */
295
+ function to_vapor_helper_path(anchor, id) {
296
+ return anchor + VAPOR_HELPER_QUERY + encodeURIComponent(id);
297
+ }
298
+
124
299
  /**
125
300
  * Bun plugin for `.tsrx` files that compiles them through `@tsrx/vue`, runs
126
301
  * the downstream `vue-jsx-vapor` transform, then strips the remaining
@@ -133,7 +308,7 @@ function resolve_vapor_options(options) {
133
308
  export function tsrxVue(options = {}) {
134
309
  const explicit_platform = validatePlatform(options.platform);
135
310
  const emit_css = options.emitCss ?? true;
136
- const vapor_options = resolve_vapor_options(options.vapor);
311
+ const vue_jsx_vapor = create_vue_jsx_vapor(resolve_vapor_options(options.vapor));
137
312
 
138
313
  /** @type {Map<string, string>} */
139
314
  const css_cache = new Map();
@@ -145,8 +320,9 @@ export function tsrxVue(options = {}) {
145
320
  // build.config is only present for Bun.build(); runtime registration
146
321
  // via Bun.plugin(), including bun:test preloads, does not provide it.
147
322
  const build_config = build.config ?? {};
323
+ const base = get_tsconfig_base(build_config);
148
324
  const platform = resolveBuildPlatform({
149
- root: build_config.root ?? process.cwd(),
325
+ root: base,
150
326
  tsconfig: typeof build_config.tsconfig === 'string' ? build_config.tsconfig : undefined,
151
327
  platform: explicit_platform,
152
328
  integration: '@tsrx/bun-plugin-vue',
@@ -160,71 +336,146 @@ export function tsrxVue(options = {}) {
160
336
  })
161
337
  );
162
338
  }
339
+ // Bun.plugin() registration has no `define`, so the flags left in compiled
340
+ // output get their values written in directly.
341
+ const inline_platform = build.config ? undefined : platform;
163
342
  const transpiler = create_transpiler(build_config.target);
164
343
 
165
- build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => ({
166
- path: args.path,
167
- }));
168
-
169
- build.onLoad({ filter: CSS_QUERY_PATTERN }, (args) => ({
170
- contents: css_cache.get(args.path) ?? '',
171
- loader: 'css',
172
- }));
344
+ const runtime = build.config ? undefined : get_runtime_state();
345
+ const stylesheets = runtime ? runtime.css : css_cache;
173
346
 
174
- build.onLoad(
175
- { filter: options.include ?? DEFAULT_INCLUDE, namespace: 'file' },
176
- async (args) => {
177
- if (!should_compile(options, args.path)) return undefined;
347
+ // Every instance sees every virtual CSS request. Leave the ones another
348
+ // instance compiled to that instance, except under Bun.plugin(), where
349
+ // the first instance has to answer and every instance shares the sheets.
350
+ build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => {
351
+ if (!stylesheets.has(args.path)) return undefined;
352
+ return { path: args.path };
353
+ });
178
354
 
179
- const source = await readFile(args.path, 'utf-8');
180
- const { code, css } = compile(source, args.path, compile_options);
181
- const css_id = to_css_id(args.path);
182
-
183
- let output = code;
184
- if (emit_css && css) {
185
- css_cache.set(css_id, css);
186
- // After the module's own imports so imported themes' sheets load first
187
- // and a rule in the applying module wins at equal specificity.
188
- output = `${code}\nimport ${JSON.stringify(css_id)};\n`;
189
- } else {
190
- css_cache.delete(css_id);
191
- }
192
-
193
- const transformed = transformVueJsxVapor(
194
- output,
195
- args.path.replace(/\.tsrx$/, '.tsx'),
196
- vapor_options,
197
- false,
198
- false,
199
- false,
200
- );
355
+ build.onLoad({ filter: CSS_QUERY_PATTERN }, (args) => {
356
+ const css = stylesheets.get(args.path);
357
+ if (css !== undefined) return { contents: css, loader: 'css' };
358
+ return runtime ? { contents: '', loader: 'css' } : undefined;
359
+ });
201
360
 
202
- if (transpiler) {
203
- return {
204
- contents: transpiler.transformSync(transformed.code),
205
- loader: 'js',
206
- };
207
- }
361
+ // Bun.build() resolves the helper modules a build's compiled modules import
362
+ // to one path each, in the directory Bun builds from, where they resolve
363
+ // `vue`. Bun.plugin() never calls onResolve hooks, and resolves only paths
364
+ // to files, so there `load_tsrx` points each helper import at the file
365
+ // `vue` resolves to from the importing module, and the helper imports
366
+ // that file as `vue`. That path has no `.tsrx` in it for another plugin's
367
+ // `include` to see.
368
+ if (build.config) {
369
+ const anchor = path.join(base, 'vue-jsx-vapor');
370
+ build.onResolve({ filter: VAPOR_HELPER_ID_PATTERN }, (args) => {
371
+ if (!should_compile(options, args.importer)) return undefined;
372
+ return { path: to_vapor_helper_path(anchor, args.path) };
373
+ });
374
+ }
208
375
 
376
+ build.onLoad({ filter: VAPOR_HELPER_PATH_PATTERN }, async (args) => {
377
+ const [, encoded_id] = /** @type {RegExpExecArray} */ (
378
+ VAPOR_HELPER_PATH_PATTERN.exec(args.path)
379
+ );
380
+ const id = decodeURIComponent(encoded_id);
381
+ const stylesheet_lang = VAPOR_STYLESHEET_LANG_PATTERN.exec(id)?.[1];
382
+ // Bun.plugin() has no CSS loader, and a stylesheet does nothing there.
383
+ if (runtime && stylesheet_lang) return { exports: { default: {} }, loader: 'object' };
384
+ if (stylesheet_lang && stylesheet_lang !== 'css') {
385
+ throw new Error(
386
+ `@tsrx/bun-plugin-vue: Bun can't load a defineStyle.${stylesheet_lang} stylesheet (${id}). Use defineStyle() or defineStyle.css().`,
387
+ );
388
+ }
389
+ if (!runtime) {
390
+ const code = await vue_jsx_vapor.load_helper(id);
391
+ return code === undefined
392
+ ? undefined
393
+ : { contents: code, loader: stylesheet_lang ? 'css' : 'js' };
394
+ }
395
+ // Bun calls this hook for every instance's helper modules.
396
+ const vue = JSON.stringify(args.path.slice(0, args.path.indexOf(VAPOR_HELPER_QUERY)));
397
+ for (const loader of runtime.loaders) {
398
+ const code = await loader.load_vapor_helper?.(id);
399
+ if (code === undefined) continue;
209
400
  return {
210
- contents: transformed.code,
211
- loader: 'ts',
401
+ contents: code.replace(VUE_IMPORT_PATTERN, (_, keyword) => keyword + vue),
402
+ loader: 'js',
212
403
  };
213
- },
214
- );
404
+ }
405
+ throw new Error(`@tsrx/bun-plugin-vue: unknown vue-jsx-vapor helper module ${id}`);
406
+ });
407
+
408
+ /**
409
+ * @param {string} file_path
410
+ * @returns {Promise<OnLoadResult>}
411
+ */
412
+ const load_tsrx = async (file_path) => {
413
+ const source = await readFile(file_path, 'utf-8');
414
+ const { code: compiled, css } = compile(source, file_path, compile_options);
415
+ const code =
416
+ inline_platform === undefined
417
+ ? compiled
418
+ : replacePlatformFlags(compiled, file_path, inline_platform).code;
419
+ const css_id = to_css_id(file_path);
215
420
 
216
- build.onLoad({ filter: SOURCE_EXTENSION_PATTERN, namespace: 'file' }, async (args) => {
217
- if (NODE_MODULES_PATTERN.test(args.path)) return undefined;
421
+ let output = code;
422
+ if (emit_css && css) {
423
+ stylesheets.set(css_id, css);
424
+ // After the module's own imports so imported themes' sheets load first
425
+ // and a rule in the applying module wins at equal specificity.
426
+ output = `${code}\nimport ${JSON.stringify(css_id)};\n`;
427
+ } else {
428
+ stylesheets.delete(css_id);
429
+ }
218
430
 
219
- const source = await readFile(args.path, 'utf-8');
220
- const transformed = addVaporInteropToCreateVaporApp(source);
221
- if (transformed === source) return undefined;
431
+ let transformed = await vue_jsx_vapor.transform(output, file_path);
432
+ if (runtime) {
433
+ /** @type {string | undefined} */
434
+ let vue;
435
+ transformed = transformed.replace(VAPOR_HELPER_IMPORT_PATTERN, (_, keyword, id) => {
436
+ vue ??= Bun.resolveSync('vue', path.dirname(file_path));
437
+ return keyword + JSON.stringify(to_vapor_helper_path(vue, id));
438
+ });
439
+ }
440
+
441
+ if (transpiler) {
442
+ return {
443
+ contents: transpiler.transformSync(transformed),
444
+ loader: 'js',
445
+ };
446
+ }
222
447
 
223
448
  return {
224
449
  contents: transformed,
225
- loader: get_source_loader(args.path),
450
+ loader: 'ts',
226
451
  };
452
+ };
453
+ runtime?.loaders.push({
454
+ compiles: (file_path) => should_compile(options, file_path),
455
+ load: load_tsrx,
456
+ load_vapor_helper: vue_jsx_vapor.load_helper,
457
+ });
458
+
459
+ build.onLoad({ filter: options.include ?? DEFAULT_INCLUDE, namespace: 'file' }, (args) => {
460
+ if (should_compile(options, args.path)) return load_tsrx(args.path);
461
+ return runtime ? load_elsewhere(runtime, args.path) : undefined;
227
462
  });
463
+
464
+ // Only Bun.build() runs the interop rewrite. Under Bun.plugin() this hook
465
+ // would have to answer for every project module, and a module an async
466
+ // hook loads can't be `require()`d, which breaks CommonJS.
467
+ if (build.config) {
468
+ build.onLoad({ filter: PROJECT_SOURCE_PATTERN, namespace: 'file' }, async (args) => {
469
+ const source = await readFile(args.path, 'utf-8');
470
+ const transformed = addVaporInteropToCreateVaporApp(source);
471
+ if (transformed === source) return undefined;
472
+
473
+ return {
474
+ contents: transformed,
475
+ loader: get_source_loader(args.path),
476
+ };
477
+ });
478
+ }
228
479
  },
229
480
  };
230
481
  }