Skip to content

Commit

Permalink
feat: get tsconfigPath and entryPath from enviroment config
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Aug 6, 2024
1 parent cfcc89f commit 9c73f7d
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 64 deletions.
4 changes: 2 additions & 2 deletions e2e/cases/dts/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`dts when bundle: false > basic - bundleless dts 1`] = `
exports[`dts when bundle: false > basic 1`] = `
{
"./dist/esm/index.d.ts": "export * from './utils/numbers';
export * from './utils/strings';
Expand All @@ -20,7 +20,7 @@ export declare const str3 = "str3";
}
`;

exports[`dts when bundle: true > basic - bundle dts 1`] = `
exports[`dts when bundle: true > basic 1`] = `
{
"esm": "export declare const num1 = 1;
Expand Down
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions e2e/cases/dts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildAndGetResults } from '@e2e/helper';
import { describe, expect, test } from 'vitest';

describe('dts when bundle: false', () => {
test('basic - bundleless dts', async () => {
test('basic', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { files, contents } = await buildAndGetResults(
fixturePath,
Expand All @@ -16,22 +16,22 @@ describe('dts when bundle: false', () => {
expect(contents.esm).toMatchSnapshot();
});

test('dts false - bundleless dts', async () => {
test('dts false', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { files } = await buildAndGetResults(
fixturePath,
'rslib.false.config.ts',
'dtsFalse.config.ts',
'dts',
);

expect(files.esm).toBe(undefined);
});

test('distPath - bundleless dts', async () => {
test('distPath', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { files } = await buildAndGetResults(
fixturePath,
'rslib.distpath.config.ts',
'distPath.config.ts',
'dts',
);
expect(files.esm?.length).toBe(4);
Expand All @@ -40,7 +40,7 @@ describe('dts when bundle: false', () => {
});

describe('dts when bundle: true', () => {
test('basic - bundle dts', async () => {
test('basic', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles, entries } = await buildAndGetResults(
fixturePath,
Expand All @@ -52,22 +52,22 @@ describe('dts when bundle: true', () => {
expect(entries).toMatchSnapshot();
});

test('dts false - bundle dts', async () => {
test('dts false', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles } = await buildAndGetResults(
fixturePath,
'rslib.false.config.ts',
'dtsFalse.config.ts',
'dts',
);

expect(entryFiles.esm).toEqual(undefined);
});

test('distPath - bundle dts', async () => {
test('distPath', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles } = await buildAndGetResults(
fixturePath,
'rslib.distpath.config.ts',
'distPath.config.ts',
'dts',
);

Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ const composeBundleConfig = (bundle = true): RsbuildConfig => {

const composeDtsConfig = async (
libConfig: LibConfig,
entryConfig: RsbuildConfig,
): Promise<RsbuildConfig> => {
const { dts, bundle, output } = libConfig;

Expand All @@ -337,9 +336,6 @@ const composeDtsConfig = async (
pluginDts({
bundle: dts?.bundle ?? bundle,
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
tsconfigPath: dts?.tsconfigPath,
// TODO: temporarily use main as dts entry
entryPath: entryConfig.source?.entry?.main as string,
}),
],
};
Expand Down Expand Up @@ -408,8 +404,7 @@ async function composeLibRsbuildConfig(
config.bundle,
dirname(configPath),
);

const dtsConfig = await composeDtsConfig(config, entryConfig);
const dtsConfig = await composeDtsConfig(config);

return mergeRsbuildConfig(
formatConfig,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type Dts =
| {
bundle: boolean;
distPath?: string;
tsconfigPath?: string;
}
| false;

Expand Down
61 changes: 34 additions & 27 deletions packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,43 @@ export function bundleDts(options: BundleOptions): void {
entry = 'index.d.ts',
tsconfigPath = 'tsconfig.json',
} = options;
const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts');
const internalConfig = {
mainEntryPointFilePath: entry,
// TODO: use !externals
// bundledPackages: [],
dtsRollup: {
enabled: true,
untrimmedFilePath,
},
compiler: {
tsconfigFilePath: join(cwd, tsconfigPath),
},
projectFolder: cwd,
};
try {
const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts');
const internalConfig = {
mainEntryPointFilePath: entry,
// TODO: use !externals
// bundledPackages: [],
dtsRollup: {
enabled: true,
untrimmedFilePath,
},
compiler: {
tsconfigFilePath: tsconfigPath.includes(cwd)
? tsconfigPath
: join(cwd, tsconfigPath),
},
projectFolder: cwd,
};

const extractorConfig = ExtractorConfig.prepare({
configObject: internalConfig,
configObjectFullPath: undefined,
packageJsonFullPath: join(cwd, 'package.json'),
});
const extractorConfig = ExtractorConfig.prepare({
configObject: internalConfig,
configObjectFullPath: undefined,
packageJsonFullPath: join(cwd, 'package.json'),
});

const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
});
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
});

if (!extractorResult.succeeded) {
if (!extractorResult.succeeded) {
throw new Error('API Extractor rollup error');
}

logger.info(
`API Extractor writing package typings succeeded: ${untrimmedFilePath}`,
);
} catch (e) {
logger.error('API Extractor', e);
throw new Error('API Extractor rollup error');
}

logger.info(
`API Extractor writing package typings succeeded: ${untrimmedFilePath}`,
);
}
11 changes: 7 additions & 4 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { emitDts } from './tsc';
import { ensureTempDeclarationDir, loadTsconfig } from './utils';

export async function generateDts(data: DtsGenOptions): Promise<void> {
const { options: pluginOptions, cwd, isWatch, name } = data;
const { tsconfigPath, distPath, bundle, entryPath, cwd, isWatch, name } =
data;
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
const { tsconfigPath, distPath, bundle, entryPath } = pluginOptions;
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
if (!configPath) {
logger.error(`tsconfig.json not found in ${cwd}`);
Expand Down Expand Up @@ -38,7 +38,10 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
declarationDir!,
relativePath,
basename(entrySourcePath),
).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts');
).replace(
/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/,
'.d.ts',
);
}

const bundleDtsIfNeeded = async () => {
Expand Down Expand Up @@ -77,7 +80,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
}

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

Expand Down
29 changes: 15 additions & 14 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { fork } from 'node:child_process';
import { extname, join } from 'node:path';
import type { RsbuildPlugin } from '@rsbuild/core';

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

export type DtsGenOptions = {
export type DtsGenOptions = PluginDtsOptions & {
name: string;
options: pluginDtsOptions;
cwd: string;
isWatch: boolean;
entryPath?: string;
tsconfigPath?: string;
};

export const PLUGIN_DTS_NAME = 'rsbuild:dts';
Expand All @@ -23,16 +22,10 @@ export const PLUGIN_DTS_NAME = 'rsbuild:dts';
// TODO: support incremental build, to build one or more projects and their dependencies
// TODO: support autoExtension for dts files
// TODO: deal alias in dts
export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({
export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
name: PLUGIN_DTS_NAME,

setup(api) {
const config = api.getRsbuildConfig();

options.bundle = options.bundle ?? false;
options.distPath =
options.distPath ?? config.output?.distPath?.root ?? 'dist';

const dtsPromises: Promise<void>[] = [];

api.onBeforeEnvironmentCompile(
Expand All @@ -41,14 +34,22 @@ export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({
return;
}

const { config } = environment;

options.bundle = options.bundle ?? false;
options.distPath = options.distPath ?? config.output?.distPath?.root;

const jsExtension = extname(__filename);
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
stdio: 'inherit',
});

const dtsGenOptions = {
const dtsGenOptions: DtsGenOptions = {
...options,
// TODO: temporarily use main as dts entry, only accept single entry
entryPath: config.source?.entry?.main as string,
tsconfigPath: config.source.tsconfigPath,
name: environment.name,
options,
cwd: api.context.rootPath,
isWatch,
};
Expand Down

0 comments on commit 9c73f7d

Please sign in to comment.