Skip to content

Commit 7c427df

Browse files
authored
fix(nuxt): fall back to wasm if oxc native bindings are missing (#31190)
1 parent 0ebaa51 commit 7c427df

File tree

5 files changed

+156
-67
lines changed

5 files changed

+156
-67
lines changed

packages/nuxt/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"@nuxt/schema": "workspace:*",
8181
"@nuxt/telemetry": "^2.6.5",
8282
"@nuxt/vite-builder": "workspace:*",
83+
"@oxc-parser/wasm": "^0.56.3",
8384
"@unhead/vue": "^2.0.0-rc.1",
8485
"@vue/shared": "^3.5.13",
8586
"c12": "^3.0.2",
@@ -111,7 +112,7 @@
111112
"ofetch": "^1.4.1",
112113
"ohash": "^2.0.11",
113114
"on-change": "^5.0.1",
114-
"oxc-parser": "^0.56.0",
115+
"oxc-parser": "^0.56.3",
115116
"pathe": "^2.0.3",
116117
"perfect-debounce": "^1.0.0",
117118
"pkg-types": "^2.1.0",

packages/nuxt/src/core/nuxt.ts

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { ResolveDeepImportsPlugin } from './plugins/resolve-deep-imports'
5151
import { ResolveExternalsPlugin } from './plugins/resolved-externals'
5252
import { PrehydrateTransformPlugin } from './plugins/prehydrate'
5353
import { VirtualFSPlugin } from './plugins/virtual'
54+
import { initParser } from './utils/parse'
5455

5556
export function createNuxt (options: NuxtOptions): Nuxt {
5657
const hooks = createHooks<NuxtHooks>()
@@ -336,6 +337,8 @@ async function initNuxt (nuxt: Nuxt) {
336337
}
337338
}
338339

340+
await initParser()
341+
339342
// Support Nuxt VFS
340343
addBuildPlugin(VirtualFSPlugin(nuxt, { mode: 'server' }), { client: false })
341344
addBuildPlugin(VirtualFSPlugin(nuxt, { mode: 'client', alias: { '#internal/nitro': join(nuxt.options.buildDir, 'nitro.client.mjs') } }), { server: false })

packages/nuxt/src/core/utils/parse.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { walk as _walk } from 'estree-walker'
22
import type { Node, SyncHandler } from 'estree-walker'
33
import type { ArrowFunctionExpression, CatchClause, FunctionDeclaration, FunctionExpression, Identifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportSpecifier, Program, VariableDeclaration } from 'estree'
4-
import { parseSync } from 'oxc-parser'
54
import { type SameShape, type TransformOptions, type TransformResult, transform as esbuildTransform } from 'esbuild'
65
import { tryUseNuxt } from '@nuxt/kit'
76

@@ -35,10 +34,25 @@ export function walk (ast: Program | Node, callback: Partial<WalkOptions>) {
3534
})
3635
}
3736

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+
3852
export function parseAndWalk (code: string, sourceFilename: string, callback: WalkerCallback): Program
3953
export function parseAndWalk (code: string, sourceFilename: string, object: Partial<WalkOptions>): Program
4054
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'
4256
const ast = parseSync(sourceFilename, code, { sourceType: 'module', lang })
4357
walk(ast.program as unknown as Program, typeof callback === 'function' ? { enter: callback } : callback)
4458
return ast.program as unknown as Program

0 commit comments

Comments
 (0)