11import { sveltekit } from "@sveltejs/kit/vite" ;
2- import { defineConfig } from "vite" ;
2+ import { defineConfig , type Plugin } from "vite" ;
33
44// @ts -ignore
55import custom_media from "postcss-custom-media" ;
@@ -28,6 +28,15 @@ const version = version_raw.replace(/\./g, "-");
2828const GRADIO_VERSION = version_raw || "asd_stub_asd" ;
2929const CDN_BASE = "https://gradio.s3-us-west-2.amazonaws.com" ;
3030
31+ import { createRequire } from "module" ;
32+
33+ const require = createRequire ( import . meta. url ) ;
34+ const svelte = require ( "svelte/package.json" ) ;
35+ const svelte_exports = Object . keys ( svelte . exports )
36+ . filter ( ( p ) => p . endsWith ( ".json" ) )
37+ . map ( ( entry ) => entry . replace ( / ^ \. / , "svelte" ) . split ( "/" ) . join ( "_" ) + ".js" ) ;
38+ console . log ( "Svelte exports:" , svelte_exports ) ;
39+
3140export default defineConfig ( ( { mode, isSsrBuild } ) => {
3241 const production = mode === "production" ;
3342 const development = mode === "development" ;
@@ -49,16 +58,12 @@ export default defineConfig(({ mode, isSsrBuild }) => {
4958 resolve : {
5059 conditions : [ "gradio" ]
5160 } ,
52- noExternal : [ "@gradio/*" , "@huggingface/space-header" ]
61+ noExternal : [ "@gradio/*" , "@huggingface/space-header" ] ,
62+ external : mode === "development" ? [ ] : [ "svelte" , "svelte/*" ]
5363 } ,
5464 build : {
5565 rollupOptions : {
56- // external: [
57- // "/svelte/svelte.js",
58- // "/svelte/svelte-submodules.js",
59- // "./svelte/svelte-submodules.js",
60- // "./svelte/svelte.js"
61- // ]
66+ external : svelte_exports
6267 } ,
6368 minify : true ,
6469 sourcemap : false
@@ -100,34 +105,122 @@ export default defineConfig(({ mode, isSsrBuild }) => {
100105 // noExternal: ["@gradio/*", "@huggingface/space-header"]
101106 // // external: mode === "development" ? [] : ["svelte", "svelte/*"]
102107 // },
103- // optimizeDeps: {
104- // exclude: ["@gradio/*"]
105- // },
108+ optimizeDeps : {
109+ exclude : [ "@gradio/*" ]
110+ } ,
106111 plugins : [
112+ inject_svelte_init_code ( { mode } ) ,
107113 sveltekit ( ) ,
108114
109- inject_component_loader ( { mode } )
110- // {
111- // name: "resolve_svelte",
112- // enforce: "pre",
113- // resolveId(id, importer, options) {
114- // if (development) {
115- // return null;
116- // }
117-
118- // // if (!options?.ssr) {
119- // // if (id === "svelte" || id === "svelte/internal") {
120- // // return { id: "../../../svelte/svelte.js", external: true };
121- // // }
122- // // if (id.startsWith("svelte/")) {
123- // // return {
124- // // id: "../../../svelte/svelte-submodules.js",
125- // // external: true
126- // // };
127- // // }
128- // // }
129- // }
130- // }
115+ inject_component_loader ( { mode } ) ,
116+ resolve_svelte ( mode === "production" ) ,
117+ handle_svelte_import ( { development : mode === "development" } )
131118 ]
132119 } ;
133120} ) ;
121+
122+ function handle_svelte_import ( {
123+ development
124+ } : {
125+ development : boolean ;
126+ } ) : Plugin {
127+ return {
128+ name : "handle_svelte_import" ,
129+ enforce : "pre" ,
130+ resolveId ( id , importer , options ) {
131+ if ( development ) {
132+ return null ;
133+ }
134+
135+ if ( ! options ?. ssr ) {
136+ if ( id === "svelte" ) {
137+ return {
138+ id : "../../../svelte/svelte_svelte.js" ,
139+ external : true
140+ } ;
141+ }
142+ if ( id . startsWith ( "svelte/" ) ) {
143+ return {
144+ id : `../../../svelte/${ id . split ( "/" ) . join ( "_" ) } .js` ,
145+ external : true
146+ } ;
147+ }
148+ return null ;
149+ }
150+ }
151+ } ;
152+ }
153+
154+ export const _svelte_exports = Object . keys ( svelte . exports )
155+
156+ . filter ( ( entry ) => {
157+ const _entry = Object . keys ( svelte . exports [ entry ] ) . filter (
158+ ( e ) => e !== "types"
159+ ) ;
160+ return (
161+ _entry . length !== 0 &&
162+ ! entry . endsWith ( ".json" ) &&
163+ entry !== "./internal" &&
164+ entry !== "./compiler" &&
165+ entry !== "./internal/disclose-version"
166+ ) ;
167+ } )
168+ . map ( ( entry ) => "svelte" + entry . replace ( / ^ \. / , "" ) ) ;
169+ export const svelte_exports_transformed = Object . keys ( svelte . exports ) . map (
170+ ( entry ) => entry . replace ( / ^ \. / , "svelte" ) . split ( "/" ) . join ( "_" ) + ".js"
171+ ) ;
172+
173+ export function inject_svelte_init_code ( { mode } : { mode : string } ) : Plugin {
174+ const v_id = "virtual:load-svelte" ;
175+ const resolved_v_id = "\0" + v_id ;
176+
177+ return {
178+ name : "inject-component-loader" ,
179+ enforce : "pre" ,
180+ resolveId ( id : string ) {
181+ if ( id === v_id ) return resolved_v_id ;
182+ } ,
183+ load ( id : string ) {
184+ if ( id === resolved_v_id ) {
185+ const s = make_init_code ( ) ;
186+ console . log ( "Svelte init code:" , s ) ;
187+ return s ;
188+ }
189+ }
190+ } ;
191+ }
192+
193+ function make_init_code ( ) : string {
194+ const import_strings = _svelte_exports
195+ . map (
196+ ( entry : string ) =>
197+ `import * as ${ entry
198+ . replace ( / \. j s $ / , "" )
199+ . replace ( / - / g, "_" )
200+ . replace ( / \/ / g, "_" ) } from "${ entry } ";`
201+ )
202+ . join ( "\n" ) ;
203+
204+ const import_mappings = _svelte_exports
205+ . map ( ( entry : string ) => {
206+ const var_name = entry . replace ( / \/ / g, "_" ) ;
207+ return `o.${ var_name } = {};
208+ for (const key in ${ var_name } ) {
209+ //@ts-ignore
210+ o.${ var_name } [key] = ${ var_name } [key];
211+ }` ;
212+ } )
213+ . join ( "\n" ) ;
214+ return `${ import_strings }
215+
216+ const is_browser = typeof window !== "undefined";
217+ if (is_browser) {
218+ const o = {};
219+ ${ import_mappings }
220+
221+ window.__gradio__svelte__ = o;
222+ window.__gradio__svelte__["globals"] = {};
223+ window.globals = window;
224+ }
225+ ` ;
226+ }
0 commit comments