Skip to content

Commit e9b760e

Browse files
authored
fix: use @oxc-parser/wasm as parser fallback (#3391)
1 parent 37f4785 commit e9b760e

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/utils/parse.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import { provider } from 'std-env'
1+
// Adapted from https://github.com/nuxt/nuxt/blob/6d85c15fb1783b003bb5d7cdefdd22b54f871f9d/packages/nuxt/src/core/utils/parse.ts
22

33
export let parseSync: typeof import('oxc-parser').parseSync
44

55
export async function initParser() {
6-
parseSync = await (
7-
provider === 'stackblitz'
8-
? (import('@oxc-parser/wasm') as unknown as Promise<typeof import('oxc-parser')>)
9-
: import('oxc-parser')
10-
).then(r => r.parseSync)
6+
try {
7+
parseSync = await import('oxc-parser').then(r => r.parseSync)
8+
} catch (_) {
9+
console.warn('[nuxt-i18n]: Unable to import `oxc-parser`, falling back to `@oxc-parser/wasm`.')
10+
11+
const { parseSync: parse } = await import('@oxc-parser/wasm')
12+
parseSync = (filename, sourceText, options) =>
13+
// @ts-expect-error sourceType property conflict
14+
parse(sourceText, {
15+
...(options || {}),
16+
sourceFilename: filename.replace(/\?.*$/, '') + `.${options?.lang || 'ts'}`,
17+
sourceType: 'module'
18+
})
19+
}
1120
}

0 commit comments

Comments
 (0)