Closed Leftium closed 2 years ago
This might be a problem with svelte-preprocess, so I opened this bug: https://github.com/sveltejs/svelte-preprocess/issues/436
This should be a Kit/Vite issue instead of svelte-preprocess. Currently, Vite only supports JS/TS for Svelte components, mainly because of Vite's prebundling scanning phase crawls the script tag of Svelte components in a hardcoded way. With that said, dev mode should fail as well but I'm surprised in sometimes work for you.
Unfortunately, there isn't a workaround for now, this would need to be supported by Vite, at least allowing its scanner to use a different esbuild loader
and adding an esbuild plugin for that.
I was able to fix this by changing lang="coffee" to the deprecated type="text/coffeescript" syntax. With the newer lang attribute, some part of build sometimes doesn't recognize the script is written in CoffeeScript.
Does this change your opinion about whether it's a Kit/Vite issue? It's a work-around, anyways.
The lang
to type
workaround is still a Kit/Vite issue, as the scanner has special handling for the lang
attribute, so setting type
only bypasses it.
To wrap up the state of CoffeeScript with SvelteKit/Vite. Currently, VIte handles pre-bundling scans for HTML-ish files, like Vue and Svelte, and they have a hardcoded way to transform them into JS. This however only works for JS/TS scripts which is based on esbuild's default loader. To have Vite support CoffeeScript, it'd need to have an esbuild plugin for it and interprets the script through the plugin. Otherwise, Vite would need to rework how it handles this case generically. And both aren't trivial tasks.
I'm commenting here to link this to the docs. And closing this as there's not much to fix in SvelteKit side. Feel free to open an issue over at VIte to support this feature though.
A work-around is to move the CoffeeScript outside the index.svelte file into its own _index.coffee file. Then use an external script tag like:
<script src='./_index.coffee'></script>
I confirmed this works with the latest SvelteKit on Vercel:
(The version with inline coffeescript still results in a build error on Vercel, even though there was none locally):
Describe the bug
Building my SvelteKit project (sometimes) results in a syntax error like:
Unexpected "load"
in the file index.svelte."Sometimes" meaning:
My best guess is the build doesn't always recognize the script is written in CoffeeScript, so Svelte/Vite sometimes process the scripts as JavaScript. (or TypeScript?)
Update/Workaround
I was able to fix this by changing
lang="coffee"
to the deprecatedtype="text/coffeescript"
syntax. With the newerlang
attribute, some part of build sometimes doesn't recognize the script is written in CoffeeScript.Old description:
Reproduction
https://github.com/Leftium/convert-crypto
Logs
(I modified the script to fix build warnings. The error is no longer
unexpected "load"
, but it is a similar syntax error.)Full Vercel build log:
Full Netlify build log:
Local build log (without the error):
System Info
Severity
serious, but I can work around it
Additional Information
It is possible this bug is related to its use of CoffeeScript.