Skip to content

Commit afe81cc

Browse files
committed
chore: improve regex and documentation for code normalization in hotUpdate
1 parent 2e1fd8c commit afe81cc

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

packages/vite-plugin-svelte/src/plugins/hot-update.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ function nullSafeEqual(prev, next) {
159159
/**
160160
* remove code that only changes metadata and does not require a js update for the component to keep working
161161
*
162-
* 1) add_location() calls. These add location metadata to elements, only used by some dev tools
163-
* 2) vite query timestamps t=1235345.
162+
* 1) location numbers argument from $.add_locations calls in svelte output eg [[1,2],[3,4]]
163+
* 2) timestamp queries added to imports by vite eg ?t=0123456789123
164164
*
165165
* @param {string | null | undefined } code
166166
* @returns {string | null | undefined}
@@ -172,9 +172,10 @@ function normalize(code) {
172172

173173
return (
174174
code
175-
// svelte5 add_location line numbers argument
176-
.replace(/(\$\.add_locations\(.*), \[(\[[.[\], \d]+])]/g, '$1, []')
177-
// vite import analysis timestamp queries
178-
.replace(/[?&]t=\d+/g, '')
175+
// svelte5 $.add_locations line numbers argument [[1,2],[3,4]]
176+
// uses matching group replace to keep the template argument intact
177+
.replace(/(\$\.add_locations\(.*), (\[\[[\d, [\]]+]])\)/g, '$1, []')
178+
// vite import analysis timestamp queries, ?t=0123456789123&
179+
.replace(/[?&]t=\d{13}\b/g, '')
179180
);
180181
}

0 commit comments

Comments
 (0)