Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 10 additions & 6 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
clearTempDeclarationDir,
loadTsconfig,
processSourceEntry,
warnIfOutside,
} from './utils';

const __filename = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -110,11 +111,18 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({

const tsConfigResult = loadTsconfig(tsconfigPath);
const { options: rawCompilerOptions } = tsConfigResult;
const { declarationDir, outDir, composite, incremental } =
rawCompilerOptions;
const dtsEmitPath =
options.distPath ??
rawCompilerOptions.declarationDir ??
declarationDir ??
outDir ??
config.output?.distPath?.root;

// check whether declarationDir or outDir is outside from current project
warnIfOutside(cwd, declarationDir, 'declarationDir');
warnIfOutside(cwd, outDir, 'outDir');

// clean dts files
if (config.output.cleanDistPath !== false) {
await cleanDtsFiles(dtsEmitPath);
Expand All @@ -126,11 +134,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
}

// clean tsbuildinfo file
if (
rawCompilerOptions.composite ||
rawCompilerOptions.incremental ||
options.build
) {
if (composite || incremental || options.build) {
await cleanTsBuildInfoFile(tsconfigPath, rawCompilerOptions);
}

Expand Down
17 changes: 17 additions & 0 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,20 @@ export async function cleanTsBuildInfoFile(
await fsP.rm(tsbuildInfoFilePath, { force: true });
}
}

export function warnIfOutside(
cwd: string,
dir: string | undefined,
label: string,
): void {
if (dir) {
const normalizedCwd = normalize(cwd);
const normalizedDir = normalize(dir);
const relDir = relative(normalizedCwd, normalizedDir);
if (relDir.startsWith('..') || relDir.startsWith(path.sep)) {
logger.warn(
`The resolved ${label} ${color.cyan(normalizedDir)} is outside the project root ${color.cyan(normalizedCwd)}, please check your tsconfig file.`,
);
}
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/dts/check/outside-root/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-check-outdise-root-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
10 changes: 10 additions & 0 deletions tests/integration/dts/check/outside-root/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
dts: true,
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/dts/check/outside-root/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
9 changes: 9 additions & 0 deletions tests/integration/dts/check/outside-root/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig/declarationDir.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"composite": true
},
"include": ["src"]
}
6 changes: 6 additions & 0 deletions tests/integration/dts/check/tsconfig/declarationDir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"declaration": true,
"declarationDir": "dist"
}
}
30 changes: 29 additions & 1 deletion tests/integration/dts/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { join, normalize } from 'node:path';
import stripAnsi from 'strip-ansi';
import {
buildAndGetResults,
Expand Down Expand Up @@ -607,3 +607,31 @@ describe('use with other features', async () => {
`);
});
});

describe('check tsconfig.json field', async () => {
test('check whether declarationDir is resolved outside from project root', async () => {
const fixturePath = join(__dirname, 'check', 'outside-root');
const { logs, restore } = proxyConsole();
const { files } = await buildAndGetResults({
fixturePath,
type: 'dts',
});
const logStrings = logs.map((log) => stripAnsi(log));
restore();

const expectDeclarationDir = normalize(
join(__dirname, 'check/tsconfig/dist'),
);
const expectRoot = normalize(join(__dirname, 'check/outside-root'));

expect(
logStrings.some((log) =>
log.includes(
`The resolved declarationDir ${expectDeclarationDir} is outside the project root ${expectRoot}, please check your tsconfig file.`,
),
),
).toEqual(true);

expect(files.esm).toMatchInlineSnapshot('undefined');
});
});
Loading