rsbuild-plugin-dts
Version:
Rsbuild plugin that supports emitting declaration files for TypeScript.
58 lines (57 loc) • 3.18 kB
JavaScript
import node_fs from "node:fs";
import { join, relative } from "node:path";
import { logger } from "@rsbuild/core";
import { color, addBannerAndFooter, getTimeCost } from "./188.js";
const logPrefixApiExtractor = color.dim('[api-extractor]');
async function bundleDts(options) {
let apiExtractor;
try {
apiExtractor = await import("@microsoft/api-extractor");
} catch {
const error = new Error(`${color.cyan('@microsoft/api-extractor')} is required when ${color.cyan('dts.bundle')} is set to ${color.cyan('true')}, please make sure it is installed. You could check https://rslib.rs/guide/advanced/dts#how-to-generate-declaration-files-in-rslib for more details.`);
error.stack = '';
throw error;
}
const { Extractor, ExtractorConfig, ExtractorLogLevel } = apiExtractor;
const { name, cwd, distPath, dtsExtension, banner, footer, dtsEntry, tsconfigPath = 'tsconfig.json', bundledPackages = [] } = options;
try {
await Promise.all(dtsEntry.map(async (entry)=>{
const start = Date.now();
const untrimmedFilePath = join(cwd, relative(cwd, distPath), `${entry.name}${dtsExtension}`);
const mainEntryPointFilePath = entry.path;
if (!node_fs.existsSync(mainEntryPointFilePath)) throw new Error(`Declaration entry file ${color.underline(entry.path)} not found.\nPlease ensure that your tsconfig file ${color.underline(tsconfigPath)} is correctly configured to generate declaration files. If needed, a custom tsconfig can be specified using the ${color.cyan('source.tsconfigPath')} option.`);
const internalConfig = {
mainEntryPointFilePath,
bundledPackages,
dtsRollup: {
enabled: true,
untrimmedFilePath
},
compiler: {
tsconfigFilePath: tsconfigPath
},
projectFolder: cwd
};
const extractorConfig = ExtractorConfig.prepare({
configObject: internalConfig,
configObjectFullPath: void 0,
packageJsonFullPath: join(cwd, 'package.json')
});
const extractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
messageCallback (message) {
if ('console-compiler-version-notice' === message.messageId || 'console-preamble' === message.messageId) message.logLevel = ExtractorLogLevel.None;
}
});
if (!extractorResult.succeeded) throw new Error(`API Extractor error. ${color.dim(`(${name})`)}`);
await addBannerAndFooter(untrimmedFilePath, banner, footer);
logger.ready(`declaration files bundled successfully: ${color.cyan(relative(cwd, untrimmedFilePath))} in ${getTimeCost(start)} ${color.dim(`(${name})`)}`);
}));
} catch (e) {
const message = e instanceof Error ? e.message : e;
const error = new Error(`${logPrefixApiExtractor} ${message} ${color.dim(`(${name})`)}`);
error.stack = '';
throw error;
}
}
export { bundleDts };