-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (137 loc) · 4.07 KB
/
index.js
File metadata and controls
158 lines (137 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @oxc-solid-js/compiler - OXC-based JSX compiler for SolidJS
*
* CJS entry point - provides the same interface as babel-preset-solid.
*/
const { platform, arch } = require('node:process');
const { join } = require('node:path');
let nativeBinding = null;
const explicitPath = process.env.NAPI_RS_NATIVE_LIBRARY_PATH;
if (explicitPath) {
try {
nativeBinding = require(explicitPath);
} catch (e) {
console.warn(`@oxc-solid-js/compiler: Failed to load native module from ${explicitPath}.`);
console.warn(e instanceof Error ? e.message : String(e));
}
}
// Map Node.js platform/arch to binary file suffix
const platformMap = {
'darwin-arm64': 'darwin-arm64',
'darwin-x64': 'darwin-x64',
'linux-x64': 'linux-x64-gnu',
'linux-arm64': 'linux-arm64-gnu',
'win32-x64': 'win32-x64-msvc',
'win32-arm64': 'win32-arm64-msvc',
};
const platformKey = `${platform}-${arch}`;
const nativeTarget = platformMap[platformKey];
const optionalBinaryPackageMap = {
'darwin-arm64': '@oxc-solid-js/compiler-darwin-arm64',
'darwin-x64': '@oxc-solid-js/compiler-darwin-x64',
'linux-x64': '@oxc-solid-js/compiler-linux-x64-gnu',
'win32-x64': '@oxc-solid-js/compiler-win32-x64-msvc',
};
// Try to load the native module
if (!nativeBinding) {
const loadErrors = [];
try {
if (nativeTarget) {
// Try platform-specific binary colocated with package first
nativeBinding = require(join(__dirname, `oxc-solid-js-compiler.${nativeTarget}.node`));
} else {
// Fallback to generic name
nativeBinding = require(join(__dirname, 'oxc-solid-js-compiler.node'));
}
} catch (e) {
loadErrors.push(e);
}
if (!nativeBinding) {
const optionalPackage = optionalBinaryPackageMap[platformKey];
if (optionalPackage) {
try {
nativeBinding = require(optionalPackage);
} catch (e) {
loadErrors.push(e);
}
}
}
if (!nativeBinding) {
console.warn(
`@oxc-solid-js/compiler: Native module not found for ${platformKey}. Run \`npm run build\` to compile.`,
);
for (const err of loadErrors) {
console.warn(err instanceof Error ? err.message : String(err));
}
}
}
/**
* Default options matching babel-preset-solid
*/
const defaultOptions = {
moduleName: 'solid-js/web',
builtIns: [
'For',
'Show',
'Switch',
'Match',
'Suspense',
'SuspenseList',
'Portal',
'Index',
'Dynamic',
'ErrorBoundary',
],
contextToCustomElements: true,
wrapConditionals: true,
generate: 'dom', // 'dom' | 'ssr' | 'universal'
hydratable: false,
delegateEvents: true,
sourceMap: false,
};
/**
* Transform JSX source code
* @param {string} source - The source code to transform
* @param {object} options - Transform options
* @returns {{ code: string, map?: string }}
*/
function transform(source, options = {}) {
if (!nativeBinding) {
throw new Error(
'@oxc-solid-js/compiler: Native module not loaded. Ensure it is built for your platform.',
);
}
const mergedOptions = { ...defaultOptions, ...options };
// NAPI-RS automatically converts camelCase (JS) to snake_case (Rust)
// so we can pass options directly without manual conversion
return nativeBinding.transformJsx(source, mergedOptions);
}
/**
* Create a preset configuration (for compatibility with babel-preset-solid interface)
* @param {object} _context - Babel context (ignored, for compatibility)
* @param {object} options - User options
* @returns {object}
*/
function preset(_context, options = {}) {
const mergedOptions = { ...defaultOptions, ...options };
return {
// Return the options that would be passed to the transform
options: mergedOptions,
// The transform function
transform: (source) => transform(source, mergedOptions),
};
}
/**
* Low-level transform function from the native binding
*/
const transformJsx = nativeBinding ? nativeBinding.transformJsx : null;
exports.transform = transform;
exports.preset = preset;
exports.defaultOptions = defaultOptions;
exports.transformJsx = transformJsx;
exports.default = {
transform,
preset,
defaultOptions,
transformJsx,
};