-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from stauren/feat/first-edition
First version of vite-plugin-deadfile
- Loading branch information
Showing
10 changed files
with
905 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
dist | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"cSpell.words": [ | ||
"deadfile" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Changelog | ||
|
||
# [1.0.1](https://github.com/stauren/vite-plugin-deadfile/tree/v1.0.1) (2023-11-14) | ||
|
||
- fix a esm related bug | ||
|
||
# [1.0.0](https://github.com/stauren/vite-plugin-deadfile/tree/v1.0.0) (2023-11-14) | ||
|
||
- first version of vite-plugin-deadfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# vite-plugin-deadfile [![npm](https://img.shields.io/npm/v/vite-plugin-deadfile.svg)](https://npmjs.com/package/vite-plugin-deadfile) | ||
|
||
This plugin helps you find unused source file(dead files) in your project. | ||
|
||
```js | ||
// vite.config.js | ||
import { defineConfig } from 'vite'; | ||
import deadFile from 'vite-plugin-deadfile'; | ||
|
||
export default defineConfig({ | ||
plugins: [deadFile()], | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
### include | ||
|
||
An array of source files/folders to be compared with files referenced during compilation. | ||
|
||
If no value is provided, all files in the root directory will be considered as source files. | ||
```js | ||
import { defineConfig } from 'vite'; | ||
import deadFile from 'vite-plugin-deadfile'; | ||
|
||
export default defineConfig({ | ||
plugins: [deadFile( | ||
include: ['src'] | ||
)], | ||
}); | ||
``` | ||
|
||
### exclude | ||
|
||
An array of files/folders to be configured as non-source files, so they won't appear in the result. | ||
|
||
```js | ||
import { defineConfig } from 'vite'; | ||
import deadFile from 'vite-plugin-deadfile'; | ||
|
||
export default defineConfig({ | ||
plugins: [deadFile( | ||
include: ['src'], | ||
exclude: ['src/vendors'] | ||
)], | ||
}); | ||
``` | ||
|
||
> `node_modules` and hidden files (file with a name start with `.`) are excluded by default. | ||
### output | ||
|
||
Output file name may only contains number/letter/hyphen/underscore. | ||
|
||
If no output file name is provided, the result will be printed on console. | ||
|
||
```js | ||
import { defineConfig } from 'vite'; | ||
import deadFile from 'vite-plugin-deadfile'; | ||
|
||
export default defineConfig({ | ||
plugins: [deadFile( | ||
output: 'dead-files.txt' | ||
)], | ||
}); | ||
``` | ||
|
||
## Output format | ||
|
||
```text | ||
All source files: 123 | ||
Used source files: 120 | ||
Unused source files: 3 | ||
./path/to/unused/file-a | ||
./path/to/unused/file-b | ||
./path/to/unused/file-c | ||
``` | ||
|
||
## Caveats | ||
|
||
### Pure Type Reference can NOT be traced | ||
Imported typescript files only have their interfaces or types being referenced will not be marked as used. | ||
|
||
In the following example, `interface-a.ts` will NOT be marked as used. | ||
|
||
```typescript | ||
// interface-a.ts | ||
export interface A {} | ||
|
||
// index.ts | ||
import type { A } from '.interface-a'; | ||
export function main(param: A) {} | ||
``` | ||
This is because vite use rollup to build a project. Since rollup only build javascript files, a typescript file must be transformed into javascript before handing to rollup, vite does this with [esbuild plugin](https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/esbuild.ts) in transform hook: | ||
|
||
```typescript | ||
// vite/src/node/plugins/esbuild.ts | ||
async transform(code, id) { | ||
if (filter(id) || filter(cleanUrl(id))) { | ||
// transform ts into js with esbuild | ||
const result = await transformWithEsbuild(code, id, transformOptions) | ||
if (result.warnings.length) { | ||
result.warnings.forEach((m) => { | ||
this.warn(prettifyMessage(m, code)) | ||
}) | ||
} | ||
if (jsxInject && jsxExtensionsRE.test(id)) { | ||
result.code = jsxInject + ';' + result.code | ||
} | ||
return { | ||
code: result.code, | ||
map: result.map, | ||
} | ||
} | ||
}, | ||
``` | ||
|
||
Similarly, [@rollup/plugin-typescript](https://github.com/rollup/plugins/blob/master/packages/typescript/src/index.ts#L161) uses the content of pre-compiled javascript files of requested typescript files in `load` hook to do the trick. | ||
|
||
Either way, the imports of pure type references are lost after files are compiled into javascript. So they will be wrongly considered as not used. | ||
|
||
There are several tsconfig about the elimination of type imports: `verbatimModuleSyntax`, `preserveValueImports`, `importsNotUsedAsValues`. It seems they are either not useful or conflicting with vite, so it not possible to trace the references of pure types for now. | ||
|
||
### Check before delete | ||
Some unreferenced files such as markdowns may be useful, check again before deleting those files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "vite-plugin-deadfile", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
"author": "stauren@qq.com", | ||
"type": "module", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"scripts": { | ||
"build": "rollup -c rollup.config.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/stauren/vite-plugin-deadfile.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/stauren/vite-plugin-deadfile/issues" | ||
}, | ||
"homepage": "https://github.com/stauren/vite-plugin-deadfile/tree/main/#readme", | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^11.1.5", | ||
"@types/node": "^20.8.10", | ||
"rollup": "^4.4.1", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.2.2", | ||
"vite": "^4.2.0" | ||
}, | ||
"peerDependencies": { | ||
"vite": "^4.2.0" | ||
} | ||
} |
Oops, something went wrong.