From e58d29ca0bd1d203747a41aef3d340c4a41487cf Mon Sep 17 00:00:00 2001 From: James Craig Date: Sun, 15 Dec 2024 11:10:17 -0500 Subject: [PATCH] fix(typescript): path validation issue in validatePaths function (#1800) * fix(typescript): fix path validation issue in validatePaths function Current code allows paths that are below the 'file' option but not nested directories. For example if file option is set to "C:/examplelib/output" then "C:/examplelib" is fine while "C:/examplelib/output/decl" is not. The order change in this commit fixes this issue introduced in 12.1.1. * fix(typescript): adding test * fix(typescript): fix path validation issue in validatePaths function * fix(typescript): fix path validation issue in validatePaths function * chore: Formatting --- packages/typescript/src/options/validate.ts | 9 ++++++++- packages/typescript/test/test.js | 22 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/typescript/src/options/validate.ts b/packages/typescript/src/options/validate.ts index ae53834a3..6c76c925a 100644 --- a/packages/typescript/src/options/validate.ts +++ b/packages/typescript/src/options/validate.ts @@ -65,13 +65,20 @@ export function validatePaths( `@rollup/plugin-typescript: Path of Typescript compiler option '${dirProperty}' must be located inside Rollup 'dir' option.` ); } - } else { + } else if (dirProperty === 'outDir') { const fromTsDirToRollup = relative(compilerOptions[dirProperty]!, outputDir); if (fromTsDirToRollup.startsWith('..')) { context.error( `@rollup/plugin-typescript: Path of Typescript compiler option '${dirProperty}' must be located inside the same directory as the Rollup 'file' option.` ); } + } else { + const fromTsDirToRollup = relative(outputDir, compilerOptions[dirProperty]!); + if (fromTsDirToRollup.startsWith('..')) { + context.error( + `@rollup/plugin-typescript: Path of Typescript compiler option '${dirProperty}' must be located inside the same directory as the Rollup 'file' option.` + ); + } } } } diff --git a/packages/typescript/test/test.js b/packages/typescript/test/test.js index e0b83fd0d..1758779f0 100644 --- a/packages/typescript/test/test.js +++ b/packages/typescript/test/test.js @@ -129,6 +129,28 @@ test.serial( } ); +test.serial( + 'ensures declarationDir is allowed in Rollup output directory when output.file is used', + async (t) => { + const bundle = await rollup({ + input: 'fixtures/basic/main.ts', + plugins: [ + typescript({ + tsconfig: 'fixtures/basic/tsconfig.json', + declarationDir: 'fixtures/basic/dist/other', + declaration: true + }) + ], + onwarn + }); + + // this should not throw an error + await t.notThrowsAsync(() => + getCode(bundle, { format: 'es', file: 'fixtures/basic/dist/index.js' }, true) + ); + } +); + test.serial( 'ensures output files can be written to subdirectories within the tsconfig outDir', async (t) => {