Skip to content

fix: share one Svelte runtime across built components and fix CodeHighlighter language loading - #129

Merged
Col0ring merged 1 commit into
mainfrom
fix/shared-svelte-runtime
Oct 9, 2026
Merged

Col0ring merged 1 commit into
mainfrom
fix/shared-svelte-runtime

Conversation

@Col0ring

@Col0ring Col0ring commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

Problem

In the production build (gradio cc build), apps built with modelscope_studio misbehave while gradio cc dev works fine:

  • a top nav / sider menu item that was visited once no longer switches the content when clicked again;
  • ms.AutoLoading no longer shows up;
  • antdx.CodeHighlighter logs [CodeHighlighter] Failed to load language: python TypeError: Failed to resolve module specifier 'react-syntax-highlighter/dist/esm/languages/prism/python' and renders code without highlighting.

Root cause

Svelte runtime. Since Gradio 6.9 (the "bridge" generation, see patches/gradio-preview/src/plugins.ts), the app no longer exposes its Svelte runtime, and every custom component bundles a private one. Our components are not independent: the React tree created by the first one watches the $state props of all the others (svelte-preprocess-react/internal/Bridge.svelte.ts), and a Svelte runtime never tracks signals owned by another runtime. Prop updates then stop re-rendering, only mounting a new node (first visit) happens to refresh the tree. gradio cc dev hands every component the same import("svelte"), so it was not affected, and neither were builds against Gradio <= 6.8.

CodeHighlighter. @ant-design/x loads the Prism languages with import(`react-syntax-highlighter/dist/esm/languages/prism/${lang}`). Bundlers only expand relative template imports, so the bare specifier reaches the browser as is.

Changes

  • Shared Svelte runtime (frontend/plugins/sharedSvelteRuntime.js): ms.Application, the provider of window.ms_globals, bundles the runtime and registers it through virtual:ms-svelte-runtime (imported by inject.ts). Every other component resolves its svelte imports to facades that wait for it with top-level await and re-export its bindings, so all components run on one runtime again, as in dev. The imports cannot be turned into plain window.ms_globals.* reads like React: the compiled components and Gradio's svelte_runtime_entry.js use the runtime as soon as the module is evaluated, and Gradio does not evaluate ms.Application first. The build fails if a component imports a svelte/* client entry that is not shared, instead of silently bundling a private runtime again.
  • CodeHighlighter (frontend/plugins/prismLanguages.js): the dynamic language import is mapped statically at build time. The same module already lazy-loads the full Prism build (refractor/all, holding every language), so it costs no extra size.
  • @ant-design/x: the shared globals table used the key antdx instead of the imported specifier @ant-design/x, so @ant-design/x was bundled into every component.
  • Plugin structure: frontend/plugin.js is split into frontend/plugins/ (index.js, globals.js, externalGlobals.js, sharedSvelteRuntime.js, prismLanguages.js, config.js). The shared globals are now read from the window.ms_globals = { ... } object of inject.ts instead of a hand-written table, so a shared library is registered in one place. The build fails on a relative import or a duplicated specifier there, and named imports are only shared with an explicit // @external <specifier> annotation.

Verification

  • Full gradio cc build: only ms.Application still ships a Svelte runtime (index-client-*.js), the other components import _ms-svelte-facade_* chunks; components/ shrinks from 111M to 95M.
  • The build output of the antd / base components is byte-identical before and after the frontend/plugins/ split, and the table derived from inject.ts is identical to the previous hand-written one.
  • Docs site served from the build:
    • top nav Base → Antd → Base → Antd and sider menu Button → Typography → Button → Typography switch the content on every click, each click sends a queue/join;
    • antdx.CodeHighlighter page: 27 code blocks, all highlighted;
    • antdx.Bubble renders with the shared @ant-design/x;
    • no console warnings or errors.

…hlighter language loading

- share the Svelte runtime of ms.Application with every other component in
  the bridge generation build (Gradio >= 6.9), so cross-component `$state`
  props are tracked again (revisited tabs / menus switch back, AutoLoading
  shows up)
- statically map the Prism languages that antdx.CodeHighlighter imports
  through a bare template `import()`
- share `@ant-design/x` through `window.ms_globals` (wrong `antdx` key)
- split `frontend/plugin.js` into `frontend/plugins/`, and read the shared
  globals from `inject.ts` instead of a hand-written table
@changeset-bot

changeset-bot Bot commented Oct 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b1ad20e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@modelscope-studio/frontend Patch
modelscope_studio Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Col0ring
Col0ring merged commit 63b7692 into main Oct 9, 2026
2 checks passed
@Col0ring
Col0ring deleted the fix/shared-svelte-runtime branch October 9, 2026 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant