@marimo-team/islands 0.19.8-dev23 → 0.19.8-dev25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -13114,11 +13114,11 @@ ${d.join("\n")}`;
13114
13114
  var r = e.toUpperCase();
13115
13115
  return methods.indexOf(r) > -1 ? r : e;
13116
13116
  }
13117
- function Request(e, r) {
13118
- if (!(this instanceof Request)) throw TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');
13117
+ function Request$1(e, r) {
13118
+ if (!(this instanceof Request$1)) throw TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');
13119
13119
  r || (r = {});
13120
13120
  var c = r.body;
13121
- if (e instanceof Request) {
13121
+ if (e instanceof Request$1) {
13122
13122
  if (e.bodyUsed) throw TypeError("Already read");
13123
13123
  this.url = e.url, this.credentials = e.credentials, r.headers || (this.headers = new Headers$2(e.headers)), this.method = e.method, this.mode = e.mode, this.signal = e.signal, !c && e._bodyInit != null && (c = e._bodyInit, e.bodyUsed = true);
13124
13124
  } else this.url = String(e);
@@ -13162,7 +13162,7 @@ ${d.join("\n")}`;
13162
13162
  }
13163
13163
  function fetch$1(e, r) {
13164
13164
  return new Promise(function(c, d) {
13165
- var f = new Request(e, r);
13165
+ var f = new Request$1(e, r);
13166
13166
  if (f.signal && f.signal.aborted) return d(new DOMException$1("Aborted", "AbortError"));
13167
13167
  var _ = new XMLHttpRequest();
13168
13168
  function v() {
@@ -13280,11 +13280,11 @@ ${d.join("\n")}`;
13280
13280
  "POST",
13281
13281
  "PUT",
13282
13282
  "TRACE"
13283
- ], Request.prototype.clone = function() {
13284
- return new Request(this, {
13283
+ ], Request$1.prototype.clone = function() {
13284
+ return new Request$1(this, {
13285
13285
  body: this._bodyInit
13286
13286
  });
13287
- }, Body.call(Request.prototype), Body.call(Response$1.prototype), Response$1.prototype.clone = function() {
13287
+ }, Body.call(Request$1.prototype), Body.call(Response$1.prototype), Response$1.prototype.clone = function() {
13288
13288
  return new Response$1(this._bodyInit, {
13289
13289
  status: this.status,
13290
13290
  statusText: this.statusText,
@@ -13319,7 +13319,7 @@ ${d.join("\n")}`;
13319
13319
  this.message = e, this.name = r, this.stack = Error(e).stack;
13320
13320
  }, DOMException$1.prototype = Object.create(Error.prototype), DOMException$1.prototype.constructor = DOMException$1;
13321
13321
  }
13322
- fetch$1.polyfill = true, g$3.fetch || (g$3.fetch = fetch$1, g$3.Headers = Headers$2, g$3.Request = Request, g$3.Response = Response$1);
13322
+ fetch$1.polyfill = true, g$3.fetch || (g$3.fetch = fetch$1, g$3.Headers = Headers$2, g$3.Request = Request$1, g$3.Response = Response$1);
13323
13323
  })), require_fetch_npm_browserify = __commonJSMin(((e, r) => {
13324
13324
  init_fetch(), r.exports = self.fetch.bind(self);
13325
13325
  })), require_HTTPTransport = __commonJSMin(((e) => {
@@ -39750,6 +39750,17 @@ ${c.sqlString}
39750
39750
  }
39751
39751
  return e;
39752
39752
  },
39753
+ markUntouched: (e, r) => {
39754
+ let { cellId: c } = r;
39755
+ if (!e.untouchedNewCells.has(c)) {
39756
+ let r2 = new Set(e.untouchedNewCells);
39757
+ return r2.add(c), {
39758
+ ...e,
39759
+ untouchedNewCells: r2
39760
+ };
39761
+ }
39762
+ return e;
39763
+ },
39753
39764
  scrollToTarget: (e) => {
39754
39765
  let r = e.scrollKey;
39755
39766
  if (r === null) return e;
@@ -44047,6 +44058,9 @@ ${c.sqlString}
44047
44058
  function isStaticNotebook() {
44048
44059
  return (window == null ? void 0 : window.__MARIMO_STATIC__) !== void 0;
44049
44060
  }
44061
+ function getStaticVirtualFiles() {
44062
+ return invariant(window.__MARIMO_STATIC__ !== void 0, "Not a static notebook"), window.__MARIMO_STATIC__.files;
44063
+ }
44050
44064
  function shallowCompare(e, r) {
44051
44065
  return e === r ? true : e == null || r == null ? false : Array.isArray(e) && Array.isArray(r) ? arrayShallowEquals(e, r) : typeof e == "object" && typeof r == "object" ? shallowCompareObjects(e, r) : false;
44052
44066
  }
@@ -64073,6 +64087,35 @@ ${O}`,
64073
64087
  }
64074
64088
  });
64075
64089
  }
64090
+ function deserializeBlob(e) {
64091
+ var _a2;
64092
+ let [r, c] = e.split(",", 2), d = (_a2 = /^data:(.+);base64$/.exec(r)) == null ? void 0 : _a2[1], f = atob(c), _ = f.length, v = new Uint8Array(_);
64093
+ for (let e2 = 0; e2 < _; e2++) v[e2] = f.charCodeAt(e2);
64094
+ return new Blob([
64095
+ v
64096
+ ], {
64097
+ type: d
64098
+ });
64099
+ }
64100
+ function withoutLeadingDot(e) {
64101
+ return e.startsWith(".") ? e.slice(1) : e;
64102
+ }
64103
+ function resolveVirtualFileURL(e, r = getStaticVirtualFiles()) {
64104
+ let c = maybeGetVirtualFile(e, r);
64105
+ if (!c) return e;
64106
+ let d = deserializeBlob(c);
64107
+ return URL.createObjectURL(d);
64108
+ }
64109
+ function maybeGetVirtualFile(e, r) {
64110
+ let c = document.baseURI;
64111
+ c.startsWith("blob:") && (c = c.replace("blob:", ""));
64112
+ let d = new URL(e, c).pathname, f = extractFilePath(e), _ = extractFilePath(d);
64113
+ return r[e] || r[withoutLeadingDot(e)] || r[d] || r[withoutLeadingDot(d)] || f && r[f] || _ && r[_];
64114
+ }
64115
+ function extractFilePath(e) {
64116
+ let r = e.indexOf("/@file/");
64117
+ return r === -1 ? null : e.slice(r);
64118
+ }
64076
64119
  function createPlugin(e, r = {}) {
64077
64120
  return {
64078
64121
  withData(c) {
@@ -64340,10 +64383,13 @@ ${O}`,
64340
64383
  return Logger.warn("AnyWidget value is not wire format:", e.value), e.value;
64341
64384
  }, [
64342
64385
  e.value
64343
- ]), { data: _, error: v, refetch: y } = useAsyncData(async () => await import(asRemoteURL(c).toString()).then(async (m2) => {
64344
- await m2.__tla;
64345
- return m2;
64346
- }), [
64386
+ ]), { data: _, error: v, refetch: y } = useAsyncData(async () => {
64387
+ let e2 = asRemoteURL(c).toString();
64388
+ return isStaticNotebook() && (e2 = resolveVirtualFileURL(e2)), await import(e2).then(async (m2) => {
64389
+ await m2.__tla;
64390
+ return m2;
64391
+ });
64392
+ }, [
64347
64393
  d
64348
64394
  ]), S = !!v;
64349
64395
  (0, import_react.useEffect)(() => {
@@ -71094,25 +71140,6 @@ Image URL: ${r.imageUrl}`)), contextToXml({
71094
71140
  }
71095
71141
  };
71096
71142
  const contextCallbacks = singleFacet();
71097
- function deserializeBlob(e) {
71098
- return new Promise((r, c) => {
71099
- var _a2;
71100
- try {
71101
- let [c2, d] = e.split(",", 2), f = (_a2 = /^data:(.+);base64$/.exec(c2)) == null ? void 0 : _a2[1], _ = atob(d), v = _.length, y = new Uint8Array(v);
71102
- for (let e2 = 0; e2 < v; e2++) y[e2] = _.charCodeAt(e2);
71103
- r(new Blob([
71104
- y
71105
- ], {
71106
- type: f
71107
- }));
71108
- } catch (e2) {
71109
- c(ensureError(e2));
71110
- }
71111
- });
71112
- }
71113
- function ensureError(e) {
71114
- return e instanceof Error ? e : Error(`${e}`);
71115
- }
71116
71143
  var DEFAULT_FILE_SEARCH_CONFIG = {
71117
71144
  maxDepth: 3,
71118
71145
  maxResults: 20,
@@ -71244,7 +71271,7 @@ Image URL: ${r.imageUrl}`)), contextToXml({
71244
71271
  type: S
71245
71272
  });
71246
71273
  else if (y.contents) try {
71247
- E = await deserializeBlob(base64ToDataURL(y.contents, S));
71274
+ E = deserializeBlob(base64ToDataURL(y.contents, S));
71248
71275
  } catch {
71249
71276
  E = new Blob([
71250
71277
  y.contents
@@ -73176,7 +73203,7 @@ Image URL: ${r.imageUrl}`)), contextToXml({
73176
73203
  return Logger.warn("Failed to get version from mount config"), null;
73177
73204
  }
73178
73205
  }
73179
- const marimoVersionAtom = atom(getVersionFromMountConfig() || "0.19.8-dev23"), showCodeInRunModeAtom = atom(true);
73206
+ const marimoVersionAtom = atom(getVersionFromMountConfig() || "0.19.8-dev25"), showCodeInRunModeAtom = atom(true);
73180
73207
  atom(null);
73181
73208
  var import_compiler_runtime$88 = require_compiler_runtime();
73182
73209
  function useKeydownOnElement(e, r) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/islands",
3
- "version": "0.19.8-dev23",
3
+ "version": "0.19.8-dev25",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -43,6 +43,7 @@ import type { CellData } from "@/core/cells/types";
43
43
  import { formatEditorViews } from "@/core/codemirror/format";
44
44
  import { toggleToLanguage } from "@/core/codemirror/language/commands";
45
45
  import { switchLanguage } from "@/core/codemirror/language/extension";
46
+ import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
46
47
  import {
47
48
  aiEnabledAtom,
48
49
  appWidthAtom,
@@ -85,6 +86,7 @@ export function useCellActionButtons({ cell, closePopover }: Props) {
85
86
  sendToBottom,
86
87
  addColumnBreakpoint,
87
88
  clearCellOutput,
89
+ markUntouched,
88
90
  } = useCellActions();
89
91
  const splitCell = useSplitCellCallback();
90
92
  const runCell = useRunCell(cell?.cellId);
@@ -209,7 +211,7 @@ export function useCellActionButtons({ cell, closePopover }: Props) {
209
211
  icon: <MarkdownIcon />,
210
212
  label: "Convert to Markdown",
211
213
  hotkey: "cell.viewAsMarkdown",
212
- handle: () => {
214
+ handle: async () => {
213
215
  const editorView = getEditorView();
214
216
  if (!editorView) {
215
217
  return;
@@ -219,6 +221,17 @@ export function useCellActionButtons({ cell, closePopover }: Props) {
219
221
  language: "markdown",
220
222
  keepCodeAsIs: false,
221
223
  });
224
+ // Code stays visible until the user blurs the cell
225
+ if (!config.hide_code && MARKDOWN_INITIAL_HIDE_CODE) {
226
+ await saveCellConfig({
227
+ configs: { [cellId]: { hide_code: MARKDOWN_INITIAL_HIDE_CODE } },
228
+ });
229
+ updateCellConfig({
230
+ cellId,
231
+ config: { hide_code: MARKDOWN_INITIAL_HIDE_CODE },
232
+ });
233
+ markUntouched({ cellId });
234
+ }
222
235
  },
223
236
  hidden: isSetupCell,
224
237
  },
@@ -12,6 +12,7 @@ import {
12
12
  import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";
13
13
  import { useCellActions } from "@/core/cells/cells";
14
14
  import { LanguageAdapters } from "@/core/codemirror/language/LanguageAdapters";
15
+ import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
15
16
  import {
16
17
  getConnectionTooltip,
17
18
  isAppInteractionDisabled,
@@ -63,7 +64,7 @@ export const CreateCellButton = ({
63
64
  maybeAddMarimoImport({ autoInstantiate: true, createNewCell });
64
65
  onClick?.({
65
66
  code: LanguageAdapters.markdown.defaultCode,
66
- hideCode: true,
67
+ hideCode: MARKDOWN_INITIAL_HIDE_CODE,
67
68
  });
68
69
  };
69
70
 
@@ -19,6 +19,7 @@ import {
19
19
  reconfigureLanguageEffect,
20
20
  switchLanguage,
21
21
  } from "@/core/codemirror/language/extension";
22
+ import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
22
23
  import type { LanguageAdapterType } from "@/core/codemirror/language/types";
23
24
  import {
24
25
  connectedDocAtom,
@@ -149,6 +150,17 @@ const CellEditorInternal = ({
149
150
  autoInstantiate,
150
151
  createNewCell: cellActions.createNewCell,
151
152
  });
153
+ // Code stays visible until the user blurs the cell
154
+ if (!cellConfig.hide_code && MARKDOWN_INITIAL_HIDE_CODE) {
155
+ void saveCellConfig({
156
+ configs: { [cellId]: { hide_code: MARKDOWN_INITIAL_HIDE_CODE } },
157
+ });
158
+ cellActions.updateCellConfig({
159
+ cellId,
160
+ config: { hide_code: MARKDOWN_INITIAL_HIDE_CODE },
161
+ });
162
+ cellActions.markUntouched({ cellId });
163
+ }
152
164
  });
153
165
 
154
166
  const aiEnabled = isAiEnabled(userConfig);
@@ -23,6 +23,7 @@ import { Tooltip } from "@/components/ui/tooltip";
23
23
  import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";
24
24
  import { SETUP_CELL_ID } from "@/core/cells/ids";
25
25
  import { LanguageAdapters } from "@/core/codemirror/language/LanguageAdapters";
26
+ import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
26
27
  import { aiEnabledAtom } from "@/core/config/config";
27
28
  import { canInteractWithAppAtom } from "@/core/network/connection";
28
29
  import { useBoolean } from "@/hooks/useBoolean";
@@ -295,7 +296,7 @@ const AddCellButtons: React.FC<{
295
296
  cellId: { type: "__end__", columnId },
296
297
  before: false,
297
298
  code: LanguageAdapters.markdown.defaultCode,
298
- hideCode: true,
299
+ hideCode: MARKDOWN_INITIAL_HIDE_CODE,
299
300
  });
300
301
  }}
301
302
  >
@@ -237,7 +237,7 @@ export class FileContextProvider extends AIContextProvider<FileContextItem> {
237
237
  fileDetails.contents as Base64String,
238
238
  mimeType,
239
239
  );
240
- blob = await deserializeBlob(dataURL);
240
+ blob = deserializeBlob(dataURL);
241
241
  } catch {
242
242
  // Fallback to treating as text
243
243
  blob = new Blob([fileDetails.contents], { type: mimeType });
@@ -2561,6 +2561,126 @@ describe("cell reducer", () => {
2561
2561
  expect(state.untouchedNewCells.has(newCellId)).toBe(false);
2562
2562
  expect(exportedForTesting.isCellCodeHidden(state, newCellId)).toBe(true);
2563
2563
  });
2564
+
2565
+ it("can mark an existing cell as untouched", () => {
2566
+ // Create a cell without hideCode (not in untouchedNewCells)
2567
+ actions.createNewCell({
2568
+ cellId: "__end__",
2569
+ before: false,
2570
+ hideCode: false,
2571
+ });
2572
+
2573
+ const newCellId =
2574
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
2575
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
2576
+
2577
+ // Mark it as untouched
2578
+ actions.markUntouched({ cellId: newCellId });
2579
+
2580
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
2581
+ });
2582
+
2583
+ it("markUntouched is idempotent", () => {
2584
+ // Create a cell without hideCode
2585
+ actions.createNewCell({
2586
+ cellId: "__end__",
2587
+ before: false,
2588
+ hideCode: false,
2589
+ });
2590
+
2591
+ const newCellId =
2592
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
2593
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
2594
+
2595
+ // Mark as untouched multiple times
2596
+ actions.markUntouched({ cellId: newCellId });
2597
+ actions.markUntouched({ cellId: newCellId });
2598
+ actions.markUntouched({ cellId: newCellId });
2599
+
2600
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
2601
+ });
2602
+
2603
+ it("markUntouched does not affect already untouched cells", () => {
2604
+ // Create a cell with hideCode (already in untouchedNewCells)
2605
+ actions.createNewCell({
2606
+ cellId: "__end__",
2607
+ before: false,
2608
+ hideCode: true,
2609
+ });
2610
+
2611
+ const newCellId =
2612
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
2613
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
2614
+
2615
+ // Calling markUntouched should not change anything
2616
+ actions.markUntouched({ cellId: newCellId });
2617
+
2618
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
2619
+ });
2620
+
2621
+ it("markTouched and markUntouched can toggle cell state", () => {
2622
+ // Create a cell without hideCode
2623
+ actions.createNewCell({
2624
+ cellId: "__end__",
2625
+ before: false,
2626
+ hideCode: false,
2627
+ });
2628
+
2629
+ const newCellId =
2630
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
2631
+
2632
+ // Initially not untouched
2633
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
2634
+
2635
+ // Mark as untouched
2636
+ actions.markUntouched({ cellId: newCellId });
2637
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
2638
+
2639
+ // Mark as touched
2640
+ actions.markTouched({ cellId: newCellId });
2641
+ expect(state.untouchedNewCells.has(newCellId)).toBe(false);
2642
+
2643
+ // Mark as untouched again
2644
+ actions.markUntouched({ cellId: newCellId });
2645
+ expect(state.untouchedNewCells.has(newCellId)).toBe(true);
2646
+ });
2647
+
2648
+ it("markUntouched works for markdown cell conversion scenario", () => {
2649
+ // Simulates converting a Python cell to Markdown
2650
+ // 1. Create a regular cell (no hideCode)
2651
+ actions.createNewCell({
2652
+ cellId: "__end__",
2653
+ before: false,
2654
+ hideCode: false,
2655
+ });
2656
+
2657
+ const cellId =
2658
+ state.cellIds.inOrderIds[state.cellIds.inOrderIds.length - 1];
2659
+
2660
+ // Cell starts without hide_code and not in untouchedNewCells
2661
+ expect(state.cellData[cellId].config.hide_code).toBe(false);
2662
+ expect(state.untouchedNewCells.has(cellId)).toBe(false);
2663
+ expect(exportedForTesting.isCellCodeHidden(state, cellId)).toBe(false);
2664
+
2665
+ // 2. Convert to markdown: set hide_code and mark as untouched
2666
+ actions.updateCellConfig({
2667
+ cellId,
2668
+ config: { hide_code: true },
2669
+ });
2670
+ actions.markUntouched({ cellId });
2671
+
2672
+ // Code should NOT be hidden because cell is untouched (user can edit)
2673
+ expect(state.cellData[cellId].config.hide_code).toBe(true);
2674
+ expect(state.untouchedNewCells.has(cellId)).toBe(true);
2675
+ expect(exportedForTesting.isCellCodeHidden(state, cellId)).toBe(false);
2676
+
2677
+ // 3. User blurs the cell (markTouched)
2678
+ actions.markTouched({ cellId });
2679
+
2680
+ // Now code should be hidden
2681
+ expect(state.untouchedNewCells.has(cellId)).toBe(false);
2682
+ expect(exportedForTesting.isCellCodeHidden(state, cellId)).toBe(true);
2683
+ });
2564
2684
  });
2565
2685
 
2566
2686
  describe("releaseCellAtoms", () => {
@@ -1033,6 +1033,20 @@ const {
1033
1033
 
1034
1034
  return state;
1035
1035
  },
1036
+ markUntouched: (state, action: { cellId: CellId }) => {
1037
+ const { cellId } = action;
1038
+
1039
+ if (!state.untouchedNewCells.has(cellId)) {
1040
+ const nextUntouchedNewCells = new Set(state.untouchedNewCells);
1041
+ nextUntouchedNewCells.add(cellId);
1042
+ return {
1043
+ ...state,
1044
+ untouchedNewCells: nextUntouchedNewCells,
1045
+ };
1046
+ }
1047
+
1048
+ return state;
1049
+ },
1036
1050
  scrollToTarget: (state) => {
1037
1051
  // Scroll to the specified cell and clear the scroll key.
1038
1052
  const scrollKey = state.scrollKey;
@@ -28,6 +28,13 @@ import type { LanguageAdapter } from "../types";
28
28
 
29
29
  export type MarkdownLanguageAdapterMetadata = MarkdownMetadata;
30
30
 
31
+ /**
32
+ * Default hide_code setting for markdown cells.
33
+ * When true, the markdown code is hidden after the cell is blurred,
34
+ * showing only the rendered output.
35
+ */
36
+ export const MARKDOWN_INITIAL_HIDE_CODE = true;
37
+
31
38
  /**
32
39
  * Language adapter for Markdown.
33
40
  */
@@ -6,7 +6,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
6
6
  import { createLoader } from "@/plugins/impl/vega/vega-loader";
7
7
  import { Functions } from "@/utils/functions";
8
8
  import type { DataURLString } from "@/utils/json/base64";
9
- import { patchFetch, patchVegaLoader } from "../files";
9
+ import { patchFetch, patchVegaLoader, resolveVirtualFileURL } from "../files";
10
10
 
11
11
  // Start a tiny server to serve virtual files
12
12
  const server = http.createServer((request, response) => {
@@ -350,6 +350,181 @@ describe("patchVegaLoader - loader.load", () => {
350
350
  });
351
351
  });
352
352
 
353
+ describe("resolveVirtualFileURL", () => {
354
+ // Mock URL.createObjectURL for jsdom environment
355
+ const mockBlobURLs = new Map<string, Blob>();
356
+ let blobCounter = 0;
357
+
358
+ beforeAll(() => {
359
+ URL.createObjectURL = vi.fn((blob: Blob) => {
360
+ const url = `blob:test-${blobCounter++}`;
361
+ mockBlobURLs.set(url, blob);
362
+ return url;
363
+ });
364
+ URL.revokeObjectURL = vi.fn((url: string) => {
365
+ mockBlobURLs.delete(url);
366
+ });
367
+ });
368
+
369
+ afterAll(() => {
370
+ mockBlobURLs.clear();
371
+ });
372
+
373
+ it("should return a blob URL for virtual files", () => {
374
+ const virtualFiles = {
375
+ "/@file/widget.js":
376
+ "data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQgeyByZW5kZXI6ICgpID0+IHt9IH0=" as DataURLString,
377
+ };
378
+
379
+ const result = resolveVirtualFileURL("/@file/widget.js", virtualFiles);
380
+
381
+ expect(result).toMatch(/^blob:/);
382
+ });
383
+
384
+ it("should return the original URL for non-virtual files", () => {
385
+ const virtualFiles = {};
386
+
387
+ const result = resolveVirtualFileURL(
388
+ "http://example.com/widget.js",
389
+ virtualFiles,
390
+ );
391
+
392
+ expect(result).toBe("http://example.com/widget.js");
393
+ });
394
+
395
+ it("should handle various URL formats", () => {
396
+ const virtualFiles = {
397
+ "/@file/module.js":
398
+ "data:text/javascript;base64,Y29uc29sZS5sb2coJ3Rlc3QnKQ==" as DataURLString,
399
+ };
400
+
401
+ const testUrls = [
402
+ "/@file/module.js",
403
+ "./@file/module.js",
404
+ "http://example.com/@file/module.js",
405
+ ];
406
+
407
+ for (const url of testUrls) {
408
+ const result = resolveVirtualFileURL(url, virtualFiles);
409
+ expect(result).toMatch(/^blob:/);
410
+ }
411
+ });
412
+
413
+ it("should create blob URL with correct content", async () => {
414
+ const jsCode = "export default { render: () => {} }";
415
+ const base64Code = btoa(jsCode);
416
+ const virtualFiles = {
417
+ "/@file/test-module.js":
418
+ `data:text/javascript;base64,${base64Code}` as DataURLString,
419
+ };
420
+
421
+ const blobUrl = resolveVirtualFileURL(
422
+ "/@file/test-module.js",
423
+ virtualFiles,
424
+ );
425
+
426
+ expect(blobUrl).toMatch(/^blob:/);
427
+ expect(URL.createObjectURL).toHaveBeenCalled();
428
+
429
+ // Verify blob content through the mock
430
+ const blob = mockBlobURLs.get(blobUrl);
431
+ expect(blob).toBeDefined();
432
+ const text = await blob!.text();
433
+ expect(text).toBe(jsCode);
434
+ });
435
+
436
+ it("should handle file:// URLs with @file/ paths", () => {
437
+ const virtualFiles = {
438
+ "/@file/local-module.js":
439
+ "data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQge30=" as DataURLString,
440
+ };
441
+
442
+ const result = resolveVirtualFileURL(
443
+ "file:///Users/test/@file/local-module.js",
444
+ virtualFiles,
445
+ );
446
+
447
+ expect(result).toMatch(/^blob:/);
448
+ });
449
+
450
+ it("should handle different MIME types", async () => {
451
+ const virtualFiles = {
452
+ "/@file/script.js":
453
+ "data:application/javascript;base64,Y29uc3QgeCA9IDE=" as DataURLString,
454
+ };
455
+
456
+ const blobUrl = resolveVirtualFileURL("/@file/script.js", virtualFiles);
457
+
458
+ // Should still be a valid blob URL
459
+ expect(blobUrl).toMatch(/^blob:/);
460
+
461
+ // Verify blob content through the mock
462
+ const blob = mockBlobURLs.get(blobUrl);
463
+ expect(blob).toBeDefined();
464
+ const text = await blob!.text();
465
+ expect(text).toBe("const x = 1");
466
+ });
467
+
468
+ it("should handle blob: base URIs correctly", () => {
469
+ // Mock document.baseURI to simulate blob: protocol
470
+ const originalBaseURI = document.baseURI;
471
+ Object.defineProperty(document, "baseURI", {
472
+ value: "blob:https://example.com/uuid",
473
+ configurable: true,
474
+ });
475
+
476
+ const virtualFiles = {
477
+ "/@file/blob-module.js":
478
+ "data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQge30=" as DataURLString,
479
+ };
480
+
481
+ const result = resolveVirtualFileURL("/@file/blob-module.js", virtualFiles);
482
+
483
+ expect(result).toMatch(/^blob:/);
484
+
485
+ // Restore original baseURI
486
+ Object.defineProperty(document, "baseURI", {
487
+ value: originalBaseURI,
488
+ configurable: true,
489
+ });
490
+ });
491
+
492
+ it("should handle data URLs with no explicit MIME type", async () => {
493
+ const virtualFiles = {
494
+ "/@file/generic.bin": "data:;base64,SGVsbG8gV29ybGQ=" as DataURLString,
495
+ };
496
+
497
+ const blobUrl = resolveVirtualFileURL("/@file/generic.bin", virtualFiles);
498
+ expect(blobUrl).toMatch(/^blob:/);
499
+
500
+ // Verify blob content through the mock
501
+ const blob = mockBlobURLs.get(blobUrl);
502
+ expect(blob).toBeDefined();
503
+ const text = await blob!.text();
504
+ expect(text).toBe("Hello World");
505
+ });
506
+
507
+ it("should match URLs with prefix paths before /@file/", async () => {
508
+ const virtualFiles = {
509
+ "/@file/4263-66-yUGhgQXp.js":
510
+ "data:application/javascript;base64,ZnVuY3Rpb24gcmVuZGVyKCkge30=" as DataURLString,
511
+ };
512
+
513
+ const blobUrl = resolveVirtualFileURL(
514
+ "https://molab.marimo.app/preview/@file/4263-66-yUGhgQXp.js",
515
+ virtualFiles,
516
+ );
517
+
518
+ expect(blobUrl).toMatch(/^blob:/);
519
+
520
+ // Verify blob content through the mock
521
+ const blob = mockBlobURLs.get(blobUrl);
522
+ expect(blob).toBeDefined();
523
+ const text = await blob!.text();
524
+ expect(text).toBe("function render() {}");
525
+ });
526
+ });
527
+
353
528
  describe("maybeGetVirtualFile utility function", () => {
354
529
  it("should handle URLs without leading dots correctly", async () => {
355
530
  const virtualFiles = {
@@ -370,6 +545,25 @@ describe("maybeGetVirtualFile utility function", () => {
370
545
  expect(text2).toBe("test");
371
546
  });
372
547
 
548
+ it("should match URLs with prefix paths before /@file/", async () => {
549
+ const virtualFiles = {
550
+ "/@file/4263-66-yUGhgQXp.js":
551
+ "data:application/javascript;base64,ZnVuY3Rpb24gcmVuZGVyKCkge30=" as DataURLString,
552
+ };
553
+
554
+ const unpatch = patchFetch(virtualFiles);
555
+
556
+ // Test URL with a prefix path before /@file/
557
+ const response = await window.fetch(
558
+ "https://molab.marimo.app/preview/@file/4263-66-yUGhgQXp.js",
559
+ );
560
+ const text = await response.text();
561
+
562
+ expect(text).toBe("function render() {}");
563
+
564
+ unpatch();
565
+ });
566
+
373
567
  it("should handle complex file:// URLs with nested paths", async () => {
374
568
  const virtualFiles = {
375
569
  "/@file/nested/data.json":
@@ -1,6 +1,8 @@
1
1
  /* Copyright 2026 Marimo. All rights reserved. */
2
2
 
3
3
  import type { Loader } from "@/plugins/impl/vega/vega-loader";
4
+ import { deserializeBlob } from "@/utils/blob";
5
+ import type { DataURLString } from "@/utils/json/base64";
4
6
  import { Logger } from "@/utils/Logger";
5
7
  import { getStaticVirtualFiles } from "./static-state";
6
8
  import type { StaticVirtualFiles } from "./types";
@@ -120,6 +122,24 @@ function withoutLeadingDot(path: string): string {
120
122
  return path.startsWith(".") ? path.slice(1) : path;
121
123
  }
122
124
 
125
+ /**
126
+ * Resolve a URL to a blob URL if it's a virtual file, for use with dynamic import().
127
+ * Unlike fetch, import() can't be patched, so we need to convert data URLs to blob URLs.
128
+ *
129
+ * @returns The original URL if not a virtual file, or a blob URL if it is
130
+ */
131
+ export function resolveVirtualFileURL(
132
+ url: string,
133
+ files: StaticVirtualFiles = getStaticVirtualFiles(),
134
+ ): string {
135
+ const vfile = maybeGetVirtualFile(url, files);
136
+ if (!vfile) {
137
+ return url;
138
+ }
139
+ const blob = deserializeBlob(vfile as DataURLString);
140
+ return URL.createObjectURL(blob);
141
+ }
142
+
123
143
  function maybeGetVirtualFile(
124
144
  url: string,
125
145
  files: StaticVirtualFiles,
@@ -130,14 +150,11 @@ function maybeGetVirtualFile(
130
150
  }
131
151
  const pathname = new URL(url, base).pathname;
132
152
 
133
- // If if the URL starts with file://, then using the document.baseURI
134
- // will not work. In this case, should just chop off from /@file/...
135
- if (url.startsWith("file://")) {
136
- const indexOfFile = url.indexOf("/@file/");
137
- if (indexOfFile !== -1) {
138
- url = url.slice(indexOfFile);
139
- }
140
- }
153
+ // Extract the /@file/... suffix from the URL or pathname
154
+ // This handles URLs like https://example.com/prefix/@file/foo.js
155
+ // or file:///path/to/@file/foo.js
156
+ const filePathFromUrl = extractFilePath(url);
157
+ const filePathFromPathname = extractFilePath(pathname);
141
158
 
142
159
  // Few variations to grab the URL.
143
160
  // This can happen if a static file was open at file:// or https://
@@ -145,6 +162,19 @@ function maybeGetVirtualFile(
145
162
  files[url] ||
146
163
  files[withoutLeadingDot(url)] ||
147
164
  files[pathname] ||
148
- files[withoutLeadingDot(pathname)]
165
+ files[withoutLeadingDot(pathname)] ||
166
+ (filePathFromUrl && files[filePathFromUrl]) ||
167
+ (filePathFromPathname && files[filePathFromPathname])
149
168
  );
150
169
  }
170
+
171
+ /**
172
+ * Extract the /@file/... path from a URL string
173
+ */
174
+ function extractFilePath(url: string): string | null {
175
+ const indexOfFile = url.indexOf("/@file/");
176
+ if (indexOfFile !== -1) {
177
+ return url.slice(indexOfFile);
178
+ }
179
+ return null;
180
+ }
@@ -8,6 +8,8 @@ import useEvent from "react-use-event-hook";
8
8
  import { z } from "zod";
9
9
  import { MarimoIncomingMessageEvent } from "@/core/dom/events";
10
10
  import { asRemoteURL } from "@/core/runtime/config";
11
+ import { resolveVirtualFileURL } from "@/core/static/files";
12
+ import { isStaticNotebook } from "@/core/static/static-state";
11
13
  import { useAsyncData } from "@/hooks/useAsyncData";
12
14
  import { useDeepCompareMemoize } from "@/hooks/useDeepCompareMemoize";
13
15
  import {
@@ -93,7 +95,11 @@ const AnyWidgetSlot = (
93
95
  error,
94
96
  refetch,
95
97
  } = useAsyncData(async () => {
96
- const url = asRemoteURL(jsUrl).toString();
98
+ let url = asRemoteURL(jsUrl).toString();
99
+ // In static notebooks, resolve virtual files to blob URLs for import()
100
+ if (isStaticNotebook()) {
101
+ url = resolveVirtualFileURL(url);
102
+ }
97
103
  return await import(/* @vite-ignore */ url);
98
104
  // Re-render on jsHash change (which is a hash of the contents of the file)
99
105
  // instead of a jsUrl change because URLs may change without the contents
@@ -20,7 +20,7 @@ describe("Blob serialization and deserialization", () => {
20
20
 
21
21
  test("deserializeBlob should deserialize a base64 string to a Blob", async () => {
22
22
  const serialized = await serializeBlob(testBlob);
23
- const deserialized = await deserializeBlob(serialized);
23
+ const deserialized = deserializeBlob(serialized);
24
24
  expect(deserialized).toBeDefined();
25
25
  expect(deserialized.size).toBe(testBlob.size);
26
26
  expect(deserialized.type).toBe(testBlob.type);
@@ -28,7 +28,7 @@ describe("Blob serialization and deserialization", () => {
28
28
 
29
29
  test("deserialized Blob should contain the original content", async () => {
30
30
  const serialized = await serializeBlob(testBlob);
31
- const deserialized = await deserializeBlob(serialized);
31
+ const deserialized = deserializeBlob(serialized);
32
32
  const reader = new FileReader();
33
33
  // eslint-disable-next-line unicorn/prefer-blob-reading-methods
34
34
  reader.readAsText(deserialized);
@@ -45,7 +45,7 @@ describe("Blob serialization and deserialization", () => {
45
45
  type: "image/png",
46
46
  });
47
47
  const serialized = await serializeBlob(imageBlob);
48
- const deserialized = await deserializeBlob(serialized);
48
+ const deserialized = deserializeBlob(serialized);
49
49
  expect(deserialized).toBeDefined();
50
50
  expect(deserialized.size).toBe(imageBlob.size);
51
51
  expect(deserialized.type).toBe(imageBlob.type);
package/src/utils/blob.ts CHANGED
@@ -14,32 +14,19 @@ export function serializeBlob<T>(blob: Blob): Promise<DataURLString> {
14
14
  });
15
15
  }
16
16
 
17
- export function deserializeBlob(serializedBlob: DataURLString): Promise<Blob> {
18
- return new Promise((resolve, reject) => {
19
- try {
20
- // Extract the base64 data from the data URL
21
- const [prefix, base64Data] = serializedBlob.split(",", 2);
22
- const mimeType = /^data:(.+);base64$/.exec(prefix)?.[1];
23
- // Decode the base64 string
24
- const binaryString = atob(base64Data);
25
- // Convert the binary string to an array buffer
26
- const len = binaryString.length;
27
- const bytes = new Uint8Array(len);
28
- for (let i = 0; i < len; i++) {
29
- bytes[i] = binaryString.charCodeAt(i);
30
- }
31
- // Create a new Blob from the array buffer
32
- const blob = new Blob([bytes], { type: mimeType });
33
- resolve(blob);
34
- } catch (error) {
35
- reject(ensureError(error));
36
- }
37
- });
38
- }
39
-
40
- function ensureError(error: unknown): Error {
41
- if (error instanceof Error) {
42
- return error;
17
+ export function deserializeBlob(serializedBlob: DataURLString): Blob {
18
+ // Extract the base64 data from the data URL
19
+ const [prefix, base64Data] = serializedBlob.split(",", 2);
20
+ const mimeType = /^data:(.+);base64$/.exec(prefix)?.[1];
21
+ // Decode the base64 string
22
+ const binaryString = atob(base64Data);
23
+ // Convert the binary string to an array buffer
24
+ const len = binaryString.length;
25
+ const bytes = new Uint8Array(len);
26
+ for (let i = 0; i < len; i++) {
27
+ bytes[i] = binaryString.charCodeAt(i);
43
28
  }
44
- return new Error(`${error}`);
29
+ // Create a new Blob from the array buffer
30
+ const blob = new Blob([bytes], { type: mimeType });
31
+ return blob;
45
32
  }