Skip to content

Commit 0981070

Browse files
committed
fix replacing "export default" in strings in user code -- fixes #34
1 parent 864366b commit 0981070

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/make-hot.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ if (typeof add_css !== 'undefined' && !document.getElementById(${json(
109109
}
110110
`
111111

112+
// replace from last occurrence of the splitter
113+
const replaceLast = (string, splitter, regex, replacer) => {
114+
const lastIndex = string.lastIndexOf(splitter)
115+
return (
116+
string.slice(0, lastIndex) +
117+
string.slice(lastIndex).replace(regex, replacer)
118+
)
119+
}
120+
112121
// https://github.com/darkskyapp/string-hash/blob/master/index.js
113122
// (via https://github.com/sveltejs/svelte/blob/91d758e35b2b2154512ddd11e6b6d9d65708a99e/src/compiler/compile/utils/hash.ts#L2)
114123
const stringHashcode = str => {
@@ -447,11 +456,13 @@ const createMakeHot = ({ resolveAbsoluteImport, pkg = {} }) => ({
447456
emitCss,
448457
})
449458

450-
return compiledCode.replace(
459+
// NOTE export default can appear in strings in use code
460+
// see: https://github.com/rixo/svelte-hmr/issues/34
461+
return replaceLast(
462+
compiledCode,
463+
'export default',
451464
/(\n?export default ([^;]*);)/,
452-
(match, $1, $2) => {
453-
return replacement.replace(/\$2/g, () => $2)
454-
}
465+
(match, $1, $2) => replacement.replace(/\$2/g, () => $2)
455466
)
456467
}
457468

0 commit comments

Comments
 (0)