Skip to content

Commit

Permalink
fix(core): Update logic to handle when outDir and rootDir are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Dec 19, 2024
1 parent 832a1c9 commit 9f3b013
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/js/src/plugins/typescript/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { existsSync, readdirSync, statSync } from 'node:fs';
import {
basename,
dirname,
extname,
join,
normalize,
relative,
Expand Down Expand Up @@ -622,25 +623,33 @@ function isValidPackageJsonBuildConfig(
joinPathFragments(projectRoot, 'package.json')
);

const rootDir = tsConfig.options.rootDir ?? 'src';
const resolvedRootDir = resolve(workspaceRoot, projectRoot, rootDir);

const outDir = tsConfig.options.outDir
? tsConfig.options.outDir
: tsConfig.options.outFile
? dirname(tsConfig.options.outFile)
: false;

const isPathSourceFile = (path: string): boolean => {
const ext = extname(path);
if (!outDir) {
// If `outDir` is not defined either from tsconfig.json or outFile the transpiled files will be in the same directory as the source files.
// In this case, we assume that the source files are `.ts`, `.cts`, or `.mts`.
return (
path.endsWith('.ts') || path.endsWith('.cts') || path.endsWith('.mts')
);
// TODO: check/handle if the project is `.js` only
return ['.ts', '.cts', '.mts'].includes(ext);
}

const resolvedOutDir = resolve(workspaceRoot, projectRoot, outDir);
const pathToCheck = resolve(workspaceRoot, projectRoot, path);

return !pathToCheck.startsWith(resolvedOutDir);
// If path is outside of the outDir, if the outDir and the root dir is same check the file type.
return (
!pathToCheck.startsWith(resolvedOutDir) ||
(pathToCheck.startsWith(resolvedRootDir) &&
['.ts', '.cts', '.mts'].includes(ext))
);
};

// If `outFile` is defined, check the validity of the path.
Expand Down

0 comments on commit 9f3b013

Please sign in to comment.