Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code of vite.base.config.ts #22

Open
rtritto opened this issue May 30, 2024 · 0 comments
Open

Improve code of vite.base.config.ts #22

rtritto opened this issue May 30, 2024 · 0 comments

Comments

@rtritto
Copy link

rtritto commented May 30, 2024

vite.base.config.ts

-export const builtins = ['electron', ...builtinModules.map((m) => [m, `node:${m}`]).flat()]
+export const builtins = ['electron', ...builtinModules.flatMap((m) => [m, `node:${m}`])]

...

export function getDefineKeys(names: string[]) {
  const define: { [name: string]: VitePluginRuntimeKeys } = {}

-  return names.reduce((acc, name) => {
+  for (const name of names) {
      const NAME = name.toUpperCase()
-     const keys: VitePluginRuntimeKeys = {
+     define[name] = {
        VITE_DEV_SERVER_URL: `${NAME}_VITE_DEV_SERVER_URL`,
        VITE_NAME: `${NAME}_VITE_NAME`
      }
+  }

-    return { ...acc, [name]: keys }
-  }, define)
+  return define
}

...

export function getBuildDefine(env: ConfigEnv<'build'>) {
  const { command, forgeConfig } = env
  const names = forgeConfig.renderer.filter(({ name }) => name != null).map(({ name }) => name!)
  const defineKeys = getDefineKeys(names)
-  const define = Object.entries(defineKeys).reduce((acc, [name, keys]) => {
-    const { VITE_DEV_SERVER_URL, VITE_NAME } = keys
-    const def = {
-      [VITE_DEV_SERVER_URL]: command === 'serve' ? JSON.stringify(process.env[VITE_DEV_SERVER_URL]) : undefined,
-      [VITE_NAME]: JSON.stringify(name)
-    }
-    return { ...acc, ...def }
-  }, {} as Record<string, any>)
+  const define: Record<string, string | undefined> = {}
+
+  for (const name in defineKeys) {
+    const { VITE_DEV_SERVER_URL, VITE_NAME } = defineKeys[name]
+    define[VITE_DEV_SERVER_URL] = command === 'serve' ? JSON.stringify(process.env[VITE_DEV_SERVER_URL]) : undefined
+    define[VITE_NAME] = JSON.stringify(name)
+  }

  return define
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant