Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaks when using ??= syntax #32

Closed
jcbhmr opened this issue Jul 14, 2023 · 4 comments · Fixed by #33
Closed

Breaks when using ??= syntax #32

jcbhmr opened this issue Jul 14, 2023 · 4 comments · Fixed by #33

Comments

@jcbhmr
Copy link

jcbhmr commented Jul 14, 2023

image
image
image

vs

image
image
image

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

@jcbhmr
Copy link
Author

jcbhmr commented Jul 14, 2023

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
Copy link
Author

jcbhmr commented Jul 14, 2023

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.

@jcbhmr jcbhmr changed the title Appears to break when using ??= syntax Breaks when using ??= syntax Jul 15, 2023
@caoxiemeihao
Copy link
Contributor

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
Copy link
Author

jcbhmr commented Jul 17, 2023

What's the reference to acorn@7? 🤔 It looks like you use acorn@8 which should work with "latest" right?

"acorn": "^8.8.2",

caoxiemeihao pushed a commit that referenced this issue Jul 17, 2023
@yejimeiming yejimeiming mentioned this issue Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants