signal-desktop/scripts/esbuild.js

170 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-02-11 21:38:52 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
const esbuild = require('esbuild');
const path = require('path');
const glob = require('glob');
const ROOT_DIR = path.join(__dirname, '..');
const BUNDLES_DIR = 'bundles';
2022-02-11 21:38:52 +00:00
const watch = process.argv.some(argv => argv === '-w' || argv === '--watch');
const isProd = process.argv.some(argv => argv === '-prod' || argv === '--prod');
const nodeDefaults = {
platform: 'node',
2024-09-10 17:41:13 +00:00
target: 'es2023',
2024-02-07 01:50:20 +00:00
// Disabled even in dev because the debugger is broken
sourcemap: false,
2022-02-11 21:38:52 +00:00
// Otherwise React components get renamed
// See: https://github.com/evanw/esbuild/issues/1147
keepNames: true,
logLevel: 'info',
};
const bundleDefaults = {
...nodeDefaults,
define: {
'process.env.NODE_ENV': isProd ? '"production"' : '"development"',
},
2022-02-11 21:38:52 +00:00
bundle: true,
minify: isProd,
2022-02-11 21:38:52 +00:00
external: [
// Native libraries
'@signalapp/libsignal-client',
'@signalapp/libsignal-client/zkgroup',
2023-01-09 18:38:57 +00:00
'@signalapp/ringrtc',
2022-12-14 20:48:36 +00:00
'@signalapp/better-sqlite3',
2024-09-26 23:25:03 +00:00
'@indutny/mac-screen-share',
'electron',
2022-02-11 21:38:52 +00:00
'fs-xattr',
'fsevents',
'mac-screen-capture-permissions',
'sass',
2022-12-16 02:03:01 +00:00
'bufferutil',
'utf-8-validate',
// Things that don't bundle well
2022-02-11 21:38:52 +00:00
'got',
'jquery',
'node-fetch',
'pino',
'proxy-agent',
2022-12-16 02:03:01 +00:00
'websocket',
// Large libraries (3.7mb total)
// See: https://esbuild.github.io/api/#analyze
'emoji-datasource',
'fabric',
'google-libphonenumber',
'moment',
'quill',
2022-02-11 21:38:52 +00:00
// Uses fast-glob and dynamic requires
'./preload_test',
],
};
const sandboxedPreloadDefaults = {
...nodeDefaults,
define: {
'process.env.NODE_ENV': isProd ? '"production"' : '"development"',
},
external: ['electron'],
bundle: true,
minify: isProd,
};
2023-01-18 20:12:19 +00:00
const sandboxedBrowserDefaults = {
...sandboxedPreloadDefaults,
chunkNames: 'chunks/[name]-[hash]',
format: 'esm',
outdir: path.join(ROOT_DIR, BUNDLES_DIR),
platform: 'browser',
splitting: true,
};
async function build({ appConfig, preloadConfig }) {
const app = await esbuild.context(appConfig);
const preload = await esbuild.context(preloadConfig);
2023-01-18 20:12:19 +00:00
if (watch) {
await Promise.all([app.watch(), preload.watch()]);
2023-01-18 20:12:19 +00:00
} else {
await Promise.all([app.rebuild(), preload.rebuild()]);
2023-01-18 20:12:19 +00:00
2023-01-19 00:17:16 +00:00
await app.dispose();
await preload.dispose();
2023-01-19 00:17:16 +00:00
}
2023-01-18 20:12:19 +00:00
}
2022-02-11 21:38:52 +00:00
async function main() {
await build({
appConfig: {
...nodeDefaults,
format: 'cjs',
mainFields: ['browser', 'main'],
entryPoints: [
'preload.wrapper.ts',
...glob
.sync('{app,ts}/**/*.{ts,tsx}', {
nodir: true,
root: ROOT_DIR,
})
.filter(file => !file.endsWith('.d.ts')),
],
outdir: path.join(ROOT_DIR),
},
preloadConfig: {
...bundleDefaults,
mainFields: ['browser', 'main'],
entryPoints: [path.join(ROOT_DIR, 'ts', 'windows', 'main', 'preload.ts')],
outfile: path.join(ROOT_DIR, 'preload.bundle.js'),
},
});
}
2023-03-14 15:55:31 +00:00
async function sandboxedEnv() {
await build({
appConfig: {
...sandboxedBrowserDefaults,
mainFields: ['browser', 'main'],
entryPoints: [
path.join(ROOT_DIR, 'ts', 'windows', 'about', 'app.tsx'),
path.join(ROOT_DIR, 'ts', 'windows', 'debuglog', 'app.tsx'),
path.join(ROOT_DIR, 'ts', 'windows', 'loading', 'start.ts'),
path.join(ROOT_DIR, 'ts', 'windows', 'permissions', 'app.tsx'),
path.join(ROOT_DIR, 'ts', 'windows', 'screenShare', 'app.tsx'),
path.join(ROOT_DIR, 'ts', 'windows', 'settings', 'app.tsx'),
path.join(
ROOT_DIR,
'ts',
'windows',
2024-05-23 22:19:12 +00:00
'calling-tools',
'webrtc_internals.ts'
),
],
},
preloadConfig: {
...sandboxedPreloadDefaults,
mainFields: ['main'],
entryPoints: [
path.join(ROOT_DIR, 'ts', 'windows', 'about', 'preload.ts'),
path.join(ROOT_DIR, 'ts', 'windows', 'debuglog', 'preload.ts'),
path.join(ROOT_DIR, 'ts', 'windows', 'loading', 'preload.ts'),
path.join(ROOT_DIR, 'ts', 'windows', 'permissions', 'preload.ts'),
2024-05-23 22:19:12 +00:00
path.join(ROOT_DIR, 'ts', 'windows', 'calling-tools', 'preload.ts'),
path.join(ROOT_DIR, 'ts', 'windows', 'screenShare', 'preload.ts'),
path.join(ROOT_DIR, 'ts', 'windows', 'settings', 'preload.ts'),
],
format: 'cjs',
outdir: 'bundles',
},
});
}
2023-03-14 15:55:31 +00:00
Promise.all([main(), sandboxedEnv()]).catch(error => {
console.error(error.stack);
process.exit(1);
2023-03-14 15:55:31 +00:00
});