|
| 1 | +import { defineConfig, Options } from 'tsup'; |
| 2 | +import fs, { readFile, writeFile } from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import packageJson from './package.json'; |
| 5 | + |
| 6 | +const opts: Options = { |
| 7 | + entry: ['src/**/*.ts'], |
| 8 | + clean: true, |
| 9 | + bundle: false, |
| 10 | + dts: true, |
| 11 | + env: { |
| 12 | + ...(process.env.NODE_ENV && { NODE_ENV: process.env.NODE_ENV }), |
| 13 | + }, |
| 14 | +}; |
| 15 | + |
| 16 | +const CWD = process.cwd(); |
| 17 | +export default defineConfig([ |
| 18 | + { |
| 19 | + ...opts, |
| 20 | + format: 'esm', |
| 21 | + outDir: 'dist/esm', |
| 22 | + outExtension: () => ({ js: '.js' }), |
| 23 | + async onSuccess() { |
| 24 | + await fs.copyFile( |
| 25 | + path.join(CWD, '..', '..', 'README.md'), |
| 26 | + path.join(CWD, 'dist', 'README.md'), |
| 27 | + ); |
| 28 | + await fs.writeFile(path.join(CWD, 'dist', 'esm', 'package.json'), '{"type": "module"}'); |
| 29 | + await fs.writeFile( |
| 30 | + path.join(CWD, 'dist', 'package.json'), |
| 31 | + JSON.stringify({ ...packageJson, devDependencies: undefined }).replaceAll('dist/', ''), |
| 32 | + ); |
| 33 | + |
| 34 | + const filePaths = [ |
| 35 | + 'estree-converter/utils.js', |
| 36 | + 'rules/graphql-js-validation.js', |
| 37 | + 'testkit.js', |
| 38 | + ]; |
| 39 | + await Promise.all( |
| 40 | + filePaths.map(async filePath => { |
| 41 | + const fullPath = path.join(CWD, 'dist', 'esm', filePath); |
| 42 | + const content = await readFile(fullPath, 'utf8'); |
| 43 | + await writeFile( |
| 44 | + fullPath, |
| 45 | + ` |
| 46 | +import { createRequire } from 'module'; |
| 47 | +const require = createRequire(import.meta.url); |
| 48 | +${content}`.trimStart(), |
| 49 | + ); |
| 50 | + }), |
| 51 | + ); |
| 52 | + |
| 53 | + console.log('✅ Success!'); |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + ...opts, |
| 58 | + format: 'cjs', |
| 59 | + outDir: 'dist/cjs', |
| 60 | + }, |
| 61 | +]); |
0 commit comments