Files
privacy.sexy/vite-config-helper.ts
undergroundwires 840adf9429 Bump Electron to latest and use native ESM
This commit bumps Electron and related dependencies to their latest
versions to leverage native ESM support. It adjusts build configuration
to use native ESM support instead of relying on CommonJS bundling.

Key changes:

- Bump Electron to latest v29.
  Electron v28 ships with native ESM/ECMAScript modules support.
  Details on Electron ESM support:
    - electron/electron#21457
    - electron/electron#37535
- Bump `electron-builder` to latest v24.13.
  `electron-builder` is used to package and publish the application.
  It supports ESM since 24.10.
  Details on `electron-builder` ESM support:
    - electron-userland/electron-builder#7936
    - electron-userland/electron-builder#7935
- Bump `electron-log` to latest v5.1.
  `electron-log` supports ESM since version 5.0.4.
  Details on `electron-log` ESM support:
    - megahertz/electron-log#390.
- Change `electron-vite` configuration to bundle as ESM instead of
  CommonJS to leverage Electron's native ESM support.

Other supporting changes:

- Add type hint for electron-builder configuration file.
- Update import statements for `electron-updater` as it still is a
  CommonJS module and does not support ESM.
  Details:
    - electron-userland/electron-builder#7976
- Improve `electron-builder` configuration file to dynamically locate
  main entry files, supporting various JavaScript file extensions
  (`.js`, `.mjs` and `.cjs`) to facilitate easier future changes.
- Change comment about Electron process-specific module alias
  registration. This issue has been fixed in `electron-vite`, but
  subpath module imports for Electron still do not work when building
  tests (`npm run test:unit`).
  Details:
   - alex8088/electron-vite#372
- Add `electron-log` in bundling process instead of externalizing to
  workaround Electron ESM loader issues with subpath imports (inability
  to do `electron-log/main`).
  Details:
    - alex8088/electron-vite#401
    - electron/electron#41241
- Improve desktop runtime error checks' assertion message for better
  clarity.
2024-03-18 11:55:56 +01:00

69 lines
2.7 KiB
TypeScript

import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { VITE_USER_DEFINED_ENVIRONMENT_KEYS } from './src/infrastructure/EnvironmentVariables/Vite/ViteEnvironmentKeys';
import tsconfigJson from './tsconfig.json' assert { type: 'json' };
import packageJson from './package.json' assert { type: 'json' };
type ViteAliasDefinitions = Record<string, string>;
export function getAliases(): ViteAliasDefinitions {
return {
...getPathAliasesFromTsConfig(),
...getElectronProcessSpecificModuleAliases(),
};
}
export function getSelfDirectoryAbsolutePath() {
const filePath = fileURLToPath(import.meta.url);
const directoryPath = dirname(filePath);
return directoryPath;
}
type ViteEnvironmentKeyValues = {
[K in
typeof VITE_USER_DEFINED_ENVIRONMENT_KEYS[keyof typeof VITE_USER_DEFINED_ENVIRONMENT_KEYS]
]: string
};
type ViteGlobalVariableReplacementDefinitions = Record<string, string>;
export function getClientEnvironmentVariables(): ViteGlobalVariableReplacementDefinitions {
const environmentVariables: ViteEnvironmentKeyValues = {
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.NAME]: packageJson.name,
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.VERSION]: packageJson.version,
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.REPOSITORY_URL]: packageJson.repository.url,
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.HOMEPAGE_URL]: packageJson.homepage,
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.SLOGAN]: packageJson.slogan,
};
return Object.entries(environmentVariables).reduce((acc, [key, value]) => {
const formattedEnvVariableKey = `import.meta.env.${key}`;
const jsonEncodedEnvVariableValue = JSON.stringify(value);
return { ...acc, [formattedEnvVariableKey]: jsonEncodedEnvVariableValue };
}, {});
}
function getPathAliasesFromTsConfig(): ViteAliasDefinitions {
const { paths } = tsconfigJson.compilerOptions;
return Object.keys(paths).reduce((aliases, pathName) => {
const pathFolder = paths[pathName][0];
const aliasFolder = pathFolder.substring(0, pathFolder.length - 1); // trim * from end
const aliasName = pathName.substring(0, pathName.length - 2); // trim /* from end
const aliasPath = resolve(getSelfDirectoryAbsolutePath(), aliasFolder);
aliases[aliasName] = aliasPath;
return aliases;
}, {});
}
function getElectronProcessSpecificModuleAliases(): ViteAliasDefinitions {
// Workaround for Vite not being able to build tests with scoped Electron module imports.
const electronProcessScopedModuleAliases = [
'electron/main',
'electron/renderer',
'electron/common',
] as const;
return electronProcessScopedModuleAliases.reduce((aliases, alias) => {
aliases[alias] = 'electron';
return aliases;
}, {});
}