@@ -131,12 +131,36 @@ const stringHashcode = str => {
131
131
return ( hash >>> 0 ) . toString ( 36 )
132
132
}
133
133
134
- const parseCssId = ( code , parseHash ) => {
134
+ const normalizeNonCss = ( code , cssHash ) =>
135
+ code
136
+ // ignore css hashes in the code (that have changed, necessarily)
137
+ . replace ( new RegExp ( '\\b' + cssHash + '\\b' , 'g' ) , '' )
138
+ // Svelte now adds locations in dev mode, code locations can change when
139
+ // CSS change, but we're unaffected (not real behaviour changes)
140
+ . replace ( / \b a d d _ l o c a t i o n \s * \( [ ^ ) ] * \) \s * ; ? / g, '' )
141
+
142
+ const parseCssId = ( code , parseHash , originalCode , compileCss ) => {
135
143
// the regex matching is very pretty conservative 'cause I don't want to
136
144
// match something else by error... I'm probably make it more lax if I have
137
145
// to fix it 3 times in a single week ¯\_(ツ)_/¯
138
146
let match = / ^ f u n c t i o n a d d _ c s s \( \) \{ [ \s \S ] * ?^ } / m. exec ( code )
139
- if ( ! match ) return { }
147
+
148
+ if ( ! match ) {
149
+ // guard: injectCss is off, no need to compute hashes
150
+ if ( ! parseHash ) return { }
151
+ // guard: compile.css is true, so we should have found the add_css function,
152
+ // something unexpected is unraveling here, fall back to caution
153
+ if ( ! compileCss ) return { }
154
+ // trying to get CSS id the same way as Svelte does it
155
+ match = / < s t y l e [ ^ > ] * > ( [ \s \S ] * ) < \/ \s * s t y l e \s * > / . exec ( originalCode )
156
+ const cssHash = match && match [ 1 ] ? stringHashcode ( match [ 1 ] ) : null
157
+ if ( ! cssHash ) return { }
158
+ return {
159
+ cssId : `svelte-${ cssHash } -style` ,
160
+ nonCssHash : stringHashcode ( normalizeNonCss ( code , cssHash ) ) ,
161
+ }
162
+ }
163
+
140
164
const codeExceptCSS =
141
165
code . slice ( 0 , match . index ) + code . slice ( match . index + match [ 0 ] . length )
142
166
@@ -146,9 +170,7 @@ const parseCssId = (code, parseHash) => {
146
170
if ( ! parseHash || ! cssId ) return { cssId }
147
171
148
172
const cssHash = cssId . split ( '-' ) [ 1 ]
149
- const nonCssHash = stringHashcode (
150
- codeExceptCSS . replace ( new RegExp ( '\\b' + cssHash + '\\b' , 'g' ) , '' )
151
- )
173
+ const nonCssHash = stringHashcode ( normalizeNonCss ( codeExceptCSS , cssHash ) )
152
174
153
175
return { cssId, nonCssHash }
154
176
}
@@ -232,7 +254,12 @@ const createMakeHot = ({ walk, meta = 'import.meta', hotApi, adapter }) => {
232
254
233
255
const hotApiImport = resolveHotApiImport ( hotOptions , hotApi )
234
256
235
- const { cssId, nonCssHash } = parseCssId ( compiledCode , hotOptions . injectCss )
257
+ const { cssId, nonCssHash } = parseCssId (
258
+ compiledCode ,
259
+ hotOptions . injectCss ,
260
+ originalCode ,
261
+ compileOptions && compileOptions . css
262
+ )
236
263
237
264
const replacement = renderApplyHmr ( {
238
265
id,
0 commit comments