sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
78.39k stars 4.1k forks source link

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript) #3144

Closed mihairaulea closed 5 years ago

mihairaulea commented 5 years ago
Stack trace [!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript) src/common/components/MenuDrawer.svelte (772:16)

What bothers me is the fact that MenuDrawer.svelte only has 258 lines. How can i translate the svelte bundled source to my file?

antony commented 5 years ago

This doesn't look like a bug in Svelte, you are importing things incorrectly in your bundler configuration. You might be better looking for help in the Svelte Discord.

Please create a reproduction using the REPL. We would also need to see your rollup/webpack configuration, and the source of MenuDrawer.svelte There isn't nearly enough information here to assist you as it stands.

Conduitry commented 5 years ago

Errors like this can also happen when the Svelte compiler produces unparseable js for a component, which would be a bug. In any case, we do need to see a repro.

mvanhalen commented 5 years ago

I have a similar issue I think. Might be related.

Based on the normal app template. I think something is off with the rollup config somehow. https://github.com/mvanhalen/svelte-app-issue

Also posted here https://github.com/kaisermann/svelte-loadable/issues/7

Here a compiled version showing the issue https://igtemplate.blob.core.windows.net/public/packages/svelte/public/index.html

korywka commented 5 years ago

@Conduitry have the same error 😞 removed all my code and created small repo: https://github.com/bravecow/svelte-example yarn watch: image

Conduitry commented 5 years ago

Your Rollup config is wrong. plugins and watch need to go outside output. https://rollupjs.org/guide/en/#configuration-files

korywka commented 5 years ago

@Conduitry Thanks! 🙄

Conduitry commented 5 years ago

@mvanhalen Your issue is that you are attempting to load a component from a URL, and aren't using an appropriate Rollup plugin to allow downloading and bundling external modules on the fly. (I would guess that someone has written such a plugin, but I've never personally used one.) In the compiled code, the import('https://igtemplate.blob.core.windows.net/public/packages/svelte/ScreenCDN.svelte') remains unchanged, and so the browser is attempting to load an uncompiled svelte file as an ES module, which won't work.

I'm going to close this issue, as none of the comments in it have been about the actual same issue, nor have we gotten a repro for the original problem.

frankieali commented 3 years ago

I encountered this error when trying to add sass support to my rollup.config.js. I commented out css({ output: 'bundle.css' }), and the error mentioned by OP appeared. The css method is from import css from 'rollup-plugin-css-only';

ezriharmusial commented 3 years ago

I had the same error. Everything worked fine before. But without changing any configs/packages, I created a regular style block in an existing component and it broke from nowhere.

Turns out that my specific error only showed up from importing components with aliased paths (@rollup/plugin-alias) trough my router.js file (svelte-spa-router).

Changing the aliased paths to relative paths removed my problem. Hope it helps someone.

dsalaj commented 3 years ago

I had the same error... because my component had the extension .Svelte instead of .svelte. 😞

icalvin102 commented 2 years ago

@ezriharmusial thanks so much for your answer. I searched for hours but I never would have thought that @rollup/plugin-alias was causing this issue.

After some tinkering I found a solution that lets me keep my aliases. The following change in my rollup.config.js did the trick for me.

alias({
  resolve: [...],
  enties: [
-   { find: '$lib', replacement: './src/lib' },
+   { find: '$lib', replacement: require('path').resolve(__dirname, './src/lib') },
  ]
})
timohausmann commented 2 years ago

On macos I got this error after renaming a file timeline.ts to Timeline.ts (and updated the imports). Restarting scripts/VSCode did not help. Renaming it to anything else like Timelinex.ts solved the issue.

Akatuoro commented 2 years ago

This issue can also appear when updating rollup-plugin-svelte to 7.0.0 or higher. The breaking change is described in the changelog: https://github.com/sveltejs/rollup-plugin-svelte/blob/master/CHANGELOG.md#700 - "Offload CSS handling to Rollup", with the suggestion to use rollup-plugin-css-only

sandeep-gh commented 2 years ago

I got this error, because I was using soft link over sshfs mount files. Took some time to figure that out.

mtechraw commented 1 year ago

image

I am having this issue what could be wrong here

antony commented 1 year ago

Please use the questions facility in the Svelte discord rather than closed github issues, you are more likely to recieve help.