Skip to content

Commit

Permalink
Bump Electron to latest and enable native ESM
Browse files Browse the repository at this point in the history
This commit bumps Electron (`electron`) and dependencies in its
ecosystem (`electron-builder` and `electron-log`) to their latest
versions.

It also adjusts the build configuration to enable native Electron ESM
support rather than relying on the bundler.

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.

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
- In `electron-builder` configuration, dynamically locate index file to
  accommodate different index file extensions outputted from bundling
  process such as `.js`, `.mjs` and `.cjs` to enable easier future
  changes.
- Remove Electron process specific module alias registration as the
  workaround is no longer necessary as it's fixed in mainstream.
  See alex8088/electron-vite$372. TODO: Not done yet
- With native Electron ESM support, Electron ESM loader cannot correctly
  resolve subpath modules. As a workaround, let `electron-vite` bundle
  `electron-log` so import from `electron-log/main` can be used.
  Details on workaround: alex8088/electron-vite$401
  Details on Electron problem: electron/electron$41241
  • Loading branch information
undergroundwires committed Mar 18, 2024
1 parent 5eff3a0 commit fdda8d5
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 164 deletions.
21 changes: 20 additions & 1 deletion electron-builder.cjs
Original file line number Diff line number Diff line change
@@ -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\.(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
Original file line number Diff line number Diff line change
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',

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

0 comments on commit fdda8d5

Please sign in to comment.