2022-02-11 13:38:52 -08:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
const esbuild = require('esbuild');
|
2025-09-16 17:39:03 -07:00
|
|
|
const path = require('node:path');
|
|
|
|
const fs = require('node:fs');
|
2024-11-13 17:20:36 -08:00
|
|
|
const fastGlob = require('fast-glob');
|
2022-02-11 13:38:52 -08:00
|
|
|
|
|
|
|
const ROOT_DIR = path.join(__dirname, '..');
|
2023-04-20 17:23:19 -04:00
|
|
|
const BUNDLES_DIR = 'bundles';
|
2025-09-16 17:39:03 -07:00
|
|
|
const NODE_MODULES_DIR = path.join(ROOT_DIR, 'node_modules');
|
2022-02-11 13:38:52 -08:00
|
|
|
|
|
|
|
const watch = process.argv.some(argv => argv === '-w' || argv === '--watch');
|
|
|
|
const isProd = process.argv.some(argv => argv === '-prod' || argv === '--prod');
|
2025-08-05 14:24:31 -07:00
|
|
|
const noBundle = process.argv.some(argv => argv === '--no-bundle');
|
|
|
|
const noScripts = process.argv.some(argv => argv === '--no-scripts');
|
2022-02-11 13:38:52 -08:00
|
|
|
|
|
|
|
const nodeDefaults = {
|
|
|
|
platform: 'node',
|
2024-09-10 10:41:13 -07:00
|
|
|
target: 'es2023',
|
2024-02-06 17:50:20 -08:00
|
|
|
// Disabled even in dev because the debugger is broken
|
|
|
|
sourcemap: false,
|
2022-02-11 13:38:52 -08:00
|
|
|
// Otherwise React components get renamed
|
|
|
|
// See: https://github.com/evanw/esbuild/issues/1147
|
|
|
|
keepNames: true,
|
|
|
|
logLevel: 'info',
|
2025-09-16 17:39:03 -07:00
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
name: 'resolve-ts',
|
|
|
|
setup(b) {
|
|
|
|
b.onResolve({ filter: /\.js$/ }, args => {
|
|
|
|
if (!args.path.startsWith('.')) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetPath = path.join(args.resolveDir, args.path);
|
|
|
|
if (targetPath.startsWith(NODE_MODULES_DIR)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const tsPath = targetPath.replace(/\.js$/, '.ts');
|
|
|
|
const tsxPath = targetPath.replace(/\.js$/, '.tsx');
|
|
|
|
if (fs.existsSync(tsPath)) {
|
|
|
|
return { path: tsPath };
|
|
|
|
}
|
|
|
|
if (fs.existsSync(tsxPath)) {
|
|
|
|
return { path: tsxPath };
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2022-02-11 13:38:52 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const bundleDefaults = {
|
|
|
|
...nodeDefaults,
|
2022-02-25 12:36:42 -08:00
|
|
|
define: {
|
|
|
|
'process.env.NODE_ENV': isProd ? '"production"' : '"development"',
|
|
|
|
},
|
2022-02-11 13:38:52 -08:00
|
|
|
bundle: true,
|
2023-04-20 17:23:19 -04:00
|
|
|
minify: isProd,
|
2022-02-11 13:38:52 -08:00
|
|
|
external: [
|
2022-02-28 09:39:39 -08:00
|
|
|
// Native libraries
|
2022-03-24 14:47:21 -07:00
|
|
|
'@signalapp/libsignal-client',
|
|
|
|
'@signalapp/libsignal-client/zkgroup',
|
2023-01-09 10:38:57 -08:00
|
|
|
'@signalapp/ringrtc',
|
2025-03-12 14:45:54 -07:00
|
|
|
'@signalapp/sqlcipher',
|
2024-09-26 16:25:03 -07:00
|
|
|
'@indutny/mac-screen-share',
|
2022-02-28 09:39:39 -08:00
|
|
|
'electron',
|
2022-02-11 13:38:52 -08:00
|
|
|
'fs-xattr',
|
|
|
|
'fsevents',
|
2022-02-28 09:39:39 -08:00
|
|
|
'mac-screen-capture-permissions',
|
|
|
|
'sass',
|
2022-12-15 18:03:01 -08:00
|
|
|
'bufferutil',
|
|
|
|
'utf-8-validate',
|
2022-02-28 09:39:39 -08:00
|
|
|
|
|
|
|
// Things that don't bundle well
|
2022-02-11 13:38:52 -08:00
|
|
|
'got',
|
|
|
|
'node-fetch',
|
|
|
|
'pino',
|
|
|
|
'proxy-agent',
|
2022-12-15 18:03:01 -08:00
|
|
|
'websocket',
|
2022-02-28 09:39:39 -08:00
|
|
|
|
|
|
|
// Large libraries (3.7mb total)
|
|
|
|
// See: https://esbuild.github.io/api/#analyze
|
|
|
|
'emoji-datasource',
|
|
|
|
'fabric',
|
|
|
|
'google-libphonenumber',
|
|
|
|
'moment',
|
|
|
|
'quill',
|
2022-02-11 13:38:52 -08:00
|
|
|
|
2025-09-16 17:39:03 -07:00
|
|
|
// Imported, but not used in production builds
|
|
|
|
'mocha',
|
|
|
|
|
2022-02-11 13:38:52 -08:00
|
|
|
// Uses fast-glob and dynamic requires
|
2025-09-16 17:39:03 -07:00
|
|
|
'./preload_test.js',
|
2022-02-11 13:38:52 -08:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-04-20 17:23:19 -04:00
|
|
|
const sandboxedPreloadDefaults = {
|
|
|
|
...nodeDefaults,
|
|
|
|
define: {
|
|
|
|
'process.env.NODE_ENV': isProd ? '"production"' : '"development"',
|
|
|
|
},
|
|
|
|
external: ['electron'],
|
|
|
|
bundle: true,
|
|
|
|
minify: isProd,
|
|
|
|
};
|
2023-01-18 12:12:19 -08:00
|
|
|
|
2023-04-20 17:23:19 -04: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 }) {
|
2025-08-05 14:24:31 -07:00
|
|
|
let app;
|
|
|
|
let preload;
|
|
|
|
|
|
|
|
if (!noScripts) {
|
|
|
|
app = await esbuild.context(appConfig);
|
|
|
|
}
|
|
|
|
if (!noBundle) {
|
|
|
|
preload = await esbuild.context(preloadConfig);
|
|
|
|
}
|
2023-01-18 12:12:19 -08:00
|
|
|
|
|
|
|
if (watch) {
|
2025-08-05 14:24:31 -07:00
|
|
|
await Promise.all([app && app.watch(), preload && preload.watch()]);
|
2023-01-18 12:12:19 -08:00
|
|
|
} else {
|
2025-08-05 14:24:31 -07:00
|
|
|
await Promise.all([app && app.rebuild(), preload && preload.rebuild()]);
|
2023-01-18 12:12:19 -08:00
|
|
|
|
2025-08-05 14:24:31 -07:00
|
|
|
if (app) {
|
|
|
|
await app.dispose();
|
|
|
|
}
|
|
|
|
if (preload) {
|
|
|
|
await preload.dispose();
|
|
|
|
}
|
2023-01-18 16:17:16 -08:00
|
|
|
}
|
2023-01-18 12:12:19 -08:00
|
|
|
}
|
2022-02-11 13:38:52 -08:00
|
|
|
|
2023-04-20 17:23:19 -04:00
|
|
|
async function main() {
|
|
|
|
await build({
|
|
|
|
appConfig: {
|
|
|
|
...nodeDefaults,
|
|
|
|
format: 'cjs',
|
|
|
|
mainFields: ['browser', 'main'],
|
2024-09-08 14:09:57 -07:00
|
|
|
entryPoints: [
|
|
|
|
'preload.wrapper.ts',
|
2024-11-13 17:20:36 -08:00
|
|
|
...fastGlob
|
2025-09-16 17:39:03 -07:00
|
|
|
.sync('{app,ts,build}/**/*.{ts,tsx}', {
|
2024-11-13 17:20:36 -08:00
|
|
|
onlyFiles: true,
|
|
|
|
cwd: ROOT_DIR,
|
2024-09-08 14:09:57 -07:00
|
|
|
})
|
|
|
|
.filter(file => !file.endsWith('.d.ts')),
|
|
|
|
],
|
2023-04-20 17:23:19 -04:00
|
|
|
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 11:55:31 -04:00
|
|
|
|
2023-04-20 17:23:19 -04: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'),
|
2024-05-22 17:28:01 -07:00
|
|
|
path.join(
|
|
|
|
ROOT_DIR,
|
|
|
|
'ts',
|
|
|
|
'windows',
|
2024-05-23 15:19:12 -07:00
|
|
|
'calling-tools',
|
|
|
|
'webrtc_internals.ts'
|
2024-05-22 17:28:01 -07:00
|
|
|
),
|
2023-04-20 17:23:19 -04:00
|
|
|
],
|
|
|
|
},
|
|
|
|
preloadConfig: {
|
|
|
|
...sandboxedPreloadDefaults,
|
2025-06-16 09:47:18 -07:00
|
|
|
mainFields: ['browser', 'main'],
|
2023-04-20 17:23:19 -04:00
|
|
|
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 15:19:12 -07:00
|
|
|
path.join(ROOT_DIR, 'ts', 'windows', 'calling-tools', 'preload.ts'),
|
2023-04-20 17:23:19 -04:00
|
|
|
path.join(ROOT_DIR, 'ts', 'windows', 'screenShare', 'preload.ts'),
|
|
|
|
],
|
|
|
|
format: 'cjs',
|
|
|
|
outdir: 'bundles',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-03-14 11:55:31 -04:00
|
|
|
|
2023-04-20 17:23:19 -04:00
|
|
|
Promise.all([main(), sandboxedEnv()]).catch(error => {
|
|
|
|
console.error(error.stack);
|
|
|
|
process.exit(1);
|
2023-03-14 11:55:31 -04:00
|
|
|
});
|