Skip to content

Commit 37f5ec1

Browse files
authored
fix: support Svelte 3/4 in playground (sveltejs#499)
closes sveltejs#433
1 parent 362c96d commit 37f5ec1

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

apps/svelte.dev/src/lib/tutorial/adapters/rollup/index.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function create(): Promise<Adapter> {
2424

2525
bundler = new Bundler({
2626
packages_url: 'https://unpkg.com',
27-
svelte_url: `https://unpkg.com/svelte@next`, // TODO remove @next once 5.0 is released
27+
svelte_url: `https://unpkg.com/svelte`,
2828
// svelte_url: `${browser ? location.origin : ''}/svelte`, // TODO think about bringing back main-build for Playground?
2929
onstatus(val) {
3030
if (!done && val === null) {

apps/svelte.dev/src/routes/(authed)/playground/[id]/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export async function load({ fetch, params, url }) {
2222
slug: example.slug
2323
}))
2424
})),
25-
version: url.searchParams.get('version') || 'next' // TODO replace with 'latest' when 5.0 is released
25+
version: url.searchParams.get('version') || 'latest'
2626
};
2727
}

packages/repl/src/lib/workers/bundler/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { File } from 'editor';
1919

2020
let packages_url: string;
2121
let svelte_url: string;
22+
let version: string;
2223
let current_id: number;
2324

2425
let fulfil_ready: (arg?: never) => void;
@@ -31,11 +32,18 @@ self.addEventListener('message', async (event: MessageEvent<BundleMessageData>)
3132
case 'init': {
3233
({ packages_url, svelte_url } = event.data);
3334

34-
const { version } = await fetch(`${svelte_url}/package.json`).then((r) => r.json());
35+
({ version } = await fetch(`${svelte_url}/package.json`).then((r) => r.json()));
3536
console.log(`Using Svelte compiler version ${version}`);
3637

37-
const compiler = await fetch(`${svelte_url}/compiler/index.js`).then((r) => r.text());
38-
(0, eval)(compiler + '\n//# sourceURL=compiler/index.js@' + version);
38+
if (version.startsWith('4.')) {
39+
// unpkg doesn't set the correct MIME type for .cjs files
40+
// https://github.com/mjackson/unpkg/issues/355
41+
const compiler = await fetch(`${svelte_url}/compiler.cjs`).then((r) => r.text());
42+
(0, eval)(compiler + '\n//# sourceURL=compiler.cjs@' + version);
43+
} else {
44+
const compiler = await fetch(`${svelte_url}/compiler/index.js`).then((r) => r.text());
45+
(0, eval)(compiler + '\n//# sourceURL=compiler/index.js@' + version);
46+
}
3947

4048
fulfil_ready();
4149
break;
@@ -359,7 +367,7 @@ async function get_bundle(
359367
dev: true
360368
});
361369

362-
if (result.css) {
370+
if (result.css?.code) {
363371
// resolve local files by inlining them
364372
result.css.code = result.css.code.replace(
365373
/url\(['"]?(\..+?\.(svg|webp|png))['"]?\)/g,
@@ -481,7 +489,9 @@ async function bundle({
481489
type: 'file',
482490
name: '__entry.js',
483491
basename: '__entry.js',
484-
contents: `
492+
contents:
493+
version.split('.')[0] >= '5'
494+
? `
485495
import { unmount as u } from 'svelte';
486496
import { styles } from './__shared.js';
487497
export { mount, untrack } from 'svelte';
@@ -490,6 +500,20 @@ async function bundle({
490500
u(component);
491501
styles.forEach(style => style.remove());
492502
}
503+
`
504+
: `
505+
import { styles } from './__shared.js';
506+
export {default as App} from './App.svelte';
507+
export function mount(component, options) {
508+
return new component(options);
509+
}
510+
export function unmount(component) {
511+
component.$destroy();
512+
styles.forEach(style => style.remove());
513+
}
514+
export function untrack(fn) {
515+
return fn();
516+
}
493517
`,
494518
text: true
495519
});

0 commit comments

Comments
 (0)