Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@
"@module-federation/rsbuild-plugin": "catalog:",
"@rslib/tsconfig": "workspace:*",
"@types/fs-extra": "catalog:",
"@types/picomatch": "catalog:",
"@types/semver": "catalog:",
"cac": "catalog:",
"fs-extra": "catalog:",
"get-tsconfig": "catalog:",
"memfs": "catalog:",
"path-serializer": "catalog:",
"picomatch": "catalog:",
"rsbuild-plugin-publint": "catalog:",
"rslib": "catalog:",
"rslog": "catalog:",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type BuildOptions = CommonOptions & {
autoExtension?: boolean;
autoExternal?: boolean;
tsconfig?: string;
exports?: boolean;
};

export type InspectOptions = CommonOptions & {
Expand Down Expand Up @@ -136,6 +137,10 @@ export function setupCommands(argv: string[]): void {
'--tsconfig <path>',
'use specific tsconfig (relative to project root)',
)
.option(
'--exports',
'generate the exports field in package.json from build outputs (use --no-exports to disable)',
)
.action(async (options: BuildOptions) => {
initCliAction('build', options);
try {
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,7 @@ export async function composeCreateRsbuildConfig(
dev: _dev,
server: _server,
logLevel,
exports: _exports,
...sharedRsbuildConfig
} = rslibConfig;
// debug mode should always verbose logs
Expand Down Expand Up @@ -1999,6 +2000,14 @@ export async function composeCreateRsbuildConfig(
config.id = libConfig.id;
}

if (userConfig.dts) {
config.dts = userConfig.dts;
}

if (userConfig.bundle === false) {
config.bundle = false;
}

return config;
});

Expand Down Expand Up @@ -2043,10 +2052,10 @@ export async function composeRsbuildEnvironments(
return candidateId;
};

for (const { format, id, config } of rsbuildConfigWithLibInfo) {
for (const { format, id, config, dts, bundle } of rsbuildConfigWithLibInfo) {
const libId = typeof id === 'string' ? id : composeDefaultId(format);
environments[libId] = config;
environmentWithInfos.push({ id: libId, format, config });
environmentWithInfos.push({ id: libId, format, config, dts, bundle });
}

const conflictIds = usedIds.filter(
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/createRslib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,34 @@ export async function createRslib(
} satisfies RsbuildPlugin);
}

const { environments } = await composeRsbuildEnvironments(config);
const { environments, environmentWithInfos } =
await composeRsbuildEnvironments(config);

const rsbuildInstance = await createRsbuildInstance(
options,
'production',
pruneEnvironments(environments, buildOptions.lib),
);

// The `exports` field describes the published package, so it is only
// generated by one-off builds of all libs. In watch mode, declaration files
// may still be in progress when the build finishes.
if (
(buildOptions.exports ?? Boolean(config.exports)) &&
!buildOptions.watch &&
!buildOptions.lib?.length
) {
// Loaded on demand to keep it and its dependencies out of the startup.
const { ExportsPlugin } = await import('./exports/plugin');
rsbuildInstance.addPlugins([
ExportsPlugin({
root: config.root!,
libInfos: environmentWithInfos,
options: typeof config.exports === 'object' ? config.exports : {},
}),
]);
}

const buildResult = await rsbuildInstance.build({
watch: buildOptions.watch,
});
Expand Down
67 changes: 67 additions & 0 deletions packages/core/src/exports/dts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fs from 'node:fs';
import path from 'node:path';
import { normalizeSlash } from '../utils/helper';

// TypeScript looks up the declaration file next to the resolved JavaScript file
// with the matching extension, and never falls back to other extensions.
const MATCHED_DECLARATION_EXTENSIONS: Record<string, string> = {
'.js': '.d.ts',
'.jsx': '.d.ts',
'.mjs': '.d.mts',
'.cjs': '.d.cts',
};

const DECLARATION_EXTENSIONS = ['.d.ts', '.d.mts', '.d.cts'];

export type DeclarationFile = {
/** Path relative to the package root, using forward slashes. */
file: string;
/** Whether TypeScript can resolve it from the JavaScript file without a `types` condition. */
matched: boolean;
};

/**
* Find the declaration file of a JavaScript output, preferring the one with the
* matching extension. It is looked up next to the output, or at the same
* relative path under `dtsDistPath` when declaration files are emitted to
* another directory, where TypeScript cannot resolve it on its own.
*/
export function findDeclarationFile({
jsFile,
root,
outputPath,
dtsDistPath,
}: {
/** Path relative to the package root. */
jsFile: string;
root: string;
/** Absolute path of the JavaScript output directory. */
outputPath: string;
/** Absolute path of `dts.distPath`. */
dtsDistPath?: string;
}): DeclarationFile | undefined {
const ext = path.extname(jsFile);
// Callers only pass JavaScript outputs, whose extensions are all listed.
const matchedExt = MATCHED_DECLARATION_EXTENSIONS[ext]!;

const jsPath = path.join(root, jsFile);
const nextToOutput =
dtsDistPath === undefined ||
path.resolve(dtsDistPath) === path.resolve(outputPath);
const base = (
nextToOutput
? jsPath
: path.join(dtsDistPath, path.relative(outputPath, jsPath))
).slice(0, -ext.length);

const declarationExt = [
matchedExt,
...DECLARATION_EXTENSIONS.filter((otherExt) => otherExt !== matchedExt),
].find((candidate) => fs.existsSync(base + candidate));

if (!declarationExt) return;
return {
file: normalizeSlash(path.relative(root, base + declarationExt)),
matched: nextToOutput && declarationExt === matchedExt,
};
}
Loading