yuanchuan / sveltekit-autoimport

Automatically detect and import components/modules for SvelteKit projects
https://npm.im/sveltekit-autoimport
MIT License
228 stars 11 forks source link

Error: Failed to parse source for import analysis because the content contains invalid JS syntax. #28

Closed hartwm closed 2 years ago

hartwm commented 2 years ago

I have a fairly blank starter theme running on sveltekit latest which uses sveltekit plugin for vite and a vite.config file.

my vite.config.js file looks like

import { sveltekit } from '@sveltejs/kit/vite'; 
import autoImport from 'sveltekit-autoimport'; 

/** @type {import('vite').UserConfig} */
const config = {
    plugins: [
        sveltekit(), 
                autoImport({
            components: [{ name: 'src/components/skeleton' } ],
        })
    ]
};

export default config;

the src/components/skeleton has one file at src/components/skeleton/Menu.svelte for testing with this markup <h1>This is a menu component</h1>

error message

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.
    at formatError (/Users/whart/www/koios/node_modules/vite/dist/node/chunks/dep-80fe9c6b.js:39055:46)
    at TransformContext.error (/Users/whart/www/koios/node_modules/vite/dist/node/chunks/dep-80fe9c6b.js:39051:19)
    at TransformContext.transform (/Users/whart/www/koios/node_modules/vite/dist/node/chunks/dep-80fe9c6b.js:58269:22)
    at async TransformContext.plugin.transform (file:///Users/whart/www/koios/node_modules/vite-plugin-inspect/dist/index.mjs:164:25)
    at async Object.transform (/Users/whart/www/koios/node_modules/vite/dist/node/chunks/dep-80fe9c6b.js:39292:30)
    at async doTransform (/Users/whart/www/koios/node_modules/vite/dist/node/chunks/dep-80fe9c6b.js:50012:29)
yuanchuan commented 2 years ago

Well, this is a headache

yuanchuan commented 2 years ago

How about to put autoimport() above sveltekit() in the plugins object?

hartwm commented 2 years ago

This works! Thank you

I did notice some type issues with component Argument of type '{ components: string[]; include: string[]; }' is not assignable to parameter of type 'string | string[] | undefined'. Object literal may only specify known properties, and 'components' does not exist in type 'string[]'.ts(2345)

yuanchuan commented 2 years ago

This works! Thank you

I did notice some type issues with component Argument of type '{ components: string[]; include: string[]; }' is not assignable to parameter of type 'string | string[] | undefined'. Object literal may only specify known properties, and 'components' does not exist in type 'string[]'.ts(2345)

Thanks for reporting. It seems the warning was caused by the JSDoc comments. I'll fix that

stolinski commented 2 years ago

For me moving the Auto Import plugin above SK gives Component is not defined errors for auto imported ones and invalid syntax errors if below.

Never mind, clearing .svelte-kit and re-installing deps fiexed.