File size: 3,061 Bytes
0ad74ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";

// @ts-ignore
import custom_media from "postcss-custom-media";
import global_data from "@csstools/postcss-global-data";
// @ts-ignore
import prefixer from "postcss-prefix-selector";
import { cpSync, readFileSync, writeFileSync } from "fs";
import { resolve, join } from "path";

import {
	inject_ejs,
	generate_cdn_entry,
	generate_dev_entry,
	handle_ce_css,
	inject_component_loader,
	resolve_svelte,
	mock_modules
} from "@self/build";

const version_path = resolve(__dirname, "../../gradio/package.json");
const theme_token_path = resolve(__dirname, "../theme/src/tokens.css");
const version_raw = JSON.parse(
	readFileSync(version_path, { encoding: "utf-8" })
).version.trim();
const version = version_raw.replace(/\./g, "-");

const GRADIO_VERSION = version_raw || "asd_stub_asd";
const CDN_BASE = "https://gradio.s3-us-west-2.amazonaws.com";

export default defineConfig(({ mode }) => {
	const production = mode === "production";
	const development = mode === "development";
	console.log(mode);
	return {
		// plugins: [],
		server: {
			port: 9876,
			open: "/"
		},
		resolve: {
			conditions: ["gradio"]
		},
		build: {
			rollupOptions: {
				external: [
					"/svelte/svelte.js",
					"/svelte/svelte-submodules.js",
					"./svelte/svelte-submodules.js",
					"./svelte/svelte.js"
				]
			},
			minify: false
		},
		define: {
			BUILD_MODE: production ? JSON.stringify("prod") : JSON.stringify("dev"),
			BACKEND_URL: production
				? JSON.stringify("")
				: JSON.stringify("http://127.0.0.1:7860/"),
			GRADIO_VERSION: JSON.stringify(version)
		},
		css: {
			postcss: {
				plugins: [
					prefixer({
						prefix: `.gradio-container-${version}`,
						// @ts-ignore
						transform(prefix, selector, prefixedSelector, fileName) {
							if (selector.indexOf("gradio-container") > -1) {
								return prefix;
							} else if (
								selector.indexOf(":root") > -1 ||
								selector.indexOf("dark") > -1 ||
								selector.indexOf("body") > -1 ||
								fileName.indexOf(".svelte") > -1
							) {
								return selector;
							}
							return prefixedSelector;
						}
					}),
					custom_media()
				]
			}
		},
		ssr: {
			noExternal: ["@gradio/*", "@huggingface/space-header"],
			external: mode === "development" ? [] : ["svelte", "svelte/*"]
		},
		optimizeDeps: {
			exclude: [
				"@gradio/*",
				"svelte",
				"svelte/*",
				"./svelte/svelte-submodules.js",
				"./svelte/svelte.js"
			]
		},
		plugins: [
			sveltekit(),

			inject_component_loader({ mode }),
			{
				name: "resolve_svelte",
				enforce: "pre",
				resolveId(id, importer, options) {
					if (development) {
						return null;
					}

					if (!options?.ssr) {
						if (id === "svelte" || id === "svelte/internal") {
							return { id: "../../../svelte/svelte.js", external: true };
						}
						if (id.startsWith("svelte/")) {
							return {
								id: "../../../svelte/svelte-submodules.js",
								external: true
							};
						}
					}
				}
			}
		]
	};
});