Skip to content

Commit f255504

Browse files
beaussanBioPhoton
andauthored
chore: setup knip to lint the workspace (#555)
This pr introduce a knip config for this repo. Setting up knip with an Nx Integrated workspace requires a bit more trickery than a regular npm workspace, so I created a library for it. The library is still in it's early form, but I will maintain and evolve it's api here if I make any breaking changes. Source for the library can be found [here](https://github.com/beaussan/nx-tools/tree/main/packages/nx-knip). To see the generated knip config, you can run `DEBUG=true npm run knip`, it's traversing Nx's graph of library, executors and configuration to infer a knip config dynamically. Running knip on this repo yeild the following output : ``` Unused files (4) packages/cli/code-pushup.config.mock.ts packages/plugin-lighthouse/src/index.ts testing/test-utils/src/lib/fixtures/configs/code-pushup.config.js testing/test-utils/src/lib/fixtures/configs/code-pushup.empty.config.js Unused devDependencies (15) @nodelib/fs.walk package.json @trivago/prettier-plugin-sort-imports package.json @types/benchmark package.json benchmark package.json commitlint-plugin-tense package.json conventional-changelog-angular package.json eslint-plugin-jsx-a11y package.json eslint-plugin-react package.json eslint-plugin-react-hooks package.json fast-glob package.json glob package.json globby package.json jsonc package.json ngx-deploy-npm package.json tsx package.json Unlisted dependencies (16) jsonc-eslint-parser .eslintrc.json @commitlint/types commitlint.config.js jsonc-eslint-parser e2e/cli-e2e/.eslintrc.json jsonc-eslint-parser packages/cli/.eslintrc.json jsonc-eslint-parser packages/core/.eslintrc.json jsonc-eslint-parser packages/models/.eslintrc.json jsonc-eslint-parser packages/nx-plugin/.eslintrc.json jsonc-eslint-parser packages/plugin-coverage/.eslintrc.json jsonc-eslint-parser packages/plugin-eslint/.eslintrc.json jsonc-eslint-parser packages/plugin-lighthouse/.eslintrc.json jsonc-eslint-parser packages/utils/.eslintrc.json jsonc-eslint-parser testing/test-setup/.eslintrc.json jsonc-eslint-parser testing/test-utils/.eslintrc.json @nx/eslint/src/executors/lint/schema tools/eslint-to-code-pushup.mjs minimatch tools/eslint-to-code-pushup.mjs tslib tsconfig.base.json Unused exports (24) renderUploadAutorunHint function packages/cli/src/lib/collect/collect-command.ts:45:17 yargsPersistConfigOptionsDefinition function packages/cli/src/lib/implementation/core-config.options.ts:17:17 yargsUploadConfigOptionsDefinition function packages/cli/src/lib/implementation/core-config.options.ts:37:17 singletonUiInstance unknown packages/cli/src/lib/implementation/logging.ts:8:12 duplicateRefsInCategoryMetricsErrorMsg function packages/models/src/lib/category-config.ts:54:17 unrefinedCoreConfigSchema unknown packages/models/src/lib/core-config.ts:11:14 refineCoreConfig function packages/models/src/lib/core-config.ts:32:17 groupMetaSchema unknown packages/models/src/lib/group.ts:20:14 weightSchema unknown packages/models/src/lib/implementation/schemas.ts:132:14 descriptionSchema unknown packages/models/src/lib/implementation/schemas.ts:40:14 docsUrlSchema unknown packages/models/src/lib/implementation/schemas.ts:49:14 titleSchema unknown packages/models/src/lib/implementation/schemas.ts:55:14 pluginDataSchema unknown packages/models/src/lib/plugin-config.ts:30:14 coverageTypeSchema unknown packages/plugin-coverage/src/lib/config.ts:3:14 coverageResultSchema unknown packages/plugin-coverage/src/lib/config.ts:6:14 WORKDIR unknown packages/plugin-eslint/src/lib/runner/index.ts:9:14 NoExportError class packages/utils/src/lib/file-system.ts:78:14 messageStyles unknown packages/utils/src/lib/progress.ts:13:14 h function packages/utils/src/lib/reports/md/headline.ts:13:17 h4 function packages/utils/src/lib/reports/md/headline.ts:25:17 h5 function packages/utils/src/lib/reports/md/headline.ts:29:17 h6 function packages/utils/src/lib/reports/md/headline.ts:33:17 GroupRefInvalidError class packages/utils/src/lib/reports/scoring.ts:10:14 throwIsNotPresentError function packages/utils/src/lib/reports/utils.ts:233:17 Unused exported types (7) CliUi type packages/cli/src/lib/implementation/logging.ts:5:13 GroupMeta type packages/models/src/lib/group.ts:26:13 LighthouseCliOptions type packages/plugin-lighthouse/src/lib/utils.ts:11:13 FontStyle type packages/utils/src/lib/reports/md/font-style.ts:8:13 Hierarchy type packages/utils/src/lib/reports/md/headline.ts:2:13 Order type packages/utils/src/lib/reports/md/list.ts:1:13 ScoredCategoryConfig type packages/utils/src/lib/reports/types.ts:9:13 Duplicate exports (2) addToProjectGenerator, default packages/nx-plugin/src/generators/configuration/generator.ts initGenerator, default packages/nx-plugin/src/generators/init/generator.ts ``` --------- Co-authored-by: Michael <michael.hladky@push-based.io> Co-authored-by: Michael Hladky <10064416+BioPhoton@users.noreply.github.com>
1 parent cbb5797 commit f255504

3 files changed

Lines changed: 1399 additions & 3 deletions

File tree

knip.config.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import {
2+
KnipConfigPlugin,
3+
combineNxKnipPlugins,
4+
withEsbuildApps,
5+
withEsbuildPublishableLibs,
6+
withEslint,
7+
withLibraryMapper,
8+
withLocalNxPlugins,
9+
withNxTsPaths,
10+
withVitest,
11+
} from '@beaussan/nx-knip';
12+
13+
const withIgnoreMockInLibs = () =>
14+
withLibraryMapper({
15+
mapperFn: ({ rootFolder }) => {
16+
return {
17+
ignore: [rootFolder + '/mocks/**', rootFolder + '/perf/**'],
18+
entry: [rootFolder + '/src/bin.ts', rootFolder + '/perf/**/index.ts'],
19+
};
20+
},
21+
});
22+
23+
const withExamplePlugins = (): KnipConfigPlugin => () => {
24+
return {
25+
// Given there is no publish target, thoes libs were not picked up by the auto discovery
26+
entry: [
27+
'examples/plugins/src/index.ts',
28+
'packages/plugin-lighthouse/src/index.ts',
29+
],
30+
};
31+
};
32+
33+
const withReactExample = (): KnipConfigPlugin => () => {
34+
return {
35+
entry: [
36+
'examples/react-todos-app/src/index.jsx',
37+
'examples/react-todos-app/test-setup.js',
38+
],
39+
eslint: {
40+
// Given there is no lint target on the project, we need to manually specify the entry point
41+
config: ['examples/react-todos-app/.eslintrc.js'],
42+
},
43+
};
44+
};
45+
46+
const withNxStandards = (): KnipConfigPlugin => () => {
47+
return {
48+
project: ['**/*.{ts,js,tsx,jsx}'],
49+
ignore: ['tmp/**', 'node_modules/**'],
50+
commitlint: {
51+
config: ['commitlint.config.js'],
52+
},
53+
exclude: ['duplicates'],
54+
entry: [
55+
// unknown why this is needed, it should be picked up by knip from the vitest setup files
56+
'testing/test-utils/src/index.ts',
57+
'testing/test-utils/src/lib/fixtures/configs/*.ts',
58+
'testing/test-setup/src/index.ts',
59+
'testing/test-setup/src/lib/**/*.{js,mjs,ts,cjs,mts,cts}',
60+
'global-setup.ts',
61+
'global-setup.e2e.ts',
62+
'examples/react-todos-app/code-pushup.config.js',
63+
'examples/plugins/code-pushup.config.ts',
64+
'testing/test-utils/src/lib/fixtures/configs/code-pushup.config.js',
65+
'testing/test-utils/src/lib/fixtures/configs/code-pushup.empty.config.js',
66+
'examples/plugins/src/package-json/src/index.ts',
67+
// missing knip plugin for now, so this is in the root entry
68+
'packages/models/zod2md.config.ts',
69+
'code-pushup.config.ts',
70+
'esbuild.config.js',
71+
'tools/**/*.{js,mjs,ts,cjs,mts,cts}',
72+
],
73+
ignoreDependencies: [
74+
'prettier',
75+
'@swc/helpers',
76+
'@swc/cli',
77+
'@nx/plugin',
78+
'@nx/workspace',
79+
// Same issue as the other vitest related, it should be picked up by knip from the vitest setup files
80+
// 'global-setup.ts',
81+
// 'global-setup.e2e.ts',
82+
83+
// Knip should be able to pick up this
84+
'tsx',
85+
// Not a npm library, and resolved in a different typescript path than the global import one
86+
'@example/custom-plugin',
87+
88+
// Prettier magic resolve is not picked up by knip
89+
'@trivago/prettier-plugin-sort-imports',
90+
],
91+
};
92+
};
93+
94+
export default combineNxKnipPlugins(
95+
withNxTsPaths(),
96+
withLocalNxPlugins({ pluginNames: ['nx-plugin'] }),
97+
withEsbuildApps(),
98+
withEsbuildPublishableLibs(),
99+
withVitest(),
100+
withIgnoreMockInLibs(),
101+
withEslint(),
102+
withReactExample(),
103+
withExamplePlugins(),
104+
withNxStandards(),
105+
);

0 commit comments

Comments
 (0)