Skip to content

Commit 3ac3ed3

Browse files
committed
fix: browser environment
1 parent 3b7ecef commit 3ac3ed3

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

playground/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Vue from '@vitejs/plugin-vue'
22
import VueJsx from '@vitejs/plugin-vue-jsx'
33
import { defineConfig } from 'vite'
44
import Inspect from 'vite-plugin-inspect'
5-
import VueReactivityFunction from '../src/vite'
5+
import VueReactivityFunction from '../dist/vite'
66

77
export default defineConfig({
88
build: {

src/core/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ export function getRequire() {
8484
}
8585

8686
let parseSync: typeof import('oxc-parser').parseSync
87-
export async function getOxcParser() {
87+
// @ts-ignore
88+
export async function getOxcParser(browser = typeof window !== 'undefined') {
8889
if (!parseSync) {
8990
parseSync = await import(
90-
// @ts-ignore
91-
typeof window !== 'undefined'
91+
browser
9292
? 'https://cdn.jsdelivr.net/npm/@oxc-parser/binding-wasm32-wasi/browser-bundle.mjs'
9393
: 'oxc-parser'
9494
).then((mod) => mod.parseSync)

src/raw.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ import type { IdentifierName, Node } from 'oxc-parser'
2323
import type { UnpluginOptions } from 'unplugin'
2424

2525
let parseSync: typeof import('oxc-parser').parseSync
26-
if (__ESM__) {
27-
parseSync = await getOxcParser()
28-
} else {
29-
const require = getRequire()
30-
if (require) {
31-
parseSync = require('oxc-parser').parseSync
32-
}
33-
}
3426

35-
export function transformReactivityFunction(
27+
export async function transformReactivityFunction(
3628
code: string,
3729
ignore: string[],
3830
s: MagicStringAST,
3931
) {
32+
if (__ESM__) {
33+
parseSync = await getOxcParser()
34+
} else {
35+
const require = getRequire()
36+
if (require) {
37+
parseSync = require('oxc-parser').parseSync
38+
}
39+
}
4040
const { program } = parseSync('index.tsx', code, {
4141
sourceType: 'module',
4242
})
@@ -203,7 +203,7 @@ const plugin = (rawOptions: Options = {}): UnpluginOptions => {
203203
transformInclude(id) {
204204
return filter(id)
205205
},
206-
transform(code, id) {
206+
async transform(code, id) {
207207
const lang = getLang(id)
208208
let asts: {
209209
text: string
@@ -233,7 +233,7 @@ const plugin = (rawOptions: Options = {}): UnpluginOptions => {
233233

234234
for (const { text, offset } of asts) {
235235
s.offset = offset
236-
transformReactivityFunction(text, options.ignore, s)
236+
await transformReactivityFunction(text, options.ignore, s)
237237
}
238238

239239
return generateTransform(s, id)

src/volar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default plugin
4848

4949
let parseSync: typeof import('oxc-parser').parseSync
5050
if (__BROWSER__) {
51-
parseSync = await getOxcParser()
51+
parseSync = await getOxcParser(true)
5252
} else {
5353
const require = getRequire()
5454
if (require) {

0 commit comments

Comments
 (0)