Skip to content

Commit 19a3288

Browse files
Support better local testing of the import flow (#620)
Use a dev proxy to avoid CORS issues.
1 parent b2803d8 commit 19a3288

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/deployment/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ import { default as df } from "theme-package";
1515
import { BoxProps } from "@chakra-ui/react";
1616
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1717
const deploymentFactory: DeploymentConfigFactory = df;
18-
export const deployment = deploymentFactory(import.meta.env);
18+
export const deployment = (() => {
19+
const deployment = deploymentFactory(import.meta.env);
20+
if (import.meta.env.DEV) {
21+
return {
22+
...deployment,
23+
// Sidestep CORS issues in development. See vite.config.ts.
24+
activitiesBaseUrl: "/microbit-org-proxy/classroom/activities/",
25+
};
26+
}
27+
return deployment;
28+
})();
1929

2030
export interface CookieConsent {
2131
analytics: boolean;

vite.config.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
IndexHtmlTransformContext,
1414
IndexHtmlTransformResult,
1515
Plugin,
16+
ServerOptions,
1617
UserConfig,
1718
defineConfig,
1819
loadEnv,
@@ -49,8 +50,6 @@ const viteEjsPlugin = (data: ejs.Data): Plugin => {
4950
};
5051

5152
export default defineConfig(async ({ mode }): Promise<UserConfig> => {
52-
const commonEnv = loadEnv(mode, process.cwd(), "");
53-
5453
const strings: TemplateStrings = themePackageExternal
5554
? // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
5655
((await import(themePackageAlias)).default({}) as TemplateStrings)
@@ -74,17 +73,7 @@ export default defineConfig(async ({ mode }): Promise<UserConfig> => {
7473
input: "index.html",
7574
},
7675
},
77-
server: commonEnv.API_PROXY
78-
? {
79-
port: 5173,
80-
proxy: {
81-
"/api/v1": {
82-
target: commonEnv.API_PROXY,
83-
changeOrigin: true,
84-
},
85-
},
86-
}
87-
: undefined,
76+
server: createServer(mode),
8877
test: {
8978
globals: true,
9079
environment: "jsdom",
@@ -97,3 +86,25 @@ export default defineConfig(async ({ mode }): Promise<UserConfig> => {
9786
},
9887
};
9988
});
89+
90+
const createServer = (mode: string): ServerOptions => {
91+
const commonEnv = loadEnv(mode, process.cwd(), "");
92+
const options = {
93+
port: 5173,
94+
proxy: {
95+
"/microbit-org-proxy/": {
96+
target: "https://microbit.org/",
97+
changeOrigin: true,
98+
rewrite: (path: string) => path.replace(/^\/microbit-org-proxy/, ""),
99+
},
100+
},
101+
};
102+
103+
if (commonEnv.API_PROXY) {
104+
options.proxy["/api/v1"] = {
105+
target: commonEnv.API_PROXY,
106+
changeOrigin: true,
107+
};
108+
}
109+
return options;
110+
};

0 commit comments

Comments
 (0)