|
| 1 | +--- |
| 2 | +title: Announcing Knip v5 |
| 3 | +date: 2024-02-10 |
| 4 | +sidebar: |
| 5 | + order: 1 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Tabs, TabItem } from '@astrojs/starlight/components'; |
| 9 | + |
| 10 | +_Published: 2024-02-10_ |
| 11 | + |
| 12 | +Today brings the smallest major release so far. Tiny yet mighty! |
| 13 | + |
| 14 | +Below are two cases to demonstrate the change in how unused exports are |
| 15 | +reported. |
| 16 | + |
| 17 | +## Case 1 (referenced) |
| 18 | + |
| 19 | +The first case shows two exports with a namespaced import that references one of |
| 20 | +those exports explicitly: |
| 21 | + |
| 22 | +```ts title="knip.js" |
| 23 | +export const version = 'v5'; |
| 24 | +export const getRocket = () => 🚀; |
| 25 | +``` |
| 26 | + |
| 27 | +```ts title="index.js" |
| 28 | +import * as NS from './knip.js'; |
| 29 | + |
| 30 | +console.log(NS.version); |
| 31 | +``` |
| 32 | + |
| 33 | +In this case we see that `getRocket` is an unused export. |
| 34 | + |
| 35 | +Previously it would go into the "Unused exports in namespaces" category |
| 36 | +(`nsExports`). This issue has been moved to the "Unused exports" category |
| 37 | +(`exports`). |
| 38 | + |
| 39 | +## Case 2 |
| 40 | + |
| 41 | +The second case is similar, but only the imported namespace itself is |
| 42 | +referenced. None of the individual exports is referenced: |
| 43 | + |
| 44 | +```ts title="index.js" |
| 45 | +import * as NS from './knip.js'; |
| 46 | +import send from 'stats'; |
| 47 | + |
| 48 | +send(NS); |
| 49 | +``` |
| 50 | + |
| 51 | +Are the `version` and `getRocket` exports used? We can't know. The same is true |
| 52 | +for the spread object pattern: |
| 53 | + |
| 54 | +```ts title="index.js" |
| 55 | +import * as Knip from './knip.js'; |
| 56 | + |
| 57 | +const Cut = { ...NS }; |
| 58 | +``` |
| 59 | + |
| 60 | +Previously those exports would go into the "Unused exports in namespaces" |
| 61 | +category. This is still the case, but this category is no longer enabled by |
| 62 | +default. |
| 63 | + |
| 64 | +## Include unused exports in namespaces |
| 65 | + |
| 66 | +To enable this type of issues in Knip v5, add this argument to the command: |
| 67 | + |
| 68 | +```shell |
| 69 | +knip --include nsExports |
| 70 | +``` |
| 71 | + |
| 72 | +Or in your configuration file: |
| 73 | + |
| 74 | +```json title="knip.json" |
| 75 | +{ |
| 76 | + "include": ["nsExports", "nsTypes"] |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Now `version` and `getRocket` will be reported as "Unused exports in |
| 81 | +namespaces". |
| 82 | + |
| 83 | +Note that `nsExports` and `nsTypes` are split for more granular control. |
| 84 | + |
| 85 | +## Handling exports in namespaced imports |
| 86 | + |
| 87 | +You have a few options to handle namespaced imports when it comes to unused |
| 88 | +exports. |
| 89 | + |
| 90 | +### 1. Use named imports |
| 91 | + |
| 92 | +Regardless of whether `nsExports` is enabled or not, it's often good practice to |
| 93 | +replace the namespaced imports with named imports: |
| 94 | + |
| 95 | +```ts title="index.js" |
| 96 | +import { version, getRocket } from './knip.js'; |
| 97 | + |
| 98 | +send({ version, getRocket }); |
| 99 | +``` |
| 100 | + |
| 101 | +Whenever possible, explicit over implicit is often the better choice. |
| 102 | + |
| 103 | +### 2. Standardized JSDoc tags |
| 104 | + |
| 105 | +Using one of the available JSDoc tags like `@public` or `@internal`: |
| 106 | + |
| 107 | +```ts title="knip.js" |
| 108 | +export const version = 'v5'; |
| 109 | +/** @public */ |
| 110 | +export const getRocket = () => 🚀; |
| 111 | +``` |
| 112 | + |
| 113 | +Assuming only imported using a namespace (like in the example cases above), this |
| 114 | +will exclude the `getRocket` export from the report, even though it isn't |
| 115 | +explicitly referenced. |
| 116 | + |
| 117 | +### 3. Arbitrary JSDoc tags |
| 118 | + |
| 119 | +Another solution is to tag individual exports arbitrarily: |
| 120 | + |
| 121 | +```ts title="knip.js" |
| 122 | +export const version = 'v5'; |
| 123 | +/** @launch */ |
| 124 | +export const getRocket = () => 🚀; |
| 125 | +``` |
| 126 | + |
| 127 | +And then exclude the tag like so: |
| 128 | + |
| 129 | +```shell |
| 130 | +$ knip --experimental-tags=-launch |
| 131 | +Exports in used namespace (1) |
| 132 | +version NS unknown knip.js:1:1 |
| 133 | +``` |
| 134 | + |
| 135 | +Assuming only imported using a namespace (like in the example cases above), this |
| 136 | +will exclude the `getRocket` export from the report, even though it isn't |
| 137 | +explicitly referenced. |
| 138 | + |
| 139 | +## A better default |
| 140 | + |
| 141 | +I believe this behavior in v5 is the better default: have all exports you want |
| 142 | +to know about in a single category, and those you probably want to ignore in |
| 143 | +another that's disabled by default. |
| 144 | + |
| 145 | +Before the [v4 refactoring][1], this would be a lot harder to implement. That |
| 146 | +refactoring turns out to be a better investment than expected. Combined with a |
| 147 | +better understanding of how people write code and use Knip, this change is a |
| 148 | +natural iteration. |
| 149 | + |
| 150 | +Why the major bump? It's not breaking for the large majority of users, but for |
| 151 | +some it may be breaking. For instance when relying on the [JSON reporter][2], |
| 152 | +other reporter output, or custom [preprocessing][3]. It's not a bug fix, it's |
| 153 | +not a new feature, but since semver is all about setting expectations I feel the |
| 154 | +change is large enough to warrant a major bump. |
| 155 | + |
| 156 | +## Let's Go! |
| 157 | + |
| 158 | +What are you waiting for? Start using Knip v5 today! |
| 159 | + |
| 160 | +<Tabs> |
| 161 | + <TabItem label="npm"> |
| 162 | + |
| 163 | + ```shell |
| 164 | + npm install -D knip |
| 165 | + ``` |
| 166 | + |
| 167 | + </TabItem> |
| 168 | + <TabItem label="pnpm"> |
| 169 | + |
| 170 | + ```shell |
| 171 | + pnpm add -D knip |
| 172 | + ``` |
| 173 | + |
| 174 | + </TabItem> |
| 175 | + <TabItem label="bun"> |
| 176 | + |
| 177 | + ```shell |
| 178 | + bun add -D knip |
| 179 | + ``` |
| 180 | + |
| 181 | + </TabItem> |
| 182 | + <TabItem label="yarn"> |
| 183 | + |
| 184 | + ```shell |
| 185 | + yarn add -D knip |
| 186 | + ``` |
| 187 | + |
| 188 | + </TabItem> |
| 189 | +</Tabs> |
| 190 | + |
| 191 | +Remember, Knip it before you ship it! Have a great day ☀️ |
| 192 | + |
| 193 | +[1]: ../blog/slim-down-to-speed-up.md |
| 194 | +[2]: ../features/reporters.md#json |
| 195 | +[3]: ../features/reporters.md#preprocessors |
0 commit comments