Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulate HMR bubbled argument to enable svelte-hmr CSS only updates #166

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/hot-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,38 @@ if (!g[globalKey]) {
};
}

const runAcceptHandlers = acceptHandlers => {
const runAcceptHandlers = (acceptHandlers, bubbled) => {
const queue = [...acceptHandlers];
const next = () => {
const cur = queue.shift();
if (cur) {
return cur(null).then(next);
return cur({ bubbled }).then(next);
} else {
return Promise.resolve(null);
}
};
return next();
};

const isBubbled = (children, lastChildren) => {
if (children.length !== lastChildren.length) return false;
return children.some((x, i) => x !== lastChildren[i]);
};

export const applyHmr = makeApplyHmr(args => {
const { notifyStart, notifyError, notifyEnd } = g[globalKey];
const { m, reload } = args;

let acceptHandlers = (m.hot.data && m.hot.data.acceptHandlers) || [];
let nextAcceptHandlers = [];
const acceptHandlers = (m.hot.data && m.hot.data.acceptHandlers) || [];
const nextAcceptHandlers = [];

const children = m.children.map(x => require.cache[x]);

const lastChildren = m.hot.data && m.hot.data.children;

m.hot.dispose(data => {
data.acceptHandlers = nextAcceptHandlers;
data.children = children;
});

const dispose = (...args) => m.hot.dispose(...args);
Expand All @@ -81,7 +91,11 @@ export const applyHmr = makeApplyHmr(args => {
if (status === 'ready') {
notifyStart();
} else if (status === 'idle') {
runAcceptHandlers(acceptHandlers)
const bubbled =
acceptHandlers.length > 0
? isBubbled(children, lastChildren)
: undefined;
runAcceptHandlers(acceptHandlers, bubbled)
.then(notifyEnd)
.catch(notifyError(reload));
}
Expand Down