Skip to content

Commit

Permalink
fix #766 (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Nov 2, 2024
1 parent eb18e82 commit e2c5fef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/editor/src/lib/Workspace.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CompileError, CompileOptions, CompileResult } from 'svelte/compiler';
import type { CompileError, CompileResult } from 'svelte/compiler';
import { Compartment, EditorState } from '@codemirror/state';
import { compile_file } from './compile-worker';
import { BROWSER } from 'esm-env';
Expand Down Expand Up @@ -77,7 +77,7 @@ const default_extensions = [
// extensions.push(vim());
// }

interface ExposedCompilerOptions {
export interface ExposedCompilerOptions {
generate: 'client' | 'server';
dev: boolean;
modernAst: boolean;
Expand Down
26 changes: 20 additions & 6 deletions packages/editor/src/lib/compile-worker/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { File } from '../Workspace.svelte';
import type { CompileResult } from 'svelte/compiler';
import type { ExposedCompilerOptions, File } from '../Workspace.svelte';

// hack for magic-string and Svelte 4 compiler
// do not put this into a separate module and import it, would be treeshaken in prod
Expand Down Expand Up @@ -39,12 +40,10 @@ addEventListener('message', async (event) => {
const { id, file, options } = event.data as {
id: number;
file: File;
options: { generate: 'client' | 'server'; dev: boolean };
options: ExposedCompilerOptions;
};

const fn = file.name.endsWith('.svelte') ? self.svelte.compile : self.svelte.compileModule;

if (!fn) {
if (!file.name.endsWith('.svelte') && !self.svelte.compileModule) {
// .svelte.js file compiled with Svelte 3/4 compiler
postMessage({
id,
Expand All @@ -69,7 +68,22 @@ addEventListener('message', async (event) => {
}

try {
const result = fn(file.contents, { ...options, filename: file.name });
let result: CompileResult;

if (file.name.endsWith('.svelte')) {
result = self.svelte.compile(file.contents, {
generate: options.generate, // TODO do we need to adjust this for 3/4?
dev: options.dev,
modernAst: options.modernAst,
filename: file.name
});
} else {
result = self.svelte.compileModule(file.contents, {
generate: options.generate,
dev: options.dev,
filename: file.name
});
}

postMessage({
id,
Expand Down

0 comments on commit e2c5fef

Please sign in to comment.