diff --git a/.changeset/wicked-pans-teach.md b/.changeset/wicked-pans-teach.md new file mode 100644 index 000000000000..018ec239a7de --- /dev/null +++ b/.changeset/wicked-pans-teach.md @@ -0,0 +1,6 @@ +--- +'@modern-js/module-tools': patch +--- + +fix(module-tools): improve ts references, not change the user tsconfig +fix(module-tools): 完善对于 ts references 的支持,不再改变用户的 tsconfig diff --git a/packages/solutions/module-tools/src/builder/dts/tsc.ts b/packages/solutions/module-tools/src/builder/dts/tsc.ts index f9132d51e1a9..8797b86578ac 100644 --- a/packages/solutions/module-tools/src/builder/dts/tsc.ts +++ b/packages/solutions/module-tools/src/builder/dts/tsc.ts @@ -1,6 +1,6 @@ import type { ChildProcess } from 'child_process'; import path from 'path'; -import { execa, fs, logger } from '@modern-js/utils'; +import { execa, logger, chalk } from '@modern-js/utils'; import type { GeneratorDtsConfig, PluginAPI, ModuleTools } from '../../types'; import { getTscBinPath, @@ -8,6 +8,7 @@ import { addDtsFiles, withLogTitle, processDtsFilesAfterTsc, + detectTSVersion, } from '../../utils'; import { watchDoneText } from '../../constants/dts'; @@ -72,21 +73,50 @@ const runTscBin = async ( if (userTsconfig.references) { params.push('-b', tsconfigPath); - // update outDir - userTsconfig.compilerOptions ??= {}; - const { baseUrl = '.', outDir = 'dist' } = userTsconfig.compilerOptions; + const { + baseUrl = '.', + outDir, + emitDeclarationOnly, + declaration, + } = userTsconfig.compilerOptions ?? {}; const abosultBaseUrl = path.isAbsolute(baseUrl) ? baseUrl : path.join(path.dirname(tsconfigPath), baseUrl); - if (path.resolve(abosultBaseUrl, outDir) !== distPath) { - userTsconfig.compilerOptions.outDir = path.relative( - abosultBaseUrl, - distPath, + + // can not set '--outDir' with '--build'. + if (!outDir || path.resolve(abosultBaseUrl, outDir) !== distPath) { + const correctOutDir = path.relative(abosultBaseUrl, distPath); + throw new Error( + `Please set outDir: "${correctOutDir}" in ${chalk.underline( + tsconfigPath, + )} to keep it same as buildConfig.`, ); - fs.writeFileSync(tsconfigPath, JSON.stringify(userTsconfig, null, 2)); + } + + // can not set '--declaration' and '--emitDeclaration' with '--build' if ts is not v5. + const tsVersion = await detectTSVersion(appDirectory); + if (tsVersion !== 5) { + if (!declaration || !emitDeclarationOnly) { + throw new Error( + `Please set declaration: true and emitDeclaration: true in ${chalk.underline( + tsconfigPath, + )}`, + ); + } + } else { + params.push('--declaration', '--emitDeclarationOnly'); } } else { - params.push('-p', tsconfigPath, '--outDir', distPath); + params.push( + '-p', + tsconfigPath, + // Same as dts.distPath + '--outDir', + distPath, + // Only emit d.ts files + '--declaration', + '--emitDeclarationOnly', + ); } if (watch) { @@ -101,9 +131,6 @@ const runTscBin = async ( '--pretty', // https://github.com/microsoft/TypeScript/issues/21824 '--preserveWatchOutput', - // Only emit d.ts files - '--declaration', - '--emitDeclarationOnly', ], { stdio: 'pipe', diff --git a/packages/solutions/module-tools/src/utils/dts.ts b/packages/solutions/module-tools/src/utils/dts.ts index 77387c67b047..63863c778116 100644 --- a/packages/solutions/module-tools/src/utils/dts.ts +++ b/packages/solutions/module-tools/src/utils/dts.ts @@ -27,6 +27,18 @@ export const getProjectTsconfig = async ( return json5.parse(fs.readFileSync(tsconfigPath, 'utf-8')); }; +export async function detectTSVersion(appDirectory?: string) { + // Detect typescript version from current cwd + // return the major version of typescript + const cwd = appDirectory ?? process.cwd(); + const reactPath = join(cwd, 'node_modules', 'typescript'); + if (await fs.pathExists(reactPath)) { + const reactPkg = await fs.readJson(join(reactPath, 'package.json')); + const version = Number(reactPkg.version.split('.')[0]); + return version; + } +} + export const getTscBinPath = async (appDirectory: string) => { const { default: findUp, exists: pathExists } = await import( '../../compiled/find-up'