Skip to content

Commit 65d8098

Browse files
committedJul 8, 2024
Start using central resolveEntry from plugin helpers (#716)
1 parent 0fc27aa commit 65d8098

File tree

8 files changed

+38
-27
lines changed

8 files changed

+38
-27
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
testResultsProcessor: 'jest-phabricator',
3+
setupFiles: ['@workspaces-plugin-config/shared/jest-setup.ts'],
34
};

‎packages/knip/fixtures/workspaces-plugin-config/packages/backend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"test": "vitest"
55
},
66
"devDependencies": {
7+
"@workspaces-plugin-config/shared": "*",
78
"eslint-plugin-no-secrets": "*",
89
"eslint-plugin-prettier": "*",
910
"vitest": "^0.34.6"

‎packages/knip/fixtures/workspaces-plugin-config/packages/package1/jest-setup.ts

Whitespace-only changes.

‎packages/knip/fixtures/workspaces-plugin-config/packages/package1/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@workspaces-plugin-config/package-using-ava",
2+
"name": "@workspaces-plugin-config/shared",
33
"scripts": {
44
"test": "ava"
55
},

‎packages/knip/src/plugins/jest/index.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { IsPluginEnabled, Plugin, PluginOptions, ResolveConfig, ResolveEntryPaths } from '#p/types/plugins.js';
22
import { dirname, isInternal, join, toAbsolute } from '#p/util/path.js';
3-
import { hasDependency, load } from '#p/util/plugin.js';
3+
import { hasDependency, load, resolveEntry } from '#p/util/plugin.js';
44
import { toEntryPattern } from '#p/util/protocols.js';
55
import type { JestConfig, JestInitialOptions } from './types.js';
66

@@ -32,6 +32,7 @@ const resolveExtensibleConfig = async (configFilePath: string) => {
3232

3333
const resolveDependencies = async (config: JestInitialOptions, options: PluginOptions): Promise<string[]> => {
3434
const { configFileDir } = options;
35+
3536
if (config?.preset) {
3637
const { preset } = config;
3738
if (isInternal(preset)) {
@@ -40,9 +41,11 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
4041
Object.assign(config, presetConfig);
4142
}
4243
}
44+
4345
const presets = (config.preset ? [config.preset] : []).map(preset =>
4446
isInternal(preset) ? preset : join(preset, 'jest-preset')
4547
);
48+
4649
const projects = [];
4750
for (const project of config.projects ?? []) {
4851
if (typeof project === 'string') {
@@ -52,6 +55,7 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
5255
for (const dependency of dependencies) projects.push(dependency);
5356
}
5457
}
58+
5559
const runner = config.runner ? [config.runner] : [];
5660
const environments = config.testEnvironment === 'jsdom' ? ['jest-environment-jsdom'] : [];
5761
const resolvers = config.resolver ? [config.resolver] : [];
@@ -62,8 +66,6 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
6266
: [];
6367
const watchPlugins =
6468
config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
65-
const setupFiles = (config.setupFiles ?? []).map(toEntryPattern);
66-
const setupFilesAfterEnv = (config.setupFilesAfterEnv ?? []).map(toEntryPattern);
6769
const transform = config.transform
6870
? Object.values(config.transform).map(transform => (typeof transform === 'string' ? transform : transform[0]))
6971
: [];
@@ -72,11 +74,16 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
7274
? Object.values(config.moduleNameMapper).map(mapper => (typeof mapper === 'string' ? mapper : mapper[0]))
7375
: []
7476
).filter(value => !/\$[0-9]/.test(value));
77+
7578
const testResultsProcessor = config.testResultsProcessor ? [config.testResultsProcessor] : [];
7679
const snapshotResolver = config.snapshotResolver ? [config.snapshotResolver] : [];
7780
const testSequencer = config.testSequencer ? [config.testSequencer] : [];
78-
const globalSetup = (config.globalSetup ? [config.globalSetup] : []).map(toEntryPattern);
79-
const globalTeardown = (config.globalTeardown ? [config.globalTeardown] : []).map(toEntryPattern);
81+
82+
const resolve = (specifier: string) => resolveEntry(options, specifier);
83+
const setupFiles = (config.setupFiles ?? []).map(resolve);
84+
const setupFilesAfterEnv = (config.setupFilesAfterEnv ?? []).map(resolve);
85+
const globalSetup = (config.globalSetup ? [config.globalSetup] : []).map(resolve);
86+
const globalTeardown = (config.globalTeardown ? [config.globalTeardown] : []).map(resolve);
8087

8188
return [
8289
...presets,

‎packages/knip/src/plugins/vitest/index.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PackageJson } from '#p/types/package-json.js';
22
import type { IsPluginEnabled, Plugin, PluginOptions, ResolveConfig, ResolveEntryPaths } from '#p/types/plugins.js';
3-
import { isAbsolute, join, relative } from '#p/util/path.js';
4-
import { hasDependency, tryResolve } from '#p/util/plugin.js';
3+
import { join } from '#p/util/path.js';
4+
import { hasDependency, resolveEntry } from '#p/util/plugin.js';
55
import { toEntryPattern } from '#p/util/protocols.js';
66
import { getEnvPackageName, getExternalReporters } from './helpers.js';
77
import type { COMMAND, MODE, ViteConfig, ViteConfigOrFn, VitestWorkspaceConfig } from './types.js';
@@ -18,15 +18,6 @@ const config = ['vitest*.config.{js,mjs,ts,cjs,mts,cts}', 'vitest.{workspace,pro
1818

1919
const entry = ['**/*.{bench,test,test-d,spec}.?(c|m)[jt]s?(x)'];
2020

21-
const resolveEntry = (options: PluginOptions, rootDir: string, specifier: string) => {
22-
const { configFileDir, configFileName } = options;
23-
const resolvedPath = isAbsolute(specifier)
24-
? specifier
25-
: tryResolve(join(configFileDir, rootDir, specifier), join(configFileDir, rootDir, configFileName));
26-
if (resolvedPath) return toEntryPattern(relative(configFileDir, resolvedPath));
27-
return specifier;
28-
};
29-
3021
const isVitestCoverageCommand = /vitest(.+)--coverage(?:\.enabled(?:=true)?)?/;
3122

3223
const hasScriptWithCoverage = (scripts: PackageJson['scripts']) =>
@@ -51,8 +42,9 @@ const findConfigDependencies = (localConfig: ViteConfig, options: PluginOptions)
5142
const coverage = hasCoverageEnabled ? [`@vitest/coverage-${testConfig.coverage?.provider ?? 'v8'}`] : [];
5243

5344
const rootDir = testConfig.root ?? '.';
54-
const setupFiles = [testConfig.setupFiles ?? []].flat().map(v => resolveEntry(options, rootDir, v));
55-
const globalSetup = [testConfig.globalSetup ?? []].flat().map(v => resolveEntry(options, rootDir, v));
45+
const setupFiles = [testConfig.setupFiles ?? []].flat().map(specifier => resolveEntry(options, specifier, rootDir));
46+
const globalSetup = [testConfig.globalSetup ?? []].flat().map(specifier => resolveEntry(options, specifier, rootDir));
47+
5648
return [...environments, ...reporters, ...coverage, ...setupFiles, ...globalSetup];
5749
};
5850

@@ -101,7 +93,7 @@ export const resolveConfig: ResolveConfig<ViteConfigOrFn | VitestWorkspaceConfig
10193
const entry = cfg.build?.lib?.entry ?? [];
10294
const rootDir = cfg.test?.root ?? '.';
10395
const deps = (typeof entry === 'string' ? [entry] : Object.values(entry)).map(specifier =>
104-
resolveEntry(options, rootDir, specifier)
96+
resolveEntry(options, specifier, rootDir)
10597
);
10698
for (const dependency of deps) dependencies.add(dependency);
10799
}

‎packages/knip/src/util/plugin.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export { _getDependenciesFromScripts as getDependenciesFromScripts } from '../binaries/index.js';
22
export { _loadJSON as loadJSON } from './fs.js';
33
export { _load as load } from './loader.js';
4-
export { _tryResolve as tryResolve } from './require.js';
54
import type { RawPluginConfiguration } from '../types/config.js';
65
import type { Plugin, PluginOptions } from '../types/plugins.js';
76
import { arrayify } from './array.js';
87
import { _load as load } from './loader.js';
98
import { get } from './object.js';
10-
import { basename } from './path.js';
9+
import { basename, isAbsolute, join, relative } from './path.js';
1110
import { toEntryPattern, toProductionEntryPattern } from './protocols.js';
11+
import { _tryResolve as tryResolve } from './require.js';
1212

1313
export const toCamelCase = (name: string) =>
1414
name.toLowerCase().replace(/(-[a-z])/g, group => group.toUpperCase().replace('-', ''));
@@ -131,3 +131,12 @@ export const toUnconfig = toConfigMap(['json', 'ts', 'mts', 'cts', 'js', 'mjs',
131131
rcSuffix: '',
132132
configFiles: false,
133133
});
134+
135+
export const resolveEntry = (options: PluginOptions, specifier: string, rootDir = '.') => {
136+
const { configFileDir, configFileName } = options;
137+
const resolvedPath = isAbsolute(specifier)
138+
? specifier
139+
: tryResolve(join(configFileDir, rootDir, specifier), join(configFileDir, rootDir, configFileName));
140+
if (resolvedPath) return toEntryPattern(relative(configFileDir, resolvedPath));
141+
return specifier;
142+
};

‎packages/knip/test/workspaces-plugin-config.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ test('Use root plugin config in workspaces', async () => {
1515

1616
assert.deepEqual(counters, {
1717
...baseCounters,
18-
total: 25,
19-
processed: 25,
18+
total: 26,
19+
processed: 26,
2020
});
2121
});
2222

@@ -33,13 +33,14 @@ test('Use root plugin config in workspaces (strict production)', async () => {
3333
new Set([
3434
join(cwd, 'packages/frontend/components/component.js'),
3535
join(cwd, 'packages/package1/components/component.js'),
36+
join(cwd, 'packages/package1/jest-setup.ts'),
3637
])
3738
);
3839

3940
assert.deepEqual(counters, {
4041
...baseCounters,
41-
files: 2,
42-
total: 6,
43-
processed: 6,
42+
files: 3,
43+
total: 7,
44+
processed: 7,
4445
});
4546
});

0 commit comments

Comments
 (0)
Please sign in to comment.