|
| 1 | +const { build } = require("esbuild"); |
| 2 | +const { copy } = require("esbuild-plugin-copy"); |
| 3 | + |
| 4 | +//@ts-check |
| 5 | +/** @typedef {import('esbuild').BuildOptions} BuildOptions **/ |
| 6 | + |
| 7 | +/** @type BuildOptions */ |
| 8 | +const baseConfig = { |
| 9 | + bundle: true, |
| 10 | + minify: process.env.NODE_ENV === "production", |
| 11 | + sourcemap: process.env.NODE_ENV !== "production", |
| 12 | +}; |
| 13 | + |
| 14 | +// Config for extension source code (to be run in a Node-based context) |
| 15 | +/** @type BuildOptions */ |
| 16 | +const extensionConfig = { |
| 17 | + ...baseConfig, |
| 18 | + platform: "node", |
| 19 | + format: "cjs", |
| 20 | + entryPoints: ["./src/extension.ts"], |
| 21 | + outfile: "./out/extension.js", |
| 22 | + external: ["vscode"], |
| 23 | +}; |
| 24 | + |
| 25 | +// Config for webview source code (to be run in a web-based context) |
| 26 | +/** @type BuildOptions */ |
| 27 | +const webviewConfig = { |
| 28 | + ...baseConfig, |
| 29 | + target: "es2020", |
| 30 | + format: "esm", |
| 31 | + entryPoints: ["./src/webview/main.ts"], |
| 32 | + outfile: "./out/webview.js", |
| 33 | + plugins: [ |
| 34 | + // Copy webview css and ttf files to `out` directory unaltered |
| 35 | + copy({ |
| 36 | + resolveFrom: "cwd", |
| 37 | + assets: { |
| 38 | + from: ["./src/webview/*.css", "./src/webview/*.ttf"], |
| 39 | + to: ["./out"], |
| 40 | + }, |
| 41 | + }), |
| 42 | + ], |
| 43 | +}; |
| 44 | + |
| 45 | +// This watch config adheres to the conventions of the esbuild-problem-matchers |
| 46 | +// extension (https://github.com/connor4312/esbuild-problem-matchers#esbuild-via-js) |
| 47 | +/** @type BuildOptions */ |
| 48 | +const watchConfig = { |
| 49 | + watch: { |
| 50 | + onRebuild(error, result) { |
| 51 | + console.log("[watch] build started"); |
| 52 | + if (error) { |
| 53 | + error.errors.forEach((error) => |
| 54 | + console.error( |
| 55 | + `> ${error.location.file}:${error.location.line}:${error.location.column}: error: ${error.text}` |
| 56 | + ) |
| 57 | + ); |
| 58 | + } else { |
| 59 | + console.log("[watch] build finished"); |
| 60 | + } |
| 61 | + }, |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +// Build script |
| 66 | +(async () => { |
| 67 | + const args = process.argv.slice(2); |
| 68 | + try { |
| 69 | + if (args.includes("--watch")) { |
| 70 | + // Build and watch extension and webview code |
| 71 | + console.log("[watch] build started"); |
| 72 | + await build({ |
| 73 | + ...extensionConfig, |
| 74 | + ...watchConfig, |
| 75 | + }); |
| 76 | + await build({ |
| 77 | + ...webviewConfig, |
| 78 | + ...watchConfig, |
| 79 | + }); |
| 80 | + console.log("[watch] build finished"); |
| 81 | + } else { |
| 82 | + // Build extension and webview code |
| 83 | + await build(extensionConfig); |
| 84 | + await build(webviewConfig); |
| 85 | + console.log("build complete"); |
| 86 | + } |
| 87 | + } catch (err) { |
| 88 | + process.stderr.write(err.stderr); |
| 89 | + process.exit(1); |
| 90 | + } |
| 91 | +})(); |
0 commit comments