Get sources building with esbuild

This commit is contained in:
Daniel Imms
2024-07-05 08:46:15 -07:00
parent 5b5e262dff
commit 1f11c7e46c
45 changed files with 666 additions and 254 deletions
+197
View File
@@ -0,0 +1,197 @@
/**
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
* @license MIT
*/
// @ts-check
import { build, context, default as esbuild } from 'esbuild';
import { argv } from 'process';
const config = {
isProd: argv.includes('--prod'),
isWatch: argv.includes('--watch'),
isDemoClient: argv.includes('--demo-client'),
addon: argv.find(e => e.startsWith('--addon='))?.replace(/^--addon=/, ''),
};
// console.info('Running with config:', JSON.stringify(config, undefined, 2));
/** @type {esbuild.BuildOptions} */
const commonOptions = {
format: 'esm',
target: 'es2021',
logLevel: 'debug',
};
/** @type {esbuild.BuildOptions} */
const devOptions = {
sourcemap: true,
minify: false,
};
/** @type {esbuild.BuildOptions} */
const prodOptions = {
minify: true,
treeShaking: true,
logLevel: 'debug',
legalComments: 'none',
// TODO: Mangling private and protected properties will reduce bundle size quite a bit, we must
// make sure we don't cast privates to `any` in order to prevent regressions.
//mangleProps: /_.+/,
banner: {
js: `/**
* Copyright (c) 2014-2024 The xterm.js authors. All rights reserved.
* @license MIT
*
* Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
* @license MIT
*
* Originally forked from (with the author's permission):
* Fabrice Bellard's javascript vt100 for jslinux:
* http://bellard.org/jslinux/
* Copyright (c) 2011 Fabrice Bellard
*/
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/`
}
// mangleProps: /_.+/,
};
/**
* @param {string} addon
*/
function getAddonEntryPoint(addon) {
let result = '';
let nextCap = true;
for (const char of addon) {
if (char === '-') {
nextCap = true;
continue;
}
result += nextCap ? char.toUpperCase() : char
nextCap = false;
}
result += 'Addon';
return result;
}
/** @type {esbuild.BuildOptions} */
let bundleConfig = {
bundle: true,
...commonOptions,
...(config.isProd ? prodOptions : devOptions)
};
/** @type {esbuild.BuildOptions} */
let outConfig = {
format: 'cjs'
}
let skipOut = false;
/** @type {esbuild.BuildOptions} */
let outTestConfig = {
format: 'cjs'
}
let skipOutTest = false;
if (config.addon) {
bundleConfig = {
...bundleConfig,
entryPoints: [`addons/addon-${config.addon}/src/${getAddonEntryPoint(config.addon)}.ts`],
outfile: `addons/addon-${config.addon}/lib/xterm-addon-${config.addon}.js`,
};
outConfig = {
...outConfig,
entryPoints: [`addons/addon-${config.addon}/src/**/*.ts`],
outdir: `addons/addon-${config.addon}/out-esbuild/`
};
outTestConfig = {
...outConfig,
entryPoints: [`addons/addon-${config.addon}/test/**/*.ts`],
outdir: `addons/addon-${config.addon}/out-esbuild-test/`
};
if (config.addon === 'ligatures') {
bundleConfig.platform = 'node';
}
if (config.addon === 'serialize') {
bundleConfig.tsconfig = 'addons/addon-serialize/src/tsconfig.json'
}
// Addons that don't have unit tests
if (['ligatures'].includes(config.addon)) {
skipOutTest = true;
}
// TODO: Fix these
if (config.addon === 'image') {
skipOut = true;
skipOutTest = true;
}
} else {
bundleConfig = {
...bundleConfig,
entryPoints: [`src/browser/public/Terminal.ts`],
outfile: `lib/xterm.js`
};
outConfig = {
...outConfig,
entryPoints: ['src/**/*.ts'],
outdir: 'out-esbuild/'
};
outTestConfig = {
...outConfig,
entryPoints: ['test/**/*.ts'],
outdir: 'out-esbuild-test/'
};
}
if (config.isDemoClient) {
bundleConfig = {
...bundleConfig,
entryPoints: [`demo/client.ts`],
outfile: 'demo/dist/client-bundle.js',
external: ['util', 'os', 'fs', 'path', 'stream', 'Terminal'],
alias: {
"@xterm/xterm": ".",
"@xterm/addon-attach": "./addons/addon-attach/lib/xterm-addon-attach.js",
"@xterm/addon-canvas": "./addons/addon-canvas/lib/xterm-addon-canvas.js",
"@xterm/addon-clipboard": "./addons/addon-clipboard/lib/xterm-addon-clipboard.js",
"@xterm/addon-fit": "./addons/addon-fit/lib/xterm-addon-fit.js",
// "@xterm/addon-image": "./addons/addon-image/lib/xterm-addon-image.js",
// "@xterm/addon-ligatures": "./addons/addon-ligatures/lib/xterm-addon-ligatures.js",
"@xterm/addon-search": "./addons/addon-search/lib/xterm-addon-search.js",
"@xterm/addon-serialize": "./addons/addon-serialize/lib/xterm-addon-serialize.js",
"@xterm/addon-web-links": "./addons/addon-web-links/lib/xterm-addon-web-links.js",
"@xterm/addon-webgl": "./addons/addon-webgl/lib/xterm-addon-webgl.js",
"@xterm/addon-unicode11": "./addons/addon-unicode11/lib/xterm-addon-unicode11.js",
"@xterm/addon-unicode-graphemes": "./addons/addon-unicode-graphemes/lib/xterm-addon-unicode-graphemes.js",
// Needed for out-tsc based image addon
"common/Lifecycle": "./src/common/Lifecycle.ts",
}
}
};
if (config.isWatch) {
// TODO: This doesn't report errors?
context(bundleConfig).then(e => e.watch());
if (!skipOut) {
context(outConfig).then(e => e.watch());
}
if (!skipOutTest) {
context(outTestConfig).then(e => e.watch());
}
} else {
await build(bundleConfig);
if (!skipOut) {
await build(outConfig);
}
if (!skipOutTest) {
await build(outTestConfig);
}
}
+55
View File
@@ -0,0 +1,55 @@
// @ts-check
import { spawn } from "child_process";
import { readdir } from "fs/promises";
import { argv } from "process";
/** @type {{cp: import("child_process").ChildProcessByStdio, name: string}[]} */
const jobs = [];
// Core job
jobs.push(createJob('xterm', []));
// Addon jobs
const addons = (await readdir('addons')).map(e => e.replace('addon-', ''));
for (const addon of addons) {
if (addon !== 'image') {
jobs.push(createJob(`xterm-addon-${addon}`, [`--addon=${addon}`]));
}
}
// Demo job - This requires the others to be built so it's not included when building all
// jobs.push(createJob('demo-client', [`--demo-client`]));
await Promise.all(jobs.map((job, i) => {
return new Promise(r => {
job.cp.on('exit', code => {
log(`Finished \x1b[32m${job.name}\x1b[0m${code ? ' \x1b[31mwith errors\x1b[0m' : ''}`);
r(code);
});
});
}));
/**
* @param {string} message
*/
function log(message) {
console.info(`[\x1b[2m${new Date().toLocaleTimeString('en-GB')}\x1b[0m] ${message}`);
}
/**
* @param {string} name
* @param {string[]} extraArgs
*/
function createJob(name, extraArgs) {
log(`Starting \x1b[32m${name}\x1b[0m...`);
const args = [
'bin/esbuild.mjs',
...extraArgs,
...argv
];
return {
name,
cp: spawn('node', args, { stdio: ["inherit", "inherit", "inherit"] })
};
}