diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-30 21:47:46 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-30 21:47:46 +0100 |
commit | 1383dde6aa0d25b5fa23d7a7b64ba7de21ec5b99 (patch) | |
tree | 94739ff48121a1d69358acf5f2c60124d1b1cdec /packages/main/src | |
parent | build: Only use vite typings in renderer (diff) | |
download | sophie-1383dde6aa0d25b5fa23d7a7b64ba7de21ec5b99.tar.gz sophie-1383dde6aa0d25b5fa23d7a7b64ba7de21ec5b99.tar.zst sophie-1383dde6aa0d25b5fa23d7a7b64ba7de21ec5b99.zip |
build: Fully switch over to ESM
Now we can run with ESM at build and test time and transpile into
commonjs for electron. This greatly simplifies testing, since we treat
everything as ESM during build with esbuild anyways. Now the test
environment and the build scripts match the apparent (but not the actual
for the main, preload, and inject modules) runtime environment.
Caveats:
- We may use top-level async expressions in tests and script, but not in
code that gets transpiled into commonjs or scripts that get imported
by vite. The limitation w.r.t. commonjs seems fundamental.
- Jest only experimentally supports ESM and there are some limitations
with mocking. Most limitations (except the lack of automatic mocks)
can be worked around by async importing code that uses mocks.
- There are packages marked as modules (so that node reads any scripts
in them as ESM) that nevertheless get transpiled into commonjs
modules. However, these should be clearly marked by using a .cjs
extension as their bundle. The worst offender is the root package,
which has a .cjs as its main entry point that gets read by electron,
but is in fact marked as a module. This doesn't seem to bother electron
at all. The service-inject package is an IIFE with a .js extension,
but it outputs a fully self-contained bundle, so the choice of module
format should be irrelevant.
Diffstat (limited to 'packages/main/src')
-rw-r--r-- | packages/main/src/devTools.ts | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/packages/main/src/devTools.ts b/packages/main/src/devTools.ts index 6c25b3e..7c7c86a 100644 --- a/packages/main/src/devTools.ts +++ b/packages/main/src/devTools.ts | |||
@@ -27,14 +27,12 @@ import type { BrowserWindow } from 'electron'; | |||
27 | * because the mobx-state-tree devtools are currently unmaintained. | 27 | * because the mobx-state-tree devtools are currently unmaintained. |
28 | */ | 28 | */ |
29 | export async function installDevToolsExtensions(): Promise<void> { | 29 | export async function installDevToolsExtensions(): Promise<void> { |
30 | const installerPackage = await import('electron-devtools-installer'); | 30 | // Hack to lazily require a CJS module from an ES module transpiled into a CJS module. |
31 | const { | 31 | const { |
32 | default: installExtension, | 32 | default: installExtension, |
33 | REACT_DEVELOPER_TOOLS, | 33 | REACT_DEVELOPER_TOOLS, |
34 | REDUX_DEVTOOLS, | 34 | REDUX_DEVTOOLS, |
35 | } = installerPackage.default instanceof Function | 35 | } = require('electron-devtools-installer'); |
36 | ? installerPackage | ||
37 | : installerPackage.default as unknown as typeof import('electron-devtools-installer'); | ||
38 | await installExtension( | 36 | await installExtension( |
39 | [ | 37 | [ |
40 | REACT_DEVELOPER_TOOLS, | 38 | REACT_DEVELOPER_TOOLS, |