Skip to content

Commit fef529c

Browse files
authored
fix(module-tools): fix ts6369 error when using project reference (web-infra-dev#5050)
1 parent 6561fbd commit fef529c

File tree

6 files changed

+70
-19
lines changed

6 files changed

+70
-19
lines changed

.changeset/nasty-onions-think.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/module-tools': patch
3+
---
4+
5+
fix(module-tools): fix ts6369
6+
fix(module-tools): 修复 ts6369 错误

packages/solutions/module-tools/src/builder/dts/tsc.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ChildProcess } from 'child_process';
2-
import { execa, logger } from '@modern-js/utils';
2+
import path from 'path';
3+
import { execa, fs, logger } from '@modern-js/utils';
34
import type { GeneratorDtsConfig, PluginAPI, ModuleTools } from '../../types';
45
import {
56
getTscBinPath,
@@ -54,39 +55,55 @@ const runTscBin = async (
5455
api: PluginAPI<ModuleTools>,
5556
config: GeneratorDtsConfig,
5657
) => {
57-
const { appDirectory, watch = false, abortOnError = true } = config;
58+
const {
59+
appDirectory,
60+
watch = false,
61+
abortOnError = true,
62+
tsconfigPath,
63+
userTsconfig,
64+
distPath,
65+
} = config;
5866

5967
const tscBinFile = await getTscBinPath(appDirectory);
6068

61-
const { tsconfigPath, userTsconfig, distPath } = config;
62-
6369
const params: string[] = [];
6470

65-
if (watch) {
66-
params.push('-w');
67-
}
68-
6971
// avoid error TS6305
7072
if (userTsconfig.references) {
71-
params.push('-b');
73+
params.push('-b', tsconfigPath);
74+
75+
// update outDir
76+
userTsconfig.compilerOptions ??= {};
77+
const { baseUrl = '.', outDir = 'dist' } = userTsconfig.compilerOptions;
78+
const abosultBaseUrl = path.isAbsolute(baseUrl)
79+
? baseUrl
80+
: path.join(path.dirname(tsconfigPath), baseUrl);
81+
if (path.resolve(abosultBaseUrl, outDir) !== distPath) {
82+
userTsconfig.compilerOptions.outDir = path.relative(
83+
abosultBaseUrl,
84+
distPath,
85+
);
86+
fs.writeFileSync(tsconfigPath, JSON.stringify(userTsconfig, null, 2));
87+
}
88+
} else {
89+
params.push('-p', tsconfigPath, '--outDir', distPath);
90+
}
91+
92+
if (watch) {
93+
params.push('-w');
7294
}
7395

7496
const childProgress = execa(
7597
tscBinFile,
7698
[
77-
'-p',
78-
tsconfigPath,
99+
...params,
79100
/* Required parameter, use it stdout have color */
80101
'--pretty',
81102
// https://github.com/microsoft/TypeScript/issues/21824
82103
'--preserveWatchOutput',
83104
// Only emit d.ts files
84105
'--declaration',
85106
'--emitDeclarationOnly',
86-
// write d.ts files to distPath that defined in buildConfig.dts
87-
'--outDir',
88-
distPath,
89-
...params,
90107
],
91108
{
92109
stdio: 'pipe',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "dts-composite",
3+
"version": "1.0.0"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 'hello world';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "@modern-js/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./",
5+
"paths": {
6+
"@src/*": ["./src/*"]
7+
},
8+
"composite": true,
9+
"declaration": true,
10+
"outDir": "dist"
11+
},
12+
"include": ["src"]
13+
}

tests/integration/module/fixtures/build/dts/tsconfig.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
"compilerOptions": {
44
"baseUrl": "./",
55
"paths": {
6-
"@src/*": ["./src/*"]
6+
"@src/*": [
7+
"./src/*"
8+
]
79
},
810
"declaration": true,
9-
"declarationMap": true
11+
"declarationMap": true,
12+
"outDir": "dist/bundleless/types"
1013
},
11-
"include": ["src"]
12-
}
14+
"include": [
15+
"src"
16+
],
17+
"references": [
18+
{
19+
"path": "../dts-composite"
20+
}
21+
]
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy