vite-plugin / vite-plugin-commonjs

A pure JavaScript implementation for CommonJs
MIT License
105 stars 16 forks source link

Breaks when using ??= syntax #32

Closed jcbhmr closed 1 year ago

jcbhmr commented 1 year ago

image image image

vs

image image image

I assume that something about this package breaks when using ??= syntax?

jcbhmr commented 1 year ago

Yeah, it's this: image

??= is invalid 2020 syntax, so it goes to the catch {} block and ignores it; thus leading to there being no exports, and thus leading to a syntax error.

To fix this, I think that all you'd need to do is change 2020 to "latest" or 2023 image

jcbhmr commented 1 year ago

Here is the diff that solved my problem:

diff --git a/node_modules/vite-plugin-commonjs/dist/index.js b/node_modules/vite-plugin-commonjs/dist/index.js
index 9e8973b..73ebd5e 100644
--- a/node_modules/vite-plugin-commonjs/dist/index.js
+++ b/node_modules/vite-plugin-commonjs/dist/index.js
@@ -390,7 +390,7 @@ async function transformCommonjs({
     return;
   let ast;
   try {
-    ast = acorn.parse(code, { sourceType: "module", ecmaVersion: 2020 });
+    ast = acorn.parse(code, { sourceType: "module", ecmaVersion: "latest" });
   } catch (error) {
     return null;
   }
diff --git a/node_modules/vite-plugin-commonjs/dist/index.mjs b/node_modules/vite-plugin-commonjs/dist/index.mjs
index faaa42a..586d5d0 100644
--- a/node_modules/vite-plugin-commonjs/dist/index.mjs
+++ b/node_modules/vite-plugin-commonjs/dist/index.mjs
@@ -388,7 +388,7 @@ async function transformCommonjs({
     return;
   let ast;
   try {
-    ast = parse(code, { sourceType: "module", ecmaVersion: 2020 });
+    ast = parse(code, { sourceType: "module", ecmaVersion: "latest" });
   } catch (error) {
     return null;
   }

This issue body was partially generated by patch-package.

caoxiemeihao commented 1 year ago

acron@7.x allowed latest ecamVersion is 11 or 2020 see here, current vite-plugin-commonjs not force use the acron@8.x. Maybe ecmaVersion: 14 is better, because acron source code use the > operate for compare rather than ===

image
jcbhmr commented 1 year ago

What's the reference to acorn@7? 🤔 It looks like you use acorn@8 which should work with "latest" right? https://github.com/vite-plugin/vite-plugin-commonjs/blob/647fe26351c7b3a662afd7fc29569f068877cc3f/package.json#L29