From ff3b6f1824d171c928c09ac1a9a5a2fe6b96f8d9 Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Thu, 4 Dec 2025 06:38:12 +0800 Subject: [PATCH] refactor: oxc parse --- src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index ca65833..9f5210d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,11 @@ import { isBuiltin } from 'node:module' import path from 'node:path' import process from 'node:process' import { fileURLToPath, pathToFileURL } from 'node:url' -import { init, parse } from 'cjs-module-lexer' +import { parse as cjsParse, init } from 'cjs-module-lexer' import { up } from 'empathic/package' import { resolve } from 'import-meta-resolve' import { generateTransform, MagicStringAST } from 'magic-string-ast' -import { parseAst } from 'rolldown/parseAst' +import { parseSync as oxcParse } from 'rolldown/experimental' import { createFilter } from 'unplugin-utils' import { resolveOptions, type Options } from './options' import type { Plugin } from 'rolldown' @@ -53,11 +53,11 @@ export function RequireCJS(userOptions: Options = {}): Plugin { async handler(code, { fileName }, { file, dir }) { if (!filter(fileName)) return - const { body } = parseAst(code, { lang: undefined }, fileName) + const { program } = oxcParse(fileName, code) const s = new MagicStringAST(code) let usingRequire = false - for (const stmt of body) { + for (const stmt of program.body) { if (stmt.type === 'ImportDeclaration') { if (stmt.importKind === 'type') continue @@ -208,7 +208,7 @@ export async function isPureCJS( // detect by parsing const contents = await readFile(importResolved, 'utf8') try { - parse(contents, importResolved) + cjsParse(contents, importResolved) return true } catch {} } -- 2.51.2