2022-02-02 02:25:20 +08:00
|
|
|
import { defineConfig } from "vite";
|
|
|
|
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
|
|
import sveltePreprocess from "svelte-preprocess";
|
2022-05-17 01:22:09 +08:00
|
|
|
|
2022-05-16 03:43:36 +08:00
|
|
|
import path from "path";
|
|
|
|
import fs from "fs";
|
2022-02-02 02:25:20 +08:00
|
|
|
|
|
|
|
// this is dupe config, gonna try fix this
|
|
|
|
import tailwind from "tailwindcss";
|
2022-03-23 23:19:12 +08:00
|
|
|
import nested from "tailwindcss/nesting/index.js";
|
2022-02-02 02:25:20 +08:00
|
|
|
|
2022-05-16 03:43:36 +08:00
|
|
|
const GRADIO_VERSION = process.env.GRADIO_VERSION;
|
|
|
|
|
2022-02-18 05:21:18 +08:00
|
|
|
//@ts-ignore
|
2022-02-02 02:25:20 +08:00
|
|
|
export default defineConfig(({ mode }) => {
|
2022-05-16 23:45:38 +08:00
|
|
|
const CDN_URL =
|
|
|
|
mode === "production:cdn"
|
|
|
|
? `https://gradio.s3-us-west-2.amazonaws.com/${GRADIO_VERSION}/`
|
|
|
|
: "/";
|
|
|
|
const production =
|
|
|
|
mode === "production:cdn" ||
|
|
|
|
mode === "production:local" ||
|
|
|
|
mode === "production:website";
|
|
|
|
const is_cdn = mode === "production:cdn" || mode === "production:website";
|
2022-02-02 02:25:20 +08:00
|
|
|
|
|
|
|
return {
|
2022-05-16 03:43:36 +08:00
|
|
|
base: is_cdn ? CDN_URL : "./",
|
2022-05-17 01:22:09 +08:00
|
|
|
|
2022-02-03 18:35:14 +08:00
|
|
|
build: {
|
2022-05-16 03:43:36 +08:00
|
|
|
target: "esnext",
|
2022-05-17 01:22:09 +08:00
|
|
|
minify: production,
|
2022-05-16 03:43:36 +08:00
|
|
|
outDir: `../../../gradio/templates/${is_cdn ? "cdn" : "frontend"}`
|
2022-02-03 18:35:14 +08:00
|
|
|
},
|
2022-02-02 02:25:20 +08:00
|
|
|
define: {
|
|
|
|
BUILD_MODE: production ? JSON.stringify("prod") : JSON.stringify("dev"),
|
2022-03-31 23:48:54 +08:00
|
|
|
BACKEND_URL: production
|
|
|
|
? JSON.stringify("")
|
|
|
|
: JSON.stringify("http://localhost:7860/")
|
2022-02-02 02:25:20 +08:00
|
|
|
},
|
2022-02-18 05:21:18 +08:00
|
|
|
css: {
|
|
|
|
postcss: {
|
|
|
|
plugins: [nested, tailwind]
|
|
|
|
}
|
|
|
|
},
|
2022-02-02 02:25:20 +08:00
|
|
|
plugins: [
|
|
|
|
svelte({
|
2022-05-13 04:27:21 +08:00
|
|
|
experimental: {
|
|
|
|
inspector: true
|
|
|
|
},
|
2022-05-17 01:22:09 +08:00
|
|
|
compilerOptions: {
|
|
|
|
dev: !production
|
|
|
|
},
|
2022-03-23 23:19:12 +08:00
|
|
|
hot: !process.env.VITEST,
|
2022-02-02 02:25:20 +08:00
|
|
|
preprocess: sveltePreprocess({
|
2022-03-12 00:00:48 +08:00
|
|
|
postcss: { plugins: [tailwind, nested] }
|
2022-02-02 02:25:20 +08:00
|
|
|
})
|
2022-05-04 05:44:17 +08:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "inject-ejs",
|
2022-05-16 03:43:36 +08:00
|
|
|
enforce: "post",
|
2022-05-04 05:44:17 +08:00
|
|
|
transformIndexHtml: (html) => {
|
|
|
|
return html.replace(
|
|
|
|
/%gradio_config%/,
|
|
|
|
`<script>window.gradio_config = {{ config | tojson }};</script>`
|
|
|
|
);
|
2022-05-16 03:43:36 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
writeBundle(config, bundle) {
|
|
|
|
if (!is_cdn) return;
|
|
|
|
|
|
|
|
const import_re = /import\(((?:'|")[\.\/a-zA-Z0-9]*(?:'|"))\)/g;
|
|
|
|
const import_meta = `${"import"}.${"meta"}.${"url"}`;
|
|
|
|
|
|
|
|
for (const file in bundle) {
|
|
|
|
const chunk = bundle[file];
|
|
|
|
if (chunk.type === "chunk") {
|
|
|
|
if (chunk.code.indexOf("import(") > -1) {
|
|
|
|
const fix_fn = `const VERSION_RE = new RegExp("${GRADIO_VERSION}\/", "g");function import_fix(mod, base) {const url = new URL(mod, base); return import(\`${CDN_URL}\${url.pathname?.startsWith('/') ? url.pathname.substring(1).replace(VERSION_RE, "") : url.pathname.replace(VERSION_RE, "")}\`);}`;
|
|
|
|
chunk.code =
|
|
|
|
fix_fn +
|
|
|
|
chunk.code.replace(
|
|
|
|
import_re,
|
|
|
|
`import_fix($1, ${import_meta})`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!config.dir) break;
|
|
|
|
const output_location = path.join(config.dir, chunk.fileName);
|
|
|
|
fs.writeFileSync(output_location, chunk.code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 05:44:17 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-23 23:19:12 +08:00
|
|
|
],
|
|
|
|
test: {
|
|
|
|
environment: "happy-dom",
|
|
|
|
include: ["**/*.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"]
|
|
|
|
}
|
2022-02-02 02:25:20 +08:00
|
|
|
};
|
|
|
|
});
|