Skip to content

Commit f718828

Browse files
lazy load native JSX compiler
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 125ffab commit f718828

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vite-plugin-solid': patch
3+
---
4+
5+
Lazy-load the native JSX compiler so environments without native addon support can still use the default Babel compiler.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ This will force SSR code in the produced files.
137137

138138
Choose the JSX compiler backend. `"babel"` keeps using `babel-preset-solid`.
139139
`"native"` uses the native compiler from `@dom-expressions/jsx-compiler`.
140+
The native compiler requires native Node addon support, so environments that
141+
disable native addons (for example StackBlitz WebContainers) should use the
142+
default `"babel"` compiler.
140143

141144
```ts
142145
import { defineConfig } from 'vite';

src/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as babel from '@babel/core';
2-
import {
3-
transformAsync as transformJsxAsync,
4-
type TransformOptions as JsxCompilerOptions,
5-
} from '@dom-expressions/jsx-compiler';
2+
import type { TransformOptions as JsxCompilerOptions } from '@dom-expressions/jsx-compiler';
63
import solid from 'babel-preset-solid';
74
import { existsSync, readFileSync } from 'fs';
85
import { mergeAndConcat } from 'merge-anything';
@@ -56,6 +53,23 @@ export interface ExtensionOptions {
5653

5754
export type Compiler = 'babel' | 'native';
5855
export type SolidOptions = Omit<JsxCompilerOptions, 'filename' | 'sourceMap'>;
56+
type NativeCompiler = typeof import('@dom-expressions/jsx-compiler');
57+
let nativeCompilerPromise: Promise<NativeCompiler> | undefined;
58+
59+
async function loadNativeCompiler() {
60+
try {
61+
return await (nativeCompilerPromise ??= import('@dom-expressions/jsx-compiler'));
62+
} catch (error) {
63+
nativeCompilerPromise = undefined;
64+
const reason = error instanceof Error ? `\n\nCause: ${error.message}` : '';
65+
throw new Error(
66+
'vite-plugin-solid: compiler: "native" requires native Node addon support. ' +
67+
'Environments that disable native addons, such as StackBlitz WebContainers, ' +
68+
'must use the default compiler: "babel".' +
69+
reason,
70+
);
71+
}
72+
}
5973

6074
/** Configuration options for vite-plugin-solid. */
6175
export interface Options {
@@ -570,7 +584,8 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
570584
return undefined;
571585
}
572586

573-
const result = await transformJsxAsync(supportResult.code || '', {
587+
const { transformAsync } = await loadNativeCompiler();
588+
const result = await transformAsync(supportResult.code || '', {
574589
...solidOptions,
575590
filename: id,
576591
sourceMap: true,

0 commit comments

Comments
 (0)