Skip to content

Commit

Permalink
fix(module-tools): improve ts references, not change the user tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
10Derozan committed Dec 12, 2023
1 parent 24e25e2 commit 79bb665
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/wicked-pans-teach.md
Original file line number Diff line number Diff line change
@@ -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
53 changes: 40 additions & 13 deletions packages/solutions/module-tools/src/builder/dts/tsc.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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,
printOrThrowDtsErrors,
addDtsFiles,
withLogTitle,
processDtsFilesAfterTsc,
detectTSVersion,
} from '../../utils';
import { watchDoneText } from '../../constants/dts';

Expand Down Expand Up @@ -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) {
Expand All @@ -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',
Expand Down
12 changes: 12 additions & 0 deletions packages/solutions/module-tools/src/utils/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 79bb665

Please sign in to comment.