1+ const path = require ( 'path' )
2+
3+ const globalName = '___SVELTE_HMR_HOT_API'
4+ const globalAdapterName = '___SVELTE_HMR_HOT_API_PROXY_ADAPTER'
5+
16const defaultHotOptions = {
27 // don't preserve local state
38 noPreserveState : false ,
@@ -26,59 +31,58 @@ const defaultHotOptions = {
2631 native : false ,
2732 // Vite mode
2833 compatVite : false ,
29-
34+ // name of the adapter import binding
35+ importAdapterName : globalAdapterName ,
3036 // use absolute file paths to import runtime deps of svelte-hmr
3137 // (see https://github.com/rixo/svelte-hmr/issues/11)
32- absoluteImports : false ,
38+ absoluteImports : true ,
3339}
3440
35- const defaultHotApi = 'svelte-hmr/runtime/hot-api-esm.js'
36-
37- const nativeAdapter = require . resolve (
38- '../runtime/svelte-native/proxy-adapter-native.js'
39- )
41+ const defaultHotApi = 'hot-api-esm.js'
4042
4143const quote = JSON . stringify
4244
4345const posixify = file => file . replace ( / [ / \\ ] / g, '/' )
4446
45- const globalName = '___SVELTE_HMR_HOT_API'
46- const globalAdapterName = '___SVELTE_HMR_HOT_API_PROXY_ADAPTER'
47+ const applyAbsoluteImports = ( absoluteImports , target ) => {
48+ const base = absoluteImports
49+ ? path . resolve ( __dirname , '../runtime' ) + '/'
50+ : 'svelte-hmr/runtime/'
51+ return base + target
52+ }
4753
4854// NOTE Native adapter cannot be required in code (as opposed to this
4955// generated code) because it requires modules from NativeScript's code that
5056// are not resolvable for non-native users (and those missing modules would
5157// prevent webpack from building).
5258//
53- const resolveImportAdapter = (
54- hotOptions ,
55- importAdapterName = globalAdapterName
56- ) => {
57- // careful with relative paths (see https://github.com/rixo/svelte-hmr/issues/11)
58- const domAdapter = hotOptions . absoluteImports
59- ? require . resolve ( '../runtime/proxy-adapter-dom.js' )
60- : 'svelte-hmr/runtime/proxy-adapter-dom.js'
61- const adapterPath = hotOptions . native
62- ? posixify ( nativeAdapter )
63- : posixify ( domAdapter )
64- return { importAdapterName, adapterPath }
59+ const resolveAdapterImport = ( { native, absoluteImports } ) => {
60+ // careful with relative paths
61+ // (see https://github.com/rixo/svelte-hmr/issues/11)
62+ const file = native
63+ ? 'svelte-native/proxy-adapter-native.js'
64+ : 'proxy-adapter-dom.js'
65+ return posixify ( applyAbsoluteImports ( absoluteImports , file ) )
6566}
6667
68+ const resolveHotApiImport = ( { absoluteImports } , hotApiOpt ) =>
69+ posixify ( hotApiOpt || applyAbsoluteImports ( absoluteImports , defaultHotApi ) )
70+
6771const renderApplyHmr = ( {
6872 id,
6973 cssId,
7074 nonCssHash,
7175 hotOptions : { injectCss, compatVite } , // object
7276 options, // serialized
73- hotApi ,
74- adapterPath ,
77+ hotApiImport ,
78+ adapterImport ,
7579 importAdapterName,
7680 meta,
7781 compileData,
7882 compileOptions,
7983 imports = [
80- `import * as ${ globalName } from '${ posixify ( hotApi ) } '` ,
81- `import ${ importAdapterName } from '${ adapterPath } '` ,
84+ `import * as ${ globalName } from '${ hotApiImport } '` ,
85+ `import ${ importAdapterName } from '${ adapterImport } '` ,
8286 ] ,
8387} ) =>
8488 // this silly formatting keeps all original characters in their position,
@@ -149,14 +153,12 @@ const parseCssId = (code, parseHash) => {
149153 return { cssId, nonCssHash }
150154}
151155
156+ // NOTE hotOptions can be customized by end user through plugin options, while
157+ // options passed to this function can only customized by the plugin implementer
158+ //
152159// meta can be 'import.meta' or 'module'
153- const createMakeHot = ( hotApi = defaultHotApi , options ) => {
154- if ( typeof hotApi !== 'string' ) {
155- options = hotApi
156- hotApi = defaultHotApi
157- }
158- const { meta = 'import.meta' , walk } = options || { }
159-
160+ // const createMakeHot = (hotApi = defaultHotApi, options) => {
161+ const createMakeHot = ( { walk, meta = 'import.meta' , hotApi, adapter } ) => {
160162 const hasAccessors = compiled => {
161163 if ( ! compiled . ast || ! compiled . ast . html ) return
162164 let accessors = false
@@ -183,6 +185,8 @@ const createMakeHot = (hotApi = defaultHotApi, options) => {
183185 ) {
184186 const hotOptions = Object . assign ( { } , defaultHotOptions , hotOptionsArg )
185187
188+ const { importAdapterName } = hotOptions
189+
186190 const noPreserveState =
187191 hotOptions . noPreserveState ||
188192 ( hotOptions . noPreserveStateKey &&
@@ -200,7 +204,9 @@ const createMakeHot = (hotApi = defaultHotApi, options) => {
200204 : null
201205 )
202206
203- const { adapterPath, importAdapterName } = resolveImportAdapter ( hotOptions )
207+ const adapterImport = adapter || resolveAdapterImport ( hotOptions )
208+
209+ const hotApiImport = resolveHotApiImport ( hotOptions , hotApi )
204210
205211 const { cssId, nonCssHash } = parseCssId ( compiledCode , hotOptions . injectCss )
206212
@@ -210,8 +216,8 @@ const createMakeHot = (hotApi = defaultHotApi, options) => {
210216 nonCssHash,
211217 hotOptions,
212218 options,
213- hotApi ,
214- adapterPath ,
219+ hotApiImport ,
220+ adapterImport ,
215221 importAdapterName,
216222 meta,
217223 compileData,
0 commit comments