|
| 1 | +import path from 'path'; |
| 2 | +import { fileURLToPath } from 'url'; |
| 3 | +import ts from "typescript"; |
| 4 | +import esbuild from "esbuild"; |
| 5 | + |
| 6 | +// we need to change up how __dirname is used for ES6 purposes |
| 7 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | + |
| 9 | +async function main() { |
| 10 | + // Absolute path to package directory |
| 11 | + const basePath = __dirname; |
| 12 | + const target = "es2017"; |
| 13 | + |
| 14 | + // Get all .ts as input files |
| 15 | + const entryPoints = [ |
| 16 | + path.resolve(basePath, "src", "index.ts"). |
| 17 | + replace(/\\/g, '/') // windows support |
| 18 | + ]; |
| 19 | + |
| 20 | + const outdir = path.join(basePath, 'build'); |
| 21 | + |
| 22 | + // Emit only .d.ts files |
| 23 | + const emitTSDeclaration = () => { |
| 24 | + console.log("Generating .d.ts..."); |
| 25 | + const program = ts.createProgram(entryPoints, { |
| 26 | + declaration: true, |
| 27 | + emitDeclarationOnly: true, |
| 28 | + skipLibCheck: true, |
| 29 | + module: "commonjs", |
| 30 | + target, |
| 31 | + outDir: outdir, |
| 32 | + esModuleInterop: true, |
| 33 | + experimentalDecorators: true, |
| 34 | + }); |
| 35 | + const emitResult = program.emit(); |
| 36 | + |
| 37 | + const allDiagnostics = ts |
| 38 | + .getPreEmitDiagnostics(program) |
| 39 | + .concat(emitResult.diagnostics); |
| 40 | + |
| 41 | + allDiagnostics.forEach(diagnostic => { |
| 42 | + if (diagnostic.file) { |
| 43 | + const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); |
| 44 | + const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); |
| 45 | + console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); |
| 46 | + } else { |
| 47 | + console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + // CommonJS output |
| 53 | + console.log("Generating CJS build..."); |
| 54 | + esbuild.build({ |
| 55 | + entryPoints, |
| 56 | + outdir, |
| 57 | + target, |
| 58 | + format: "cjs", |
| 59 | + target: "es2017", |
| 60 | + sourcemap: "external", |
| 61 | + platform: "node", |
| 62 | + }); |
| 63 | + |
| 64 | + // ESM output |
| 65 | + console.log("Generating ESM build..."); |
| 66 | + esbuild.build({ |
| 67 | + entryPoints, |
| 68 | + outdir, |
| 69 | + target, |
| 70 | + format: "esm", |
| 71 | + bundle: true, |
| 72 | + sourcemap: "external", |
| 73 | + platform: "node", |
| 74 | + outExtension: { '.js': '.mjs', }, |
| 75 | + plugins: [{ |
| 76 | + name: 'add-mjs', |
| 77 | + setup(build) { |
| 78 | + build.onResolve({ filter: /.*/ }, (args) => { |
| 79 | + if (args.importer) return { path: args.path.replace(/^\.(.*)\.js$/, '.$1.mjs'), external: true } |
| 80 | + }) |
| 81 | + }, |
| 82 | + }] |
| 83 | + }); |
| 84 | + |
| 85 | + // emit .d.ts files |
| 86 | + emitTSDeclaration(); |
| 87 | + console.log("Done!"); |
| 88 | +} |
| 89 | + |
| 90 | +export default await main(); |
| 91 | + |
0 commit comments