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

plugin-react: parse error when work with asset suffix #5752

Closed
7 tasks done
ClarkXia opened this issue Nov 19, 2021 · 2 comments
Closed
7 tasks done

plugin-react: parse error when work with asset suffix #5752

ClarkXia opened this issue Nov 19, 2021 · 2 comments

Comments

@ClarkXia
Copy link

Describe the bug

fail to import script as a Worker, when using @vitejs/plugin-react

id resolved with suffix like /xxx/src/worker.ts?worker can't apply parserPlugins of typescript

if (/\.tsx?$/.test(id)) {
parserPlugins.push('typescript')
}

Reproduction

Importing Script as a Worker

import Worker from './worker.ts?worker'
const worker = new Worker()

Use typescript in worker.ts

onmessage = function(e) {
  console.log('Worker: Message received from main script');
  const result: number = e.data[0] * e.data[1];
  if (isNaN(result)) {
    postMessage('Please write two numbers');
  } else {
    const workerResult = `Result: ${result}`;
    console.log('Worker: Posting message back to main script');
    postMessage(workerResult);
  }
};

System Info

System:
    OS: macOS 10.15.7
    CPU: (4) x64 Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz
    Memory: 139.68 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.15.0 - /usr/local/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.14.8 - /usr/local/bin/npm
  Browsers:
    Chrome: 95.0.4638.69
    Safari: 15.0

Used Package Manager

yarn

Logs

No response

Validations

@IanVS
Copy link
Contributor

IanVS commented Nov 19, 2021

I just spent a few hours digging into why my web-test-runner tests started to fail, and I think it's due to exactly this issue. Starting in version 1.0.8 of @vitejs/plugin-react, the logic was changed from:

const isTypeScript = /\.tsx?$/.test(extension)
if (isTypeScript) {
    parserPlugins.push('typescript')
}

to:

if (/\.tsx?$/.test(id)) {
    parserPlugins.push("typescript");
}

Note that the entire id is being checked now, not just the extension. I'll submit a PR.

@IanVS
Copy link
Contributor

IanVS commented Nov 19, 2021

Hah, someone beat me to it. I think this can be closed by #5761.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants