Closed chris-pope closed 10 months ago
Okay! Can you provide a minimal reproduction repo? 👀
Thank you @chris-pope . This helped me a lot!
Ok, I dove into this a little more.
The class you defined above and the ones I'm using are using Class field declarations. According to chatGPT
Class field declarations are part of ECMAScript 2022 (ES12) and are a more recent addition to the language, providing a convenient way to define and initialize instance variables directly within the class.
So by only parsing to ecmaVersion: 2020 it will fail. You need atleast 2022
@caoxiemeihao
You can see the flowing code snippets:
https://github.com/caoxiemeihao/vite-electron-plugin/blob/main/examples/alias/vite.config.ts#L19-L21
plugins: [
alias(
[
{ find: '@common', replacement: path.join(__dirname, 'common') },
],
+ {
+ acornOptions: { ecmaVersion: 2022 },
+ },
),
]
First, I really like this plugin so thank you for making it available.
I'm not sure what the underlying cause of this is, but it appears a change in the acorn package will now result in a "Syntax Error" whenever a Class declares a Class Member. For example, declaring the follow class
class Foo { bar: string; }
Will now throw this unhelpful error message:
file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:3554 var err = new SyntaxError(message); ^ SyntaxError: Unexpected token (39:8) at pp$4.raise (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:3554:13) at pp$9.unexpected (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:762:8) at pp$9.expect (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:756:26) at pp$5.parseMethod (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:3309:8) at pp$8.parseClassMethod (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:1523:35) at pp$8.parseClassElement (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:1481:10) at pp$8.parseClass (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:1399:24) at pp$8.parseStatement (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:908:17) at pp$8.parseTopLevel (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:819:21) at Parser.parse (file:///C:/Users/Chris/Documents/test1/node_modules/acorn/dist/acorn.mjs:591:15) { pos: 1664, loc: Position { line: 39, column: 8 }, raisedAt: 1665 }
I found the "alias" plugin uses acorn to parse out the "requires" and "imports" statements, and hardcodes the emcaVersion to 2020. This worked on previous versions of the acorn package but doesn't anymore. I replaced the line const ast = acorn.parse(code, { ecmaVersion: 2020 }); with const ast = acorn.parse(code, { ecmaVersion: "latest" });
And the error goes away. It's not a long term solution but at least I can continue development. Hope this saves someone else a day of debugging.
Chris