Skip to content

Commit

Permalink
fix: dts lost when esm and cjs has different ignores (#656)
Browse files Browse the repository at this point in the history
* fix: dts lost when esm and cjs has different ignores

* test: add case for diff dts
  • Loading branch information
PeachScript committed May 31, 2023
1 parent e50f7af commit 3ca7e38
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/builder/bundless/dts/index.ts
Expand Up @@ -116,28 +116,43 @@ export default async function getDeclarations(
tsHost.writeFile = (fileName, content, _a, _b, sourceFiles) => {
const sourceFile = sourceFiles?.[0].fileName;

// only collect dts for input files, to avoid output error in watch mode
// ref: https://github.com/umijs/father/issues/43
if (sourceFile && inputFiles.includes(sourceFile)) {
if (fileName === tsconfig.options.tsBuildInfoFile) {
// save incremental cache
fsExtra.writeFileSync(tsconfig.options.tsBuildInfoFile, content);
} else if (sourceFile) {
// write d.ts & d.ts.map and save cache
const ret = {
file: path.basename(fileName),
content,
sourceFile,
};
const index = output.findIndex(
(out) => out.file === ret.file && out.sourceFile === ret.sourceFile,
);
if (index > -1) {
output.splice(index, 1, ret);
} else {
output.push(ret);

// only collect dts for input files, to avoid output error in watch mode
// ref: https://github.com/umijs/father-next/issues/43
if (inputFiles.includes(sourceFile)) {
const index = output.findIndex(
(out) => out.file === ret.file && out.sourceFile === ret.sourceFile,
);
if (index > -1) {
output.splice(index, 1, ret);
} else {
output.push(ret);
}
}

// group cache by file (d.ts & d.ts.map)
cacheRets[cacheKeys[sourceFile]] ??= [];
cacheRets[cacheKeys[sourceFile]].push(ret);
} else if (fileName === tsconfig.options.tsBuildInfoFile) {
fsExtra.writeFileSync(tsconfig.options.tsBuildInfoFile, content);
// always save cache even if it's not input file, to avoid cache miss
// because it probably can be used in next bundless run
const cacheKey =
cacheKeys[sourceFile] ||
[
sourceFile,
fs.lstatSync(sourceFile).mtimeMs,
JSON.stringify(tsconfig.options),
].join(':');

cacheRets[cacheKey] ??= [];
cacheRets[cacheKey].push(ret);
}
};

Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/build/bundless-diff-dts/.fatherrc.ts
@@ -0,0 +1,6 @@
export default {
cjs: {},
esm: {
ignores: ['src/ignore.ts'],
},
};
3 changes: 3 additions & 0 deletions tests/fixtures/build/bundless-diff-dts/expect.ts
@@ -0,0 +1,3 @@
export default (files: Record<string, string>) => {
expect(files['cjs/ignore.d.ts']).not.toBeUndefined();
};
1 change: 1 addition & 0 deletions tests/fixtures/build/bundless-diff-dts/src/ignore.ts
@@ -0,0 +1 @@
export default 1;
5 changes: 5 additions & 0 deletions tests/fixtures/build/bundless-diff-dts/src/index.ts
@@ -0,0 +1,5 @@
export default async () => {
try {
await import('./ignore');
} catch {}
};
5 changes: 5 additions & 0 deletions tests/fixtures/build/bundless-diff-dts/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"declaration": true
}
}

0 comments on commit 3ca7e38

Please sign in to comment.