aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-30 00:26:01 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-30 02:24:28 +0100
commit61fd13c55f5e69a9d8b32dd0d74b08870783bcce (patch)
tree4f3f97b1629f3c262bea076b596bc7245ccbc0bd /config
parentRevert "refactor: Switch back to consola for prettyness" (diff)
downloadsophie-61fd13c55f5e69a9d8b32dd0d74b08870783bcce.tar.gz
sophie-61fd13c55f5e69a9d8b32dd0d74b08870783bcce.tar.zst
sophie-61fd13c55f5e69a9d8b32dd0d74b08870783bcce.zip
build: Switch to esbuild
We will build all packages except the frontend (where vite remains in use) with esbuild. For some reason, the @yarnpkg/esbuild-plugin-pnp doesn't allow esbuild to load esm modules and we fall back to commonjs for dependencies. Hence we had to switch back to node_modules (but still rely on yarn hardlinking for a more efficient install).
Diffstat (limited to 'config')
-rw-r--r--config/build-common.js33
-rw-r--r--config/esbuild-config.js50
-rw-r--r--config/vite-common.js67
3 files changed, 83 insertions, 67 deletions
diff --git a/config/build-common.js b/config/build-common.js
new file mode 100644
index 0000000..aea2335
--- /dev/null
+++ b/config/build-common.js
@@ -0,0 +1,33 @@
1// @ts-check
2
3// `resolveJsonModule` is disabled for this package, but vite will load the json nevertheless.
4// @ts-ignore
5const { chrome: chromeVersion, node: nodeVersion } = require('../.electron-vendors.cache.json');
6
7/** @type {string} */
8module.exports.banner = `/*!
9 * Copyright (C) 2021-2022 Sophie contributors
10 *
11 * This file is part of Sophie.
12 *
13 * Sophie is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License as
15 * published by the Free Software Foundation, version 3.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 *
25 * SPDX-License-Identifier: AGPL-3.0-only
26 */
27`;
28
29/** @type {string} */
30module.exports.chrome = `chrome${chromeVersion}`;
31
32/** @type {string} */
33module.exports.node = `node${nodeVersion}`;
diff --git a/config/esbuild-config.js b/config/esbuild-config.js
new file mode 100644
index 0000000..9140b89
--- /dev/null
+++ b/config/esbuild-config.js
@@ -0,0 +1,50 @@
1// @ts-check
2
3const { banner } = require('./build-common');
4
5/** @type {string} */
6const mode = process.env.MODE || 'development';
7
8/** @type {boolean} */
9const isDevelopment = mode === 'development';
10
11const modeString = JSON.stringify(mode);
12
13const defineEnv = {
14 'import.meta.env.DEV': JSON.stringify(isDevelopment),
15 'import.meta.env.MODE': modeString,
16 'import.meta.env.PROD': JSON.stringify(!isDevelopment),
17 'process.env.NODE_ENV': modeString,
18 'process.env.MODE': modeString,
19};
20
21/**
22 * @param {import('esbuild').BuildOptions} config
23 * @param {object | unknown} metaEnvVars
24 * @returns {import('esbuild').BuildOptions}
25 */
26module.exports.getConfig = function(config, metaEnvVars) {
27 const defineMeta = {};
28 for (const varName in metaEnvVars) {
29 defineMeta[`import.meta.env.${varName}`] = JSON.stringify(metaEnvVars[varName]);
30 }
31 return {
32 logLevel: 'info',
33 bundle: true,
34 treeShaking: !isDevelopment,
35 minify: !isDevelopment,
36 banner: {
37 js: banner,
38 },
39 ...config,
40 sourcemap: isDevelopment ? (config.sourcemap || true) : false,
41 define: {
42 ...defineEnv,
43 ...defineMeta,
44 ...config.define,
45 },
46 plugins: [
47 ...(config.plugins || []),
48 ],
49 };
50}
diff --git a/config/vite-common.js b/config/vite-common.js
deleted file mode 100644
index 6b1d7c7..0000000
--- a/config/vite-common.js
+++ /dev/null
@@ -1,67 +0,0 @@
1// @ts-check
2
3// `resolveJsonModule` is disabled for this package, but vite will load the json nevertheless.
4// @ts-expect-error
5import { chrome as chromeVersion, node as nodeVersion } from '../.electron-vendors.cache.json';
6
7/** @type {string} */
8const banner = `/*!
9 * Copyright (C) 2021-2022 Sophie contributors
10 *
11 * This file is part of Sophie.
12 *
13 * Sophie is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License as
15 * published by the Free Software Foundation, version 3.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 *
25 * SPDX-License-Identifier: AGPL-3.0-only
26 */
27`;
28
29/** @type {string} */
30export const chrome = `chrome${chromeVersion}`;
31
32/** @type {string} */
33export const node = `node${nodeVersion}`;
34
35/**
36 * Extends a vite configuration with common options.
37 *
38 * @param {import('vite').UserConfig} config The configuration to extend.
39 * @return {import('vite').UserConfig} The configuration extended with common options.
40 */
41export function makeConfig(config) {
42 return {
43 mode: process.env.NODE,
44 envDir: process.cwd(),
45 ...config,
46 build: {
47 assetsDir: '.',
48 outDir: 'dist',
49 emptyOutDir: true,
50 sourcemap: true,
51 minify: process.env.MODE !== 'development',
52 brotliSize: false,
53 ...config.build,
54 lib: (typeof config.build.lib === 'object') ? {
55 fileName: (format) => format === 'cjs' ? 'index.cjs' : `index.${format}.js`,
56 ...config.build.lib,
57 } : undefined,
58 rollupOptions: {
59 ...config.build?.rollupOptions,
60 output: {
61 banner,
62 ...config.build?.rollupOptions?.output,
63 },
64 },
65 },
66 };
67}