Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { createRequire } from 'node:module'
3import { analyze } from 'yuku-analyzer'
4import { generate } from 'yuku-codegen'
5import { walk } from 'yuku-ast'
6import { load as loadNativeCore } from 'yuku-core'
7import { loadSync as loadWasmCore } from '@yuku-core/wasm'
8import type { Binding, Module } from 'yuku-analyzer'
9import type { Core, Expression, Node, Program } from '@yuku-toolchain/types'
10import type { GenerateResult as NativeGenerateResult } from 'yuku-codegen'
11β
12let core: Core | undefined
13β
14function loadWasm(): Core {
15 // `@yuku-core/wasm`'s `loadSync()` needs the bytes, so resolve the file
16 // from whichever build runs. Check `import.meta` first: the CJS build
17 // compiles it to `{}`, and `node -e` defines a global `__filename` in ESM
18 const require = createRequire(
19 // @ts-ignore TS1470: `import.meta` is only read in the ESM build
20 import.meta.url ?? __filename, // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- `{}.url` in the CJS build
21 )
22 return loadWasmCore(
23 readFileSync(require.resolve('@yuku-core/wasm/yuku-core.wasm')),
24 )
25}
26β
27function getCore(): Core {
28 if (!core) {
29 // WebContainers (StackBlitz, bolt.new) cannot load native addons, and
30 // Yuku no longer falls back to WebAssembly on its own since
31 // https://github.com/yuku-toolchain/yuku/releases/tag/v0.17.0
32 if (process.versions.webcontainer != null) {
33 core = loadWasm()
34 } else {
35 // Platforms without a native binary, or installs without optional
36 // dependencies, use WebAssembly
37 try {
38 core = loadNativeCore()
39 } catch (nativeError) {
40 try {
41 core = loadWasm()
42 } catch (wasmError) {
43 throw new AggregateError(
44 [nativeError, wasmError],
45 'Yuku could load neither its native binding nor its WebAssembly core',
46 )
47 }
48 }
49 }
50 }
51 return core
52}
53β
54export interface GenerateResult extends Omit<NativeGenerateResult, 'map'> {
55 map: {
56 version: number
57 names: Array<string>
58 sources: Array<string>
59 mappings: string
60 file?: string
61 sourceRoot?: string
62 sourcesContent?: Array<string | null>
63 } | null
64}
65β
66export interface AnalyzeModuleOptions {
67 code: string
68 filename?: string
69}
70β
71/** Parse and bind once. Treat this module's AST as the immutable source model. */
72export function analyzeModule({
73 code,
74 filename = 'input.tsx',
75}: AnalyzeModuleOptions): Module {
76 // Without the query, and a hash after the last path segment's extension (a
77 // `#` may also be part of a directory name)
78 const physicalName = filename.replace(/\?.*$/, '').replace(/#[^/\\]*$/, '')
79 const module = analyze(code, {
80 path: filename,
81 // JavaScript route files may contain JSX but never TypeScript, where
82 // `a < b > (c)` would be a call with type arguments
83 lang: /\.[cm]?ts$/.test(physicalName)
84 ? 'ts'
85 : /\.[cm]?jsx?$/.test(physicalName)
86 ? 'jsx'
87 : 'tsx',
88 sourceType: 'module',
89 attachComments: true,
90 core: getCore(),
91 })
92 const error = module.diagnostics.find(
93 (diagnostic) => diagnostic.severity === 'error',
94 )
95 if (error) {
96 const lines = code.slice(0, error.start).split(/\r\n|[\n\r\u2028\u2029]/)
97 const loc = { line: lines.length, column: lines[lines.length - 1]!.length }
98 throw Object.assign(
99 new SyntaxError(
100 `${filename}: ${error.message} (${loc.line}:${loc.column})`,
101 ),
102 { loc, pos: error.start },
103 )
104 }
105 return module
106}
107β
108/** Print an output tree without modifying the source model or its semantic tables. */
109/**
110 * File-level pragmas: JSX transform configuration, React Refresh's
111 * `@refresh reset`, and solid-refresh's `@refresh reload` and `@refresh skip`.
112 */
113const filePragma =
114 /@jsx(?:Frag|ImportSource|Runtime)?\b|@refresh (?:reload|reset|skip)\b/
115β
116/** Legal comments, which minifiers keep: `/*! β¦ *\/`, `@license`, `@preserve`. */
117const legalComment = /^!|@license\b|@preserve\b/
118β
119/**
120 * Pragma comments configure transforms that run after these compilers for the
121 * whole file, and Oxc only reads them from the comments that lead the file. So
122 * an output must start with the source's top-level pragmas: removing a
123 * statement drops the comments attached to it, and inserted statements would
124 * otherwise land above them. The legal comments that lead the source lead
125 * every output too.
126 */
127export function keepFilePragmas(source: Program, output: Program): void {
128 const first = output.body[0]
129 const kept = source.body.flatMap(
130 (statement, index) =>
131 statement.comments?.filter(
132 (comment) =>
133 filePragma.test(comment.value) ||
134 (index === 0 &&
135 comment.position === 'before' &&
136 legalComment.test(comment.value)),
137 ) ?? [],
138 )
139 if (!kept.length || !first) {
140 return
141 }
142 const values = new Set(kept.map((comment) => comment.value))
143 for (const statement of output.body) {
144 if (statement.comments) {
145 statement.comments = statement.comments.filter(
146 (comment) => !values.has(comment.value),
147 )
148 }
149 }
150 first.comments = [
151 ...kept.map((comment) => ({
152 ...comment,
153 position: 'before' as const,
154 sameLine: false,
155 })),
156 ...(first.comments ?? []),
157 ]
158}
159β
160/**
161 * The declaration of an `export` statement, to replace the statement: it keeps
162 * the statement's comments, such as a `#__NO_SIDE_EFFECTS__` annotation.
163 */
164export function unwrapExport<T extends Node>(
165 statement: Node,
166 declaration: T,
167): T {
168 if (statement !== declaration && statement.comments?.length) {
169 declaration.comments = [
170 ...statement.comments,
171 ...(declaration.comments ?? []),
172 ]
173 }
174 return declaration
175}
176β
177export function generateModule(
178 program: Program,
179 options?: { source: string; filename: string },
180): GenerateResult {
181 const result = generate(program, {
182 comments: 'all',
183 ...(options
184 ? {
185 sourceMap: {
186 source: options.source,
187 sourceFileName: options.filename,
188 sourcesContent: options.source,
189 },
190 }
191 : {}),
192 })
193 if (result.diagnostics.length) {
194 throw new Error(
195 result.diagnostics.map((diagnostic) => diagnostic.message).join('\n'),
196 )
197 }
198 return {
199 ...result,
200 map: result.map
201 ? {
202 version: result.map.version,
203 names: result.map.names,
204 sources: result.map.sources,
205 mappings: result.map.mappings,
206 ...(result.map.file === null ? {} : { file: result.map.file }),
207 ...(result.map.sourceRoot === null
208 ? {}
209 : { sourceRoot: result.map.sourceRoot }),
210 ...(result.map.sourcesContent === null
211 ? {}
212 : { sourcesContent: result.map.sourcesContent }),
213 }
214 : null,
215 }
216}
217β
218export interface ModuleAstClone {
219 program: Program
220 /** Resolve surviving copied nodes against the original semantic snapshot. */
221 originalNodes: WeakMap<Node, Node>
222}
223β
224export function cloneModuleAst(module: Module): ModuleAstClone {
225 const program = structuredClone(module.ast)
226 const originals: Array<Node> = []
227 walk(module.ast, {
228 enter(node) {
229 originals.push(node)
230 },
231 })
232 const originalNodes = new WeakMap<Node, Node>()
233 let index = 0
234 walk(program, {
235 enter(node) {
236 originalNodes.set(node, originals[index++]!)
237 },
238 })
239 return { program, originalNodes }
240}
241β
242const generatedReferences = new WeakMap<Node, Binding | string>()
243β
244/** Explicitly connect a new reference to a source binding; never infer local scope. */
245export function linkGeneratedReference<T extends Node>(
246 node: T,
247 reference: Binding | string,
248): T {
249 generatedReferences.set(node, reference)
250 return node
251}
252β
253export function generatedReferenceOf(node: Node): Binding | string | undefined {
254 return generatedReferences.get(node)
255}
256β
257/** Copy a generated fragment without dropping its native reference provenance. */
258export function cloneGeneratedNode<T extends Node>(node: T): T {
259 const copy = structuredClone(node)
260 const originals: Array<Node> = []
261 walk(node, {
262 enter(original) {
263 originals.push(original)
264 },
265 })
266 let index = 0
267 walk(copy, {
268 enter(current) {
269 const reference = generatedReferences.get(originals[index++]!)
270 if (reference) {
271 generatedReferences.set(current, reference)
272 }
273 },
274 })
275 return copy
276}
277β
278/** Parse compiler-owned snippets; their nodes have no original-source spans. */
279export function parseStatements(code: string): Program['body'] {
280 const module = analyzeModule({ code, filename: 'generated.tsx' })
281 for (const reference of module.unresolvedReferences) {
282 if (!reference.inTypePosition) {
283 generatedReferences.set(reference.node, reference.name)
284 }
285 }
286 walk(module.ast, {
287 enter(node) {
288 node.start = 0
289 node.end = 0
290 },
291 })
292 return module.ast.body
293}
294β
295export function parseExpression(code: string): Expression {
296 const statement = parseStatements(`(${code});`)[0]
297 if (statement?.type !== 'ExpressionStatement') {
298 throw new Error('Expected an expression')
299 }
300 return statement.expression.type === 'ParenthesizedExpression'
301 ? statement.expression.expression
302 : statement.expression
303}