Skip to content

Commit

Permalink
✨ feat: 增加loading;补充readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuuuuyu committed Jul 24, 2024
1 parent 9c874b9 commit 68630c4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# registry=https://registry.npmjs.org/
registry=https://registry.npmjs.org/
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# vite-plugin-clean
Vite插件,用于构建前清理上次构建的产物

## Install
```bash
pnpm install vite-plugin-clean -D
```

## Usage
```js
import { defineConfig } from 'vite'
import vitePluginClean from 'vite-plugin-clean'

export default defineConfig({
plugins: [
vitePluginClean()
]
})
```

## API

### options

```js
export type PluginOptions = {
folder: string | string[]
hooks?: {
buildStart?: () => void | Promise<void>
closeBundle?: () => void | Promise<void>
}
}
```
### example
```js
export default defineConfig({
plugins: [
vitePluginClean({
folder: 'dist', // default: dist or ['dist'] or ['dist', 'lib']
hooks: {
buildStart() {
console.log('build start')
}
}
})
]
})
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-clean",
"version": "0.0.2",
"version": "1.0.0",
"description": "",
"main": "dist/vite-plugin-clean.umd.js",
"module": "dist/vite-plugin-clean.es.js",
Expand All @@ -27,6 +27,7 @@
"devDependencies": {
"@types/node": "^20.14.11",
"chalk": "^5.3.0",
"ora": "^8.0.1",
"path": "^0.12.7",
"rollup-plugin-polyfill-node": "^0.13.0",
"vite-plugin-dts": "4.0.0-beta.1"
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Plugin } from "vite"
import path from "path"
import chalk from "chalk"
import ora from "ora"

import { PluginOptions } from "./types"
import { deleteFolderRecursive, log } from "./utils"
Expand All @@ -15,19 +16,21 @@ export default function vitePluginClean(options: PluginOptions): Plugin {
)
}
const folders = typeof folder === "string" ? [folder] : folder

let spinner = ora("Loading...")
return {
name: "vite-plugin-clean",
async buildStart() {
log(
chalk.blue("[vite:clean]"),
chalk.blue("\n[vite:clean]"),
chalk.green(`Start cleaning: ${folders.join(", ")}`)
)
spinner = ora("Loading...").start()

for (const folder of folders) {
const folderPath = path.resolve(process.cwd(), folder)
await deleteFolderRecursive(folderPath)
log(
chalk.blue("[vite:clean]"),
chalk.blue("\n[vite:clean]"),
chalk.green.bold(`Successfully deleted: ${folderPath}`)
)
}
Expand All @@ -36,6 +39,7 @@ export default function vitePluginClean(options: PluginOptions): Plugin {
}
},
closeBundle() {
spinner.stop()
if (hooks.closeBundle) {
hooks.closeBundle()
}
Expand Down
7 changes: 3 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import path from "path"
import dts from "vite-plugin-dts"
import vitePluginClean from "./src/index"

import chalk from "chalk" // 确保在这里导入到了 chalk
console.log("Chalk version:", chalk) // 调试输出
export default defineConfig({
plugins: [
vitePluginClean({
folder: "222",
folder: "dist",
}),
dts({
entryRoot: "src",
Expand All @@ -25,10 +23,11 @@ export default defineConfig({
},
rollupOptions: {
plugins: [polyfillNode()],
external: ["fs/promises"],
external: ["fs/promises", "ora"],
output: {
globals: {
"fs/promises": "fs/promises",
ora: "ora",
},
exports: "named",
},
Expand Down

0 comments on commit 68630c4

Please sign in to comment.