Skip to content

Commit

Permalink
fix: update warning codes for svelte5 (#1044)
Browse files Browse the repository at this point in the history
* fix: update warning codes for svelte5

* fix(audit): bump nanoid

* fix: replace css error code detection so that it works for svelte5

* chore: update changeset
  • Loading branch information
dominikg authored Dec 12, 2024
1 parent 1b302f1 commit e41edff
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
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
---

adapt internal handling of warning and error `code` property to changes in svelte5
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);
}
};
12 changes: 11 additions & 1 deletion packages/vite-plugin-svelte/src/utils/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ function formatFrameForVite(frame) {
.join('\n');
}

/**
*
* @param {string} code the svelte error code
* @see https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/compiler/errors.js
* @returns {boolean}
*/
function couldBeFixedByCssPreprocessor(code) {
return code === 'expected_token' || code === 'unexpected_eof' || code.startsWith('css_');
}

/**
* @param {import('svelte/compiler').Warning & Error} err a svelte compiler error, which is a mix of Warning and an error
* @param {string} originalCode
Expand All @@ -113,7 +123,7 @@ export function enhanceCompileError(err, originalCode, preprocessors) {
const additionalMessages = [];

// Handle incorrect CSS preprocessor usage
if (err.code === 'css-syntax-error') {
if (couldBeFixedByCssPreprocessor(err.code)) {
// 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
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e41edff

Please sign in to comment.