Repository navigation
fix: share one Svelte runtime across built components and fix CodeHighlighter language loading - #129
Merged
Merged
Conversation
…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 detectedLatest commit: b1ad20e The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In the production build (
gradio cc build), apps built with modelscope_studio misbehave whilegradio cc devworks fine:ms.AutoLoadingno longer shows up;antdx.CodeHighlighterlogs[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$stateprops 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 devhands every component the sameimport("svelte"), so it was not affected, and neither were builds against Gradio <= 6.8.CodeHighlighter.
@ant-design/xloads the Prism languages withimport(`react-syntax-highlighter/dist/esm/languages/prism/${lang}`). Bundlers only expand relative template imports, so the bare specifier reaches the browser as is.Changes
frontend/plugins/sharedSvelteRuntime.js):ms.Application, the provider ofwindow.ms_globals, bundles the runtime and registers it throughvirtual:ms-svelte-runtime(imported byinject.ts). Every other component resolves itssvelteimports 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 plainwindow.ms_globals.*reads like React: the compiled components and Gradio'ssvelte_runtime_entry.jsuse the runtime as soon as the module is evaluated, and Gradio does not evaluatems.Applicationfirst. The build fails if a component imports asvelte/*client entry that is not shared, instead of silently bundling a private runtime again.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 keyantdxinstead of the imported specifier@ant-design/x, so@ant-design/xwas bundled into every component.frontend/plugin.jsis split intofrontend/plugins/(index.js,globals.js,externalGlobals.js,sharedSvelteRuntime.js,prismLanguages.js,config.js). The shared globals are now read from thewindow.ms_globals = { ... }object ofinject.tsinstead 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
gradio cc build: onlyms.Applicationstill ships a Svelte runtime (index-client-*.js), the other components import_ms-svelte-facade_*chunks;components/shrinks from 111M to 95M.frontend/plugins/split, and the table derived frominject.tsis identical to the previous hand-written one.queue/join;antdx.CodeHighlighterpage: 27 code blocks, all highlighted;antdx.Bubblerenders with the shared@ant-design/x;