Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to disable warnings like css-unused-selector #2253

Closed
livehtml opened this issue Aug 20, 2021 · 11 comments · Fixed by #2267
Closed

Ability to disable warnings like css-unused-selector #2253

livehtml opened this issue Aug 20, 2021 · 11 comments · Fixed by #2267
Labels
bug Something isn't working

Comments

@livehtml
Copy link

livehtml commented Aug 20, 2021

Describe the problem

With Rollup we can use onwarn event, modify it to exclude specific warnings. But how to do it in SvelteKit?

Describe the proposed solution

Option in svelte.config.js to ignore specific warnings.
Or (best): allow comments like "svelte-ignore css-unused-selector" inside corresponding style files/blocks, not only at the html markup level.

Importance

would make my life easier

@dummdidumm
Copy link
Member

If I'm not mistaken it should be enough to place that onwarn function you know from Rollup within your svelte.config.js like
export default { onwarn: ..., ...otherstuff } . @dominikg might know more, AFAIK the property is picked up by vite-plugin-svelte which prints these warnings.

@dominikg
Copy link
Member

you should be able to define a custom warning handler in svelte.config.js. it works similar to rollup-plugin-svelte

https://github.com/sveltejs/vite-plugin-svelte/blob/066be0c591dec3448e9db12ddff74f5d150a99eb/packages/vite-plugin-svelte/src/utils/options.ts#L299

@dummdidumm dummdidumm added the documentation Improvements or additions to documentation label Aug 20, 2021
@livehtml
Copy link
Author

livehtml commented Aug 20, 2021

Thanks @dominikg , @dummdidumm

I didn’t catch exactly where I can put this onwarn in the svelte.config.js.

In paths:
config.onwarn,
config.kit.onwarn
thrown an error like "Unexpected option config.onwarn"

In the config.kit.vite it's ok, no errors, but nothing works also:

vite: () => ({
            logLevel: "info",
            onwarn: (warning, handler) => {
                console.log('111');
                const { code, frame } = warning;
                if (code === "css-unused-selector")
                    return;

                handler(warning);
            }
})

@dominikg
Copy link
Member

it should be in the root, a sibling of kit

@bluwy
Copy link
Member

bluwy commented Aug 20, 2021

Looks like kit defines custom validation for the svelte config, which is blocking usages of onwarn and any other vite-plugin-svelte's options. I think kit should allow any unknown keys though, or only validate the kit option.

@livehtml
Copy link
Author

@dominikg
don't work, error happens: Unexpected option config.onwarn

Full svelte.config.js:

import preprocess from 'svelte-preprocess';
import adapterNode from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    preprocess: preprocess({
        sass: {
            includePaths: ["./src/styles", "./sources/src/styles", "./src/lib", "./sources/src/lib"],
            prependData: `@import '_prepend.sass'`,
            renderSync: true
        }
    }),
    kit: {
        target: '#app',
        adapter: adapterNode({
            out: './../build-node-serve',
            esbuild(defaultOptions) {
                return {
                    ...defaultOptions,
                    outfile: './../build-node-serve/index.mjs',
                    external: [],
                };
            }
        }),
        vite: () => ({
            logLevel: "info",
            plugins: [
                // imagetools(),
            ]
        })
    },
    onwarn: (warning, handler) => {
        console.log('111');
        const { code, frame } = warning;
        if (code === "css-unused-selector")
            return;

        handler(warning);
    }
};

export default config;

@dominikg
Copy link
Member

Bluwy already posted the reason for this. sveltekit's config validation currently doesn't allow extra options to be set. So right now this is not supported.

@dominikg dominikg added bug Something isn't working and removed documentation Improvements or additions to documentation labels Aug 20, 2021
@Mlocik97
Copy link
Contributor

Mlocik97 commented Aug 20, 2021

is it not possible to change rollup config through config.kit.vite.build.rollupOptions.onwarn ? I though this option is exposed and even documented. @dominikg

@dominikg
Copy link
Member

The option should be better documented on vite-plugin-svelte and we need to figure out how multiple projects in the svelte universe share the same svelte.config.js

@Mlocik97 that option doesn't work on dev obviously and also not on build as vite-plugin-svelte used its own custom logging instead of this.warn and so the svelte compiler warnings would not reach the custom rollup warning handler.

@ignatiusmb
Copy link
Member

Config properties defined at the top-level should not throw anymore in .156 and above, thanks @bluwy!

@Madd0g
Copy link

Madd0g commented Oct 10, 2022

Was the ability to pass onwarn specifically fixed too?

I tried putting it in svelte.config.js (in the root), I tried putting it in vite.config.mjs as a sibling of kit, neither worked.

EDIT: I also tried passing it to the SK vite plugin, but according to the code - it's impossible to pass options to @sveltejs/vite-plugin-svelte directly from the kit plugin - https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/vite/index.js#L62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants