Skip to content

Commit

Permalink
feat: use fork to run
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Jul 31, 2024
1 parent c1ea1b6 commit 7b7f591
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 88 deletions.
3 changes: 0 additions & 3 deletions e2e/cases/dts/bundle-false/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ export default defineConfig({
}),
generateBundleCjsConfig(__dirname, {
bundle: false,
dts: {
bundle: false,
},
}),
],
source: {
Expand Down
6 changes: 1 addition & 5 deletions e2e/cases/dts/bundle/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ export default defineConfig({
bundle: true,
},
}),
generateBundleCjsConfig(__dirname, {
dts: {
bundle: true,
},
}),
generateBundleCjsConfig(__dirname),
],
source: {
entry: {
Expand Down
3 changes: 0 additions & 3 deletions e2e/cases/dts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ test('dts when bundle: false', async () => {
const { files } = await buildAndGetResults(fixturePath, 'dts');

expect(files.esm?.length).toBe(4);

Check failure on line 9 in e2e/cases/dts/index.test.ts

View workflow job for this annotation

GitHub Actions / e2e-ubuntu (18.x)

e2e/cases/dts/index.test.ts > dts when bundle: false

AssertionError: expected undefined to be 4 // Object.is equality - Expected: 4 + Received: undefined ❯ e2e/cases/dts/index.test.ts:9:29
expect(files.cjs?.length).toBe(4);
expect(files.esm?.[0]!.endsWith('.d.ts')).toEqual(true);
expect(files.cjs?.[0]!.endsWith('.d.ts')).toEqual(true);
});

test('dts when bundle: true', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles } = await buildAndGetResults(fixturePath, 'dts');

expect(entryFiles.esm!.endsWith('index.d.ts')).toEqual(true);

Check failure on line 17 in e2e/cases/dts/index.test.ts

View workflow job for this annotation

GitHub Actions / e2e-ubuntu (18.x)

e2e/cases/dts/index.test.ts > dts when bundle: true

TypeError: Cannot read properties of undefined (reading 'endsWith') ❯ e2e/cases/dts/index.test.ts:17:26
expect(entryFiles.cjs!.endsWith('index.d.ts')).toEqual(true);
});
21 changes: 16 additions & 5 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ const getBundleConfig = (bundle = true): RsbuildConfig => {

const getDefaultDtsConfig = (
libConfig: LibConfig,
entryConfig: RsbuildConfig,
isWatch = false,
): RsbuildConfig => {
const { dts, bundle, output } = libConfig;
Expand All @@ -309,6 +310,8 @@ const getDefaultDtsConfig = (
bundle: dts?.bundle ?? bundle,
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
tsconfigPath: dts?.tsconfigPath ?? 'tsconfig.json',
// TODO: temporarily use main as dts entry
entryPath: entryConfig.source?.entry?.main as string,
isWatch,
}),
],
Expand All @@ -318,7 +321,6 @@ const getDefaultDtsConfig = (
export function convertLibConfigToRsbuildConfig(
libConfig: LibConfig,
configPath: string,
options?: BuildOptions,
): RsbuildConfig {
const { format, autoExtension = false } = libConfig;

Expand All @@ -330,21 +332,20 @@ export function convertLibConfigToRsbuildConfig(
);
const syntaxConfig = getDefaultSyntaxConfig(libConfig.output?.syntax);
const bundleConfig = getBundleConfig(libConfig.bundle);
const dtsConfig = getDefaultDtsConfig(libConfig, options?.watch);

return mergeRsbuildConfig(
formatConfig,
autoExtensionConfig,
syntaxConfig,
bundleConfig,
dtsConfig,
);
}

async function postUpdateRsbuildConfig(
libConfig: LibConfig,
rsbuildConfig: RsbuildConfig,
configPath: string,
options?: BuildOptions,
) {
const defaultTargetConfig = getDefaultTargetConfig(
rsbuildConfig.output?.target ?? 'web',
Expand All @@ -356,7 +357,17 @@ async function postUpdateRsbuildConfig(
dirname(configPath),
);

return mergeRsbuildConfig(defaultTargetConfig, defaultEntryConfig);
const defaultDtsConfig = getDefaultDtsConfig(
libConfig,
defaultEntryConfig,
options?.watch,
);

return mergeRsbuildConfig(
defaultTargetConfig,
defaultEntryConfig,
defaultDtsConfig,
);
}

const getDefaultTargetConfig = (target: string): RsbuildConfig => {
Expand Down Expand Up @@ -402,7 +413,6 @@ export async function composeCreateRsbuildConfig(
const libConvertedRsbuildConfig = convertLibConfigToRsbuildConfig(
libConfig,
configPath,
options,
);

const mergedRsbuildConfig = mergeRsbuildConfig(
Expand All @@ -417,6 +427,7 @@ export async function composeCreateRsbuildConfig(
libConfig,
mergedRsbuildConfig,
configPath,
options,
);

// Reset some fields as they will be totally overridden by the following merge
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-dts/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ export default defineConfig({
{
format: 'cjs',
target: 'es2020',
buildType: 'bundle',
buildType: 'bundleless',
autoExtension: true,
externals,
dts: false,
shims: true,
define,
},
{
format: 'esm',
target: 'es2020',
buildType: 'bundle',
buildType: 'bundleless',
autoExtension: true,
externals,
dts: false,
shims: true,
define,
},
{
Expand Down
85 changes: 85 additions & 0 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { basename, dirname, join, relative } from 'node:path';
import { logger } from '@rsbuild/core';
import type { sendData } from 'src';
import { emitDts } from './tsc';
import { ensureTempDeclarationDir, loadTsconfig } from './utils';

export async function generateDts(data: sendData) {
logger.info('Generating DTS...');
const { options: pluginOptions } = data;
const { tsconfigPath, distPath, bundle, entryPath, isWatch } = pluginOptions;
const cwd = process.cwd();
const configPath = tsconfigPath
? join(cwd, tsconfigPath)
: join(cwd, 'tsconfig.json');
const { options: rawCompilerOptions } = loadTsconfig(configPath);
const rootDir = rawCompilerOptions.rootDir ?? 'src';
const outDir = distPath
? distPath
: rawCompilerOptions.declarationDir || './dist';

const getDeclarationDir = (bundle: boolean, distPath?: string) => {
if (bundle) {
return ensureTempDeclarationDir();
}
return distPath ? distPath : rawCompilerOptions.declarationDir;
};

const declarationDir = getDeclarationDir(bundle, distPath) || './dist';
let entry = '';

if (bundle === true && entryPath) {
const entrySourcePath = join(cwd, entryPath);
const relativePath = relative(rootDir, dirname(entrySourcePath));
entry = join(
declarationDir!,
relativePath,
basename(entrySourcePath),
).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts');
}

const onComplete = async (isSuccess: boolean) => {
if (isSuccess && bundle === true) {
const { bundleDts } = await import('./apiExtractor');
bundleDts({
cwd,
outDir,
entry,
tsconfigPath,
});
}
};

emitDts(
{
cwd,
configPath,
rootDir,
declarationDir,
},
onComplete,
isWatch,
);

if (bundle === true && !isWatch) {
const { bundleDts } = await import('./apiExtractor');
bundleDts({
cwd,
outDir,
entry,
tsconfigPath,
});
}
}

process.on('message', async (data: sendData) => {
if (!data.options) {
return;
}

await generateDts(data);

if (!data.options.isWatch) {
process.exit();
}
});
85 changes: 15 additions & 70 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { basename, dirname, join, relative } from 'node:path';
import { fork } from 'node:child_process';
import { join } from 'node:path';
import type { RsbuildPlugin } from '@rsbuild/core';
import { bundleDts } from './apiExtractor';
import { emitDts } from './tsc';
import { ensureTempDeclarationDir, loadTsconfig } from './utils';

export type pluginDtsOptions = {
bundle: boolean;
distPath?: string;
tsconfigPath?: string;
entryPath?: string;
isWatch?: boolean;
};

export type sendData = {
options: pluginDtsOptions;
};

export const PLUGIN_DTS_NAME = 'rsbuild:dts';

// use ts compiler API to generate bundleless dts
Expand All @@ -23,73 +26,15 @@ export const pluginDts = (
): RsbuildPlugin => ({
name: PLUGIN_DTS_NAME,

setup(api) {
const { tsconfigPath, distPath, bundle, isWatch } = options;

api.onBeforeBuild(async ({ environments }) => {
const cwd = process.cwd();
const configPath = tsconfigPath
? join(cwd, tsconfigPath)
: join(cwd, 'tsconfig.json');
const { options: rawCompilerOptions } = loadTsconfig(configPath);
const rootDir = rawCompilerOptions.rootDir ?? 'src';
const outDir = distPath
? distPath
: rawCompilerOptions.declarationDir || './dist';

const getDeclarationDir = (bundle: boolean, distPath?: string) => {
if (bundle) {
return ensureTempDeclarationDir();
}
return distPath ? distPath : rawCompilerOptions.declarationDir;
};

const declarationDir = getDeclarationDir(bundle, distPath) || './dist';
let entry = '';

if (bundle === true) {
const entrySourcePath = join(
cwd,
environments.esm?.config.source.entry?.main as string,
);
const relativePath = relative(rootDir, dirname(entrySourcePath));
entry = join(
declarationDir!,
relativePath,
basename(entrySourcePath),
).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts');
}

const onComplete = (isSuccess: boolean) => {
if (isSuccess && bundle === true) {
bundleDts({
cwd,
outDir,
entry,
tsconfigPath,
});
}
};
setup() {
const childProcess = fork(join(__dirname, './dts.js'), [], {
stdio: 'inherit',
});

emitDts(
{
cwd,
configPath,
rootDir,
declarationDir,
},
onComplete,
isWatch,
);
const sendData = {
options,
};

if (bundle === true && !isWatch) {
bundleDts({
cwd,
outDir,
entry,
tsconfigPath,
});
}
});
childProcess.send(sendData);
},
});

0 comments on commit 7b7f591

Please sign in to comment.