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

Commonjs resolver plugin error since Waku 0.20.1 #677

Closed
daanlenaerts opened this issue Apr 27, 2024 · 9 comments · Fixed by #687
Closed

Commonjs resolver plugin error since Waku 0.20.1 #677

daanlenaerts opened this issue Apr 27, 2024 · 9 comments · Fixed by #687

Comments

@daanlenaerts
Copy link
Contributor

daanlenaerts commented Apr 27, 2024

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

dai-shi commented Apr 27, 2024

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

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

dai-shi commented Apr 29, 2024

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

@dai-shi
Copy link
Owner

dai-shi commented Apr 29, 2024

we could experiment with swc cli.

@daanlenaerts
Copy link
Contributor Author

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

dai-shi commented Apr 29, 2024

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

@daanlenaerts
Copy link
Contributor Author

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

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

dai-shi commented Apr 30, 2024

Thanks for your investigation.

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

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