question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

maybeJSX throws a fatal error instead of a warning

See original GitHub issue

Describe the bug

There should be some environmental variable to skip the maybeJSX variable bailing, or it should be a warning.

This flags incorrectly for us, as we’re specifically serving Svelte files without the Svelte plugin for integration with a file editor.

This comes from line 198 of importAnalysis.ts: https://github.com/vitejs/vite/blob/31f5ff3ef9ee071afa8cc66870e13e9753c3ab93/packages/vite/src/node/plugins/importAnalysis.ts#L198

Also see: https://github.com/vitejs/vite/discussions/6246

Reproduction

https://stackblitz.com/edit/vitejs-vite-9nnfuu?file=counter.js

System Info

System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 87.70 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Firefox: 104.0.1
    Safari: 15.6.1

Used Package Manager

npm

Logs

17:34:04 [vite] Internal server error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.
  Plugin: vite:import-analysis
  File: /home/projects/vitejs-vite-9nnfuu/foo/bar.svelte
  1  |  <script>
  2  |    let foo = 'bar';
  3  |  </script>
     |           ^
  4  |  <h1>this is a file that should be served as is</h1>
      at formatError (file:///home/projects/vitejs-vite-9nnfuu/node_modules/vite/dist/node/chunks/dep-665b0112.js:40828:46)
      at TransformContext.error (file:///home/projects/vitejs-vite-9nnfuu/node_modules/vite/dist/node/chunks/dep-665b0112.js:40824:19)
      at TransformContext.transform (file:///home/projects/vitejs-vite-9nnfuu/node_modules/vite/dist/node/chunks/dep-665b0112.js:37495:22)
      at async Object.transform (file:///home/projects/vitejs-vite-9nnfuu/node_modules/vite/dist/node/chunks/dep-665b0112.js:41077:30)
      at async loadAndTransform (file:///home/projects/vitejs-vite-9nnfuu/node_modules/vite/dist/node/chunks/dep-665b0112.js:37338:29)

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sapphi-redcommented, Sep 20, 2022

Although this would work, ideally we’d just have the server serve the file as-is, is this not possible with Vite currently?

Yes, ideally Vite should return the raw file with fetch('foo.svelte'). But it’s not currently possible. Vite needs to pass svelte files to plugin pipeline when it’s imported (e.g. import('foo.svelte')) and serve it as-is foo.svelte when it’s fetched. So Vite needs to differentiate fetch('foo.svelte') and import('foo.svelte') by HTTP header or something. But there isn’t any (Sec-Fetch-Dest is not supported by Safari yet.). For now, Vite currently assumes svelte files are imported and not fetched.

I rather add workarounds to the server than the client worker (if possible!)

I’ve come up with another idea. If you always need a raw file, a plugin like this might work as an workaround:

const plugin = {
  name: 'plugin',
  configureServer(server) {
    server.middleware.use(async (req, res, next) => {
      if (req.url.endsWith('.svelte')) { // needs to handle more precisely
        const content = await fs.readFile(path.resolve(root, req.url))
        res.end(content)
        return
      }
      next()
    })
  }
}
0reactions
sapphi-redcommented, Sep 20, 2022

There’s slightly related discussion here: #9981.

I think it would make sense to add the relevant functionality for ?raw to wrap in export default depending on Sec-Fetch-Dest even without Safari support, as this only seems to affect the development environment and you could easily warn on the console if the header was missing.

This won’t work. Even if the plugin handling ?raw returned the raw content without export default, the plugin after that plugin will try to parse that content as JS and the errors will happen.

Happy to fork this off into another issue if one doesn’t exist to avoid confusion with the maybeJSX bug ?

IMO this error is correct because plugin pipeline expects the content to be JS. The problem here is that foo.svelte is passed to plugin pipeline, even if it is fetched and not imported.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP converts Warnings to Fatal Errors - Stack Overflow
Suddenly, PHP starts converting common warnings to fatal errors (and thus I'm getting 500's). Examples: Fatal error: Comments starting with '#' ...
Read more >
Why is my site showing warnings/fatal errors? - SiteGround KB
Fatal errors and warnings can appear for many reasons. It may be a broken or misconfigured plugin or a theme or another misconfiguration...
Read more >
PHP Errors: 4 Different Types (Warning, Parse, Fatal, and ...
Learn about the 4 types of errors in PHP (Warning Errors, Notice Errors, Parse Errors, and Fatal Errors) and fix your script.
Read more >
PHP 8.0: @ Error Suppression operator does not silent fatal ...
For example, the unlink function emits a warning if the file does not exist, and calling it with the @ operator can suppress...
Read more >
trigger_error - Manual - PHP
trigger_error — Generates a user-level error/warning/notice message ... If error_type is E_USER_ERROR then trigger_error throw FATAL ERROR and script ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found