|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2022-2026 EclipseSource and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | + * with the GNU Classpath Exception which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + ********************************************************************************/ |
| 16 | +// @ts-check |
| 17 | +const { spawn } = require('node:child_process'); |
| 18 | +const { resolve } = require('node:path'); |
| 19 | +const esbuild = require('esbuild'); |
| 20 | + |
| 21 | +const argv = process.argv.slice(2); |
| 22 | +const watch = argv.includes('--watch'); |
| 23 | +// Dev mode: build only the node target and (re)start the server after each successful build. |
| 24 | +const runNode = argv.includes('--run-node'); |
| 25 | +// Everything after `--` is forwarded verbatim to the spawned node server (e.g. `--port 5007`). |
| 26 | +const dashDash = argv.indexOf('--'); |
| 27 | +const serverArgs = dashDash >= 0 ? argv.slice(dashDash + 1) : []; |
| 28 | + |
| 29 | +const bundledDir = resolve(__dirname, '..', 'workflow-server-bundled'); |
| 30 | +const bundledWebDir = resolve(__dirname, '..', 'workflow-server-bundled-web'); |
| 31 | + |
| 32 | +/** |
| 33 | + * Reports the build progress and surfaces errors/warnings in a format that |
| 34 | + * VS Code's `$esbuild-watch` problem matcher can pick up. |
| 35 | + * @type {import('esbuild').Plugin} |
| 36 | + */ |
| 37 | +const esbuildProblemMatcherPlugin = { |
| 38 | + name: 'esbuild-problem-matcher', |
| 39 | + setup(build) { |
| 40 | + build.onStart(() => { |
| 41 | + console.log(`${watch ? '[watch] ' : ''}build started`); |
| 42 | + }); |
| 43 | + build.onEnd(result => { |
| 44 | + result.errors.forEach(({ text, location }) => { |
| 45 | + console.error(`✘ [ERROR] ${text}`); |
| 46 | + if (location) { |
| 47 | + console.error(` ${location.file}:${location.line}:${location.column}:`); |
| 48 | + } |
| 49 | + }); |
| 50 | + console.log(`${watch ? '[watch] ' : ''}build finished`); |
| 51 | + }); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +/** @type {import('esbuild').BuildOptions} */ |
| 56 | +const common = { |
| 57 | + bundle: true, |
| 58 | + // Minify one-shot production builds; keep watch/dev output readable and fast to rebuild. |
| 59 | + // `sourcemap` still maps the (minified) output back to the original TypeScript sources. |
| 60 | + minify: !watch, |
| 61 | + // Preserve original class/function names through minification — GLSP's logger and error |
| 62 | + // messages use `constructor.name` for labels, which would otherwise be mangled. |
| 63 | + keepNames: true, |
| 64 | + sourcemap: true, |
| 65 | + logLevel: 'silent', |
| 66 | + tsconfig: resolve(__dirname, 'tsconfig.json'), |
| 67 | + plugins: [esbuildProblemMatcherPlugin] |
| 68 | +}; |
| 69 | + |
| 70 | +/** @type {import('esbuild').BuildOptions} */ |
| 71 | +const nodeConfig = { |
| 72 | + ...common, |
| 73 | + entryPoints: [resolve(__dirname, 'src/node/app.ts')], |
| 74 | + outfile: resolve(bundledDir, 'wf-glsp-server-node.js'), |
| 75 | + platform: 'node', |
| 76 | + format: 'cjs', |
| 77 | + target: 'node22', |
| 78 | + // `ws` requires these optional native deps in a try/catch; keep them external so the |
| 79 | + // graceful-fallback path works and esbuild does not error on the missing modules. |
| 80 | + external: ['bufferutil', 'utf-8-validate'] |
| 81 | +}; |
| 82 | + |
| 83 | +/** @type {import('esbuild').BuildOptions} */ |
| 84 | +const webworkerConfig = { |
| 85 | + ...common, |
| 86 | + entryPoints: [resolve(__dirname, 'src/browser/app.ts')], |
| 87 | + outfile: resolve(bundledWebDir, 'wf-glsp-server-webworker.js'), |
| 88 | + platform: 'browser', |
| 89 | + format: 'iife', |
| 90 | + target: 'es2019', |
| 91 | + // Honor the legacy `browser` package field + browser condition for transitive deps. |
| 92 | + mainFields: ['browser', 'module', 'main'], |
| 93 | + conditions: ['browser'] |
| 94 | +}; |
| 95 | + |
| 96 | +/** Restart the bundled node server after every successful build. */ |
| 97 | +function runNodePlugin() { |
| 98 | + /** @type {import('node:child_process').ChildProcess | undefined} */ |
| 99 | + let child; |
| 100 | + const stop = () => child?.kill('SIGTERM'); |
| 101 | + process.on('SIGINT', () => { |
| 102 | + stop(); |
| 103 | + process.exit(0); |
| 104 | + }); |
| 105 | + return { |
| 106 | + name: 'run-node-server', |
| 107 | + setup(build) { |
| 108 | + build.onEnd(result => { |
| 109 | + if (result.errors.length > 0) { |
| 110 | + // Keep the last good server running so a broken build does not crash the loop. |
| 111 | + return; |
| 112 | + } |
| 113 | + stop(); |
| 114 | + child = spawn('node', ['--enable-source-maps', nodeConfig.outfile, ...serverArgs], { stdio: 'inherit' }); |
| 115 | + }); |
| 116 | + } |
| 117 | + }; |
| 118 | +} |
| 119 | + |
| 120 | +async function run(config) { |
| 121 | + if (watch) { |
| 122 | + const ctx = await esbuild.context(config); |
| 123 | + await ctx.watch(); |
| 124 | + } else { |
| 125 | + await esbuild.build(config); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +async function main() { |
| 130 | + if (runNode) { |
| 131 | + await run({ ...nodeConfig, plugins: [esbuildProblemMatcherPlugin, runNodePlugin()] }); |
| 132 | + } else { |
| 133 | + await Promise.all([run(nodeConfig), run(webworkerConfig)]); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +main().catch(e => { |
| 138 | + console.error(e); |
| 139 | + process.exit(1); |
| 140 | +}); |
0 commit comments