Skip to content

Commit 52339e3

Browse files
committed
add vite support
1 parent ea0879f commit 52339e3

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

lib/make-hot.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ const defaultHotOptions = {
2121
injectCss: true,
2222
// to mitigate FOUC between dispose (remove stylesheet) and accept
2323
cssEjectDelay: 100,
24-
}
2524

26-
// careful with relative paths (see https://github.com/rixo/svelte-hmr/issues/11)
27-
const domAdapter = require.resolve('../runtime/proxy-adapter-dom.js')
28-
// const domAdapter = 'svelte-hmr/runtime/proxy-adapter-dom.js'
25+
// Svelte Native mode
26+
native: false,
27+
// Vite mode
28+
compatVite: false,
29+
30+
// use absolute file paths to import runtime deps of svelte-hmr
31+
// (see https://github.com/rixo/svelte-hmr/issues/11)
32+
absoluteImports: false,
33+
}
2934

3035
const nativeAdapter = require.resolve(
3136
'../runtime/svelte-native/proxy-adapter-native.js'
@@ -47,6 +52,10 @@ const resolveImportAdapter = (
4752
hotOptions,
4853
importAdapterName = globalAdapterName
4954
) => {
55+
// careful with relative paths (see https://github.com/rixo/svelte-hmr/issues/11)
56+
const domAdapter = hotOptions.absoluteImports
57+
? require.resolve('../runtime/proxy-adapter-dom.js')
58+
: 'svelte-hmr/runtime/proxy-adapter-dom.js'
5059
const adapterPath = hotOptions.native
5160
? posixify(nativeAdapter)
5261
: posixify(domAdapter)
@@ -57,7 +66,7 @@ const renderApplyHmr = ({
5766
id,
5867
cssId,
5968
nonCssHash,
60-
hotOptions, // object
69+
hotOptions: { injectCss, compatVite }, // object
6170
options, // serialized
6271
hotApi,
6372
adapterPath,
@@ -71,13 +80,10 @@ const renderApplyHmr = ({
7180
],
7281
}) =>
7382
// this silly formatting keeps all original characters in their position,
74-
// except for the ';' in `export default Foo;`, and except if this is the
75-
// first line (very unlikely) -- thus saving us from having to provide a
76-
// sourcemap
77-
`${imports.join(';')};
78-
export default $2
79-
= ${meta} && ${meta}.hot
80-
? ${globalName}.applyHmr({
83+
// thus saving us from having to provide a sourcemap
84+
`${imports.join(';')};${`
85+
if (${compatVite ? '' : 'import.meta && '}import.meta.hot) {
86+
$2 = ${globalName}.applyHmr({
8187
m: ${meta},
8288
id: ${quote(id)},
8389
hotOptions: ${options},
@@ -87,14 +93,21 @@ export default $2
8793
compileOptions: ${compileOptions},
8894
cssId: ${quote(cssId)},
8995
nonCssHash: ${quote(nonCssHash)},
90-
})
91-
: $2;
96+
});
97+
${compatVite ? 'import.meta.hot.accept();' : ''}
98+
}
99+
`
100+
.split('\n')
101+
.map(x => x.trim())
102+
.filter(Boolean)
103+
.join(' ')}
104+
export default $2;
92105
${
93106
// NOTE when doing CSS only voodoo, we have to inject the stylesheet as soon
94107
// as the component is loaded because Svelte normally do that when a component
95108
// is instantiated, but we might already have instances in the large when a
96109
// component is loaded with HMR
97-
hotOptions.injectCss && cssId
110+
injectCss && cssId
98111
? `
99112
if (typeof add_css !== 'undefined' && !document.getElementById(${quote(
100113
cssId
@@ -135,7 +148,13 @@ const parseCssId = (code, parseHash) => {
135148
}
136149

137150
// meta can be 'import.meta' or 'module'
138-
const createMakeHot = (hotApi, { meta = 'import.meta', walk } = {}) => {
151+
const createMakeHot = (_hotApi, options) => {
152+
if (typeof _hotApi !== 'string') {
153+
options = _hotApi
154+
_hotApi = null
155+
}
156+
const { meta = 'import.meta', walk } = options || {}
157+
139158
const hasAccessors = compiled => {
140159
if (!compiled.ast || !compiled.ast.html) return
141160
let accessors = false
@@ -162,6 +181,9 @@ const createMakeHot = (hotApi, { meta = 'import.meta', walk } = {}) => {
162181
) {
163182
const hotOptions = Object.assign({}, defaultHotOptions, hotOptionsArg)
164183

184+
const hotApi =
185+
_hotApi || (hotOptions.compatVite && 'svelte-hmr/runtime/esm.js')
186+
165187
const noPreserveState =
166188
hotOptions.noPreserveState ||
167189
(hotOptions.noPreserveStateKey &&

runtime/esm.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { makeApplyHmr } from '../runtime/index.js'
2+
3+
export const applyHmr = makeApplyHmr(args =>
4+
Object.assign({}, args, {
5+
hot: args.m.hot,
6+
})
7+
)

0 commit comments

Comments
 (0)