|
| 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 | +/** |
| 9 | + * @type {import('esbuild').Plugin} |
| 10 | + */ |
| 11 | +const esbuildProblemMatcherPlugin = { |
| 12 | + name: "esbuild-problem-matcher", |
| 13 | + |
| 14 | + setup(build) { |
| 15 | + build.onStart(() => { |
| 16 | + console.log("[watch] build started"); |
| 17 | + }); |
| 18 | + build.onEnd((result) => { |
| 19 | + result.errors.forEach(({ text, location }) => { |
| 20 | + console.error(`✘ [ERROR] ${text}`); |
| 21 | + console.error(` ${location.file}:${location.line}:${location.column}:`); |
| 22 | + }); |
| 23 | + console.log("[watch] build finished"); |
| 24 | + }); |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Helper function to find the actual file with proper extension |
| 30 | + */ |
| 31 | +function findFileWithExtension(basePath) { |
| 32 | + const extensions = [".ts", ".tsx", ".js", ".jsx", ".json"]; |
| 33 | + |
| 34 | + // If basePath is a file with extension, return it |
| 35 | + if (fs.existsSync(basePath) && fs.statSync(basePath).isFile()) { |
| 36 | + return basePath; |
| 37 | + } |
| 38 | + |
| 39 | + for (const ext of extensions) { |
| 40 | + const fullPath = basePath + ext; |
| 41 | + if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) { |
| 42 | + return fullPath; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (fs.existsSync(basePath) && fs.statSync(basePath).isDirectory()) { |
| 47 | + for (const ext of extensions) { |
| 48 | + const indexPath = path.join(basePath, `index${ext}`); |
| 49 | + if (fs.existsSync(indexPath) && fs.statSync(indexPath).isFile()) { |
| 50 | + return indexPath; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return basePath; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Plugin to resolve TypeScript path aliases |
| 60 | + * @type {import('esbuild').Plugin} |
| 61 | + */ |
| 62 | +const pathAliasPlugin = { |
| 63 | + name: "path-alias", |
| 64 | + setup(build) { |
| 65 | + /* eslint-disable @typescript-eslint/naming-convention */ |
| 66 | + const aliases = { |
| 67 | + "@ak-proto-ts": "src/autokitteh/proto/gen/ts/autokitteh", |
| 68 | + "@eventEmitter": "src/eventEmitter", |
| 69 | + "@api": "src/api", |
| 70 | + "@constants": "src/constants", |
| 71 | + "@controllers": "src/controllers", |
| 72 | + "@enums": "src/enums", |
| 73 | + "@models": "src/models", |
| 74 | + "@i18n": "src/i18n", |
| 75 | + "@interfaces": "src/interfaces", |
| 76 | + "@providers": "src/providers", |
| 77 | + "@services": "src/services", |
| 78 | + "@type": "src/types", |
| 79 | + "@utilities": "src/utilities", |
| 80 | + "@views": "src/views", |
| 81 | + "@vscommands": "src/vscommands", |
| 82 | + "@tests": "tests", |
| 83 | + }; |
| 84 | + /* eslint-enable @typescript-eslint/naming-convention */ |
| 85 | + |
| 86 | + for (const [alias, target] of Object.entries(aliases)) { |
| 87 | + build.onResolve({ filter: new RegExp(`^${alias.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`) }, () => { |
| 88 | + const basePath = path.resolve(__dirname, target); |
| 89 | + const resolvedPath = findFileWithExtension(basePath); |
| 90 | + return { path: resolvedPath }; |
| 91 | + }); |
| 92 | + |
| 93 | + build.onResolve({ filter: new RegExp(`^${alias.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/`) }, (args) => { |
| 94 | + const subPath = args.path.substring(alias.length + 1); |
| 95 | + const basePath = path.resolve(__dirname, target, subPath); |
| 96 | + const resolvedPath = findFileWithExtension(basePath); |
| 97 | + return { path: resolvedPath }; |
| 98 | + }); |
| 99 | + } |
| 100 | + }, |
| 101 | +}; |
| 102 | + |
| 103 | +async function main() { |
| 104 | + const ctx = await esbuild.context({ |
| 105 | + entryPoints: ["src/extension.ts"], |
| 106 | + bundle: true, |
| 107 | + format: "cjs", |
| 108 | + minify: production, |
| 109 | + sourcemap: !production, |
| 110 | + sourcesContent: false, |
| 111 | + platform: "node", |
| 112 | + outfile: "dist/main.js", |
| 113 | + external: ["vscode", "fswin"], |
| 114 | + logLevel: "silent", |
| 115 | + plugins: [pathAliasPlugin, esbuildProblemMatcherPlugin], |
| 116 | + }); |
| 117 | + |
| 118 | + if (watch) { |
| 119 | + await ctx.watch(); |
| 120 | + } else { |
| 121 | + await ctx.rebuild(); |
| 122 | + await ctx.dispose(); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +main().catch((e) => { |
| 127 | + console.error(e); |
| 128 | + process.exit(1); |
| 129 | +}); |
0 commit comments