From 5e46079a02a10ec464815e2b5dd8d0967c223ce4 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Mon, 25 Sep 2023 22:06:56 +0200 Subject: [PATCH] Add definition paths from TS config to the program --- src/index.ts | 7 +++++-- src/plugins/typescript/index.ts | 3 +-- src/util/tsconfig-loader.ts | 6 ++++-- tests/plugins/svelte.test.ts | 6 +++--- tests/subpath-imports-from-plugin.test.ts | 18 +++++++++--------- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index a1320e301..f55554f80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,7 @@ import { import { dirname, isInNodeModules, join, isInternal, toAbsolute } from './util/path.js'; import { _resolveSpecifier, _tryResolve } from './util/require.js'; import { _require } from './util/require.js'; -import { loadTSConfig as loadCompilerOptions } from './util/tsconfig-loader.js'; +import { loadTSConfig } from './util/tsconfig-loader.js'; import { WorkspaceWorker } from './WorkspaceWorker.js'; import type { Workspace } from './ConfigurationChief.js'; import type { CommandLineOptions } from './types/cli.js'; @@ -140,7 +140,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => { deputy.addWorkspace({ name, dir, manifestPath, manifest, ignoreDependencies, ignoreBinaries }); - const compilerOptions = await loadCompilerOptions(join(dir, tsConfigFile ?? 'tsconfig.json')); + const { compilerOptions, definitionPaths } = await loadTSConfig(join(dir, tsConfigFile ?? 'tsconfig.json')); const principal = factory.getPrincipal({ cwd: dir, paths, compilerOptions, compilers, pkgName }); @@ -159,6 +159,9 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => { await worker.init(); + principal.addEntryPaths(definitionPaths); + debugLogArray(`Found definition paths (${name})`, definitionPaths); + const sharedGlobOptions = { cwd, workingDir: dir, gitignore, ignore: worker.getIgnorePatterns() }; const entryPathsFromManifest = await getEntryPathFromManifest(cwd, dir, manifest); diff --git a/src/plugins/typescript/index.ts b/src/plugins/typescript/index.ts index b390df06b..30864d121 100644 --- a/src/plugins/typescript/index.ts +++ b/src/plugins/typescript/index.ts @@ -5,7 +5,6 @@ import { hasDependency, load } from '../../util/plugin.js'; import { loadTSConfig } from '../../util/tsconfig-loader.js'; import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js'; import type { TsConfigJson } from 'type-fest'; -import type { CompilerOptions } from 'typescript'; // https://www.typescriptlang.org/tsconfig @@ -34,7 +33,7 @@ const resolveExtensibleConfig = async (configFilePath: string) => { }; export const findTypeScriptDependencies: GenericPluginCallback = async configFilePath => { - const compilerOptions: CompilerOptions = await loadTSConfig(configFilePath); + const { compilerOptions } = await loadTSConfig(configFilePath); const config: TsConfigJson = await resolveExtensibleConfig(configFilePath); // Dual loader to get external `extends` dependencies if (!compilerOptions || !config) return []; diff --git a/src/util/tsconfig-loader.ts b/src/util/tsconfig-loader.ts index 187ca74fe..16fbc26f4 100644 --- a/src/util/tsconfig-loader.ts +++ b/src/util/tsconfig-loader.ts @@ -6,7 +6,9 @@ export const loadTSConfig = async (tsConfigFilePath: string) => { if (isFile(tsConfigFilePath)) { const config = ts.readConfigFile(tsConfigFilePath, ts.sys.readFile); const parsedConfig = ts.parseJsonConfigFileContent(config.config, ts.sys, dirname(tsConfigFilePath)); - return parsedConfig.options ?? {}; + const compilerOptions = parsedConfig.options ?? {}; + const definitionPaths = parsedConfig.fileNames.filter(filePath => filePath.endsWith('.d.ts')); + return { compilerOptions, definitionPaths }; } - return {}; + return { compilerOptions: {}, definitionPaths: [] }; }; diff --git a/tests/plugins/svelte.test.ts b/tests/plugins/svelte.test.ts index 74d21c000..5d8876838 100644 --- a/tests/plugins/svelte.test.ts +++ b/tests/plugins/svelte.test.ts @@ -7,7 +7,7 @@ import baseCounters from '../helpers/baseCounters.js'; const cwd = resolve('fixtures/plugins/svelte'); -test('Use compilers', async () => { +test('Use compilers (svelte)', async () => { const { issues, counters } = await main({ ...baseArguments, cwd, @@ -19,7 +19,7 @@ test('Use compilers', async () => { assert.deepEqual(counters, { ...baseCounters, devDependencies: 2, - processed: 10, // This includes .svelte and .css files - total: 10, + processed: 11, // This includes .svelte and .css files + total: 11, }); }); diff --git a/tests/subpath-imports-from-plugin.test.ts b/tests/subpath-imports-from-plugin.test.ts index d91227077..83dc37cf3 100644 --- a/tests/subpath-imports-from-plugin.test.ts +++ b/tests/subpath-imports-from-plugin.test.ts @@ -7,7 +7,7 @@ import baseCounters from './helpers/baseCounters.js'; const cwd = resolve('fixtures/subpath-import-from-plugin'); -test('Allows subpath-imports', async () => { +test('Allows subpath-imports from plugin', async () => { const { issues, counters } = await main({ ...baseArguments, cwd, @@ -18,12 +18,12 @@ test('Allows subpath-imports', async () => { assert.deepEqual(counters, { ...baseCounters, dependencies: 1, - processed: 1, - total: 1, + processed: 2, + total: 2, }); }); -test('Allows subpath-imports (production)', async () => { +test('Allows subpath-imports from plugin (production)', async () => { const { issues, counters } = await main({ ...baseArguments, cwd, @@ -35,12 +35,12 @@ test('Allows subpath-imports (production)', async () => { assert.deepEqual(counters, { ...baseCounters, dependencies: 1, - processed: 1, - total: 1, + processed: 2, + total: 2, }); }); -test('Allows subpath-imports (strict)', async () => { +test('Allows subpath-imports from plugin (strict)', async () => { const { issues, counters } = await main({ ...baseArguments, cwd, @@ -53,7 +53,7 @@ test('Allows subpath-imports (strict)', async () => { assert.deepEqual(counters, { ...baseCounters, dependencies: 1, - processed: 1, - total: 1, + processed: 2, + total: 2, }); });