|
| 1 | +const esbuild = require("esbuild"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +const production = process.argv.includes("--production"); |
| 6 | +const watch = process.argv.includes("--watch"); |
| 7 | + |
| 8 | +const extensionPackage = require("./package.json"); |
| 9 | + |
| 10 | +/** |
| 11 | + * @type {import('esbuild').Plugin} |
| 12 | + */ |
| 13 | +const esbuildProblemMatcherPlugin = { |
| 14 | + name: "esbuild-problem-matcher", |
| 15 | + setup(build) { |
| 16 | + build.onStart(() => { |
| 17 | + console.log("[watch] build started"); |
| 18 | + }); |
| 19 | + build.onEnd((result) => { |
| 20 | + result.errors.forEach(({ text, location }) => { |
| 21 | + console.error(`✘ [ERROR] ${text}`); |
| 22 | + if (location) { |
| 23 | + console.error( |
| 24 | + ` ${location.file}:${location.line}:${location.column}:`, |
| 25 | + ); |
| 26 | + } |
| 27 | + }); |
| 28 | + console.log("[watch] build finished"); |
| 29 | + }); |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * @type {import('esbuild').Plugin} |
| 35 | + */ |
| 36 | +const copyWorkerPlugin = { |
| 37 | + name: "copy-worker", |
| 38 | + setup(build) { |
| 39 | + build.onEnd(() => { |
| 40 | + const srcDir = path.join(__dirname, "src/worker"); |
| 41 | + const destDir = path.join(__dirname, "dist/worker"); |
| 42 | + |
| 43 | + if (fs.existsSync(srcDir)) { |
| 44 | + fs.mkdirSync(destDir, { recursive: true }); |
| 45 | + fs.readdirSync(srcDir).forEach((file) => { |
| 46 | + fs.copyFileSync(path.join(srcDir, file), path.join(destDir, file)); |
| 47 | + }); |
| 48 | + console.log("[copy] worker files copied"); |
| 49 | + } |
| 50 | + }); |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * @type {import('esbuild').Plugin} |
| 56 | + */ |
| 57 | +const browserAliasPlugin = { |
| 58 | + name: "browser-alias", |
| 59 | + setup(build) { |
| 60 | + // Replace ModuleResolver imports with BrowserModuleResolver for browser build |
| 61 | + build.onResolve({ filter: /\.\/ModuleResolver$/ }, (args) => { |
| 62 | + return { |
| 63 | + path: path.join(args.resolveDir, "BrowserModuleResolver.ts"), |
| 64 | + }; |
| 65 | + }); |
| 66 | + }, |
| 67 | +}; |
| 68 | + |
| 69 | +// Node extension configuration |
| 70 | +const nodeConfig = { |
| 71 | + entryPoints: ["src/extension.ts"], |
| 72 | + bundle: true, |
| 73 | + format: "cjs", |
| 74 | + minify: production, |
| 75 | + sourcemap: !production, |
| 76 | + sourcesContent: false, |
| 77 | + platform: "node", |
| 78 | + outfile: "dist/extension.js", |
| 79 | + external: ["vscode", "prettier"], |
| 80 | + define: { |
| 81 | + "process.env.EXTENSION_NAME": JSON.stringify( |
| 82 | + `${extensionPackage.publisher}.${extensionPackage.name}`, |
| 83 | + ), |
| 84 | + "process.env.EXTENSION_VERSION": JSON.stringify(extensionPackage.version), |
| 85 | + }, |
| 86 | + logLevel: "silent", |
| 87 | + plugins: [copyWorkerPlugin, esbuildProblemMatcherPlugin], |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * @type {import('esbuild').Plugin} |
| 92 | + */ |
| 93 | +const browserShimsPlugin = { |
| 94 | + name: "browser-shims", |
| 95 | + setup(build) { |
| 96 | + // Provide empty shims for Node.js built-ins not available in browser |
| 97 | + build.onResolve({ filter: /^os$/ }, () => { |
| 98 | + return { path: "os", namespace: "browser-shim" }; |
| 99 | + }); |
| 100 | + build.onLoad({ filter: /.*/, namespace: "browser-shim" }, (args) => { |
| 101 | + if (args.path === "os") { |
| 102 | + return { |
| 103 | + contents: `export function homedir() { return ""; }`, |
| 104 | + loader: "js", |
| 105 | + }; |
| 106 | + } |
| 107 | + }); |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +// Browser/web extension configuration |
| 112 | +const browserConfig = { |
| 113 | + entryPoints: ["src/extension.ts"], |
| 114 | + bundle: true, |
| 115 | + format: "cjs", |
| 116 | + minify: production, |
| 117 | + sourcemap: !production, |
| 118 | + sourcesContent: false, |
| 119 | + platform: "browser", |
| 120 | + outfile: "dist/web-extension.js", |
| 121 | + external: ["vscode"], |
| 122 | + define: { |
| 123 | + "process.env.EXTENSION_NAME": JSON.stringify( |
| 124 | + `${extensionPackage.publisher}.${extensionPackage.name}`, |
| 125 | + ), |
| 126 | + "process.env.EXTENSION_VERSION": JSON.stringify(extensionPackage.version), |
| 127 | + "process.env.BROWSER_ENV": JSON.stringify("true"), |
| 128 | + global: "globalThis", |
| 129 | + }, |
| 130 | + alias: { |
| 131 | + path: "path-browserify", |
| 132 | + }, |
| 133 | + banner: { |
| 134 | + js: `var process = {env: {EXTENSION_NAME: "${extensionPackage.publisher}.${extensionPackage.name}", EXTENSION_VERSION: "${extensionPackage.version}", BROWSER_ENV: "true"}, platform: "browser", cwd: function() { return "/"; }, nextTick: function(cb) { setTimeout(cb, 0); }};`, |
| 135 | + }, |
| 136 | + logLevel: "silent", |
| 137 | + plugins: [ |
| 138 | + browserAliasPlugin, |
| 139 | + browserShimsPlugin, |
| 140 | + esbuildProblemMatcherPlugin, |
| 141 | + ], |
| 142 | +}; |
| 143 | + |
| 144 | +// Web test bundle configuration |
| 145 | +const webTestConfig = { |
| 146 | + entryPoints: ["src/test/web/suite/index.ts"], |
| 147 | + bundle: true, |
| 148 | + format: "cjs", |
| 149 | + minify: false, |
| 150 | + sourcemap: true, |
| 151 | + sourcesContent: false, |
| 152 | + platform: "browser", |
| 153 | + outfile: "dist/web/test/suite/index.js", |
| 154 | + external: ["vscode"], |
| 155 | + define: { |
| 156 | + "process.env.BROWSER_ENV": JSON.stringify("true"), |
| 157 | + "process.platform": JSON.stringify("browser"), |
| 158 | + }, |
| 159 | + alias: { |
| 160 | + path: "path-browserify", |
| 161 | + }, |
| 162 | + logLevel: "silent", |
| 163 | + plugins: [esbuildProblemMatcherPlugin], |
| 164 | +}; |
| 165 | + |
| 166 | +async function main() { |
| 167 | + const nodeCtx = await esbuild.context(nodeConfig); |
| 168 | + const browserCtx = await esbuild.context(browserConfig); |
| 169 | + const webTestCtx = await esbuild.context(webTestConfig); |
| 170 | + |
| 171 | + if (watch) { |
| 172 | + await Promise.all([ |
| 173 | + nodeCtx.watch(), |
| 174 | + browserCtx.watch(), |
| 175 | + webTestCtx.watch(), |
| 176 | + ]); |
| 177 | + } else { |
| 178 | + await Promise.all([ |
| 179 | + nodeCtx.rebuild(), |
| 180 | + browserCtx.rebuild(), |
| 181 | + webTestCtx.rebuild(), |
| 182 | + ]); |
| 183 | + await Promise.all([ |
| 184 | + nodeCtx.dispose(), |
| 185 | + browserCtx.dispose(), |
| 186 | + webTestCtx.dispose(), |
| 187 | + ]); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +main().catch((e) => { |
| 192 | + console.error(e); |
| 193 | + process.exit(1); |
| 194 | +}); |
0 commit comments