Skip to content

Commit cf20346

Browse files
authored
use module workers (sveltejs#1228)
* use module workers * fix * fix, i think
1 parent 3b045a6 commit cf20346

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

apps/svelte.dev/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ const config: UserConfig = {
7676
ssr: {
7777
noExternal: ['@sveltejs/site-kit', '@sveltejs/repl'],
7878
external: ['shiki', '@shikijs/twoslash']
79+
},
80+
worker: {
81+
format: 'es'
7982
}
8083
};
8184

packages/editor/src/lib/compile-worker/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { BROWSER } from 'esm-env';
2-
import CompileWorker from './worker?worker';
32
import type { Compiled, File } from '../Workspace.svelte';
43

54
const callbacks = new Map<string, Map<number, (compiled: Compiled) => void>>();
@@ -9,7 +8,9 @@ let worker: Worker;
98
let uid = 1;
109

1110
if (BROWSER) {
12-
worker = new CompileWorker();
11+
worker = new Worker(new URL('./worker', import.meta.url), {
12+
type: 'module'
13+
});
1314

1415
worker.addEventListener('message', (event) => {
1516
const { filename, id, payload } = event.data;

packages/repl/src/lib/Bundler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { CompileOptions } from 'svelte/compiler';
21
import type { BundleResult } from './workers/bundler';
3-
import Worker from './workers/bundler/index.js?worker';
42
import type { BundleMessageData } from './workers/workers';
53
import type { File } from 'editor';
64

@@ -27,7 +25,10 @@ export default class Bundler {
2725
this.hash = `${packages_url}:${svelte_version}`;
2826

2927
if (!workers.has(this.hash)) {
30-
const worker = new Worker();
28+
const worker = new Worker(new URL('./workers/bundler/index', import.meta.url), {
29+
type: 'module'
30+
});
31+
3132
worker.postMessage({ type: 'init', packages_url, svelte_version });
3233
workers.set(this.hash, worker);
3334
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import '@sveltejs/site-kit/polyfills';
2-
import * as tailwindcss from 'tailwindcss';
32
import { walk } from 'zimmerframe';
43
import '../patch_window';
54
import { sleep } from '../../utils';
@@ -23,10 +22,6 @@ import type { Node } from 'estree';
2322
import { parseTar, type FileDescription } from 'tarparser';
2423
import { max } from './semver';
2524

26-
import tailwind_preflight from 'tailwindcss/preflight.css?raw';
27-
import tailwind_theme from 'tailwindcss/theme.css?raw';
28-
import tailwind_utilities from 'tailwindcss/utilities.css?raw';
29-
3025
// hack for magic-string and rollup inline sourcemaps
3126
// do not put this into a separate module and import it, would be treeshaken in prod
3227
self.window = self;
@@ -259,6 +254,12 @@ let previous: {
259254
let tailwind: Awaited<ReturnType<typeof init_tailwind>>;
260255

261256
async function init_tailwind() {
257+
const tailwindcss = await import('tailwindcss');
258+
259+
const { default: tailwind_preflight } = await import('tailwindcss/preflight.css?raw');
260+
const { default: tailwind_theme } = await import('tailwindcss/theme.css?raw');
261+
const { default: tailwind_utilities } = await import('tailwindcss/utilities.css?raw');
262+
262263
const tailwind_files: Record<string, string> = {
263264
'tailwindcss/theme.css': tailwind_theme,
264265
'tailwindcss/preflight.css': tailwind_preflight,

packages/site-kit/src/lib/search/SearchBox.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ It appears when the user clicks on the `Search` component or presses the corresp
1010
import { focusable_children, forcefocus, trap } from '../actions/focus.js';
1111
import Icon from '../components/Icon.svelte';
1212
import SearchResults from './SearchResults.svelte';
13-
import SearchWorker from './search-worker.js?worker';
1413
import { page } from '$app/stores';
1514
1615
interface Props {
@@ -37,7 +36,9 @@ It appears when the user clicks on the `Search` component or presses the corresp
3736
const pending = new Set();
3837
3938
onMount(async () => {
40-
worker = new SearchWorker();
39+
worker = new Worker(new URL('./search-worker', import.meta.url), {
40+
type: 'module'
41+
});
4142
4243
worker.addEventListener('message', (event) => {
4344
const { type, payload } = event.data;

0 commit comments

Comments
 (0)