Skip to content

Commit b855833

Browse files
committed
fix: replace css error code detection so that it works for svelte5
1 parent ea897b9 commit b855833

File tree

1 file changed

+11
-2
lines changed
  • packages/vite-plugin-svelte/src/utils

1 file changed

+11
-2
lines changed

packages/vite-plugin-svelte/src/utils/error.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ function formatFrameForVite(frame) {
101101
.join('\n');
102102
}
103103

104+
/**
105+
*
106+
* @param {string} code the svelte error code
107+
* @see https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/compiler/errors.js
108+
* @returns {boolean}
109+
*/
110+
function couldBeFixedByCssPreprocessor(code) {
111+
return code === 'expected_token' || code === 'unexpected_eof' || code.startsWith('css_');
112+
}
113+
104114
/**
105115
* @param {import('svelte/compiler').Warning & Error} err a svelte compiler error, which is a mix of Warning and an error
106116
* @param {string} originalCode
@@ -113,8 +123,7 @@ export function enhanceCompileError(err, originalCode, preprocessors) {
113123
const additionalMessages = [];
114124

115125
// Handle incorrect CSS preprocessor usage
116-
if (err.code === 'css-syntax-error') {
117-
// TODO find the correct new code or new way to identify css syntax error
126+
if (couldBeFixedByCssPreprocessor(err.code)) {
118127
// Reference from Svelte: https://github.com/sveltejs/svelte/blob/9926347ad9dbdd0f3324d5538e25dcb7f5e442f8/packages/svelte/src/compiler/preprocess/index.js#L257
119128
const styleRe =
120129
/<!--[^]*?-->|<style((?:\s+[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"/]+)*\s*)(?:\/>|>([\S\s]*?)<\/style>)/g;

0 commit comments

Comments
 (0)