| 1 | const fs = require("fs"); |
| 2 | |
| 3 | const { writeBuildTimestamp } = require("./utils"); |
| 4 | |
| 5 | const esbuild = require("esbuild"); |
| 6 | |
| 7 | const flags = process.argv.slice(2); |
| 8 | |
| 9 | const esbuildConfig = { |
| 10 | entryPoints: ["src/extension.ts"], |
| 11 | bundle: true, |
| 12 | outfile: "out/extension.js", |
| 13 | external: ["vscode", "esbuild", "./xhr-sync-worker.js"], |
| 14 | format: "cjs", |
| 15 | platform: "node", |
| 16 | sourcemap: flags.includes("--sourcemap"), |
| 17 | loader: { |
| 18 | // eslint-disable-next-line @typescript-eslint/naming-convention |
| 19 | ".node": "file", |
| 20 | }, |
| 21 | |
| 22 | // To allow import.meta.path for transformers.js |
| 23 | // https://github.com/evanw/esbuild/issues/1492#issuecomment-893144483 |
| 24 | inject: ["./scripts/importMetaUrl.js"], |
| 25 | define: { "import.meta.url": "importMetaUrl" }, |
| 26 | supported: { "dynamic-import": false }, |
| 27 | metafile: true, |
| 28 | plugins: [ |
| 29 | { |
| 30 | name: "on-end-plugin", |
| 31 | setup(build) { |
| 32 | build.onEnd((result) => { |
| 33 | if (result.errors.length > 0) { |
| 34 | console.error("Build failed with errors:", result.errors); |
| 35 | throw new Error(result.errors); |
| 36 | } else { |
| 37 | try { |
| 38 | fs.writeFileSync( |
| 39 | "./build/meta.json", |
| 40 | JSON.stringify(result.metafile, null, 2), |
| 41 | ); |
| 42 | } catch (e) { |
| 43 | console.error("Failed to write esbuild meta file", e); |
| 44 | } |
| 45 | console.log("VS Code Extension esbuild complete"); // used verbatim in vscode tasks to detect completion |
| 46 | } |
| 47 | }); |
| 48 | }, |
| 49 | }, |
| 50 | ], |
| 51 | }; |
| 52 | |
| 53 | void (async () => { |
| 54 | // Create .buildTimestamp.js before starting the first build |
| 55 | writeBuildTimestamp(); |
| 56 | // Bundles the extension into one file |
| 57 | if (flags.includes("--watch")) { |
| 58 | const ctx = await esbuild.context(esbuildConfig); |
| 59 | await ctx.watch(); |
| 60 | } else if (flags.includes("--notify")) { |
| 61 | const inFile = esbuildConfig.entryPoints[0]; |
| 62 | const outFile = esbuildConfig.outfile; |
| 63 | |
| 64 | // The watcher automatically notices changes to source files |
| 65 | // so the only thing it needs to be notified about is if the |
| 66 | // output file gets removed. |
| 67 | if (fs.existsSync(outFile)) { |
| 68 | console.log("VS Code Extension esbuild up to date"); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | fs.watchFile(outFile, (current, previous) => { |
| 73 | if (current.size > 0) { |
| 74 | console.log("VS Code Extension esbuild rebuild complete"); |
| 75 | fs.unwatchFile(outFile); |
| 76 | process.exit(0); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | console.log("Triggering VS Code Extension esbuild rebuild..."); |
| 81 | writeBuildTimestamp(); |
| 82 | } else { |
| 83 | await esbuild.build(esbuildConfig); |
| 84 | } |
| 85 | })(); |