Skip to content

Commit 1333f3c

Browse files
author
John Doe
committed
refactor: adjust tsd cfg 9
1 parent 4401713 commit 1333f3c

File tree

2 files changed

+90
-23
lines changed

2 files changed

+90
-23
lines changed
Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,70 @@
1+
import { copyFile, readFile, writeFile } from 'node:fs/promises';
2+
import { join } from 'node:path';
13
import { defineConfig } from 'tsdown';
24
import { baseConfig, getExternalDependencies } from '../../tsdown.base';
35

46
const __dirname = import.meta.dirname;
57

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+
618
export default defineConfig({
7-
...baseConfig({ projectRoot: __dirname }),
19+
...base,
820
format: ['cjs'], // NX supports only commonjs
921
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+
},
2870
});

tsdown.base.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1+
import { readFileSync } from 'node:fs';
12
import { readFile, writeFile } from 'node:fs/promises';
23
import { join } from 'node:path';
34
import { type UserConfig, defineConfig } from 'tsdown';
45

5-
export function baseConfig(options: {
6-
projectRoot: string;
7-
projectName?: string;
8-
}): UserConfig {
6+
export function baseConfig(options: { projectRoot: string }): UserConfig {
97
const { projectRoot } = options;
108

9+
// Read package.json to get additional files to copy
10+
const packageJsonPath = join(projectRoot, 'package.json');
11+
let additionalCopyFiles: Array<{ from: string; to: string }> = [];
12+
13+
try {
14+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
15+
16+
if (packageJson.files && Array.isArray(packageJson.files)) {
17+
additionalCopyFiles = packageJson.files
18+
.filter((file: string) => {
19+
// Skip negation patterns (starting with !)
20+
if (file.startsWith('!')) return false;
21+
// Skip 'src' as it's already handled by the build output
22+
if (file === 'src') return false;
23+
// Include files that start with ./ or are specific files
24+
return file.startsWith('./') || !file.includes('/');
25+
})
26+
.map((file: string) => ({
27+
from: join(projectRoot, file),
28+
to: join(projectRoot, 'dist', file),
29+
}));
30+
}
31+
} catch (error) {
32+
// If package.json doesn't exist or can't be read, continue without additional files
33+
}
34+
1135
return defineConfig({
1236
entry: `${projectRoot}/src/**/!(*.test|*.mock).ts`,
1337
tsconfig: `${projectRoot}/tsconfig.lib.json`,
@@ -28,6 +52,7 @@ export function baseConfig(options: {
2852
from: `${projectRoot}/README.md`,
2953
to: `${projectRoot}/dist/README.md`,
3054
},
55+
...additionalCopyFiles,
3156
],
3257
async onSuccess() {
3358
const distPackageJsonPath = join(projectRoot, 'dist', 'package.json');

0 commit comments

Comments
 (0)