svelte-preprocess: Latest version is causing TypeScript unexpected token errors
Describe the bug Upgrading from 4.9.5 to 4.10.1, a new bug seems to appear with TypeScript classes resulting in svelte-preprocess to return unexpected token errors.
Currently to prevent this issue, you need to downgrade svelte-preprocess to 4.9.5.
Logs
12:12:46 PM [vite] Internal server error: Unexpected token
Plugin: vite-plugin-svelte
39 | class BackgroundImageDraw {
40 | context = null;
^
41 | drawAlways = false;
42 | image = null;
To Reproduce
class Square {
size = 10
}
Note that TypeScript allows class variables to have initializers in them and this was working before upgrading svelte-preprocess from 4.9.5 to 4.10.1.
Expected behavior There should be no errors returning when this happens.
Information about your project: Browser: Version 89.0.4389.114 (Developer Build, ungoogled-chromium) (64-bit) Operating System: Windows 10 svelte-preprocess: 4.10.1 Build Process: Vite
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 9
- Comments: 35 (5 by maintainers)
In my case, I had an old svelte script that I moved to a new sveltekit project, and typescript was specified like this :
I changed type to lang and it works !
Looks like everyone has a different explanation for the same error !
Leaving it here in case someone makes the same mistake as me, as this is the top result on Google: If your SvelteKit config is called
svelte.config.ts(not.js), this will happen too. Renaming it to.jssolves the issue.That fixed the problem with Svelte/Vite.
I moved my
index.htmlinsidesrcand changed the project root tosrcinsvelte.config.js.svelte.config.jsneeds to referencevite.config.tsrelative to the project root. In my case:Oddly, I’m still experiencing this with Sveltekit
1.0.0created viavite project-name, choosingSvelte, choosingSveltekit,Yes, using Typescript syntaxand still the same Unexpected Token error as before.The
name : stringon line 4 still throws the Unexpected Token error. I’ve tried everything listed in this thread. Any thoughts?I found that I had to place the optimizeImports from carbon-preprocess-svelte, after the preprocess with postcss.
Ran into the same issue. Fresh vite project as well created using
npm create svelte@latest my-app.EDIT: In my case it was a missing lang prop in script tag like:
Adding it solved the problem.
For what it’s worth, coming from the Vercel-generated project, I solved this for myself follows:
Found this solution by running
npm create vite@latestand picking all the project config files from the new project into my existing one until it worked.Fixed by specifiying
"target": "es2021"intsconfig.jsonRelated https://github.com/sveltejs/svelte/issues/6900
I’m getting this issue with almost every typescript-exclusive syntax
Boost to this! I changed my root to
root : 'src'which was the problem! When I pointed svelte to the config file relative to the root directory it worked!In my case, preprocess version and target version didn’t change anything. the fix was even simpler: moving “svelte.config.ts” to “svelte.config.js”. Not sure where along the way that got changed, but make sure your svelte config file has a .js extension.
For what it’s worth: I returned to earlier version of
svelte-preprocessto work around this problem.From my package.json:
get the same error on svelte-preprocess 4.10.7 if my tsconfig.json has “target”: “es2022” strangely it starts working if I set “target”: “es2021”
That’s https://github.com/sveltejs/svelte/issues/6900