fix cdn build (#6191)

* fix cdn build

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
pngwn 2023-10-31 15:34:20 +00:00 committed by GitHub
parent 141afe1cf9
commit b555bc09ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 31 deletions

View File

@ -0,0 +1,7 @@
---
"@gradio/app": patch
"@gradio/preview": patch
"gradio": patch
---
fix:fix cdn build

View File

@ -32,26 +32,22 @@ export function patch_dynamic_import({
name: "patch-dynamic-import",
enforce: "post",
writeBundle(config, bundle) {
if (mode !== "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 = join(config.dir, chunk.fileName);
writeFileSync(output_location, chunk.code);
}
}
}
// 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 = join(config.dir, chunk.fileName);
// writeFileSync(output_location, chunk.code);
// }
// }
// }
}
};
}

View File

@ -36,11 +36,11 @@ import {
resolve_svelte
} from "./build_plugins";
const GRADIO_VERSION = process.env.GRADIO_VERSION || "asd_stub_asd";
const GRADIO_VERSION = 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}/`;
: `https://gradio.s3-us-west-2.amazonaws.com/${version_raw}/`;
const TEST_MODE = process.env.TEST_MODE || "jsdom";
//@ts-ignore

View File

@ -18,6 +18,7 @@ const vite_client = require.resolve("vite/dist/client/client.mjs");
const hmr = require.resolve("svelte-hmr");
const output_svelte_dir = "../../gradio/templates/frontend/assets/svelte";
const output_svelte_dir_cdn = "../../gradio/templates/cdn/assets/svelte";
const onwarn = (warning, warn) => {
if (warning.plugin === "typescript") return;
@ -152,20 +153,32 @@ export default [
},
{
input: "src/svelte-submodules.ts",
output: {
file: join(output_svelte_dir, "svelte-submodules.js"),
format: "esm"
},
output: [
{
file: join(output_svelte_dir, "svelte-submodules.js"),
format: "esm"
},
{
file: join(output_svelte_dir_cdn, "svelte-submodules.js"),
format: "esm"
}
],
onwarn,
plugins
},
{
input: "src/svelte-internal.ts",
output: {
file: join(output_svelte_dir, "svelte.js"),
format: "esm"
},
output: [
{
file: join(output_svelte_dir, "svelte.js"),
format: "esm"
},
{
file: join(output_svelte_dir_cdn, "svelte.js"),
format: "esm"
}
],
onwarn,
plugins
},

View File

@ -6,7 +6,7 @@
"dev": "pnpm css && pnpm --filter @gradio/client build && pnpm --filter @gradio/preview build && pnpm --filter @gradio/app dev",
"css": "pnpm --filter @gradio/theme generate",
"build": "pnpm css && pnpm --filter @gradio/client build && pnpm --filter @gradio/app build:local --emptyOutDir && pnpm --filter @gradio/preview build",
"build:cdn": "pnpm --filter @gradio/client build && pnpm --filter @gradio/app build:cdn --emptyOutDir",
"build:cdn": "pnpm --filter @gradio/client build && pnpm --filter @gradio/app build:cdn --emptyOutDir && pnpm --filter @gradio/preview build",
"build:website": "pnpm --filter @gradio/app build:website --emptyOutDir",
"build:cdn-local": "TEST_CDN=TRUE pnpm build:cdn",
"preview:cdn-server": "sirv ./gradio/templates/cdn --single --port=4321 --cors",