@tsrx/bun-plugin-solid 0.0.81 → 0.0.83

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/README.md CHANGED
@@ -36,15 +36,24 @@ Bun.plugin(tsrxSolid());
36
36
 
37
37
  ## Options
38
38
 
39
- - `solid`: options forwarded to `babel-preset-solid`.
39
+ - `solid`: options forwarded to `babel-preset-solid`. TSRX2034 (a `yield` in a
40
+ template value that Solid's JSX compiler moves out of its generator) follows the
41
+ DOM output, and with `generate: 'ssr'` and `hydratable: true` the SSR output as
42
+ well. With `generate: 'ssr'` alone it follows the DOM output only, so a `yield`
43
+ that only the non-hydratable SSR output moves isn't reported.
40
44
  - `runtimeImports`: helper import mode (`'compiler'` by default, or `'direct'` for
41
45
  standalone runtime imports).
42
46
  - `platform`: optional override/fallback for the compile-time platform. Normally
43
47
  the plugin reads `tsrx.platform` from Bun's selected/nearest tsconfig, including
44
48
  `extends`. An override must match tsconfig. For `Bun.build`, all three exact
45
- flags are defined and conflicting definitions are rejected.
49
+ flags are defined and conflicting definitions are rejected. With `Bun.plugin()`,
50
+ including `bun:test` preloads, flags left in compiled `.tsrx` output are
51
+ replaced with their values.
46
52
  - `emitCss`: whether to emit virtual CSS imports (default: `true`).
47
- - `include`, `exclude`: regex filters for source files.
53
+ - `include`, `exclude`: regex filters for source files. Under `Bun.plugin()`, an
54
+ instance hands a `.tsrx` file it leaves out to the first registered TSRX plugin
55
+ instance that includes it. When no instance includes the file, Bun loads it as
56
+ it would without a plugin.
48
57
 
49
58
  When using `runtimeImports: 'direct'`, install the runtime as a direct production
50
59
  dependency of the package that owns the compiled modules:
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Bun plugin for @tsrx/solid (.tsrx modules)",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.81",
6
+ "version": "0.0.83",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -23,8 +23,8 @@
23
23
  "@babel/core": "^7.29.0",
24
24
  "@babel/preset-typescript": "^7.28.5",
25
25
  "babel-preset-solid": "2.0.0-rc.3",
26
- "@tsrx/core": "0.5.5",
27
- "@tsrx/solid": "0.3.5"
26
+ "@tsrx/core": "0.6.1",
27
+ "@tsrx/solid": "0.4.1"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@solidjs/web": "2.0.0-rc.3",
@@ -43,6 +43,7 @@
43
43
  "types"
44
44
  ],
45
45
  "scripts": {
46
- "test": "pnpm -w test --project bun-plugin-solid"
46
+ "test": "pnpm -w test --project bun-plugin-solid",
47
+ "test:native": "bun test tests/native.test.ts"
47
48
  }
48
49
  }
package/src/index.js CHANGED
@@ -1,10 +1,11 @@
1
- /** @import { BunPlugin } from 'bun' */
1
+ /** @import { BunPlugin, OnLoadResult } from 'bun' */
2
2
  /** @import { Platform, RuntimeImportMode } from '@tsrx/solid' */
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/solid';
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
 
10
11
  const require = createRequire(import.meta.url);
@@ -68,6 +69,80 @@ function to_css_id(file_path) {
68
69
  return file_path + CSS_QUERY;
69
70
  }
70
71
 
72
+ const RUNTIME_STATE = Symbol.for('@tsrx/bun-plugin.runtime.v1');
73
+
74
+ /**
75
+ * @typedef {{
76
+ * loaders: Array<{ compiles: (file_path: string) => boolean, load: (file_path: string) => Promise<OnLoadResult> }>,
77
+ * css: Map<string, string>,
78
+ * }} RuntimeState
79
+ */
80
+
81
+ /**
82
+ * State that every TSRX Bun plugin instance registered with Bun.plugin()
83
+ * shares. There Bun calls only the first onLoad hook whose filter matches and
84
+ * needs an object back, so the instance it calls serves the stylesheets any
85
+ * instance compiled and hands on the `.tsrx` files it leaves out. The key has
86
+ * a version, so a later change to this shape can take a new key without
87
+ * breaking older plugins installed alongside. `Bun.plugin.clearAll()` doesn't
88
+ * tell plugins, so instances registered before it stay in the list.
89
+ *
90
+ * @returns {RuntimeState}
91
+ */
92
+ function get_runtime_state() {
93
+ const scope = /** @type {any} */ (globalThis);
94
+ return (scope[RUNTIME_STATE] ??= { loaders: [], css: new Map() });
95
+ }
96
+
97
+ /**
98
+ * Loads a `.tsrx` file this instance leaves out under Bun.plugin(): with the
99
+ * first registered instance that compiles it, or else the way Bun loads it
100
+ * without a plugin, as a module whose default export is the file's path.
101
+ *
102
+ * @param {RuntimeState} runtime
103
+ * @param {string} file_path
104
+ * @returns {OnLoadResult | Promise<OnLoadResult>}
105
+ */
106
+ function load_elsewhere(runtime, file_path) {
107
+ const owner = runtime.loaders.find((loader) => loader.compiles(file_path));
108
+ return owner ? owner.load(file_path) : { exports: { default: file_path }, loader: 'object' };
109
+ }
110
+
111
+ /**
112
+ * Directory the tsconfig lookup starts from. Without `root`, Bun builds from
113
+ * the common ancestor directory of the entrypoints, or from the working
114
+ * directory when every entrypoint is an in-memory `files` entry. The nearest
115
+ * tsconfig is searched from there. A relative `tsconfig` option resolves from
116
+ * the working directory.
117
+ *
118
+ * @param {{ root?: string, tsconfig?: unknown, entrypoints?: unknown[], files?: object }} build_config
119
+ * @returns {string}
120
+ */
121
+ function get_tsconfig_base(build_config) {
122
+ if (build_config.root) return build_config.root;
123
+ const entrypoints = (build_config.entrypoints ?? []).filter(
124
+ (entrypoint) => typeof entrypoint === 'string',
125
+ );
126
+ const virtual_files = new Set(
127
+ Object.keys(build_config.files ?? {}).map((file) => path.resolve(file)),
128
+ );
129
+ if (
130
+ typeof build_config.tsconfig === 'string' ||
131
+ entrypoints.every((entrypoint) => virtual_files.has(path.resolve(entrypoint)))
132
+ ) {
133
+ return process.cwd();
134
+ }
135
+
136
+ let base = path.dirname(path.resolve(entrypoints[0]));
137
+ for (const entrypoint of entrypoints) {
138
+ const file_path = path.resolve(entrypoint);
139
+ while (/^\.\.(?:[/\\]|$)/.test(path.relative(base, file_path))) {
140
+ base = path.dirname(base);
141
+ }
142
+ }
143
+ return base;
144
+ }
145
+
71
146
  /**
72
147
  * @param {string} source
73
148
  * @param {string} file_path
@@ -115,12 +190,21 @@ export function tsrxSolid(options = {}) {
115
190
  setup(build) {
116
191
  const build_config = build.config ?? {};
117
192
  const platform = resolveBuildPlatform({
118
- root: build_config.root ?? process.cwd(),
193
+ root: get_tsconfig_base(build_config),
119
194
  tsconfig: typeof build_config.tsconfig === 'string' ? build_config.tsconfig : undefined,
120
195
  platform: explicit_platform,
121
196
  integration: '@tsrx/bun-plugin-solid',
122
197
  });
123
- const compile_options = { runtimeImports: options.runtimeImports, platform };
198
+ const solid_options = /** @type {{ generate?: string, hydratable?: boolean } | undefined} */ (
199
+ options.solid
200
+ );
201
+ const compile_options = {
202
+ runtimeImports: options.runtimeImports,
203
+ platform,
204
+ // babel-preset-solid compiles the SSR output with `generate: 'ssr'`.
205
+ // TSRX2034 follows it for the hydratable output, which moves more.
206
+ ssr: solid_options?.generate === 'ssr' && !!solid_options.hydratable,
207
+ };
124
208
  if (platform !== undefined && build.config) {
125
209
  build.config.define = /** @type {Record<string, string>} */ (
126
210
  mergePlatformDefinitions(build.config.define, platform, {
@@ -129,40 +213,64 @@ export function tsrxSolid(options = {}) {
129
213
  })
130
214
  );
131
215
  }
132
- build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => ({
133
- path: args.path,
134
- }));
135
-
136
- build.onLoad({ filter: CSS_QUERY_PATTERN }, (args) => ({
137
- contents: css_cache.get(args.path) ?? '',
138
- loader: 'css',
139
- }));
140
-
141
- build.onLoad(
142
- { filter: options.include ?? DEFAULT_INCLUDE, namespace: 'file' },
143
- async (args) => {
144
- if (!should_compile(options, args.path)) return undefined;
145
-
146
- const source = await readFile(args.path, 'utf-8');
147
- const { code, css } = compile(source, args.path, compile_options);
148
- const css_id = to_css_id(args.path);
149
-
150
- let output = code;
151
- if (emit_css && css) {
152
- css_cache.set(css_id, css);
153
- // After the module's own imports so imported themes' sheets load first
154
- // and a rule in the applying module wins at equal specificity.
155
- output = `${code}\nimport ${JSON.stringify(css_id)};\n`;
156
- } else {
157
- css_cache.delete(css_id);
158
- }
159
-
160
- return {
161
- contents: await transform_solid(output, args.path, options.solid),
162
- loader: 'js',
163
- };
164
- },
165
- );
216
+ // Bun.plugin() registration has no `define`, so the flags left in compiled
217
+ // output get their values written in directly.
218
+ const inline_platform = build.config ? undefined : platform;
219
+
220
+ const runtime = build.config ? undefined : get_runtime_state();
221
+ const stylesheets = runtime ? runtime.css : css_cache;
222
+
223
+ // Every instance sees every virtual CSS request. Leave the ones another
224
+ // instance compiled to that instance, except under Bun.plugin(), where
225
+ // the first instance has to answer and every instance shares the sheets.
226
+ build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => {
227
+ if (!stylesheets.has(args.path)) return undefined;
228
+ return { path: args.path };
229
+ });
230
+
231
+ build.onLoad({ filter: CSS_QUERY_PATTERN }, (args) => {
232
+ const css = stylesheets.get(args.path);
233
+ if (css !== undefined) return { contents: css, loader: 'css' };
234
+ return runtime ? { contents: '', loader: 'css' } : undefined;
235
+ });
236
+
237
+ /**
238
+ * @param {string} file_path
239
+ * @returns {Promise<OnLoadResult>}
240
+ */
241
+ const load_tsrx = async (file_path) => {
242
+ const source = await readFile(file_path, 'utf-8');
243
+ const { code: compiled, css } = compile(source, file_path, compile_options);
244
+ const code =
245
+ inline_platform === undefined
246
+ ? compiled
247
+ : replacePlatformFlags(compiled, file_path, inline_platform).code;
248
+ const css_id = to_css_id(file_path);
249
+
250
+ let output = code;
251
+ if (emit_css && css) {
252
+ stylesheets.set(css_id, css);
253
+ // After the module's own imports so imported themes' sheets load first
254
+ // and a rule in the applying module wins at equal specificity.
255
+ output = `${code}\nimport ${JSON.stringify(css_id)};\n`;
256
+ } else {
257
+ stylesheets.delete(css_id);
258
+ }
259
+
260
+ return {
261
+ contents: await transform_solid(output, file_path, options.solid),
262
+ loader: 'js',
263
+ };
264
+ };
265
+ runtime?.loaders.push({
266
+ compiles: (file_path) => should_compile(options, file_path),
267
+ load: load_tsrx,
268
+ });
269
+
270
+ build.onLoad({ filter: options.include ?? DEFAULT_INCLUDE, namespace: 'file' }, (args) => {
271
+ if (should_compile(options, args.path)) return load_tsrx(args.path);
272
+ return runtime ? load_elsewhere(runtime, args.path) : undefined;
273
+ });
166
274
  },
167
275
  };
168
276
  }
package/types/index.d.ts CHANGED
@@ -9,6 +9,11 @@ export interface TsrxSolidBunPluginOptions {
9
9
  include?: RegExp;
10
10
  exclude?: RegExp | RegExp[];
11
11
  emitCss?: boolean;
12
+ /**
13
+ * Options forwarded to `babel-preset-solid`. With `generate: 'ssr'` and
14
+ * `hydratable: true`, TSRX2034 also follows the SSR output, which moves
15
+ * more template values into arrows than the DOM output does.
16
+ */
12
17
  solid?: object;
13
18
  }
14
19