gradio/js/app/vite.config.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

172 lines
5.0 KiB
TypeScript
Raw Normal View History

import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import sveltePreprocess from "svelte-preprocess";
// @ts-ignore
import custom_media from "postcss-custom-media";
import global_data from "@csstools/postcss-global-data";
2023-01-28 17:47:14 +08:00
// @ts-ignore
import prefixer from "postcss-prefix-selector";
import { readFileSync } from "fs";
2023-06-27 15:09:50 +08:00
import { resolve } from "path";
2023-01-28 17:47:14 +08:00
2023-06-27 15:09:50 +08:00
const version_path = resolve(__dirname, "../../gradio/version.txt");
const theme_token_path = resolve(__dirname, "../theme/src/tokens.css");
const version_raw = readFileSync(version_path, { encoding: "utf-8" }).trim();
const version = version_raw.replace(/\./g, "-");
2023-06-27 15:09:50 +08:00
const client_version_path = resolve(
__dirname,
"../../client/python/gradio_client/version.txt"
);
const client_version_raw = readFileSync(client_version_path, {
encoding: "utf-8"
}).trim();
const client_version = client_version_raw.replace(/\./g, "-");
import {
inject_ejs,
patch_dynamic_import,
generate_cdn_entry,
handle_ce_css
} from "./build_plugins";
const GRADIO_VERSION = process.env.GRADIO_VERSION || "asd_stub_asd";
const TEST_CDN = !!process.env.TEST_CDN;
const CDN = TEST_CDN
? "http://localhost:4321/"
: `https://gradio.s3-us-west-2.amazonaws.com/${GRADIO_VERSION}/`;
2023-06-23 20:22:12 +08:00
const TEST_MODE = process.env.TEST_MODE || "jsdom";
//@ts-ignore
export default defineConfig(({ mode }) => {
const CDN_URL = mode === "production:cdn" ? CDN : "/";
Add embedded demos to website (#1270) * adding gallery * added netlify files * new navbar design * header section new design * used by section new design * cards section new design * integrates with section new design * customer stories section new design * footer and gradient * demos section new design * docs fixes * docs reorg * docs reorg * upgrading to tailwind 3 * tailwind config changes * navbar new design * fixing body on all pages * Updating Guides (#1012) * updating getting started * updated codecov version * tweaks to gs * added netlify file * added netlify file * website prebuild script * increased code size * blocks * edits * blocks_hello * hello world * guide * merge main * added flipper demo * guide * guide * add guides * tweak to refresh website * header section new design * demos section new design * cards design * used by section * tweets section * footer on all pages * mobile responsive fixes * mobile responsive fixes * https fonts * completed blocks guide * unify components * minor tweaks * docs headers styling and navigation pane * parameter code blocks * styling description and input type * parameter tables and other styling * only documenting interactive components when possible * guides * embedding not working * demos not working * fixing demo code * fixing demos * demo fix * updated demos * updated demos * ui update * updated docstrings * updated code snippets so they run * updating docs * Interface docs * updating interface * fixing parameters in interface.py * required and defaults for interface, and styling * fixing up interface (#1207) * fixing up interface * fixed interface methods * formatting * updating interface docs * updating interface docs * formatting * docstring to load from docstrings * fixed colors * finalize interface content * interface examples * fixed examples * added some blocks docs * blocks * component fixes * reorganized some files (#1210) * formatting * added info for every component * fixes * blocks docs * added blocks demos * adding combined interfaces * added parallel, series * Doc: layout update (#1216) * doc layout * home spacing Co-authored-by: Abubakar Abid <abubakar@huggingface.co> * adding layouts * layouts done * added events for components * formatting and https * brings back dropdown and other components * fix header ids * moved ids and fixed nav * added parameters for remaining component * docstring fixes * landing page demos * demo window placeholder * demo nav * fixed test * formatting * demo code * correctly importing gradio css/js * remove keyvalues * modify launch script to move gradio assetS * components embedded test * correct demo name * hide try demo and embedding * local devserver changes * create embedding json with configs * changes * fixes * comment out layout docs * demo work * demo fixes * demo embedding fixes * typo * jinja fix * demo nav fix * hide demo button * formatting * removed whitespace * remove newline from parameter * styling demo window * copy work * hide demo buttons * remove index * remove layouts from docs * remove merge artifact * merge dockerfile * chagnes * changes * changes * changes * styling demo window * revert most things * whoops * chagnes * changes * changes * changes * changes * changes * changes * format * changes * fixed Dataset docstring * fix component signature and add required * adding string shortcuts * fix small issues parameter docstringss * string shortcuts * fixed gallery and label values * added for remaining parameters * formatting * extended embedded components * typo * remove capitalization from embeddings * try examples button * added demos for blocks and combining interfaces * demos styling * homepage update (#1275) * spacing * remove console.log * demo window fixes * new spaces * shorten codeblock width * fix plot * better string shortcuts * add embedding config for json * remove console log Co-authored-by: Abubakar Abid <abubakar@huggingface.co> Co-authored-by: aliabd <ali.si3luwa@gmail.com> Co-authored-by: Victor Muštar <victor.mustar@gmail.com>
2022-05-16 23:45:38 +08:00
const production =
mode === "production:cdn" ||
mode === "production:local" ||
2023-06-27 15:09:50 +08:00
mode === "production:website" ||
mode === "production:lite";
Add embedded demos to website (#1270) * adding gallery * added netlify files * new navbar design * header section new design * used by section new design * cards section new design * integrates with section new design * customer stories section new design * footer and gradient * demos section new design * docs fixes * docs reorg * docs reorg * upgrading to tailwind 3 * tailwind config changes * navbar new design * fixing body on all pages * Updating Guides (#1012) * updating getting started * updated codecov version * tweaks to gs * added netlify file * added netlify file * website prebuild script * increased code size * blocks * edits * blocks_hello * hello world * guide * merge main * added flipper demo * guide * guide * add guides * tweak to refresh website * header section new design * demos section new design * cards design * used by section * tweets section * footer on all pages * mobile responsive fixes * mobile responsive fixes * https fonts * completed blocks guide * unify components * minor tweaks * docs headers styling and navigation pane * parameter code blocks * styling description and input type * parameter tables and other styling * only documenting interactive components when possible * guides * embedding not working * demos not working * fixing demo code * fixing demos * demo fix * updated demos * updated demos * ui update * updated docstrings * updated code snippets so they run * updating docs * Interface docs * updating interface * fixing parameters in interface.py * required and defaults for interface, and styling * fixing up interface (#1207) * fixing up interface * fixed interface methods * formatting * updating interface docs * updating interface docs * formatting * docstring to load from docstrings * fixed colors * finalize interface content * interface examples * fixed examples * added some blocks docs * blocks * component fixes * reorganized some files (#1210) * formatting * added info for every component * fixes * blocks docs * added blocks demos * adding combined interfaces * added parallel, series * Doc: layout update (#1216) * doc layout * home spacing Co-authored-by: Abubakar Abid <abubakar@huggingface.co> * adding layouts * layouts done * added events for components * formatting and https * brings back dropdown and other components * fix header ids * moved ids and fixed nav * added parameters for remaining component * docstring fixes * landing page demos * demo window placeholder * demo nav * fixed test * formatting * demo code * correctly importing gradio css/js * remove keyvalues * modify launch script to move gradio assetS * components embedded test * correct demo name * hide try demo and embedding * local devserver changes * create embedding json with configs * changes * fixes * comment out layout docs * demo work * demo fixes * demo embedding fixes * typo * jinja fix * demo nav fix * hide demo button * formatting * removed whitespace * remove newline from parameter * styling demo window * copy work * hide demo buttons * remove index * remove layouts from docs * remove merge artifact * merge dockerfile * chagnes * changes * changes * changes * styling demo window * revert most things * whoops * chagnes * changes * changes * changes * changes * changes * changes * format * changes * fixed Dataset docstring * fix component signature and add required * adding string shortcuts * fix small issues parameter docstringss * string shortcuts * fixed gallery and label values * added for remaining parameters * formatting * extended embedded components * typo * remove capitalization from embeddings * try examples button * added demos for blocks and combining interfaces * demos styling * homepage update (#1275) * spacing * remove console.log * demo window fixes * new spaces * shorten codeblock width * fix plot * better string shortcuts * add embedding config for json * remove console log Co-authored-by: Abubakar Abid <abubakar@huggingface.co> Co-authored-by: aliabd <ali.si3luwa@gmail.com> Co-authored-by: Victor Muštar <victor.mustar@gmail.com>
2022-05-16 23:45:38 +08:00
const is_cdn = mode === "production:cdn" || mode === "production:website";
2023-06-27 15:09:50 +08:00
const is_lite = mode.endsWith(":lite");
return {
base: is_cdn ? CDN_URL : "./",
server: {
2023-06-27 15:09:50 +08:00
port: 9876,
open: is_lite ? "/lite.html" : "/"
},
build: {
sourcemap: true,
target: "esnext",
minify: production,
2023-06-27 15:09:50 +08:00
outDir: is_lite
? resolve(__dirname, "../lite/dist")
: `../../gradio/templates/${is_cdn ? "cdn" : "frontend"}`,
// To build Gradio-lite as a library, we can't use the library mode
// like `lib: is_lite && {}`
// because it inevitably enables inlining of all the static file assets,
// while we need to disable inlining for the wheel files to pass their URLs to `micropip.install()`.
// So we build it as an app and only use the bundled JS and CSS files as library assets, ignoring the HTML file.
// See also `lite.ts` about it.
rollupOptions: is_lite && {
input: "./lite.html",
output: {
// To use it as a library, we don't add the hash to the file name.
entryFileNames: "lite.js",
assetFileNames: (file) => {
if (file.name?.endsWith(".whl")) {
// Python wheel files must follow the naming rules to be installed, so adding a hash to the name is not allowed.
return `assets/[name].[ext]`;
}
if (file.name === "lite.css") {
// To use it as a library, we don't add the hash to the file name.
return `[name].[ext]`;
} else {
return `assets/[name]-[hash].[ext]`;
}
}
}
}
},
define: {
BUILD_MODE: production ? JSON.stringify("prod") : JSON.stringify("dev"),
BACKEND_URL: production
? JSON.stringify("")
2023-01-28 17:47:14 +08:00
: JSON.stringify("http://localhost:7860/"),
GRADIO_VERSION: JSON.stringify(version)
},
css: {
postcss: {
plugins: [
2023-01-28 17:47:14 +08:00
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 ||
fileName.indexOf(".svelte") > -1
) {
return selector;
}
return prefixedSelector;
2023-01-28 17:47:14 +08:00
}
}),
custom_media()
]
}
},
plugins: [
svelte({
inspector: true,
compilerOptions: {
dev: !production
},
hot: !process.env.VITEST && !production,
preprocess: sveltePreprocess({
2023-01-28 17:47:14 +08:00
postcss: {
plugins: [
global_data({ files: [theme_token_path] }),
custom_media()
]
2023-01-28 17:47:14 +08:00
}
})
}),
inject_ejs(),
patch_dynamic_import({
mode: is_cdn ? "cdn" : "local",
gradio_version: GRADIO_VERSION,
cdn_url: CDN_URL
}),
generate_cdn_entry({ enable: is_cdn, cdn_url: CDN_URL }),
handle_ce_css()
],
test: {
setupFiles: [resolve(__dirname, "../../.config/setup_vite_tests.ts")],
2023-05-12 23:22:25 +08:00
environment: TEST_MODE,
include:
TEST_MODE === "node"
? ["**/*.node-test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"]
: ["**/*.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
globals: true
2023-06-27 15:09:50 +08:00
},
resolve: {
alias: {
// For the Wasm app to import the wheel file URLs.
"gradio.whl": resolve(
__dirname,
`../../dist/gradio-${version_raw}-py3-none-any.whl`
),
"gradio_client.whl": resolve(
__dirname,
`../../client/python/dist/gradio_client-${client_version_raw}-py3-none-any.whl`
)
}
},
assetsInclude: ["**/*.whl"] // To pass URLs of built wheel files to the Wasm worker.
};
});