fix(ngcc): correctly include internal .d.ts files (#33875)
Some declaration files may not be referenced from an entry-point's main typings file, as it may declare types that are only used internally. ngcc has logic to include declaration files based on all source files, to ensure internal declaration files are available. For packages following APF layout, however, this logic was insufficient. Consider an entry-point with base path of `/esm2015/testing` and typings residing in `/testing`, the file `/esm2015/testing/src/nested/internal.js` has its typings file at `/testing/src/nested/internal.d.ts`. Previously, the declaration was assumed to be located at `/esm2015/testing/testing/internal.d.ts` (by means of `/esm2015/testing/src/nested/../../testing/internal.d.ts`) which is not where the declaration file can be found. This commit resolves the issue by looking in the correct directory. PR Close #33875
This commit is contained in:
@ -73,16 +73,20 @@ export function makeEntryPointBundle(
|
||||
function computePotentialDtsFilesFromJsFiles(
|
||||
fs: FileSystem, srcProgram: ts.Program, formatPath: AbsoluteFsPath,
|
||||
typingsPath: AbsoluteFsPath) {
|
||||
const relativePath = fs.relative(fs.dirname(formatPath), fs.dirname(typingsPath));
|
||||
const formatRoot = fs.dirname(formatPath);
|
||||
const typingsRoot = fs.dirname(typingsPath);
|
||||
const additionalFiles: AbsoluteFsPath[] = [];
|
||||
for (const sf of srcProgram.getSourceFiles()) {
|
||||
if (!sf.fileName.endsWith('.js')) {
|
||||
continue;
|
||||
}
|
||||
const dtsPath = fs.resolve(
|
||||
fs.dirname(sf.fileName), relativePath, fs.basename(sf.fileName, '.js') + '.d.ts');
|
||||
if (fs.exists(dtsPath)) {
|
||||
additionalFiles.push(dtsPath);
|
||||
|
||||
// Given a source file at e.g. `esm2015/src/some/nested/index.js`, try to resolve the
|
||||
// declaration file under the typings root in `src/some/nested/index.d.ts`.
|
||||
const mirroredDtsPath =
|
||||
fs.resolve(typingsRoot, fs.relative(formatRoot, sf.fileName.replace(/\.js$/, '.d.ts')));
|
||||
if (fs.exists(mirroredDtsPath)) {
|
||||
additionalFiles.push(mirroredDtsPath);
|
||||
}
|
||||
}
|
||||
return additionalFiles;
|
||||
|
Reference in New Issue
Block a user