@@ -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
5152export 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 ( / ^ \/ m i c r o b i t - o r g - p r o x y / , "" ) ,
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