diff --git a/src/builder/bundless/dts/index.ts b/src/builder/bundless/dts/index.ts index cb0657c4..3dd869b0 100644 --- a/src/builder/bundless/dts/index.ts +++ b/src/builder/bundless/dts/index.ts @@ -163,7 +163,7 @@ export default async function getDeclarations( }); const incrProgram = ts.createIncrementalProgram({ - rootNames: inputFiles, + rootNames: tsconfig.fileNames, options: tsconfig.options, host: tsHost, }); diff --git a/src/builder/bundless/loaders/index.ts b/src/builder/bundless/loaders/index.ts index 098127ff..2bd10b71 100644 --- a/src/builder/bundless/loaders/index.ts +++ b/src/builder/bundless/loaders/index.ts @@ -1,3 +1,4 @@ +import { winPath } from '@umijs/utils'; import fs from 'fs'; import { runLoaders } from 'loader-runner'; import type { IApi } from '../../../types'; @@ -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({ ...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) => { diff --git a/src/builder/bundless/loaders/javascript/index.ts b/src/builder/bundless/loaders/javascript/index.ts index f901daf1..b3da638d 100644 --- a/src/builder/bundless/loaders/javascript/index.ts +++ b/src/builder/bundless/loaders/javascript/index.ts @@ -1,3 +1,4 @@ +import { winPath } from '@umijs/utils'; import { getTsconfig } from '../../dts'; import type { IBundlessLoader, IJSTransformer, ILoaderOutput } from '../types'; @@ -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; } diff --git a/tests/fixtures/build/bundle-alias-tsconfig/tsconfig.json b/tests/fixtures/build/bundle-alias-tsconfig/tsconfig.json index 1228d9f4..6eb0b932 100644 --- a/tests/fixtures/build/bundle-alias-tsconfig/tsconfig.json +++ b/tests/fixtures/build/bundle-alias-tsconfig/tsconfig.json @@ -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/*"], diff --git a/tests/fixtures/build/bundle-targets/src/index.ts b/tests/fixtures/build/bundle-targets/src/index.ts index 02ae7784..fae80c97 100644 --- a/tests/fixtures/build/bundle-targets/src/index.ts +++ b/tests/fixtures/build/bundle-targets/src/index.ts @@ -2,4 +2,4 @@ async function hello() { return await Promise.resolve('ok'); } -hello(); +export default hello; diff --git a/tests/fixtures/build/bundless-alias/tsconfig.json b/tests/fixtures/build/bundless-alias/tsconfig.json index 3e4f685d..a7fde3d5 100644 --- a/tests/fixtures/build/bundless-alias/tsconfig.json +++ b/tests/fixtures/build/bundless-alias/tsconfig.json @@ -1,8 +1,7 @@ { + "extends": "../tsconfig.json", "compilerOptions": { - "strict": true, "declaration": true, - "skipLibCheck": true, "baseUrl": "./", "paths": { "@/*": ["./src/*"], diff --git a/tests/fixtures/build/bundless-babel-targets/src/index.ts b/tests/fixtures/build/bundless-babel-targets/src/index.ts index 02ae7784..fae80c97 100644 --- a/tests/fixtures/build/bundless-babel-targets/src/index.ts +++ b/tests/fixtures/build/bundless-babel-targets/src/index.ts @@ -2,4 +2,4 @@ async function hello() { return await Promise.resolve('ok'); } -hello(); +export default hello; diff --git a/tests/fixtures/build/bundless-diff-dts/tsconfig.json b/tests/fixtures/build/bundless-diff-dts/tsconfig.json index 6b0eea50..52547fe0 100644 --- a/tests/fixtures/build/bundless-diff-dts/tsconfig.json +++ b/tests/fixtures/build/bundless-diff-dts/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": "../tsconfig.json", "compilerOptions": { "declaration": true } diff --git a/tests/fixtures/build/bundless-esbuild-targets/src/index.ts b/tests/fixtures/build/bundless-esbuild-targets/src/index.ts index 02ae7784..fae80c97 100644 --- a/tests/fixtures/build/bundless-esbuild-targets/src/index.ts +++ b/tests/fixtures/build/bundless-esbuild-targets/src/index.ts @@ -2,4 +2,4 @@ async function hello() { return await Promise.resolve('ok'); } -hello(); +export default hello; diff --git a/tests/fixtures/build/bundless-include-dts/.fatherrc.ts b/tests/fixtures/build/bundless-include-dts/.fatherrc.ts new file mode 100644 index 00000000..50b0486c --- /dev/null +++ b/tests/fixtures/build/bundless-include-dts/.fatherrc.ts @@ -0,0 +1,3 @@ +export default { + esm: {}, +}; diff --git a/tests/fixtures/build/bundless-include-dts/expect.ts b/tests/fixtures/build/bundless-include-dts/expect.ts new file mode 100644 index 00000000..99617f4a --- /dev/null +++ b/tests/fixtures/build/bundless-include-dts/expect.ts @@ -0,0 +1,4 @@ +export default (files: Record) => { + // expect global types to be included + expect(files['esm/index.d.ts']).toContain(': string;'); +}; diff --git a/tests/fixtures/build/bundless-include-dts/global.d.ts b/tests/fixtures/build/bundless-include-dts/global.d.ts new file mode 100644 index 00000000..d3a65027 --- /dev/null +++ b/tests/fixtures/build/bundless-include-dts/global.d.ts @@ -0,0 +1,7 @@ +declare global { + interface Window { + hello: string; + } +} + +export {}; diff --git a/tests/fixtures/build/bundless-include-dts/src/index.ts b/tests/fixtures/build/bundless-include-dts/src/index.ts new file mode 100644 index 00000000..544fff59 --- /dev/null +++ b/tests/fixtures/build/bundless-include-dts/src/index.ts @@ -0,0 +1 @@ +export default window.hello; diff --git a/tests/fixtures/build/bundless-include-dts/tsconfig.json b/tests/fixtures/build/bundless-include-dts/tsconfig.json new file mode 100644 index 00000000..52547fe0 --- /dev/null +++ b/tests/fixtures/build/bundless-include-dts/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "declaration": true + } +} diff --git a/tests/fixtures/build/bundless-outer-paths/tsconfig.json b/tests/fixtures/build/bundless-outer-paths/tsconfig.json index 1ed57949..c9bb6bee 100644 --- a/tests/fixtures/build/bundless-outer-paths/tsconfig.json +++ b/tests/fixtures/build/bundless-outer-paths/tsconfig.json @@ -1,9 +1,8 @@ { + "extends": "../tsconfig.json", "compilerOptions": { - "strict": true, - "skipLibCheck": true, "declaration": true, - "baseUrl": ".", + "baseUrl": "./", "paths": { "@bundless-overrides": ["../bundless-overrides/src/client"] } diff --git a/tests/fixtures/build/bundless-swc-alias/tsconfig.json b/tests/fixtures/build/bundless-swc-alias/tsconfig.json index 3e4f685d..a7fde3d5 100644 --- a/tests/fixtures/build/bundless-swc-alias/tsconfig.json +++ b/tests/fixtures/build/bundless-swc-alias/tsconfig.json @@ -1,8 +1,7 @@ { + "extends": "../tsconfig.json", "compilerOptions": { - "strict": true, "declaration": true, - "skipLibCheck": true, "baseUrl": "./", "paths": { "@/*": ["./src/*"], diff --git a/tests/fixtures/build/bundless-swc-targets/src/index.ts b/tests/fixtures/build/bundless-swc-targets/src/index.ts index 02ae7784..fae80c97 100644 --- a/tests/fixtures/build/bundless-swc-targets/src/index.ts +++ b/tests/fixtures/build/bundless-swc-targets/src/index.ts @@ -2,4 +2,4 @@ async function hello() { return await Promise.resolve('ok'); } -hello(); +export default hello; diff --git a/tests/fixtures/build/tsconfig.json b/tests/fixtures/build/tsconfig.json index 57c4da7c..af51a745 100644 --- a/tests/fixtures/build/tsconfig.json +++ b/tests/fixtures/build/tsconfig.json @@ -1,8 +1,7 @@ { - "extends": "../../../tsconfig.json", "compilerOptions": { - "rootDir": ".", + "strict": true, + "skipLibCheck": true, "jsx": "react" - }, - "include": ["**/*.ts", "**/*.tsx"] + } }