knip 2.41.1 → 2.41.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,51 +31,51 @@ The dots don't connect themselves. This is where Knip comes in:
31
31
 
32
32
  Knip shines in both small and large projects. It's a fresh take on keeping your projects clean & tidy!
33
33
 
34
- [![An orange cow with scissors, Van Gogh style][10]][9] <sup>_“An orange cow with scissors, Van Gogh style” - generated
35
- with OpenAI_</sup>
34
+ ![An orange cow with scissors, Van Gogh style][9] <sup>_“An orange cow with scissors, Van Gogh style” - generated with
35
+ OpenAI_</sup>
36
36
 
37
- For updates or questions, come hang out in [The Knip Barn (Discord)][11], or follow [@webprolific (Twitter)][12] or
38
- [@webpro (fosstodon.org)][13]. Please use GitHub to [report issues][14].
37
+ For updates or questions, come hang out in [The Knip Barn (Discord)][10], or follow [@webprolific (Twitter)][11] or
38
+ [@webpro (fosstodon.org)][12]. Please use GitHub to [report issues][13].
39
39
 
40
40
  ## Contents
41
41
 
42
- - [Getting Started][15]
43
- - [Installation][16]
44
- - [Default Configuration][17]
45
- - [Let's Go!][18]
46
- - [Configuration][19]
47
- - [Entry Files][20]
42
+ - [Getting Started][14]
43
+ - [Installation][15]
44
+ - [Default Configuration][16]
45
+ - [Let's Go!][17]
46
+ - [Configuration][18]
47
+ - [Entry Files][19]
48
48
  - [Workspaces][1]
49
49
  - [Plugins][2]
50
50
  - [Compilers][3]
51
- - [Ignore files, binaries, dependencies and workspaces][21]
52
- - [Public exports][22]
53
- - [Ignore exports used in file][23]
54
- - [Include exports in entry files][24]
55
- - [Paths][25]
51
+ - [Ignore files, binaries, dependencies and workspaces][20]
52
+ - [Public exports][21]
53
+ - [Ignore exports used in file][22]
54
+ - [Include exports in entry files][23]
55
+ - [Paths][24]
56
56
  - [Production Mode][4]
57
- - [Strict][26]
58
- - [Ignore `@internal` exports][27]
59
- - [Plugins][28]
60
- - [Output][29]
61
- - [Screenshots][30]
62
- - [Reading the report][31]
63
- - [Rules & Filters][32]
57
+ - [Strict][25]
58
+ - [Ignore `@internal` exports][26]
59
+ - [Plugins][27]
60
+ - [Output][28]
61
+ - [Screenshots][29]
62
+ - [Reading the report][30]
63
+ - [Rules & Filters][31]
64
64
  - [Reporters][5]
65
- - [Fixing Issues][33]
65
+ - [Fixing Issues][32]
66
66
  - [JSDoc tags][8]
67
- - [Command Line Options][34]
68
- - [Potential boost with `--no-gitignore`][35]
69
- - [Comparison & Migration][36]
70
- - [depcheck][37]
71
- - [unimported][38]
72
- - [ts-unused-exports][39]
73
- - [ts-prune][40]
74
- - [Projects using Knip][41]
75
- - [Articles, etc.][42]
76
- - [Why "Knip"?][43]
77
- - [Really, another unused file/dependency/export finder?][44]
78
- - [Contributors][45]
67
+ - [Command Line Options][33]
68
+ - [Potential boost with `--no-gitignore`][34]
69
+ - [Comparison & Migration][35]
70
+ - [depcheck][36]
71
+ - [unimported][37]
72
+ - [ts-unused-exports][38]
73
+ - [ts-prune][39]
74
+ - [Projects using Knip][40]
75
+ - [Articles, etc.][41]
76
+ - [Why "Knip"?][42]
77
+ - [Really, another unused file/dependency/export finder?][43]
78
+ - [Contributors][44]
79
79
 
80
80
  ## Getting Started
81
81
 
@@ -100,7 +100,7 @@ Knip has good defaults and aims for no or little configuration. The (simplified)
100
100
  }
101
101
  ```
102
102
 
103
- There's more, jump to [Entry Files][20] for details.
103
+ There's more, jump to [Entry Files][19] for details.
104
104
 
105
105
  Knip looks for this configuration file (ordered by priority):
106
106
 
@@ -156,7 +156,7 @@ Then use `npm run knip` to analyze the project and output unused files, dependen
156
156
 
157
157
  Knip works just fine with `yarn` or `pnpm` or `bun`.
158
158
 
159
- See [Command Line Options][34] for an overview of available CLI options.
159
+ See [Command Line Options][33] for an overview of available CLI options.
160
160
 
161
161
  ## Configuration
162
162
 
@@ -219,7 +219,7 @@ Here's an example `knip.json` configuration with some custom `entry` and `projec
219
219
  ```
220
220
 
221
221
  It might be useful to run Knip first with no or little configuration to see where it needs custom `entry` and/or
222
- `project` files. Each workspace has the same [default configuration][19].
222
+ `project` files. Each workspace has the same [default configuration][18].
223
223
 
224
224
  The root workspace is named `"."` under `workspaces` (like in the example).
225
225
 
@@ -320,7 +320,7 @@ themselves and/or `entry` files for Knip to analyze.
320
320
 
321
321
  See each plugin's documentation for its default values.
322
322
 
323
- In an Astro, Svelte or Vue project? Make sure to see [compilers][46] and add some extra configuration.
323
+ In an Astro, Svelte or Vue project? Make sure to see [compilers][45] and add some extra configuration.
324
324
 
325
325
  #### `config`
326
326
 
@@ -359,7 +359,7 @@ level by setting it to `false` there, and vice versa.
359
359
  #### Multi-project repositories
360
360
 
361
361
  Some repositories have a single `package.json`, but consist of multiple projects with configuration files across the
362
- repository (such as the [Nx "intregrated repo" style][47]). Let's assume some of these projects are apps and have their
362
+ repository (such as the [Nx "intregrated repo" style][46]). Let's assume some of these projects are apps and have their
363
363
  own Cypress configuration and test files. In that case, we could configure the Cypress plugin like this:
364
364
 
365
365
  ```json
@@ -372,7 +372,7 @@ own Cypress configuration and test files. In that case, we could configure the C
372
372
 
373
373
  #### Create a new plugin
374
374
 
375
- Getting false positives because a plugin is missing? Want to help out? Please read more at [writing a plugin][48]. This
375
+ Getting false positives because a plugin is missing? Want to help out? Please read more at [writing a plugin][47]. This
376
376
  guide also contains more details if you want to learn more about plugins and why they are useful.
377
377
 
378
378
  ### Compilers
@@ -395,7 +395,7 @@ export default {
395
395
  };
396
396
  ```
397
397
 
398
- Read [Compilers][46] for more details and examples.
398
+ Read [Compilers][45] for more details and examples.
399
399
 
400
400
  ### Ignore files, binaries, dependencies and workspaces
401
401
 
@@ -567,7 +567,7 @@ Note that `devDependencies` is covered within a single key for all `dependencies
567
567
 
568
568
  When an issue type has zero issues, it is not shown.
569
569
 
570
- Getting too many reported issues and false positives? Read more about [handling issues][49].
570
+ Getting too many reported issues and false positives? Read more about [handling issues][48].
571
571
 
572
572
  ### Rules & Filters
573
573
 
@@ -594,7 +594,7 @@ Example:
594
594
  }
595
595
  ```
596
596
 
597
- See [reading the report][31] for the list of issue types.
597
+ See [reading the report][30] for the list of issue types.
598
598
 
599
599
  The rules are modeled after the ESLint `rules` configuration, and could be extended in the future. For instance, to
600
600
  apply filters or configurations only to a specific issue type.
@@ -619,7 +619,7 @@ knip --include files --exclude classMembers,enumMembers
619
619
 
620
620
  Use `--dependencies` or `--exports` as shortcuts to combine groups of related types.
621
621
 
622
- See [reading the report][31] for the list of issue types.
622
+ See [reading the report][30] for the list of issue types.
623
623
 
624
624
  #### When to use rules or filters
625
625
 
@@ -643,13 +643,13 @@ Knip provides the following built-in reporters:
643
643
 
644
644
  When the provided built-in reporters are not sufficient, a custom reporter can be implemented.
645
645
 
646
- Find more details in [reporters and preprocessors][50].
646
+ Find more details in [reporters and preprocessors][49].
647
647
 
648
648
  #### Preprocessers
649
649
 
650
650
  Use preprocessers to modify the results before they're passed to the reporter(s).
651
651
 
652
- Find more details in [reporters and preprocessors][50].
652
+ Find more details in [reporters and preprocessors][49].
653
653
 
654
654
  ## Fixing Issues
655
655
 
@@ -667,7 +667,7 @@ Tip: back up files or use an VCS like Git before deleting files or making change
667
667
 
668
668
  Repeat the process to reveal new unused files and exports. It's so liberating to remove unused things!
669
669
 
670
- Getting too many reported issues and false positives? Read more about [handling issues][49] describing potential causes
670
+ Getting too many reported issues and false positives? Read more about [handling issues][48] describing potential causes
671
671
  for false positives, and how to handle them.
672
672
 
673
673
  ## JSDoc tags
@@ -733,13 +733,13 @@ $ npx knip --help
733
733
 
734
734
  ## Potential boost with `--no-gitignore`
735
735
 
736
- To increase performance in a large monorepo, check out [Potential boost with `--no-gitignore`][51].
736
+ To increase performance in a large monorepo, check out [Potential boost with `--no-gitignore`][50].
737
737
 
738
738
  ## Comparison & Migration
739
739
 
740
740
  This table is an ongoing comparison. Based on their docs (please report any mistakes):
741
741
 
742
- | Feature | **knip** | [depcheck][52] | [unimported][53] | [ts-unused-exports][54] | [ts-prune][55] |
742
+ | Feature | **knip** | [depcheck][51] | [unimported][52] | [ts-unused-exports][53] | [ts-prune][54] |
743
743
  | :---------------------- | :------: | :------------: | :--------------: | :---------------------: | :------------: |
744
744
  | Unused files | ✅ | - | ✅ | - | - |
745
745
  | Unused dependencies | ✅ | ✅ | ✅ | - | - |
@@ -805,25 +805,25 @@ knip --exports # Adds unused exports/types in namespaces and unused enum/class
805
805
 
806
806
  Many thanks to some of the early adopters of Knip:
807
807
 
808
- - [Block Protocol][56]
809
- - [DeepmergeTS][57]
810
- - [eslint-plugin-functional][58]
811
- - [freeCodeCamp.org][59]
812
- - [is-immutable-type][60]
813
- - [IsaacScript][61]
814
- - [Nuxt][62]
815
- - [Owncast][63]
816
- - [release-it][64]
817
- - [Template TypeScript Node Package][65]
818
- - [Tipi][66]
808
+ - [Block Protocol][55]
809
+ - [DeepmergeTS][56]
810
+ - [eslint-plugin-functional][57]
811
+ - [freeCodeCamp.org][58]
812
+ - [is-immutable-type][59]
813
+ - [IsaacScript][60]
814
+ - [Nuxt][61]
815
+ - [Owncast][62]
816
+ - [release-it][63]
817
+ - [Template TypeScript Node Package][64]
818
+ - [Tipi][65]
819
819
 
820
820
  ## Articles, etc.
821
821
 
822
- - Discord: hang out in [The Knip Barn][11]
823
- - Ask your questions in the [Knip knowledge base][67] (powered by OpenAI and [7-docs][68], experimental!)
824
- - Smashing Magazine: [Knip: An Automated Tool For Finding Unused Files, Exports, And Dependencies][69]
825
- - Effective TypeScript: [Recommendation Update: ✂️ Use knip to detect dead code and types][70]
826
- - Josh Goldberg: [Speeding Up Centered Part 4: Unused Code Bloat][71]
822
+ - Discord: hang out in [The Knip Barn][10]
823
+ - Ask your questions in the [Knip knowledge base][66] (powered by OpenAI and [7-docs][67], experimental!)
824
+ - Smashing Magazine: [Knip: An Automated Tool For Finding Unused Files, Exports, And Dependencies][68]
825
+ - Effective TypeScript: [Recommendation Update: ✂️ Use knip to detect dead code and types][69]
826
+ - Josh Goldberg: [Speeding Up Centered Part 4: Unused Code Bloat][70]
827
827
 
828
828
  ## Why "Knip"?
829
829
 
@@ -841,7 +841,7 @@ each file, and traversing all of this, why not collect the various issues in one
841
841
 
842
842
  Special thanks to the wonderful people who have contributed to this project:
843
843
 
844
- [![Contributors][73]][72]
844
+ [![Contributors][72]][71]
845
845
 
846
846
  [1]: #workspaces
847
847
  [2]: #plugins
@@ -851,71 +851,70 @@ Special thanks to the wonderful people who have contributed to this project:
851
851
  [6]: #custom-reporters
852
852
  [7]: #preprocessers
853
853
  [8]: #jsdoc-tags
854
- [9]: https://labs.openai.com/s/xZQACaLepaKya0PRUPtIN5dC
855
- [10]: ./assets/cow-with-orange-scissors-van-gogh-style.webp
856
- [11]: https://discord.gg/r5uXTtbTpc
857
- [12]: https://twitter.com/webprolific
858
- [13]: https://fosstodon.org/@webpro
859
- [14]: https://github.com/webpro/knip/issues
860
- [15]: #getting-started
861
- [16]: #installation
862
- [17]: #default-configuration
863
- [18]: #lets-go
864
- [19]: #configuration
865
- [20]: #entry-files
866
- [21]: #ignore-files-binaries-dependencies-and-workspaces
867
- [22]: #public-exports
868
- [23]: #ignore-exports-used-in-file
869
- [24]: #include-exports-in-entry-files
870
- [25]: #paths
871
- [26]: #strict
872
- [27]: #ignore-internal-exports
873
- [28]: #plugins-1
874
- [29]: #output
875
- [30]: #screenshots
876
- [31]: #reading-the-report
877
- [32]: #rules--filters
878
- [33]: #fixing-issues
879
- [34]: #command-line-options
880
- [35]: #potential-boost-with---no-gitignore
881
- [36]: #comparison--migration
882
- [37]: #depcheck
883
- [38]: #unimported
884
- [39]: #ts-unused-exports
885
- [40]: #ts-prune
886
- [41]: #projects-using-knip
887
- [42]: #articles-etc
888
- [43]: #why-knip
889
- [44]: #really-another-unused-filedependencyexport-finder
890
- [45]: #contributors
891
- [46]: ./docs/compilers.md
892
- [47]: https://nx.dev/concepts/integrated-vs-package-based
893
- [48]: ./docs/writing-a-plugin.md
894
- [49]: ./docs/handling-issues.md
895
- [50]: ./docs/reporters-and-preprocessors.md
896
- [51]: ./docs/perf-boost-with-no-gitignore.md
897
- [52]: https://github.com/depcheck/depcheck
898
- [53]: https://github.com/smeijer/unimported
899
- [54]: https://github.com/pzavolinsky/ts-unused-exports
900
- [55]: https://github.com/nadeesha/ts-prune
901
- [56]: https://github.com/blockprotocol/blockprotocol
902
- [57]: https://github.com/RebeccaStevens/deepmerge-ts
903
- [58]: https://github.com/eslint-functional/eslint-plugin-functional
904
- [59]: https://github.com/freeCodeCamp/freeCodeCamp
905
- [60]: https://github.com/RebeccaStevens/is-immutable-type
906
- [61]: https://github.com/IsaacScript/isaacscript
907
- [62]: https://github.com/nuxt/nuxt
908
- [63]: https://github.com/owncast/owncast
909
- [64]: https://github.com/release-it/release-it
910
- [65]: https://github.com/JoshuaKGoldberg/template-typescript-node-package
911
- [66]: https://github.com/meienberger/runtipi
912
- [67]: https://knip.deno.dev
913
- [68]: https://github.com/7-docs/7-docs
914
- [69]: https://www.smashingmagazine.com/2023/08/knip-automated-tool-find-unused-files-exports-dependencies/
915
- [70]: https://effectivetypescript.com/2023/07/29/knip/
916
- [71]: https://www.joshuakgoldberg.com/blog/speeding-up-centered-part-4-unused-code-bloat/
917
- [72]: https://github.com/webpro/knip/graphs/contributors
918
- [73]: https://contrib.rocks/image?repo=webpro/knip
854
+ [9]: ./assets/cow-with-orange-scissors-van-gogh-style.webp
855
+ [10]: https://discord.gg/r5uXTtbTpc
856
+ [11]: https://twitter.com/webprolific
857
+ [12]: https://fosstodon.org/@webpro
858
+ [13]: https://github.com/webpro/knip/issues
859
+ [14]: #getting-started
860
+ [15]: #installation
861
+ [16]: #default-configuration
862
+ [17]: #lets-go
863
+ [18]: #configuration
864
+ [19]: #entry-files
865
+ [20]: #ignore-files-binaries-dependencies-and-workspaces
866
+ [21]: #public-exports
867
+ [22]: #ignore-exports-used-in-file
868
+ [23]: #include-exports-in-entry-files
869
+ [24]: #paths
870
+ [25]: #strict
871
+ [26]: #ignore-internal-exports
872
+ [27]: #plugins-1
873
+ [28]: #output
874
+ [29]: #screenshots
875
+ [30]: #reading-the-report
876
+ [31]: #rules--filters
877
+ [32]: #fixing-issues
878
+ [33]: #command-line-options
879
+ [34]: #potential-boost-with---no-gitignore
880
+ [35]: #comparison--migration
881
+ [36]: #depcheck
882
+ [37]: #unimported
883
+ [38]: #ts-unused-exports
884
+ [39]: #ts-prune
885
+ [40]: #projects-using-knip
886
+ [41]: #articles-etc
887
+ [42]: #why-knip
888
+ [43]: #really-another-unused-filedependencyexport-finder
889
+ [44]: #contributors
890
+ [45]: ./docs/compilers.md
891
+ [46]: https://nx.dev/concepts/integrated-vs-package-based
892
+ [47]: ./docs/writing-a-plugin.md
893
+ [48]: ./docs/handling-issues.md
894
+ [49]: ./docs/reporters-and-preprocessors.md
895
+ [50]: ./docs/perf-boost-with-no-gitignore.md
896
+ [51]: https://github.com/depcheck/depcheck
897
+ [52]: https://github.com/smeijer/unimported
898
+ [53]: https://github.com/pzavolinsky/ts-unused-exports
899
+ [54]: https://github.com/nadeesha/ts-prune
900
+ [55]: https://github.com/blockprotocol/blockprotocol
901
+ [56]: https://github.com/RebeccaStevens/deepmerge-ts
902
+ [57]: https://github.com/eslint-functional/eslint-plugin-functional
903
+ [58]: https://github.com/freeCodeCamp/freeCodeCamp
904
+ [59]: https://github.com/RebeccaStevens/is-immutable-type
905
+ [60]: https://github.com/IsaacScript/isaacscript
906
+ [61]: https://github.com/nuxt/nuxt
907
+ [62]: https://github.com/owncast/owncast
908
+ [63]: https://github.com/release-it/release-it
909
+ [64]: https://github.com/JoshuaKGoldberg/template-typescript-node-package
910
+ [65]: https://github.com/meienberger/runtipi
911
+ [66]: https://knip.deno.dev
912
+ [67]: https://github.com/7-docs/7-docs
913
+ [68]: https://www.smashingmagazine.com/2023/08/knip-automated-tool-find-unused-files-exports-dependencies/
914
+ [69]: https://effectivetypescript.com/2023/07/29/knip/
915
+ [70]: https://www.joshuakgoldberg.com/blog/speeding-up-centered-part-4-unused-code-bloat/
916
+ [71]: https://github.com/webpro/knip/graphs/contributors
917
+ [72]: https://contrib.rocks/image?repo=webpro/knip
919
918
  [plugin-angular]: ./src/plugins/angular
920
919
  [plugin-astro]: ./src/plugins/astro
921
920
  [plugin-ava]: ./src/plugins/ava
package/dist/constants.js CHANGED
@@ -9,6 +9,7 @@ export const IGNORED_GLOBAL_BINARIES = [
9
9
  'cat',
10
10
  'cd',
11
11
  'chmod',
12
+ 'corepack',
12
13
  'cp',
13
14
  'curl',
14
15
  'deno',
@@ -1,11 +1,18 @@
1
+ import { isInternal } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
4
+ import { toEntryPattern } from '../../util/protocols.js';
3
5
  import { isConfigurationOutput } from './types.js';
4
6
  export const NAME = 'GraphQL Codegen';
5
7
  export const ENABLERS = [/^@graphql-codegen\//];
6
8
  export const PACKAGE_JSON_PATH = 'codegen';
7
9
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
8
- export const CONFIG_FILE_PATTERNS = ['codegen.{ts,js,json,yml,mjs,cts}', 'package.json'];
10
+ export const CONFIG_FILE_PATTERNS = [
11
+ 'codegen.{json,yml,yaml,js,ts,mjs,cts}',
12
+ '.codegenrc.{json,yml,yaml,js,ts}',
13
+ 'codegen.config.js',
14
+ 'package.json',
15
+ ];
9
16
  const findPluginDependencies = async (configFilePath, options) => {
10
17
  const { manifest, isProduction } = options;
11
18
  if (isProduction)
@@ -27,7 +34,13 @@ const findPluginDependencies = async (configFilePath, options) => {
27
34
  .map(plugin => `@graphql-codegen/${plugin}`);
28
35
  const nestedPlugins = configurationOutput
29
36
  .flatMap(configOutput => (configOutput.plugins ? configOutput.plugins : []))
30
- .map(plugin => `@graphql-codegen/${plugin}`);
37
+ .flatMap(plugin => {
38
+ if (typeof plugin !== 'string')
39
+ return [];
40
+ if (isInternal(plugin))
41
+ return [toEntryPattern(plugin)];
42
+ return [`@graphql-codegen/${plugin}`];
43
+ });
31
44
  return [...presets, ...flatPlugins, ...nestedPlugins];
32
45
  };
33
46
  export const findDependencies = timerify(findPluginDependencies);
@@ -4,7 +4,7 @@ import { findVitestDependencies } from '../vitest/index.js';
4
4
  export const NAME = 'Vite';
5
5
  export const ENABLERS = ['vite'];
6
6
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
7
- export const CONFIG_FILE_PATTERNS = ['vite.config.{js,ts}'];
7
+ export const CONFIG_FILE_PATTERNS = ['vite.config.{js,mjs,ts,cjs,mts,cts}'];
8
8
  const findViteDependencies = async (configFilePath, options) => {
9
9
  const localConfig = await load(configFilePath);
10
10
  if (!localConfig)
@@ -24,7 +24,9 @@ const findConfigDependencies = (configFilePath, localConfig, options) => {
24
24
  return entryPatterns;
25
25
  const environments = testConfig.environment ? [getEnvPackageName(testConfig.environment)] : [];
26
26
  const reporters = getExternalReporters(testConfig.reporters);
27
- const coverage = testConfig.coverage ? [`@vitest/coverage-${testConfig.coverage.provider ?? 'v8'}`] : [];
27
+ const coverage = testConfig.coverage && testConfig.coverage.enabled !== false
28
+ ? [`@vitest/coverage-${testConfig.coverage.provider ?? 'v8'}`]
29
+ : [];
28
30
  const setupFiles = [testConfig.setupFiles ?? []].flat().map(v => resolveEntry(configFilePath, v));
29
31
  const globalSetup = [testConfig.globalSetup ?? []].flat().map(v => resolveEntry(configFilePath, v));
30
32
  return [...entryPatterns, ...environments, ...reporters, ...coverage, ...setupFiles, ...globalSetup];
@@ -2,6 +2,7 @@ interface VitestConfig {
2
2
  test: {
3
3
  include: string[];
4
4
  coverage?: {
5
+ enabled?: boolean;
5
6
  provider: string;
6
7
  };
7
8
  environment?: string;
package/dist/util/fs.d.ts CHANGED
@@ -5,4 +5,4 @@ export declare const loadJSON: (filePath: string) => Promise<any>;
5
5
  export declare const loadYAML: (filePath: string) => Promise<unknown>;
6
6
  export declare const parseJSON: (filePath: string, contents: string) => Promise<any>;
7
7
  export declare const parseYAML: (contents: string) => Promise<unknown>;
8
- export declare function isTypeModule(filePath: string): boolean;
8
+ export declare function isTypeModule(path: string): boolean;
package/dist/util/fs.js CHANGED
@@ -40,25 +40,18 @@ export const parseJSON = async (filePath, contents) => {
40
40
  export const parseYAML = async (contents) => {
41
41
  return yaml.load(contents);
42
42
  };
43
- export function isTypeModule(filePath) {
44
- if (!filePath.endsWith('.js'))
45
- return false;
46
- try {
47
- let currentDir = dirname(filePath);
48
- while (true) {
49
- const packagePath = join(currentDir, 'package.json');
43
+ export function isTypeModule(path) {
44
+ while (path && path !== '.' && path !== '/') {
45
+ path = dirname(path);
46
+ try {
47
+ const pkg = readFileSync(join(path, 'package.json'), 'utf-8');
50
48
  try {
51
- const data = JSON.parse(readFileSync(packagePath, 'utf-8'));
52
- return data.type === 'module';
49
+ return JSON.parse(pkg).type === 'module';
53
50
  }
54
51
  catch { }
55
- const parentDir = dirname(currentDir);
56
- if (parentDir === currentDir)
57
- return false;
58
- currentDir = parentDir;
52
+ break;
59
53
  }
54
+ catch { }
60
55
  }
61
- catch (error) {
62
- return false;
63
- }
56
+ return false;
64
57
  }
@@ -4,7 +4,7 @@ import { loadJSON, loadYAML, loadFile, parseJSON, parseYAML } from './fs.js';
4
4
  import { isTypeModule } from './fs.js';
5
5
  import { extname } from './path.js';
6
6
  import { timerify } from './Performance.js';
7
- import { jiti } from './register.js';
7
+ import { jitiCJS, jitiESM } from './register.js';
8
8
  export const FAKE_PATH = '__FAKE__';
9
9
  const load = async (filePath) => {
10
10
  if (filePath === FAKE_PATH)
@@ -26,7 +26,12 @@ const load = async (filePath) => {
26
26
  const imported = await import(fileUrl.href);
27
27
  return imported.default ?? imported;
28
28
  }
29
- return await jiti(filePath);
29
+ if (isTypeModule(filePath)) {
30
+ return await jitiESM(filePath);
31
+ }
32
+ else {
33
+ return await jitiCJS(filePath);
34
+ }
30
35
  }
31
36
  catch (error) {
32
37
  throw new LoaderError(`Error loading ${filePath}`, { cause: error });
@@ -1 +1,2 @@
1
- export declare const jiti: any;
1
+ export declare const jitiCJS: any;
2
+ export declare const jitiESM: any;
@@ -1,14 +1,10 @@
1
- import module from 'node:module';
2
- import createJITI from 'jiti';
3
- import { IGNORED_FILE_EXTENSIONS, DEFAULT_EXTENSIONS } from '../constants.js';
4
- const _extensions = module.Module._extensions;
5
- export const jiti = createJITI(process.cwd(), { interopDefault: true, extensions: DEFAULT_EXTENSIONS });
6
- if (!('.ts' in _extensions)) {
7
- jiti.register();
8
- }
9
- const exportFilePath = (module, filePath) => {
10
- module.exports = filePath;
1
+ import createJITI, {} from 'jiti';
2
+ import { DEFAULT_EXTENSIONS } from '../constants.js';
3
+ const options = {
4
+ interopDefault: true,
5
+ extensions: DEFAULT_EXTENSIONS,
6
+ esmResolve: false,
11
7
  };
12
- IGNORED_FILE_EXTENSIONS.forEach(ext => {
13
- _extensions[ext] = exportFilePath;
14
- });
8
+ const createLoader = (options) => createJITI(process.cwd(), options);
9
+ export const jitiCJS = createLoader(options);
10
+ export const jitiESM = createLoader({ ...options, esmResolve: true });
@@ -5,11 +5,11 @@ import { debugLog } from './debug.js';
5
5
  import { getPackageNameFromModuleSpecifier } from './modules.js';
6
6
  import { cwd, toPosix, join } from './path.js';
7
7
  import { timerify } from './Performance.js';
8
- import { jiti } from './register.js';
8
+ import { jitiCJS } from './register.js';
9
9
  const createRequire = (path) => nodeCreateRequire(pathToFileURL(path ?? cwd));
10
10
  const require = createRequire();
11
11
  export const _require = timerify(require);
12
- const resolve = (specifier) => toPosix(jiti.resolve(specifier));
12
+ const resolve = (specifier) => toPosix(jitiCJS.resolve(specifier));
13
13
  const tryResolve = (specifier, from) => {
14
14
  try {
15
15
  return resolve(specifier);
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.41.1";
1
+ export declare const version = "2.41.3";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.41.1';
1
+ export const version = '2.41.3';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "2.41.1",
3
+ "version": "2.41.3",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://github.com/webpro/knip",
6
6
  "repository": "github:webpro/knip",