dai-shi / waku

⛩️ The minimal React framework
https://waku.gg
MIT License
3.91k stars 102 forks source link

Commonjs resolver plugin error since Waku 0.20.1 #677

Closed daanlenaerts closed 2 weeks ago

daanlenaerts commented 2 weeks ago

When building my project with Waku 0.20.1 I get the following error, which I didn't get in Waku 0.20.0. This specifically occurs because of this single line change. When reverting this line to the original implementation, everything builds fine again.

// vite-plugin-rsc-analyze.js
syntax: ext === '.ts' || ext === '.tsx' ? 'typescript' : 'ecmascript',

I am not entirely sure why this change was necessary, but I assume there's some good reasoning behind it. Do you have some information on this? I'd love to help figure out a solution.

Build failed in 1.42s
/Users/daan/Documents/GIT/project/node_modules/@swc/core/index.js:141
            return JSON.parse(bindings.parseSync(src, toBuffer(options), filename));
                                       ^

Error: [commonjs--resolver] 
  × Expected a semicolon
    ╭─[4:1]
  4 │   return new Promise(async(resolve, reject) => {
  5 │     await sql.begin(async sql => {
  6 │       let finish
  7 │       !oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)
    ·        ───
  8 │       const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`
  9 │ 
 10 │       const lo = {
    ╰────

Caused by:
    Syntax Error
file: /Users/daan/Documents/GIT/project/node_modules/postgres/src/large.js
    at Compiler.parseSync (/Users/daan/Documents/GIT/project/node_modules/@swc/core/index.js:141:40)
    at Module.parseSync (/Users/daan/Documents/GIT/project/node_modules/@swc/core/index.js:327:21)
    at Object.transform (file:///Users/daan/Documents/GIT/project/node_modules/waku/dist/lib/plugins/vite-plugin-rsc-analyze.js:24:33)
    at Object.handler (file:///Users/daan/Documents/GIT/project/node_modules/waku/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:67689:19)
    at file:///Users/daan/Documents/GIT/project/node_modules/rollup/dist/es/shared/node-entry.js:19611:40 {
  code: 'PLUGIN_ERROR',
  pluginCode: 'GenericFailure',
  plugin: 'commonjs--resolver',
  hook: 'resolveId',
  id: '/Users/daan/Documents/GIT/project/node_modules/postgres/src/large.js',
  watchFiles: [
...
dai-shi commented 2 weeks ago

Thanks for reporting!

Oh, this is interesting and unexpected. I thought typescript parser can parse any JS code. I wonder exactly what code causes the parse error.

daanlenaerts commented 2 weeks ago

I would expect the same, but apparently not.

This is part of the postgres module, which I was trying out in combination with Drizzle ORM. This is the code for which the error occurs: https://github.com/porsager/postgres/blob/master/src/large.js I don't see anything special though. Do you?

Would it be an option to revert back to the following, or do you have any arguments against it?

// vite-plugin-rsc-analyze.js
syntax: ext === '.ts' || ext === '.tsx' ? 'typescript' : 'ecmascript',
dai-shi commented 2 weeks ago

I'm fine to revert, but we need to understand the exact problem and the reason why typescript doesn't work.

dai-shi commented 2 weeks ago

we could experiment with swc cli.

daanlenaerts commented 2 weeks ago

Totally agree! I have done a quick test by compiling postgres/src/large.js with the swc cli. It works fine when using the ecmascript syntax, but the same error surfaces when using the typescript syntax. I'm not really sure yet what the reason might be.

➜  waku-test npx swc ./node_modules/postgres/src/large.js

  × Expected a semicolon
    ╭─[node_modules/postgres/src/large.js:4:1]
  4 │   return new Promise(async(resolve, reject) => {
  5 │     await sql.begin(async sql => {
  6 │       let finish
  7 │       !oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)
    ·        ───
  8 │       const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`
  9 │ 
 10 │       const lo = {
    ╰────

Caused by:
    Syntax Error
Error: Failed to compile 1 file with swc.
    at Object.assertCompilationResult (/Users/daan/Downloads/waku-test/node_modules/@swc/cli/lib/swc/util.js:164:15)
    at files (/Users/daan/Downloads/waku-test/node_modules/@swc/cli/lib/swc/file.js:205:19)
    at async _default (/Users/daan/Downloads/waku-test/node_modules/@swc/cli/lib/swc/file.js:224:9)
dai-shi commented 2 weeks ago

Thanks for trying! Can you try with tsc too? Then, we will know if it's swc issue or TS issue.

daanlenaerts commented 2 weeks ago

I extracted the large.js file and ran tsc as follows:

npx tsc ./large.js --allowJs --outDir ./dist

This seems to work fine, no errors reported. When I run npx swc ./large.js, again with the typescript syntax, the same error as earlier occurs.

So to me it seems like an issue with swc, although I don't fully understand why it happens.

daanlenaerts commented 2 weeks ago

I've also gone over the SWC docs on using swc instead of tsc, but haven't really found anything that makes a difference there.

dai-shi commented 2 weeks ago

Thanks for your investigation.

Let's assume that it's a bug in SWC. I'll open a PR for the workaround.