Skip to content

Commit

Permalink
Bump Electron to latest and use native ESM
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
undergroundwires committed Mar 18, 2024
1 parent 5eff3a0 commit 840adf9
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 166 deletions.
21 changes: 20 additions & 1 deletion electron-builder.cjs
@@ -1,8 +1,13 @@
/* eslint-disable no-template-curly-in-string */

const { join } = require('node:path');
const { readdirSync } = require('fs');
const { electronBundled, electronUnbundled } = require('./dist-dirs.json');

/**
* @type {import('electron-builder').Configuration}
* @see https://www.electron.build/configuration/configuration
*/
module.exports = {
// Common options
publish: {
Expand All @@ -14,7 +19,9 @@ module.exports = {
output: electronBundled,
},
extraMetadata: {
main: join(electronUnbundled, 'main/index.cjs'), // do not `path.resolve`, it expects a relative path
main: findMainEntryFile(
join(electronUnbundled, 'main'), // do not `path.resolve`, it expects a relative path
),
},

// Windows
Expand All @@ -41,3 +48,15 @@ module.exports = {
artifactName: '${name}-${version}.${ext}',
},
};

/**
* Finds by accommodating different JS file extensions and module formats.
*/
function findMainEntryFile(parentDirectory) {
const files = readdirSync(parentDirectory);
const entryFile = files.find((file) => /^index\.(cjs|mjs|js)$/.test(file));
if (!entryFile) {
throw new Error(`Main entry file not found in ${parentDirectory}.`);
}
return join(parentDirectory, entryFile);
}
20 changes: 15 additions & 5 deletions electron.vite.config.ts
Expand Up @@ -14,7 +14,7 @@ const ELECTRON_DIST_SUBDIRECTORIES = {
renderer: resolveElectronDistSubdirectory('renderer'),
};

process.env.ELECTRON_ENTRY = resolve(ELECTRON_DIST_SUBDIRECTORIES.main, 'index.cjs');
process.env.ELECTRON_ENTRY = resolve(ELECTRON_DIST_SUBDIRECTORIES.main, 'index.mjs');

export default defineConfig({
main: getSharedElectronConfig({
Expand Down Expand Up @@ -54,13 +54,23 @@ function getSharedElectronConfig(options: {
},
rollupOptions: {
output: {
// Mark: electron-esm-support
// This is needed so `type="module"` works
entryFileNames: '[name].cjs',
format: 'es',

// Ensure all generated files use '.mjs' for module consistency.
// Otherwise, preloader process get `.mjs` extension but main process get `.js` extension, see https://github.com/alex8088/electron-vite/issues/397.
entryFileNames: '[name].mjs',
},
},
},
plugins: [externalizeDepsPlugin()],
plugins: [externalizeDepsPlugin({
exclude: [
// Keep 'electron-log' in bundling process.
// This is a workaround for inability of Electron's ESM loader to resolve subpath imports.
// Do not externalize `electron-log` so subpath imports such as `electron-log/main` works.
// See https://github.com/electron/electron/issues/41241, https://github.com/alex8088/electron-vite/issues/401
'electron-log',
],
})],
define: {
...getClientEnvironmentVariables(),
},
Expand Down

0 comments on commit 840adf9

Please sign in to comment.