repl: implement version=local

pull/1994/head
Conduitry 6 years ago
parent 5be480d7ac
commit 01ff158e52

@ -14,11 +14,13 @@
let name = 'loading...'; let name = 'loading...';
onMount(() => { onMount(() => {
fetch(`https://unpkg.com/svelte@${version}/package.json`) if (version !== 'local') {
.then(r => r.json()) fetch(`https://unpkg.com/svelte@${version}/package.json`)
.then(pkg => { .then(r => r.json())
version = pkg.version; .then(pkg => {
}); version = pkg.version;
});
}
if (query.gist) { if (query.gist) {
fetch(`gist/${query.gist}`).then(r => r.json()).then(data => { fetch(`gist/${query.gist}`).then(r => r.json()).then(data => {

@ -45,11 +45,13 @@
}); });
onMount(() => { onMount(() => {
fetch(`https://unpkg.com/svelte@${version || 'alpha'}/package.json`) if (version !== 'local') {
.then(r => r.json()) fetch(`https://unpkg.com/svelte@${version || 'alpha'}/package.json`)
.then(pkg => { .then(r => r.json())
version = pkg.version; .then(pkg => {
}); version = pkg.version;
});
}
if (gist_id) { if (gist_id) {
fetch(`gist/${gist_id}`).then(r => r.json()).then(data => { fetch(`gist/${gist_id}`).then(r => r.json()).then(data => {

@ -0,0 +1,16 @@
import { createReadStream } from 'fs';
export function get(req, res) {
if (process.env.NODE_ENV !== 'development' || !/^[a-z.]+$/.test(req.query.file)) {
res.writeHead(403);
res.end();
return;
}
createReadStream('../' + req.query.file)
.on('error', () => {
res.writeHead(403);
res.end();
})
.pipe(res);
res.writeHead(200, { 'Content-Type': 'text/javascript' });
}

@ -11,7 +11,9 @@ self.addEventListener('message', async event => {
version = event.data.version; version = event.data.version;
importScripts( importScripts(
`https://unpkg.com/svelte@${version}/compiler.js`, version === 'local' ?
'/repl/local?file=compiler.js' :
`https://unpkg.com/svelte@${version}/compiler.js`,
`https://unpkg.com/rollup@0.68/dist/rollup.browser.js` `https://unpkg.com/rollup@0.68/dist/rollup.browser.js`
); );
fulfil(); fulfil();
@ -47,7 +49,7 @@ const is_svelte_module = id => id === 'svelte' || id.startsWith('svelte/');
const cache = new Map(); const cache = new Map();
function fetch_if_uncached(url) { function fetch_if_uncached(url) {
if (!cache.has(url)) { if (!cache.has(url)) {
cache.set(url, fetch(url) cache.set(url, fetch(url.startsWith('https://unpkg.com/svelte@local/') ? '/repl/local?file=' + url.slice(31) : url)
.then(r => r.text()) .then(r => r.text())
.catch(err => { .catch(err => {
console.error(err); console.error(err);
@ -78,7 +80,7 @@ async function getBundle(mode, cache, lookup) {
resolveId(importee, importer) { resolveId(importee, importer) {
// v3 hack // v3 hack
if (importee === `svelte`) return `https://unpkg.com/svelte@${version}/index.mjs`; if (importee === `svelte`) return `https://unpkg.com/svelte@${version}/index.mjs`;
if (importee.startsWith(`svelte`)) return `https://unpkg.com/svelte@${version}/${importee.slice(7)}.mjs`; if (importee.startsWith(`svelte/`)) return `https://unpkg.com/svelte@${version}/${importee.slice(7)}.mjs`;
if (importer && importer.startsWith(`https://`)) { if (importer && importer.startsWith(`https://`)) {
return new URL(`${importee}.mjs`, importer).href; return new URL(`${importee}.mjs`, importer).href;

@ -8,7 +8,11 @@ const ready = new Promise(f => {
self.addEventListener('message', async event => { self.addEventListener('message', async event => {
switch (event.data.type) { switch (event.data.type) {
case 'init': case 'init':
importScripts(`https://unpkg.com/svelte@${event.data.version}/compiler.js`); importScripts(
event.data.version === 'local' ?
'/repl/local?file=compiler.js' :
`https://unpkg.com/svelte@${event.data.version}/compiler.js`
);
fulfil_ready(); fulfil_ready();
break; break;

Loading…
Cancel
Save