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

fix: update warning codes for svelte5 #1044

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-pears-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

use correct warning code to hide unused css selector warning coming from vite-plugin-svelte injected code during development
2 changes: 1 addition & 1 deletion packages/e2e-tests/import-queries/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
preprocess: [vitePreprocess()],
onwarn(warning, defaultHandler) {
// import query test generates one of these
if (warning.code === 'custom-element-no-tag') return;
if (warning.code === 'options_missing_custom_element') return;
defaultHandler(warning);
}
};
1 change: 1 addition & 0 deletions packages/vite-plugin-svelte/src/utils/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export function enhanceCompileError(err, originalCode, preprocessors) {

// Handle incorrect CSS preprocessor usage
if (err.code === 'css-syntax-error') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dummdidumm whats the new way to identify css syntax errors? i looked but didn't find the corrosponding code in svelte5

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two things you can look for:

  • any error starting with css_ - right now it's css_expected_identifier, css_selector_invalid and css_empty_declaration
  • more general errors: expected_token and unexpected_eof - though these could also happen in other parts outside the style sheet (not sure if that is a problem)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added with generic startswith css_ , technically this could introduce false positives for users who don't use a css preprocessor, so if we want to play it conservative, we'd have to use a hardcoded list at the cost of that getting out of date if new codes are added.

// TODO find the correct new code or new way to identify css syntax error
// Reference from Svelte: https://github.com/sveltejs/svelte/blob/9926347ad9dbdd0f3324d5538e25dcb7f5e442f8/packages/svelte/src/compiler/preprocess/index.js#L257
const styleRe =
/<!--[^]*?-->|<style((?:\s+[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"/]+)*\s*)(?:\/>|>([\S\s]*?)<\/style>)/g;
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-svelte/src/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export function logCompilerWarnings(svelteRequest, warnings, options) {
*/
function ignoreCompilerWarning(warning, isBuild, emitCss) {
return (
(!emitCss && warning.code === 'css-unused-selector') || // same as rollup-plugin-svelte
(!emitCss && warning.code === 'css_unused_selector') || // same as rollup-plugin-svelte
(!isBuild && isNoScopableElementWarning(warning))
);
}
Expand All @@ -194,7 +194,7 @@ function ignoreCompilerWarning(warning, isBuild, emitCss) {
*/
function isNoScopableElementWarning(warning) {
// see https://github.com/sveltejs/vite-plugin-svelte/issues/153
return warning.code === 'css-unused-selector' && warning.message.includes('"*"');
return warning.code === 'css_unused_selector' && warning.message.includes('"*"');
}

/**
Expand Down
Loading