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/preload | |
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/preload')
-rw-r--r-- | packages/preload/esbuild.config.js | 10 | ||||
-rw-r--r-- | packages/preload/jest.config.js | 6 | ||||
-rw-r--r-- | packages/preload/package.json | 4 | ||||
-rw-r--r-- | packages/preload/src/__mocks__/electron.ts | 25 | ||||
-rw-r--r-- | packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts | 14 | ||||
-rw-r--r-- | packages/preload/tsconfig.json | 3 |
6 files changed, 19 insertions, 43 deletions
diff --git a/packages/preload/esbuild.config.js b/packages/preload/esbuild.config.js index 76ac33f..de51fc5 100644 --- a/packages/preload/esbuild.config.js +++ b/packages/preload/esbuild.config.js | |||
@@ -1,10 +1,8 @@ | |||
1 | // @ts-check | 1 | import { chrome, fileURLToDirname } from '../../config/build-common.js'; |
2 | import { getConfig } from '../../config/esbuild-config.js'; | ||
2 | 3 | ||
3 | const { chrome } = require('../../config/build-common'); | 4 | export default getConfig({ |
4 | const { getConfig } = require('../../config/esbuild-config'); | 5 | absWorkingDir: fileURLToDirname(import.meta.url), |
5 | |||
6 | module.exports = getConfig({ | ||
7 | absWorkingDir: __dirname, | ||
8 | entryPoints: [ | 6 | entryPoints: [ |
9 | 'src/index.ts', | 7 | 'src/index.ts', |
10 | ], | 8 | ], |
diff --git a/packages/preload/jest.config.js b/packages/preload/jest.config.js index 5334b14..faecf9a 100644 --- a/packages/preload/jest.config.js +++ b/packages/preload/jest.config.js | |||
@@ -1,9 +1,7 @@ | |||
1 | // @ts-check | 1 | import rootConfig from '../../jest.config.js'; |
2 | |||
3 | const rootConfig = require('../../jest.config.js'); | ||
4 | 2 | ||
5 | /** @type {import('ts-jest').InitialOptionsTsJest} */ | 3 | /** @type {import('ts-jest').InitialOptionsTsJest} */ |
6 | module.exports = { | 4 | export default { |
7 | ...rootConfig, | 5 | ...rootConfig, |
8 | testEnvironment: 'jsdom', | 6 | testEnvironment: 'jsdom', |
9 | }; | 7 | }; |
diff --git a/packages/preload/package.json b/packages/preload/package.json index ac21618..e6dd0ee 100644 --- a/packages/preload/package.json +++ b/packages/preload/package.json | |||
@@ -3,10 +3,10 @@ | |||
3 | "version": "0.1.0", | 3 | "version": "0.1.0", |
4 | "private": true, | 4 | "private": true, |
5 | "sideEffects": false, | 5 | "sideEffects": false, |
6 | "main": "dist/index.cjs", | 6 | "type": "module", |
7 | "types": "dist-types/index.d.ts", | 7 | "types": "dist-types/index.d.ts", |
8 | "scripts": { | 8 | "scripts": { |
9 | "test": "jest", | 9 | "test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js", |
10 | "typecheck": "tsc" | 10 | "typecheck": "tsc" |
11 | }, | 11 | }, |
12 | "dependencies": { | 12 | "dependencies": { |
diff --git a/packages/preload/src/__mocks__/electron.ts b/packages/preload/src/__mocks__/electron.ts deleted file mode 100644 index 52cddf9..0000000 --- a/packages/preload/src/__mocks__/electron.ts +++ /dev/null | |||
@@ -1,25 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | export const ipcRenderer = { | ||
22 | invoke: jest.fn(), | ||
23 | on: jest.fn(), | ||
24 | send: jest.fn(), | ||
25 | }; | ||
diff --git a/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts b/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts index 71ac2a1..e40dcf5 100644 --- a/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts +++ b/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts | |||
@@ -18,8 +18,8 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { describe, it, jest } from '@jest/globals'; | ||
21 | import { mocked } from 'jest-mock'; | 22 | import { mocked } from 'jest-mock'; |
22 | import { ipcRenderer } from 'electron'; | ||
23 | import type { IJsonPatch } from 'mobx-state-tree'; | 23 | import type { IJsonPatch } from 'mobx-state-tree'; |
24 | import { | 24 | import { |
25 | Action, | 25 | Action, |
@@ -29,9 +29,17 @@ import { | |||
29 | SophieRenderer, | 29 | SophieRenderer, |
30 | } from '@sophie/shared'; | 30 | } from '@sophie/shared'; |
31 | 31 | ||
32 | import { createSophieRenderer } from '../SophieRendererImpl'; | 32 | jest.unstable_mockModule('electron', () => ({ |
33 | ipcRenderer: { | ||
34 | invoke: jest.fn(), | ||
35 | on: jest.fn(), | ||
36 | send: jest.fn(), | ||
37 | }, | ||
38 | })); | ||
33 | 39 | ||
34 | jest.mock('electron'); | 40 | const { ipcRenderer } = await import('electron'); |
41 | |||
42 | const { createSophieRenderer } = await import('../SophieRendererImpl'); | ||
35 | 43 | ||
36 | const event: Electron.IpcRendererEvent = null as unknown as Electron.IpcRendererEvent; | 44 | const event: Electron.IpcRendererEvent = null as unknown as Electron.IpcRendererEvent; |
37 | 45 | ||
diff --git a/packages/preload/tsconfig.json b/packages/preload/tsconfig.json index 741d435..0f27305 100644 --- a/packages/preload/tsconfig.json +++ b/packages/preload/tsconfig.json | |||
@@ -6,9 +6,6 @@ | |||
6 | "dom", | 6 | "dom", |
7 | "dom.iterable", | 7 | "dom.iterable", |
8 | "esnext" | 8 | "esnext" |
9 | ], | ||
10 | "types": [ | ||
11 | "@types/jest" | ||
12 | ] | 9 | ] |
13 | }, | 10 | }, |
14 | "references": [ | 11 | "references": [ |