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 /config | |
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 'config')
-rw-r--r-- | config/build-common.js | 28 | ||||
-rw-r--r-- | config/esbuild-config.js | 17 |
2 files changed, 31 insertions, 14 deletions
diff --git a/config/build-common.js b/config/build-common.js index aea2335..ff5a218 100644 --- a/config/build-common.js +++ b/config/build-common.js | |||
@@ -1,11 +1,17 @@ | |||
1 | // @ts-check | 1 | import { readFileSync } from 'fs'; |
2 | import { dirname, join } from 'path'; | ||
3 | import { fileURLToPath } from 'url'; | ||
2 | 4 | ||
3 | // `resolveJsonModule` is disabled for this package, but vite will load the json nevertheless. | 5 | const thisDir = fileURLToDirname(import.meta.url); |
4 | // @ts-ignore | 6 | |
5 | const { chrome: chromeVersion, node: nodeVersion } = require('../.electron-vendors.cache.json'); | 7 | // We import this from a vite config, where top-level await is not available (es2021), |
8 | // so we have to use the synchronous filesystem API. | ||
9 | const electronVendorsJson = readFileSync(join(thisDir, '../.electron-vendors.cache.json'), 'utf8'); | ||
10 | |||
11 | const { chrome: chromeVersion, node: nodeVersion } = JSON.parse(electronVendorsJson); | ||
6 | 12 | ||
7 | /** @type {string} */ | 13 | /** @type {string} */ |
8 | module.exports.banner = `/*! | 14 | export const banner = `/*! |
9 | * Copyright (C) 2021-2022 Sophie contributors | 15 | * Copyright (C) 2021-2022 Sophie contributors |
10 | * | 16 | * |
11 | * This file is part of Sophie. | 17 | * This file is part of Sophie. |
@@ -27,7 +33,15 @@ module.exports.banner = `/*! | |||
27 | `; | 33 | `; |
28 | 34 | ||
29 | /** @type {string} */ | 35 | /** @type {string} */ |
30 | module.exports.chrome = `chrome${chromeVersion}`; | 36 | export const chrome = `chrome${chromeVersion}`; |
31 | 37 | ||
32 | /** @type {string} */ | 38 | /** @type {string} */ |
33 | module.exports.node = `node${nodeVersion}`; | 39 | export const node = `node${nodeVersion}`; |
40 | |||
41 | /** | ||
42 | * @param {string} url | ||
43 | * @returns {string} | ||
44 | */ | ||
45 | export function fileURLToDirname(url) { | ||
46 | return dirname(fileURLToPath(url)); | ||
47 | } | ||
diff --git a/config/esbuild-config.js b/config/esbuild-config.js index 9140b89..52d1a59 100644 --- a/config/esbuild-config.js +++ b/config/esbuild-config.js | |||
@@ -1,6 +1,4 @@ | |||
1 | // @ts-check | 1 | import { banner } from './build-common.js'; |
2 | |||
3 | const { banner } = require('./build-common'); | ||
4 | 2 | ||
5 | /** @type {string} */ | 3 | /** @type {string} */ |
6 | const mode = process.env.MODE || 'development'; | 4 | const mode = process.env.MODE || 'development'; |
@@ -8,8 +6,10 @@ const mode = process.env.MODE || 'development'; | |||
8 | /** @type {boolean} */ | 6 | /** @type {boolean} */ |
9 | const isDevelopment = mode === 'development'; | 7 | const isDevelopment = mode === 'development'; |
10 | 8 | ||
9 | /** @type {string} */ | ||
11 | const modeString = JSON.stringify(mode); | 10 | const modeString = JSON.stringify(mode); |
12 | 11 | ||
12 | /** @type {Record<string, string>} */ | ||
13 | const defineEnv = { | 13 | const defineEnv = { |
14 | 'import.meta.env.DEV': JSON.stringify(isDevelopment), | 14 | 'import.meta.env.DEV': JSON.stringify(isDevelopment), |
15 | 'import.meta.env.MODE': modeString, | 15 | 'import.meta.env.MODE': modeString, |
@@ -20,13 +20,16 @@ const defineEnv = { | |||
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @param {import('esbuild').BuildOptions} config | 22 | * @param {import('esbuild').BuildOptions} config |
23 | * @param {object | unknown} metaEnvVars | 23 | * @param {Record<string, string>} [metaEnvVars] |
24 | * @returns {import('esbuild').BuildOptions} | 24 | * @returns {import('esbuild').BuildOptions} |
25 | */ | 25 | */ |
26 | module.exports.getConfig = function(config, metaEnvVars) { | 26 | export function getConfig(config, metaEnvVars) { |
27 | /** @type {Record<string, string>} */ | ||
27 | const defineMeta = {}; | 28 | const defineMeta = {}; |
28 | for (const varName in metaEnvVars) { | 29 | if (metaEnvVars) { |
29 | defineMeta[`import.meta.env.${varName}`] = JSON.stringify(metaEnvVars[varName]); | 30 | for (const varName in metaEnvVars) { |
31 | defineMeta[`import.meta.env.${varName}`] = JSON.stringify(metaEnvVars[varName]); | ||
32 | } | ||
30 | } | 33 | } |
31 | return { | 34 | return { |
32 | logLevel: 'info', | 35 | logLevel: 'info', |