mirror of https://github.com/sveltejs/svelte
parent
9f0630c3fb
commit
795ca0c291
@ -0,0 +1,44 @@
|
||||
const workers = new Map();
|
||||
|
||||
let uid = 1;
|
||||
|
||||
export default class Bundler {
|
||||
constructor(version) {
|
||||
if (!workers.has(version)) {
|
||||
const worker = new Worker('/workers/bundler.js');
|
||||
worker.postMessage({ type: 'init', version });
|
||||
workers.set(version, worker);
|
||||
}
|
||||
|
||||
this.worker = workers.get(version);
|
||||
|
||||
this.handlers = new Map();
|
||||
|
||||
this.worker.addEventListener('message', event => {
|
||||
const handler = this.handlers.get(event.data.id);
|
||||
|
||||
if (handler) { // if no handler, was meant for a different REPL
|
||||
handler(event.data);
|
||||
this.handlers.delete(event.data.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bundle(components) {
|
||||
return new Promise(fulfil => {
|
||||
const id = uid++;
|
||||
|
||||
this.handlers.set(id, fulfil);
|
||||
|
||||
this.worker.postMessage({
|
||||
id,
|
||||
type: 'bundle',
|
||||
components
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.worker.terminate();
|
||||
}
|
||||
}
|
@ -1,149 +1,105 @@
|
||||
|
||||
(function (){
|
||||
const importCache = {};
|
||||
|
||||
function fetchImport(id) {
|
||||
return new Promise((fulfil, reject) => {
|
||||
curl([`https://bundle.run/${id}`]).then(module => {
|
||||
importCache[id] = module;
|
||||
fulfil(module);
|
||||
}, err => {
|
||||
console.error(err.stack);
|
||||
reject(new Error(`Error loading ${id} from bundle.run`));
|
||||
(function() {
|
||||
const import_cache = {};
|
||||
|
||||
function fetch_import(id) {
|
||||
return new Promise((fulfil, reject) => {
|
||||
curl([`https://bundle.run/${id}`]).then(module => {
|
||||
import_cache[id] = module;
|
||||
fulfil(module);
|
||||
}, err => {
|
||||
console.error(err.stack);
|
||||
reject(new Error(`Error loading ${id} from bundle.run`));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function fetchImports(imports, progressFunc) {
|
||||
const missingImports = imports.filter(x => !importCache[x]);
|
||||
let pendingImports = missingImports.length;
|
||||
|
||||
if (missingImports.length) {
|
||||
let promise = Promise.all(
|
||||
missingImports.map(id => fetchImport(id).then(() => {
|
||||
pendingImports -= 1;
|
||||
if (progressFunc) progressFunc(pendingImports);
|
||||
}))
|
||||
);
|
||||
|
||||
return promise
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
function handleMessage(ev) {
|
||||
let { action, cmdId } = ev.data;
|
||||
const sendMessage = (payload) => parent.postMessage( { ...payload }, ev.origin);
|
||||
const sendReply = (payload) => sendMessage({ ...payload, cmdId })
|
||||
const sendOk = () => sendReply({ action: "cmdOk" });
|
||||
const sendError = (message, stack) => sendReply({ action: "cmdError", message, stack })
|
||||
|
||||
|
||||
if (action == "eval") {
|
||||
let { script } = ev.data.args;
|
||||
try {
|
||||
eval(script);
|
||||
sendOk();
|
||||
} catch (e) {
|
||||
sendError(e.message, e.stack);
|
||||
function fetch_imports(imports, progress_func) {
|
||||
const missing_imports = imports.filter(x => !import_cache[x]);
|
||||
let pending_imports = missing_imports.length;
|
||||
|
||||
if (missing_imports.length) {
|
||||
let promise = Promise.all(
|
||||
missing_imports.map(id => fetch_import(id).then(() => {
|
||||
pending_imports -= 1;
|
||||
if (progress_func) progress_func(pending_imports);
|
||||
}))
|
||||
);
|
||||
|
||||
return promise;
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
if (action == "bind_props") {
|
||||
let { props } = ev.data.args
|
||||
|
||||
if (!window.component) {
|
||||
// TODO can this happen?
|
||||
console.warn('no component to bind to');
|
||||
sendOk();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
props.forEach(prop => {
|
||||
// TODO should there be a public API for binding?
|
||||
// e.g. `component.$watch(prop, handler)`?
|
||||
// (answer: probably)
|
||||
window.component.$$.bound[prop] = value => {
|
||||
sendMessage({ action:"prop_update", args: { prop, value } })
|
||||
};
|
||||
});
|
||||
sendOk();
|
||||
} catch (e) {
|
||||
|
||||
sendError(e.message, e.stack);
|
||||
}
|
||||
}
|
||||
|
||||
if (action == "set_prop") {
|
||||
try {
|
||||
if (!window.component) {
|
||||
return;
|
||||
function handle_message(ev) {
|
||||
let { action, cmd_id } = ev.data;
|
||||
const send_message = (payload) => parent.postMessage( { ...payload }, ev.origin);
|
||||
const send_reply = (payload) => send_message({ ...payload, cmd_id });
|
||||
const send_ok = () => send_reply({ action: 'cmd_ok' });
|
||||
const send_error = (message, stack) => send_reply({ action: 'cmd_error', message, stack });
|
||||
|
||||
if (action === 'eval') {
|
||||
try {
|
||||
const { script } = ev.data.args;
|
||||
eval(script);
|
||||
send_ok();
|
||||
} catch (e) {
|
||||
send_error(e.message, e.stack);
|
||||
}
|
||||
let { prop, value } = ev.data.args;
|
||||
component[prop] = value;
|
||||
sendOk();
|
||||
} catch (e) {
|
||||
sendError(e.message, e.stack);
|
||||
}
|
||||
}
|
||||
|
||||
if (action == "catch_clicks") {
|
||||
try {
|
||||
let topOrigin = ev.origin;
|
||||
document.body.addEventListener('click', event => {
|
||||
if (event.which !== 1) return;
|
||||
if (event.metaKey || event.ctrlKey || event.shiftKey) return;
|
||||
if (event.defaultPrevented) return;
|
||||
|
||||
// ensure target is a link
|
||||
let el = event.target;
|
||||
while (el && el.nodeName !== 'A') el = el.parentNode;
|
||||
if (!el || el.nodeName !== 'A') return;
|
||||
|
||||
if (el.hasAttribute('download') || el.getAttribute('rel') === 'external' || el.target) return;
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
if (el.href.startsWith(topOrigin)) {
|
||||
const url = new URL(el.href);
|
||||
if (url.hash[0] === '#') {
|
||||
window.location.hash = url.hash;
|
||||
return;
|
||||
if (action === 'catch_clicks') {
|
||||
try {
|
||||
const top_origin = ev.origin;
|
||||
document.body.addEventListener('click', event => {
|
||||
if (event.which !== 1) return;
|
||||
if (event.metaKey || event.ctrlKey || event.shiftKey) return;
|
||||
if (event.defaultPrevented) return;
|
||||
|
||||
// ensure target is a link
|
||||
let el = event.target;
|
||||
while (el && el.nodeName !== 'A') el = el.parentNode;
|
||||
if (!el || el.nodeName !== 'A') return;
|
||||
|
||||
if (el.hasAttribute('download') || el.getAttribute('rel') === 'external' || el.target) return;
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
if (el.href.startsWith(top_origin)) {
|
||||
const url = new URL(el.href);
|
||||
if (url.hash[0] === '#') {
|
||||
window.location.hash = url.hash;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.open(el.href, '_blank');
|
||||
});
|
||||
sendOk();
|
||||
} catch(e) {
|
||||
sendError(e.message, e.stack);
|
||||
window.open(el.href, '_blank');
|
||||
});
|
||||
send_ok();
|
||||
} catch(e) {
|
||||
send_error(e.message, e.stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (action == "fetch_imports") {
|
||||
let { imports, import_map } = ev.data.args;
|
||||
fetchImports(imports, (remaining) => {
|
||||
sendMessage({action: "fetch_progress", args: { remaining }});
|
||||
})
|
||||
.then(() => {
|
||||
imports.forEach(x=> {
|
||||
const module = importCache[x];
|
||||
const name = import_map.get(x);
|
||||
window[name] = module;
|
||||
if (action === 'fetch_imports') {
|
||||
const { imports, import_map } = ev.data.args;
|
||||
fetch_imports(imports, (remaining) => {
|
||||
send_message({action: 'fetch_progress', args: { remaining }});
|
||||
})
|
||||
.then(() => {
|
||||
imports.forEach(x=> {
|
||||
const module = import_cache[x];
|
||||
const name = import_map.get(x);
|
||||
window[name] = module;
|
||||
});
|
||||
send_ok();
|
||||
})
|
||||
.catch(e => {
|
||||
send_error(e.message, e.stack);
|
||||
});
|
||||
sendOk();
|
||||
})
|
||||
.catch(e => {
|
||||
sendError(e.message, e.stack);
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("message", handleMessage, false)
|
||||
|
||||
console.log("repl-runner initialized");
|
||||
|
||||
window.addEventListener('message', handle_message, false);
|
||||
})();
|
||||
|
Loading…
Reference in new issue