From 270a3794cf9900967752cb40196464f87f27313f Mon Sep 17 00:00:00 2001 From: michael faith Date: Wed, 9 Oct 2024 20:55:46 -0500 Subject: [PATCH] fix(typescript): allow for files to be nested within outDir This change corrects a regression introduced by #1728, preventing dual bundle configs from writing output files to directories nested within the directory defined in the tsconfig's outDir property. Example: ```js export default { input: 'src/index.ts', // Replace with the path to your entry TypeScript file output: [ { dir: 'dist/cjs', format: 'cjs', sourcemap: true, preserveModules: true }, { dir: 'dist/esm', format: 'esm', sourcemap: true, preserveModules: true } ], plugins: [ json(), resolve(), commonjs(), typescript({ tsconfig: 'tsconfig.json', compilerOptions: { /* Basic Options */ target: 'ES5', module: 'ES2020', lib: ['ES2019', 'ES2020'], allowJs: true, checkJs: false, /* Strict Type-Checking Options */ strict: true, /* Module Resolution Options */ moduleResolution: 'node', esModuleInterop: true, /* Advanced Options */ forceConsistentCasingInFileNames: true, skipDefaultLibCheck: true, skipLibCheck: true, outDir: path.join(__dirname, 'dist') } }) ], external: deps // Add any external dependencies you don't want to bundle }; ``` --- packages/typescript/src/options/validate.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/typescript/src/options/validate.ts b/packages/typescript/src/options/validate.ts index d0f22e3de..ae53834a3 100644 --- a/packages/typescript/src/options/validate.ts +++ b/packages/typescript/src/options/validate.ts @@ -58,13 +58,16 @@ export function validatePaths( for (const dirProperty of DIRECTORY_PROPS) { if (compilerOptions[dirProperty] && outputDir) { // Checks if the given path lies within Rollup output dir - const fromRollupDirToTs = relative(outputDir, compilerOptions[dirProperty]!); - if (fromRollupDirToTs.startsWith('..')) { - if (outputOptions.dir) { + if (outputOptions.dir) { + const fromRollupDirToTs = relative(outputDir, compilerOptions[dirProperty]!); + if (fromRollupDirToTs.startsWith('..')) { context.error( `@rollup/plugin-typescript: Path of Typescript compiler option '${dirProperty}' must be located inside Rollup 'dir' option.` ); - } else { + } + } else { + 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.` );