Skip to content

Commit 4c684f5

Browse files
authored
fix: use @oxc-parser/wasm in stackblitz env (#3388)
1 parent 710d4c1 commit 4c684f5

8 files changed

Lines changed: 44 additions & 10 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"@intlify/utils": "^0.13.0",
104104
"@miyaneee/rollup-plugin-json5": "^1.2.0",
105105
"@nuxt/kit": "^3.15.4",
106+
"@oxc-parser/wasm": "^0.53.0",
106107
"@rollup/plugin-yaml": "^4.1.2",
107108
"@vue/compiler-sfc": "^3.5.13",
108109
"debug": "^4.3.5",
@@ -115,6 +116,7 @@
115116
"oxc-parser": "^0.53.0",
116117
"pathe": "^1.1.1",
117118
"scule": "^1.1.1",
119+
"std-env": "^3.8.0",
118120
"ufo": "^1.3.1",
119121
"unplugin": "^1.10.1",
120122
"unplugin-vue-router": "^0.10.8",

pnpm-lock.yaml

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { prepareLayers } from './prepare/layers'
1919
import { prepareTranspile } from './prepare/transpile'
2020
import { prepareVite } from './prepare/vite'
2121
import { prepareTypeGeneration } from './prepare/type-generation'
22+
import { initParser } from './utils/parse'
2223

2324
export * from './types'
2425

@@ -35,6 +36,8 @@ export default defineNuxtModule<NuxtI18nOptions>({
3536
async setup(i18nOptions, nuxt) {
3637
const ctx = createContext(i18nOptions, nuxt)
3738

39+
await initParser()
40+
3841
/**
3942
* Prepare options
4043
*/

src/transform/i18n-function-injection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import createDebug from 'debug'
99
import MagicString from 'magic-string'
1010
import { walk } from 'estree-walker'
11-
import { parseSync } from 'oxc-parser'
1211
import { createUnplugin } from 'unplugin'
1312
import { parse as parseSFC } from '@vue/compiler-sfc'
1413
import { isVue } from './utils'
14+
import { parseSync } from '../utils/parse'
1515
import type { CallExpression, Pattern, Program } from 'estree'
1616
import type { BundlerPluginOptions } from './utils'
1717

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { promises as fs, readFileSync as _readFileSync, constants as FS_CONSTANT
22
import { createHash, type BinaryLike } from 'node:crypto'
33
import { resolvePath, useNuxt } from '@nuxt/kit'
44
import { parse as parsePath, resolve, relative, join } from 'pathe'
5-
import { parseSync } from 'oxc-parser'
65
import { defu } from 'defu'
76
import { genSafeVariableName } from 'knitwork'
87
import { isString, isArray } from '@intlify/shared'
98
import { NUXT_I18N_MODULE_ID, EXECUTABLE_EXTENSIONS, NULL_HASH } from './constants'
9+
import { parseSync } from './utils/parse'
1010

1111
import type { NuxtI18nOptions, LocaleInfo, VueI18nConfigPathInfo, LocaleType, LocaleFile, LocaleObject } from './types'
1212
import type { Nuxt, NuxtConfigLayer } from '@nuxt/schema'

src/utils/parse.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { provider } from 'std-env'
2+
3+
export let parseSync: typeof import('oxc-parser').parseSync
4+
5+
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)
11+
}

test/gen.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { generateLoaderOptions } from '../src/gen'
22
import { getNormalizedLocales, resolveLocales, resolveRelativeLocales, resolveVueI18nConfigInfo } from '../src/utils'
3-
import { vi, beforeEach, afterEach, test, expect } from 'vitest'
3+
import { vi, beforeEach, afterEach, test, expect, beforeAll } from 'vitest'
44

55
import type { LocaleInfo, LocaleObject, NuxtI18nOptions, VueI18nConfigPathInfo } from '../src/types'
66
import type { Nuxt } from '@nuxt/schema'
7+
import { initParser } from '../src/utils/parse'
78

89
vi.mock('node:fs')
910

@@ -29,6 +30,10 @@ vi.mock('@nuxt/kit', async () => {
2930
}
3031
})
3132

33+
beforeAll(async () => {
34+
await initParser()
35+
})
36+
3237
beforeEach(async () => {
3338
vi.spyOn(await import('node:fs'), 'readFileSync').mockReturnValue('export default {}')
3439
})

test/utils.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { resolveLocales } from '../src/utils'
22
import { parseSegment, getRoutePath } from '../src/utils/route-parsing'
33
import type { LocaleObject } from '../src/types'
4-
import { vi, beforeEach, afterEach, test, expect } from 'vitest'
4+
import { vi, beforeEach, afterEach, test, expect, beforeAll } from 'vitest'
5+
import { initParser } from '../src/utils/parse'
56

67
vi.mock('pathe', async () => {
78
const mod = await vi.importActual<typeof import('pathe')>('pathe')
@@ -23,6 +24,10 @@ vi.mock('@nuxt/kit', () => {
2324

2425
vi.mock('node:fs')
2526

27+
beforeAll(async () => {
28+
await initParser()
29+
})
30+
2631
beforeEach(async () => {
2732
vi.spyOn(await import('node:fs'), 'readFileSync').mockReturnValue(
2833
'export default defineI18nLocale(() => { return {} })'

0 commit comments

Comments
 (0)