|
1 | 1 | 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'; |
6 | 3 | import solid from 'babel-preset-solid'; |
7 | 4 | import { existsSync, readFileSync } from 'fs'; |
8 | 5 | import { mergeAndConcat } from 'merge-anything'; |
@@ -56,6 +53,23 @@ export interface ExtensionOptions { |
56 | 53 |
|
57 | 54 | export type Compiler = 'babel' | 'native'; |
58 | 55 | 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 | +} |
59 | 73 |
|
60 | 74 | /** Configuration options for vite-plugin-solid. */ |
61 | 75 | export interface Options { |
@@ -570,7 +584,8 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin { |
570 | 584 | return undefined; |
571 | 585 | } |
572 | 586 |
|
573 | | - const result = await transformJsxAsync(supportResult.code || '', { |
| 587 | + const { transformAsync } = await loadNativeCompiler(); |
| 588 | + const result = await transformAsync(supportResult.code || '', { |
574 | 589 | ...solidOptions, |
575 | 590 | filename: id, |
576 | 591 | sourceMap: true, |
|
0 commit comments