|
| 1 | +import { copyFile, readFile, writeFile } from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
1 | 3 | import { defineConfig } from 'tsdown'; |
2 | 4 | import { baseConfig, getExternalDependencies } from '../../tsdown.base'; |
3 | 5 |
|
4 | 6 | const __dirname = import.meta.dirname; |
5 | 7 |
|
| 8 | +const base = baseConfig({ projectRoot: __dirname }); |
| 9 | + |
| 10 | +// Remove generators.json and executors.json from copy array |
| 11 | +// We'll handle them in onSuccess to update the .cjs references |
| 12 | +const copyWithoutJsonFiles = ((base as any).copy || []).filter( |
| 13 | + (item: any) => |
| 14 | + !item.to.endsWith('/generators.json') && |
| 15 | + !item.to.endsWith('/executors.json'), |
| 16 | +); |
| 17 | + |
6 | 18 | export default defineConfig({ |
7 | | - ...baseConfig({ projectRoot: __dirname }), |
| 19 | + ...base, |
8 | 20 | format: ['cjs'], // NX supports only commonjs |
9 | 21 | external: await getExternalDependencies(__dirname), |
10 | | - copy: [ |
11 | | - { |
12 | | - from: `${__dirname}/package.json`, |
13 | | - to: `${__dirname}/dist/package.json`, |
14 | | - }, |
15 | | - { |
16 | | - from: `${__dirname}/README.md`, |
17 | | - to: `${__dirname}/dist/README.md`, |
18 | | - }, |
19 | | - { |
20 | | - from: `${__dirname}/generators.json`, |
21 | | - to: `${__dirname}/dist/generators.json`, |
22 | | - }, |
23 | | - { |
24 | | - from: `${__dirname}/executors.json`, |
25 | | - to: `${__dirname}/dist/executors.json`, |
26 | | - }, |
27 | | - ], |
| 22 | + copy: copyWithoutJsonFiles, |
| 23 | + async onSuccess() { |
| 24 | + // Call base onSuccess first |
| 25 | + const baseOnSuccess = (base as any).onSuccess; |
| 26 | + if (typeof baseOnSuccess === 'function') { |
| 27 | + await baseOnSuccess(); |
| 28 | + } |
| 29 | + |
| 30 | + // Copy and update generators.json |
| 31 | + const generatorsJsonPath = join(__dirname, 'dist', 'generators.json'); |
| 32 | + await copyFile(join(__dirname, 'generators.json'), generatorsJsonPath); |
| 33 | + |
| 34 | + const generatorsJson = JSON.parse( |
| 35 | + await readFile(generatorsJsonPath, 'utf8'), |
| 36 | + ); |
| 37 | + |
| 38 | + for (const generator of Object.values(generatorsJson.generators)) { |
| 39 | + const gen = generator as { factory: string }; |
| 40 | + if (gen.factory && !gen.factory.endsWith('.cjs')) { |
| 41 | + gen.factory = gen.factory.replace(/\.js$/, '') + '.cjs'; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + await writeFile( |
| 46 | + generatorsJsonPath, |
| 47 | + JSON.stringify(generatorsJson, null, 2) + '\n', |
| 48 | + 'utf8', |
| 49 | + ); |
| 50 | + |
| 51 | + // Copy and update executors.json |
| 52 | + const executorsJsonPath = join(__dirname, 'dist', 'executors.json'); |
| 53 | + await copyFile(join(__dirname, 'executors.json'), executorsJsonPath); |
| 54 | + |
| 55 | + const executorsJson = JSON.parse(await readFile(executorsJsonPath, 'utf8')); |
| 56 | + |
| 57 | + for (const executor of Object.values(executorsJson.executors)) { |
| 58 | + const exec = executor as { implementation: string }; |
| 59 | + if (exec.implementation && !exec.implementation.endsWith('.cjs')) { |
| 60 | + exec.implementation = exec.implementation.replace(/\.js$/, '') + '.cjs'; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + await writeFile( |
| 65 | + executorsJsonPath, |
| 66 | + JSON.stringify(executorsJson, null, 2) + '\n', |
| 67 | + 'utf8', |
| 68 | + ); |
| 69 | + }, |
28 | 70 | }); |
0 commit comments