Skip to content

Commit afef30e

Browse files
committed
make accept handler stateless for support of CSS injection in Snowpack
1 parent 53ad569 commit afef30e

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

runtime/hot-api.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,15 @@ function applyHmr(args) {
110110
nonCssHash &&
111111
existing.current.nonCssHash === nonCssHash
112112

113-
r.update({ Component, hotOptions, canAccept, cssId, nonCssHash, cssOnly })
113+
r.update({
114+
Component,
115+
hotOptions,
116+
canAccept,
117+
nonCssHash,
118+
cssId,
119+
previousCssId: r.current.cssId,
120+
cssOnly,
121+
})
114122

115123
hot.dispose(data => {
116124
// handle previous fatal errors
@@ -139,18 +147,20 @@ function applyHmr(args) {
139147
if (canAccept) {
140148
hot.accept(async arg => {
141149
const { bubbled } = arg || {}
142-
const newCssId = r.current.cssId
143-
const cssChanged = newCssId !== cssId
150+
// NOTE Snowpack registers accept handlers only once, so we can NOT rely
151+
// on the surrounding scope variables -- they're not the last module!
152+
const { cssId: newCssId, previousCssId } = r.current
153+
const cssChanged = newCssId !== previousCssId
144154
// ensure old style sheet has been removed by now
145-
if (cssChanged) removeStylesheet(cssId)
155+
if (cssChanged) removeStylesheet(previousCssId)
146156
// guard: css only change
147157
if (
148158
// NOTE bubbled is provided only by rollup-plugin-hot, and we
149159
// can't safely assume a CSS only change without it... this means we
150160
// can't support CSS only injection with Nollup or Webpack currently
151161
bubbled === false && // WARNING check false, not falsy!
152162
r.current.cssOnly &&
153-
(!cssChanged || replaceCss(cssId, newCssId))
163+
(!cssChanged || replaceCss(previousCssId, newCssId))
154164
) {
155165
return
156166
}

0 commit comments

Comments
 (0)