Skip to content

Commit

Permalink
fix: include types lost when generating dts (#707)
Browse files Browse the repository at this point in the history
* fix: include types lost when generating dts

* test: add case for include types

* test: correct type error for build cases

* refactor: compatible with windows os
  • Loading branch information
PeachScript authored Sep 7, 2023
1 parent 9dbfe07 commit 3197293
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/builder/bundless/dts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default async function getDeclarations(
});

const incrProgram = ts.createIncrementalProgram({
rootNames: inputFiles,
rootNames: tsconfig.fileNames,
options: tsconfig.options,
host: tsHost,
});
Expand Down
15 changes: 11 additions & 4 deletions src/builder/bundless/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { winPath } from '@umijs/utils';
import fs from 'fs';
import { runLoaders } from 'loader-runner';
import type { IApi } from '../../../types';
Expand Down Expand Up @@ -63,17 +64,23 @@ export default async (
const cacheRet = await cache.get(cacheKey, '');

// use cache first
if (cacheRet)
/* istanbul ignore if -- @preserve */
if (cacheRet) {
const tsconfig = /\.tsx?$/.test(fileAbsPath)
? getTsconfig(opts.cwd)
: undefined;

return Promise.resolve<ILoaderOutput>({
...cacheRet,
options: {
...cacheRet.options,
// FIXME: shit code for avoid invalid declaration value when tsconfig changed
declaration: /\.tsx?$/.test(fileAbsPath)
? getTsconfig(opts.cwd)?.options.declaration
: false,
declaration:
tsconfig?.options.declaration &&
tsconfig?.fileNames.includes(winPath(fileAbsPath)),
},
});
}

// get matched loader by test
const matched = loaders.find((item) => {
Expand Down
8 changes: 6 additions & 2 deletions src/builder/bundless/loaders/javascript/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { winPath } from '@umijs/utils';
import { getTsconfig } from '../../dts';
import type { IBundlessLoader, IJSTransformer, ILoaderOutput } from '../types';

Expand Down Expand Up @@ -32,9 +33,12 @@ const jsLoader: IBundlessLoader = function (content) {
}

// mark for output declaration file
const tsconfig = /\.tsx?$/.test(this.resource)
? getTsconfig(this.context!)
: undefined;
if (
/\.tsx?$/.test(this.resource) &&
getTsconfig(this.context!)?.options.declaration
tsconfig?.options.declaration &&
tsconfig?.fileNames.includes(winPath(this.resource))
) {
outputOpts.declaration = true;
}
Expand Down
14 changes: 2 additions & 12 deletions tests/fixtures/build/bundle-alias-tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"strict": true,
"skipLibCheck": true,
"target": "es2015",
"jsx": "react",
"baseUrl": ".",
"baseUrl": "./",
"paths": {
"alias-module": ["./src/alias.ts"],
"@/*": ["./src/*"],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/build/bundle-targets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ async function hello() {
return await Promise.resolve('ok');
}

hello();
export default hello;
3 changes: 1 addition & 2 deletions tests/fixtures/build/bundless-alias/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"strict": true,
"declaration": true,
"skipLibCheck": true,
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/build/bundless-babel-targets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ async function hello() {
return await Promise.resolve('ok');
}

hello();
export default hello;
1 change: 1 addition & 0 deletions tests/fixtures/build/bundless-diff-dts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": true
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/build/bundless-esbuild-targets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ async function hello() {
return await Promise.resolve('ok');
}

hello();
export default hello;
3 changes: 3 additions & 0 deletions tests/fixtures/build/bundless-include-dts/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
esm: {},
};
4 changes: 4 additions & 0 deletions tests/fixtures/build/bundless-include-dts/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default (files: Record<string, string>) => {
// expect global types to be included
expect(files['esm/index.d.ts']).toContain(': string;');
};
7 changes: 7 additions & 0 deletions tests/fixtures/build/bundless-include-dts/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare global {
interface Window {
hello: string;
}
}

export {};
1 change: 1 addition & 0 deletions tests/fixtures/build/bundless-include-dts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default window.hello;
6 changes: 6 additions & 0 deletions tests/fixtures/build/bundless-include-dts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": true
}
}
5 changes: 2 additions & 3 deletions tests/fixtures/build/bundless-outer-paths/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"strict": true,
"skipLibCheck": true,
"declaration": true,
"baseUrl": ".",
"baseUrl": "./",
"paths": {
"@bundless-overrides": ["../bundless-overrides/src/client"]
}
Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/build/bundless-swc-alias/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"strict": true,
"declaration": true,
"skipLibCheck": true,
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/build/bundless-swc-targets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ async function hello() {
return await Promise.resolve('ok');
}

hello();
export default hello;
7 changes: 3 additions & 4 deletions tests/fixtures/build/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"strict": true,
"skipLibCheck": true,
"jsx": "react"
},
"include": ["**/*.ts", "**/*.tsx"]
}
}

0 comments on commit 3197293

Please sign in to comment.