Skip to content

Commit

Permalink
Replace acorn with oxc-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-cb authored and cyco130 committed Nov 30, 2024
1 parent 5ac50fe commit 4e18cd5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
3 changes: 1 addition & 2 deletions vite-plugin-cjs-interop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
"vitest": "^2.1.6"
},
"dependencies": {
"acorn": "^8.14.0",
"acorn-import-assertions": "^1.9.0",
"oxc-parser": "^0.36.0",
"estree-walker": "^3.0.3",
"magic-string": "^0.30.14",
"minimatch": "^10.0.1"
Expand Down
16 changes: 6 additions & 10 deletions vite-plugin-cjs-interop/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Plugin } from "vite";
import { Parser } from "acorn";
import { importAssertions } from "acorn-import-assertions";
import oxc from "oxc-parser";
import MagicString from "magic-string";
import { minimatch } from "minimatch";

Expand Down Expand Up @@ -53,12 +52,7 @@ export function cjsInterop(options: CjsInteropOptions): Plugin {
if (!client && !options?.ssr) return;
if (CSS_LANGS_RE.test(id)) return;

const ast = Parser.extend(importAssertions).parse(code, {
sourceType: "module",
ecmaVersion: "latest",
locations: true,
allowHashBang: true,
});
const { program: ast } = await oxc.parseAsync(code);

const toBeFixed: any[] = [];
const dynamicImportsToBeFixed: any[] = [];
Expand All @@ -74,7 +68,9 @@ export function cjsInterop(options: CjsInteropOptions): Plugin {
}
} else if (node.type === "ImportExpression") {
if (
node.source.type === "Literal" &&
// @ts-expect-error OXC uses StringLiteral and not Literal
node.source.type === "StringLiteral" &&
// @ts-expect-error OXC uses StringLiteral and not Literal
matchesDependencies(node.source.value as string)
) {
dynamicImportsToBeFixed.push(node);
Expand Down Expand Up @@ -112,7 +108,7 @@ export function cjsInterop(options: CjsInteropOptions): Plugin {
const name = `__cjsInterop${counter++}__`;
let changed = false;

for (const specifier of node.specifiers) {
for (const specifier of node.specifiers || []) {
if (specifier.type === "ImportDefaultSpecifier") {
changed = true;
destructurings.push(
Expand Down

0 comments on commit 4e18cd5

Please # to comment.