|
| 1 | +import chalk from "chalk"; |
| 2 | +import { analyzeMetafile, build } from "esbuild"; |
| 3 | +import { readFile } from "fs/promises"; |
| 4 | +import path from "path"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | + |
| 7 | +const nodeTargets = ["node14", "node16", "node18"]; |
| 8 | + |
| 9 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 10 | +const packageJsonPath = path.resolve(__dirname, "package.json"); |
| 11 | + |
| 12 | +async function getExternals() { |
| 13 | + const packageJson = JSON.parse(await readFile(packageJsonPath)); |
| 14 | + |
| 15 | + return Object.keys(packageJson.peerDependencies); |
| 16 | +} |
| 17 | + |
| 18 | +(async () => { |
| 19 | + try { |
| 20 | + const startTime = Date.now(); |
| 21 | + console.info( |
| 22 | + chalk.bold( |
| 23 | + `🚀 ${chalk.blueBright("cosmiconfig-typescript-loader")} Build\n` |
| 24 | + ) |
| 25 | + ); |
| 26 | + |
| 27 | + const externalDeps = await getExternals(); |
| 28 | + |
| 29 | + const result = await build({ |
| 30 | + entryPoints: ["./lib/index.ts"], |
| 31 | + outfile: "dist/cjs/index.js", |
| 32 | + metafile: true, |
| 33 | + bundle: true, |
| 34 | + format: "cjs", |
| 35 | + external: externalDeps, |
| 36 | + platform: "node", |
| 37 | + target: nodeTargets, |
| 38 | + treeShaking: true, |
| 39 | + }); |
| 40 | + |
| 41 | + const analysis = await analyzeMetafile(result.metafile); |
| 42 | + console.info(`📝 Bundle Analysis:${analysis}`); |
| 43 | + |
| 44 | + console.info( |
| 45 | + `${chalk.bold.green("✔ Bundled successfully!")} (${ |
| 46 | + Date.now() - startTime |
| 47 | + }ms)` |
| 48 | + ); |
| 49 | + } catch (error) { |
| 50 | + console.error(`🧨 ${chalk.red.bold("Failed:")} ${error.message}`); |
| 51 | + console.debug(`📚 ${chalk.blueBright.bold("Stack:")} ${error.stack}`); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | +})(); |
0 commit comments