Skip to content

Commit 1ce0b40

Browse files
beaussanwebpro
andauthoredFeb 6, 2024
feat: support nx crystal for nx plugin (#496)
* feat: support nx crystal for nx plugin * chore: fix lint * fix: make target defaults only rely on scopped plugins * tests: added an extra test case * chore: fix typo * Update packages/knip/fixtures/plugins/nx-crystal/package.json Co-authored-by: Lars Kappert <lars@webpro.nl> --------- Co-authored-by: Lars Kappert <lars@webpro.nl>
1 parent a369c40 commit 1ce0b40

File tree

5 files changed

+156
-2
lines changed

5 files changed

+156
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"plugins": [
3+
{
4+
"plugin": "@nx/webpack/plugin",
5+
"options": {
6+
"buildTargetName": "build",
7+
"serveTargetName": "serve",
8+
"previewTargetName": "preview"
9+
}
10+
},
11+
{
12+
"plugin": "@nx/eslint/plugin",
13+
"options": {
14+
"targetName": "lint"
15+
}
16+
},
17+
{
18+
"plugin": "@nx/playwright/plugin",
19+
"options": {
20+
"targetName": "e2e"
21+
}
22+
},
23+
{
24+
"plugin": "@nx/jest/plugin",
25+
"options": {
26+
"targetName": "test"
27+
}
28+
},
29+
{
30+
"plugin": "@nx/vite/plugin",
31+
"options": {
32+
"buildTargetName": "build",
33+
"previewTargetName": "preview",
34+
"testTargetName": "test",
35+
"serveTargetName": "serve",
36+
"serveStaticTargetName": "serve-static"
37+
}
38+
}
39+
],
40+
"generators": {
41+
"@nx/react": {
42+
"application": {
43+
"babel": true,
44+
"style": "css",
45+
"linter": "eslint",
46+
"bundler": "webpack"
47+
},
48+
"component": {
49+
"style": "css"
50+
},
51+
"library": {
52+
"style": "css",
53+
"linter": "eslint",
54+
"unitTestRunner": "vitest"
55+
}
56+
}
57+
},
58+
"targetDefaults": {
59+
"@nx/rollup:rollup": {
60+
"cache": true,
61+
"dependsOn": ["^build"],
62+
"inputs": ["production", "^production"]
63+
},
64+
"my-custom-thing": {
65+
"cache": true,
66+
"dependsOn": ["^my-custom-thing"],
67+
"inputs": ["production", "^production"]
68+
},
69+
"test:custom-thing": {
70+
"cache": true,
71+
"dependsOn": ["^my-custom-thing"],
72+
"inputs": ["production", "^production"]
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@fixtures/nx-crystal",
3+
"scripts": {
4+
"nx": "nx"
5+
},
6+
"devDependencies": {
7+
"@nx/webpack": "*",
8+
"@nx/eslint": "*",
9+
"@nx/playwright": "*",
10+
"@nx/jest": "*",
11+
"@nx/vite": "*",
12+
"@nx/cypress": "*",
13+
"@nx/react": "*",
14+
"@nx/rollup": "*",
15+
"@nrwl/workspace": "*"
16+
}
17+
}

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { compact } from '../../util/array.js';
2+
import { getPackageNameFromModuleSpecifier } from '../../util/modules.js';
23
import { timerify } from '../../util/Performance.js';
34
import { getDependenciesFromScripts, hasDependency, load } from '../../util/plugin.js';
4-
import type { NxProjectConfiguration } from './types.js';
5+
import type { NxConfigRoot, NxProjectConfiguration } from './types.js';
56
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
67

78
const NAME = 'Nx';
@@ -10,13 +11,44 @@ const ENABLERS = ['nx', /^@nrwl\//, /^@nx\//];
1011

1112
const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
1213

13-
const CONFIG_FILE_PATTERNS = ['project.json', '{apps,libs}/**/project.json'];
14+
const CONFIG_FILE_PATTERNS = ['nx.json', 'project.json', '{apps,libs}/**/project.json'];
15+
16+
const findNxDependenciesInNxJson: GenericPluginCallback = async configFilePath => {
17+
const localConfig: NxConfigRoot | undefined = await load(configFilePath);
18+
19+
if (!localConfig) return [];
20+
21+
const targetsDefault = localConfig.targetDefaults
22+
? Object.keys(localConfig.targetDefaults)
23+
// Ensure we only grab executors from plugins instead of manual targets
24+
// Limiting to scoped packages to ensure we don't have false positives
25+
.filter(it => it.includes(':') && it.startsWith('@'))
26+
.map(it => it.split(':')[0])
27+
: [];
28+
29+
const plugins =
30+
localConfig.plugins && Array.isArray(localConfig.plugins)
31+
? localConfig.plugins.map(it => getPackageNameFromModuleSpecifier(it.plugin)).filter(value => value !== undefined)
32+
: [];
33+
34+
const generators = localConfig.generators
35+
? Object.keys(localConfig.generators)
36+
.map(it => getPackageNameFromModuleSpecifier(it))
37+
.filter(value => value !== undefined)
38+
: [];
39+
40+
return compact([...targetsDefault, ...plugins, ...generators]);
41+
};
1442

1543
const findNxDependencies: GenericPluginCallback = async (configFilePath, options) => {
1644
const { isProduction } = options;
1745

1846
if (isProduction) return [];
1947

48+
if (configFilePath.endsWith('nx.json')) {
49+
return findNxDependenciesInNxJson(configFilePath, options);
50+
}
51+
2052
const localConfig: NxProjectConfiguration | undefined = await load(configFilePath);
2153

2254
if (!localConfig) return [];

‎packages/knip/src/plugins/nx/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ export interface NxProjectConfiguration {
99
};
1010
};
1111
}
12+
13+
export interface NxConfigRoot {
14+
plugins?: Array<{
15+
plugin: string;
16+
}>
17+
generators?: Record<string, unknown>;
18+
targetDefaults?: Record<string, unknown>;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import assert from 'node:assert/strict';
2+
import test from 'node:test';
3+
import { default as nx } from '../../src/plugins/nx/index.js';
4+
import { resolve, join } from '../../src/util/path.js';
5+
import { buildOptions } from '../helpers/index.js';
6+
7+
const cwd = resolve('fixtures/plugins/nx-crystal');
8+
const options = buildOptions(cwd);
9+
10+
test('Find dependencies in Nx configuration nx.json', async () => {
11+
const configFilePath = join(cwd, 'nx.json');
12+
const dependencies = await nx.findDependencies(configFilePath, options);
13+
assert.deepEqual(dependencies, [
14+
'@nx/rollup',
15+
'@nx/webpack',
16+
'@nx/eslint',
17+
'@nx/playwright',
18+
'@nx/jest',
19+
'@nx/vite',
20+
'@nx/react',
21+
]);
22+
});

0 commit comments

Comments
 (0)
Please sign in to comment.