Skip to content

Commit

Permalink
Cover cases where twind and partytown is used through a proxy API
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Sep 29, 2023
1 parent 82814ca commit 761bb23
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions scripts/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,12 @@ const createSiteTs = async (): Promise<Patch> => {
): App<Manifest, Props, [
StdApp,
]> {
const stdApp = std(state);
return {
state,
state: { ...state, ...stdApp.state },
manifest,
dependencies: [
std(state),
stdApp,
],
};
}
Expand Down Expand Up @@ -431,18 +432,9 @@ const addAppsImportMap = async (): Promise<Patch> => {
},
};
};
const changeMainTs = async (): Promise<Patch> => {
const mainTs = join(Deno.cwd(), "main.ts");
const currentContent = await Deno.readTextFile(mainTs);

return {
from: {
path: mainTs,
content: currentContent,
},
to: {
path: mainTs,
content: await format(`
const withPlugins = async (plugins: string[], imports: string[]) =>
await format(`
/// <reference no-default-lib="true"/>
/// <reference lib="dom" />
/// <reference lib="deno.ns" />
Expand All @@ -453,6 +445,7 @@ import plugins from "deco-sites/std/plugins/mod.ts";
import partytownPlugin from "partytown/mod.ts";
import manifest from "./fresh.gen.ts";
import decoManifest from "./manifest.gen.ts";
${imports.join("\n")}
await start(manifest, {
plugins: [
Expand All @@ -461,10 +454,41 @@ await start(manifest, {
manifest: decoManifest,
},
),
partytownPlugin(),
${plugins.join(",")}
],
});
`),
`);
const changeMainTs = async (): Promise<Patch> => {
const mainTs = join(Deno.cwd(), "main.ts");
const currentContent = await Deno.readTextFile(mainTs);
const plugins: string[] = [];
const imports: string[] = [];
if (currentContent.includes("twindPlugin")) {
imports.push(`import twindPlugin from "$fresh/plugins/twind.ts";`);
imports.push(`import twindConfig from "./twind.config.ts";`);
plugins.push(`twindPlugin({
...twindConfig,
selfURL: new URL("./twind.config.ts", import.meta.url).href,
})`);
}

if (currentContent.includes("proxyUrl:")) {
plugins.push(`partytownPlugin({
proxyUrl: "/proxy",
mainWindowAccessors: ["navigator", "scheduler"],
})`);
} else {
plugins.push(`partytownPlugin()`);
}

return {
from: {
path: mainTs,
content: currentContent,
},
to: {
path: mainTs,
content: await withPlugins(plugins, imports),
},
};
};
Expand Down

0 comments on commit 761bb23

Please sign in to comment.