Skip to content

Commit 4563646

Browse files
authored
fix(sw): get transpiled files from bundler (#1196)
* fix(sw): load transpiled files * fix(sw): load transpiled files
1 parent c0d47f5 commit 4563646

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

sandpack-client/src/clients/runtime/index.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
ListenerFunction,
1010
Modules,
1111
SandboxSetup,
12+
SandpackBundlerFile,
1213
SandpackBundlerFiles,
1314
SandpackError,
1415
UnsubscribeFunction,
@@ -225,17 +226,46 @@ export class SandpackRuntime extends SandpackClient {
225226
this.iframe.addEventListener("load", sendMessage);
226227
}
227228

228-
private handleWorkerRequest(
229+
private async handleWorkerRequest(
229230
request: IPreviewRequestMessage,
230231
port: MessagePort
231232
) {
233+
const notFound = () => {
234+
const responseMessage: IPreviewResponseMessage = {
235+
$channel: CHANNEL_NAME,
236+
$type: "preview/response",
237+
id: request.id,
238+
headers: {
239+
"Content-Type": "text/html; charset=utf-8",
240+
},
241+
status: 404,
242+
body: "File not found",
243+
};
244+
245+
port.postMessage(responseMessage);
246+
};
232247
try {
233248
const filepath = new URL(request.url, this.bundlerURL).pathname;
234249

235250
const headers: Record<string, string> = {};
236251

237252
const files = this.getFiles();
238-
const body = files[filepath].code;
253+
let file = files[filepath];
254+
255+
if (!file) {
256+
const modulesFromManager = await this.getTranspiledFiles();
257+
258+
file = modulesFromManager.find((item) =>
259+
item.path.endsWith(filepath)
260+
) as SandpackBundlerFile;
261+
262+
if (!file) {
263+
notFound();
264+
return;
265+
}
266+
}
267+
268+
const body = file.code;
239269

240270
if (!headers["Content-Type"]) {
241271
const extension = getExtension(filepath);
@@ -256,18 +286,8 @@ export class SandpackRuntime extends SandpackClient {
256286

257287
port.postMessage(responseMessage);
258288
} catch (err) {
259-
const responseMessage: IPreviewResponseMessage = {
260-
$channel: CHANNEL_NAME,
261-
$type: "preview/response",
262-
id: request.id,
263-
headers: {
264-
"Content-Type": "text/html; charset=utf-8",
265-
},
266-
status: 404,
267-
body: "File not found",
268-
};
269-
270-
port.postMessage(responseMessage);
289+
console.error(err);
290+
notFound();
271291
}
272292
}
273293

@@ -447,6 +467,22 @@ export class SandpackRuntime extends SandpackClient {
447467
this.dispatch({ type: "get-transpiler-context" });
448468
});
449469

470+
public getTranspiledFiles = (): Promise<
471+
Array<{ path: string; code: string }>
472+
> => {
473+
return new Promise((resolve) => {
474+
const unsubscribe = this.listen((message) => {
475+
if (message.type === "all-modules") {
476+
resolve(message.data);
477+
478+
unsubscribe();
479+
}
480+
});
481+
482+
this.dispatch({ type: "get-modules" });
483+
});
484+
};
485+
450486
private getFiles(): SandpackBundlerFiles {
451487
const { sandboxSetup } = this;
452488

sandpack-client/src/clients/runtime/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ export type SandpackRuntimeMessage = BaseSandpackMessage &
9494
| {
9595
type: "get-transpiler-context";
9696
}
97+
| { type: "get-modules" }
98+
| { type: "all-modules"; data: Array<{ path: string; code: string }> }
9799
| {
98100
type: "activate-react-devtools";
99101
}

sandpack-react/src/components/FileTabs/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface FileTabsProps {
8787
export const FileTabs = ({
8888
closableTabs,
8989
className,
90+
activeFileUniqueId,
9091
...props
9192
}: FileTabsProps & React.HTMLAttributes<HTMLDivElement>): JSX.Element => {
9293
const { sandpack } = useSandpack();
@@ -200,19 +201,19 @@ export const FileTabs = ({
200201
>
201202
{visibleFiles.map((filePath, index) => (
202203
<div
203-
aria-controls={`${filePath}-${props.activeFileUniqueId}-tab-panel`}
204+
aria-controls={`${filePath}-${activeFileUniqueId}-tab-panel`}
204205
aria-selected={filePath === activeFile}
205206
className={classNames("tab-container", [tabContainer])}
206207
onKeyDown={(e) => onKeyDown({ e, index })}
207208
onMouseEnter={() => setIsHoveredIndex(index)}
208209
onMouseLeave={() => setIsHoveredIndex(null)}
209210
role="tab"
211+
key={filePath}
210212
>
211213
<button
212-
key={filePath}
213214
className={classNames("tab-button", [buttonClassName, tabButton])}
214215
data-active={filePath === activeFile}
215-
id={`${filePath}-${props.activeFileUniqueId}-tab`}
216+
id={`${filePath}-${activeFileUniqueId}-tab`}
216217
onClick={(): void => setActiveFile(filePath)}
217218
tabIndex={filePath === activeFile ? 0 : -1}
218219
title={filePath}

0 commit comments

Comments
 (0)