Skip to content

Commit

Permalink
feat: support DTS autoExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Aug 8, 2024
1 parent 12066db commit 29d3511
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 39 deletions.
2 changes: 1 addition & 1 deletion 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 1`] = `
exports[`dts when bundle: false > basic 2`] = `
{
"./dist/esm/index.d.ts": "export * from './utils/numbers';
export * from './utils/strings';
Expand Down
21 changes: 21 additions & 0 deletions e2e/cases/dts/bundle-false/autoExtension.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
bundle: false,
}),
generateBundleCjsConfig(__dirname, {
bundle: false,
dts: {
bundle: false,
},
}),
],
source: {
entry: {
main: ['./src/**'],
},
},
});
6 changes: 6 additions & 0 deletions e2e/cases/dts/bundle-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-bundle-false-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
18 changes: 18 additions & 0 deletions e2e/cases/dts/bundle/autoExtension.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname),
generateBundleCjsConfig(__dirname, {
dts: {
bundle: true,
},
}),
],
source: {
entry: {
main: './src/index.ts',
},
},
});
54 changes: 48 additions & 6 deletions e2e/cases/dts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ describe('dts when bundle: false', () => {
'dts',
);

expect(files.esm?.length).toBe(4);
expect(files.esm?.[0]!.endsWith('.d.ts')).toEqual(true);
expect(files.esm).toMatchInlineSnapshot(`
[
"./dist/esm/index.d.ts",
"./dist/esm/sum.d.ts",
"./dist/esm/utils/numbers.d.ts",
"./dist/esm/utils/strings.d.ts",
]
`);
expect(contents.esm).toMatchSnapshot();
});

Expand All @@ -34,8 +40,15 @@ describe('dts when bundle: false', () => {
'distPath.config.ts',
'dts',
);
expect(files.esm?.length).toBe(4);
expect(files.esm?.[0]!.startsWith('./dist/custom')).toEqual(true);

expect(files.esm).toMatchInlineSnapshot(`
[
"./dist/custom/index.d.ts",
"./dist/custom/sum.d.ts",
"./dist/custom/utils/numbers.d.ts",
"./dist/custom/utils/strings.d.ts",
]
`);
});

test('abortOnError: false', async () => {
Expand All @@ -48,6 +61,24 @@ describe('dts when bundle: false', () => {

expect(isSuccess).toBe(true);
});

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

expect(files.cjs).toMatchInlineSnapshot(`
[
"./dist/cjs/index.d.cts",
"./dist/cjs/sum.d.cts",
"./dist/cjs/utils/numbers.d.cts",
"./dist/cjs/utils/strings.d.cts",
]
`);
});
});

describe('dts when bundle: true', () => {
Expand All @@ -59,7 +90,7 @@ describe('dts when bundle: true', () => {
'dts',
);

expect(entryFiles.esm!.endsWith('index.d.ts')).toEqual(true);
expect(entryFiles.esm).toEqual('./dist/esm/index.d.ts');
expect(entries).toMatchSnapshot();
});

Expand All @@ -82,7 +113,7 @@ describe('dts when bundle: true', () => {
'dts',
);

expect(entryFiles.esm!.startsWith('./dist/custom')).toEqual(true);
expect(entryFiles.esm).toEqual('./dist/custom/index.d.ts');
});

test('abortOnError: false', async () => {
Expand All @@ -95,4 +126,15 @@ describe('dts when bundle: true', () => {

expect(isSuccess).toBe(true);
});

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

expect(entryFiles.cjs).toEqual('./dist/cjs/index.d.cts');
});
});
1 change: 0 additions & 1 deletion examples/express-plugin/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { defineConfig } from '@rslib/core';

const shared = {
autoExtension: true,
dts: {
bundle: false,
},
Expand Down
1 change: 0 additions & 1 deletion examples/react-component/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';

const shared = {
autoExtension: true,
dts: {
bundle: false,
},
Expand Down
27 changes: 16 additions & 11 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,25 @@ const composeAutoExtensionConfig = (
format: Format,
root: string,
autoExtension: boolean,
): RsbuildConfig => {
const { jsExtension } = getDefaultExtension({
): {
config: RsbuildConfig;
dtsExtension: string;
} => {
const { jsExtension, dtsExtension } = getDefaultExtension({
format,
root,
autoExtension,
});

return {
output: {
filename: {
js: `[name]${jsExtension}`,
config: {
output: {
filename: {
js: `[name]${jsExtension}`,
},
},
},
dtsExtension,
};
};

Expand Down Expand Up @@ -325,6 +331,7 @@ const composeBundleConfig = (bundle = true): RsbuildConfig => {

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

Expand All @@ -337,6 +344,7 @@ const composeDtsConfig = async (
bundle: dts?.bundle ?? bundle,
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
abortOnError: dts?.abortOnError ?? true,
dtsExtension,
}),
],
};
Expand Down Expand Up @@ -389,11 +397,8 @@ async function composeLibRsbuildConfig(

const { format, autoExtension = true } = config;
const formatConfig = composeFormatConfig(format!);
const autoExtensionConfig = composeAutoExtensionConfig(
format!,
dirname(configPath),
autoExtension,
);
const { config: autoExtensionConfig, dtsExtension } =
composeAutoExtensionConfig(format!, dirname(configPath), autoExtension);
const bundleConfig = composeBundleConfig(config.bundle);
const targetConfig = composeTargetConfig(config.output?.target);
const syntaxConfig = composeSyntaxConfig(
Expand All @@ -405,7 +410,7 @@ async function composeLibRsbuildConfig(
config.bundle,
dirname(configPath),
);
const dtsConfig = await composeDtsConfig(config);
const dtsConfig = await composeDtsConfig(config, dtsExtension);

return mergeRsbuildConfig(
formatConfig,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-dts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dev": "modern build --watch"
},
"dependencies": {
"fast-glob": "^3.3.2",
"picocolors": "1.0.1"
},
"devDependencies": {
Expand Down
19 changes: 15 additions & 4 deletions packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@ import {
type ExtractorResult,
} from '@microsoft/api-extractor';
import { logger } from '@rsbuild/core';
import color from 'picocolors';
import { getTimeCost } from './utils';

export type BundleOptions = {
name: string;
cwd: string;
outDir: string;
dtsExtension: string;
entry?: string;
tsconfigPath?: string;
};

export function bundleDts(options: BundleOptions): void {
export async function bundleDts(options: BundleOptions): Promise<void> {
const {
name,
cwd,
outDir,
dtsExtension,
entry = 'index.d.ts',
tsconfigPath = 'tsconfig.json',
} = options;
try {
const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts');
const start = Date.now();
const untrimmedFilePath = join(
cwd,
relative(cwd, outDir),
`index${dtsExtension}`,
);
const internalConfig = {
mainEntryPointFilePath: entry,
// TODO: use !externals
Expand Down Expand Up @@ -49,11 +60,11 @@ export function bundleDts(options: BundleOptions): void {
});

if (!extractorResult.succeeded) {
throw new Error('API Extractor rollup error');
throw new Error(`API Extractor error. ${color.gray(`(${name})`)}`);
}

logger.info(
`API Extractor writing package typings succeeded: ${untrimmedFilePath}`,
`API Extractor bundle DTS succeeded: ${color.cyan(untrimmedFilePath)} in ${getTimeCost(start)} ${color.gray(`(${name})`)}`,
);
} catch (e) {
logger.error('API Extractor', e);
Expand Down
20 changes: 16 additions & 4 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import { emitDts } from './tsc';
import { ensureTempDeclarationDir, loadTsconfig } from './utils';

export async function generateDts(data: DtsGenOptions): Promise<void> {
const { tsconfigPath, distPath, bundle, entryPath, cwd, isWatch, name } =
data;
const {
bundle,
distPath,
entryPath,
tsconfigPath,
name,
cwd,
isWatch,
dtsExtension = '.d.ts',
} = data;
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
if (!configPath) {
Expand Down Expand Up @@ -47,11 +55,13 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
const bundleDtsIfNeeded = async () => {
if (bundle === true) {
const { bundleDts } = await import('./apiExtractor');
bundleDts({
await bundleDts({
name,
cwd,
outDir,
entry,
tsconfigPath,
dtsExtension,
});
}
};
Expand All @@ -62,15 +72,17 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
}
};

emitDts(
await emitDts(
{
name,
cwd,
configPath,
rootDir,
declarationDir,
dtsExtension,
},
onComplete,
bundle,
isWatch,
);

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type PluginDtsOptions = {
bundle?: boolean;
distPath?: string;
abortOnError?: boolean;
dtsExtension?: string;
};

export type DtsGenOptions = PluginDtsOptions & {
Expand Down
Loading

0 comments on commit 29d3511

Please sign in to comment.