Skip to content

Commit d6bceb9

Browse files
committed
fix CSS injection to support add_location, & add support for css:false
1 parent 4c041d5 commit d6bceb9

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

lib/make-hot.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,36 @@ const stringHashcode = str => {
131131
return (hash >>> 0).toString(36)
132132
}
133133

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(/\badd_location\s*\([^)]*\)\s*;?/g, '')
141+
142+
const parseCssId = (code, parseHash, originalCode, compileCss) => {
135143
// the regex matching is very pretty conservative 'cause I don't want to
136144
// match something else by error... I'm probably make it more lax if I have
137145
// to fix it 3 times in a single week ¯\_(ツ)_/¯
138146
let match = /^function add_css\(\) \{[\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 = /<style[^>]*>([\s\S]*)<\/\s*style\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+
140164
const codeExceptCSS =
141165
code.slice(0, match.index) + code.slice(match.index + match[0].length)
142166

@@ -146,9 +170,7 @@ const parseCssId = (code, parseHash) => {
146170
if (!parseHash || !cssId) return { cssId }
147171

148172
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))
152174

153175
return { cssId, nonCssHash }
154176
}
@@ -232,7 +254,12 @@ const createMakeHot = ({ walk, meta = 'import.meta', hotApi, adapter }) => {
232254

233255
const hotApiImport = resolveHotApiImport(hotOptions, hotApi)
234256

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+
)
236263

237264
const replacement = renderApplyHmr({
238265
id,

0 commit comments

Comments
 (0)