|
1 | 1 | import { walk as _walk } from 'estree-walker'
|
2 | 2 | import type { Node, SyncHandler } from 'estree-walker'
|
3 | 3 | import type { ArrowFunctionExpression, CatchClause, FunctionDeclaration, FunctionExpression, Identifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportSpecifier, Program, VariableDeclaration } from 'estree'
|
4 |
| -import { parseSync } from 'oxc-parser' |
5 | 4 | import { type SameShape, type TransformOptions, type TransformResult, transform as esbuildTransform } from 'esbuild'
|
6 | 5 | import { tryUseNuxt } from '@nuxt/kit'
|
7 | 6 |
|
@@ -35,10 +34,25 @@ export function walk (ast: Program | Node, callback: Partial<WalkOptions>) {
|
35 | 34 | })
|
36 | 35 | }
|
37 | 36 |
|
| 37 | +let parseSync: typeof import('oxc-parser').parseSync |
| 38 | + |
| 39 | +export async function initParser () { |
| 40 | + try { |
| 41 | + parseSync = await import('oxc-parser').then(r => r.parseSync) |
| 42 | + } catch { |
| 43 | + // this can fail on stackblitz so we fall back to wasm build |
| 44 | + const { parseSync: wasmParse } = await import('@oxc-parser/wasm') |
| 45 | + parseSync = (sourceFilename, code, options) => wasmParse(code, { |
| 46 | + sourceFilename: sourceFilename.replace(/\?.*$/, '') + `.${options?.lang || 'ts'}`, |
| 47 | + sourceType: 'module', |
| 48 | + }) as any |
| 49 | + } |
| 50 | +} |
| 51 | + |
38 | 52 | export function parseAndWalk (code: string, sourceFilename: string, callback: WalkerCallback): Program
|
39 | 53 | export function parseAndWalk (code: string, sourceFilename: string, object: Partial<WalkOptions>): Program
|
40 | 54 | export function parseAndWalk (code: string, sourceFilename: string, callback: Partial<WalkOptions> | WalkerCallback) {
|
41 |
| - const lang = sourceFilename.match(/\.[cm]?([jt]sx?)$/)?.[1] as 'js' | 'ts' | 'jsx' | 'tsx' | undefined |
| 55 | + const lang = sourceFilename.match(/\.[cm]?([jt]sx?)$/)?.[1] as 'js' | 'ts' | 'jsx' | 'tsx' | undefined || 'ts' |
42 | 56 | const ast = parseSync(sourceFilename, code, { sourceType: 'module', lang })
|
43 | 57 | walk(ast.program as unknown as Program, typeof callback === 'function' ? { enter: callback } : callback)
|
44 | 58 | return ast.program as unknown as Program
|
|
0 commit comments