-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace broken Svelte 2 HMR with rixo's (#156)
- Loading branch information
Showing
6 changed files
with
170 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,103 @@ | ||
import { Registry, configure as configureProxy, createProxy } from 'svelte-dev-helper'; | ||
import { makeApplyHmr } from 'svelte-hmr/runtime'; | ||
|
||
let hotOptions = { | ||
noPreserveState: false | ||
}; | ||
// eslint-disable-next-line no-undef | ||
const g = typeof window !== 'undefined' ? window : global; | ||
|
||
export function configure(options) { | ||
hotOptions = Object.assign(hotOptions, options); | ||
configureProxy(hotOptions); | ||
} | ||
const globalKey = | ||
typeof Symbol !== 'undefined' | ||
? Symbol('SVELTE_LOADER_HOT') | ||
: '__SVELTE_LOADER_HOT'; | ||
|
||
export function register(id, component) { | ||
if (!g[globalKey]) { | ||
// do updating refs counting to know when a full update has been applied | ||
let updatingCount = 0; | ||
|
||
//store original component in registry | ||
Registry.set(id, { | ||
rollback: null, | ||
component, | ||
instances: [] | ||
}); | ||
const notifyStart = () => { | ||
updatingCount++; | ||
}; | ||
|
||
//create the proxy itself | ||
const proxy = createProxy(id); | ||
const notifyError = reload => err => { | ||
const errString = (err && err.stack) || err; | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'[HMR] Failed to accept update (nollup compat mode)', | ||
errString | ||
); | ||
reload(); | ||
notifyEnd(); | ||
}; | ||
|
||
//patch the registry record with proxy constructor | ||
const record = Registry.get(id); | ||
record.proxy = proxy; | ||
Registry.set(id, record); | ||
const notifyEnd = () => { | ||
updatingCount--; | ||
if (updatingCount === 0) { | ||
// NOTE this message is important for timing in tests | ||
// eslint-disable-next-line no-console | ||
console.log('[HMR:Svelte] Up to date'); | ||
} | ||
}; | ||
|
||
return proxy; | ||
g[globalKey] = { | ||
hotStates: {}, | ||
notifyStart, | ||
notifyError, | ||
notifyEnd, | ||
}; | ||
} | ||
|
||
export function reload(id, component) { | ||
const runAcceptHandlers = acceptHandlers => { | ||
const queue = [...acceptHandlers]; | ||
const next = () => { | ||
const cur = queue.shift(); | ||
if (cur) { | ||
return cur(null).then(next); | ||
} else { | ||
return Promise.resolve(null); | ||
} | ||
}; | ||
return next(); | ||
}; | ||
|
||
const record = Registry.get(id); | ||
export const applyHmr = makeApplyHmr(args => { | ||
const { notifyStart, notifyError, notifyEnd } = g[globalKey]; | ||
const { m, reload } = args; | ||
|
||
//keep reference to previous version to enable rollback | ||
record.rollback = record.component; | ||
let acceptHandlers = (m.hot.data && m.hot.data.acceptHandlers) || []; | ||
let nextAcceptHandlers = []; | ||
|
||
//replace component in registry with newly loaded component | ||
record.component = component; | ||
m.hot.dispose(data => { | ||
data.acceptHandlers = nextAcceptHandlers; | ||
}); | ||
|
||
Registry.set(id, record); | ||
const dispose = (...args) => m.hot.dispose(...args); | ||
|
||
//re-render the proxy instances | ||
record.instances.slice().forEach(function(instance) { | ||
instance && instance._rerender(); | ||
const accept = handler => { | ||
if (nextAcceptHandlers.length === 0) { | ||
m.hot.accept(); | ||
} | ||
nextAcceptHandlers.push(handler); | ||
}; | ||
|
||
const check = status => { | ||
if (status === 'ready') { | ||
notifyStart(); | ||
} else if (status === 'idle') { | ||
runAcceptHandlers(acceptHandlers) | ||
.then(notifyEnd) | ||
.catch(notifyError(reload)); | ||
} | ||
}; | ||
|
||
m.hot.addStatusHandler(check); | ||
|
||
m.hot.dispose(() => { | ||
m.hot.removeStatusHandler(check); | ||
}); | ||
|
||
//return the original proxy constructor that was `register()`-ed | ||
return record.proxy; | ||
} | ||
const hot = { | ||
data: m.hot.data, | ||
dispose, | ||
accept, | ||
}; | ||
|
||
return Object.assign({}, args, { hot }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { walk } = require('svelte/compiler'); | ||
const { createMakeHot } = require('svelte-hmr'); | ||
|
||
const hotApi = require.resolve('./hot-api.js'); | ||
|
||
const makeHot = createMakeHot({ | ||
walk, | ||
meta: 'module', | ||
hotApi, | ||
}); | ||
|
||
module.exports.makeHot = makeHot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.