Skip to content

Commit 7126c67

Browse files
committed
feat: support frok process
1 parent 1bb588e commit 7126c67

File tree

3 files changed

+72
-62
lines changed

3 files changed

+72
-62
lines changed

packages/plugin-dts/modern.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineConfig({
1515
autoExtension: true,
1616
externals,
1717
dts: false,
18+
shims: true,
1819
define,
1920
},
2021
{
@@ -24,6 +25,7 @@ export default defineConfig({
2425
autoExtension: true,
2526
externals,
2627
dts: false,
28+
shims: true,
2729
define,
2830
},
2931
{

packages/plugin-dts/src/index.ts

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { fork } from 'node:child_process';
12
import { basename, dirname, join, relative } from 'node:path';
2-
import type { RsbuildPlugin } from '@rsbuild/core';
3+
import { type RsbuildPlugin, logger } from '@rsbuild/core';
34
import { bundleDts } from './apiExtractor';
45
import { emitDts } from './tsc';
56
import { ensureTempDeclarationDir, loadTsconfig } from './utils';
@@ -24,78 +25,86 @@ export const pluginDts = (
2425
name: PLUGIN_DTS_NAME,
2526

2627
setup(api) {
27-
const { tsconfigPath, distPath, bundle, isWatch } = options;
28-
let isFirstCompile = true;
29-
3028
api.onBeforeBuild(async ({ environments }) => {
31-
const cwd = process.cwd();
32-
const configPath = tsconfigPath
33-
? join(cwd, tsconfigPath)
34-
: join(cwd, 'tsconfig.json');
35-
const { options: rawCompilerOptions } = loadTsconfig(configPath);
36-
const rootDir = rawCompilerOptions.rootDir ?? 'src';
37-
const outDir = distPath
38-
? distPath
39-
: rawCompilerOptions.declarationDir || './dist';
29+
const childProcess = fork(__filename, [], {
30+
stdio: 'inherit',
31+
});
4032

41-
const getDeclarationDir = (bundle: boolean, distPath?: string) => {
42-
if (bundle) {
43-
return ensureTempDeclarationDir();
44-
}
45-
return distPath ? distPath : rawCompilerOptions.declarationDir;
46-
};
33+
childProcess.send({ options, environments });
34+
});
35+
},
36+
});
4737

48-
const declarationDir = getDeclarationDir(bundle, distPath) || './dist';
49-
let entry = '';
38+
if (process.send) {
39+
process.on('message', async (data) => {
40+
logger.info('Generating DTS...');
41+
const { options: pluginOptions, environments } = data as any;
42+
const cwd = process.cwd();
43+
const configPath = pluginOptions.tsconfigPath
44+
? join(cwd, pluginOptions.tsconfigPath)
45+
: join(cwd, 'tsconfig.json');
46+
const { options: rawCompilerOptions } = loadTsconfig(configPath);
47+
const rootDir = rawCompilerOptions.rootDir ?? 'src';
48+
const outDir = pluginOptions.distPath
49+
? pluginOptions.distPath
50+
: rawCompilerOptions.declarationDir || './dist';
5051

51-
if (options.bundle === true) {
52-
const entrySourcePath = join(
53-
cwd,
54-
environments.esm?.config.source.entry?.main as string,
55-
);
56-
const relativePath = relative(rootDir, dirname(entrySourcePath));
57-
entry = join(
58-
declarationDir!,
59-
relativePath,
60-
basename(entrySourcePath),
61-
).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts');
52+
const getDeclarationDir = (bundle: boolean, distPath?: string) => {
53+
if (bundle) {
54+
return ensureTempDeclarationDir();
6255
}
56+
return distPath ? distPath : rawCompilerOptions.declarationDir;
57+
};
6358

64-
const onComplete = (isSuccess: boolean) => {
65-
if (isSuccess && options.bundle === true) {
66-
bundleDts({
67-
cwd,
68-
outDir,
69-
entry,
70-
tsconfigPath,
71-
});
72-
}
73-
};
59+
const declarationDir =
60+
getDeclarationDir(pluginOptions.bundle, pluginOptions.distPath) ||
61+
'./dist';
62+
let entry = '';
7463

75-
emitDts(
76-
{
77-
cwd,
78-
configPath,
79-
rootDir,
80-
declarationDir,
81-
},
82-
isFirstCompile,
83-
onComplete,
84-
isWatch,
64+
if (pluginOptions.bundle === true) {
65+
const entrySourcePath = join(
66+
cwd,
67+
environments.esm?.config.source.entry?.main as string,
8568
);
69+
const relativePath = relative(rootDir, dirname(entrySourcePath));
70+
entry = join(
71+
declarationDir!,
72+
relativePath,
73+
basename(entrySourcePath),
74+
).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts');
75+
}
8676

87-
if (options.bundle === true && !isWatch) {
77+
const onComplete = (isSuccess: boolean) => {
78+
if (isSuccess && pluginOptions.bundle === true) {
8879
bundleDts({
8980
cwd,
9081
outDir,
9182
entry,
92-
tsconfigPath,
83+
tsconfigPath: pluginOptions.tsconfigPath,
9384
});
9485
}
86+
};
9587

96-
if (isFirstCompile) {
97-
isFirstCompile = false;
98-
}
99-
});
100-
},
101-
});
88+
emitDts(
89+
{
90+
cwd,
91+
configPath,
92+
rootDir,
93+
declarationDir,
94+
},
95+
onComplete,
96+
pluginOptions.isWatch,
97+
);
98+
99+
if (pluginOptions.bundle === true && !pluginOptions.isWatch) {
100+
bundleDts({
101+
cwd,
102+
outDir,
103+
entry,
104+
tsconfigPath: pluginOptions.tsconfigPath,
105+
});
106+
}
107+
108+
// process.exit();
109+
});
110+
}

packages/plugin-dts/src/tsc.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export type emitDtsOptions = {
1111

1212
export function emitDts(
1313
options: emitDtsOptions,
14-
isFirstCompile: boolean,
1514
onComplete: (isSuccess: boolean) => void,
1615
isWatch = false,
1716
) {
@@ -73,7 +72,7 @@ export function emitDts(
7372
}
7473

7574
logger.info('TypeScript compilation succeeded\n');
76-
} else if (isFirstCompile) {
75+
} else {
7776
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
7877
const formatHost: ts.FormatDiagnosticsHost = {
7978
getCanonicalFileName: (path) => path,

0 commit comments

Comments
 (0)