From 68630c4d8e5f57e3253e1ae846d70964cca0f213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E6=99=BA=E5=8B=87?= Date: Wed, 24 Jul 2024 16:15:29 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E5=A2=9E=E5=8A=A0loading?= =?UTF-8?q?=EF=BC=9B=E8=A1=A5=E5=85=85readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .npmrc | 2 +- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- src/index.ts | 10 +++++++--- vite.config.ts | 7 +++---- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/.npmrc b/.npmrc index 75086b7..214c29d 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1 @@ -# registry=https://registry.npmjs.org/ +registry=https://registry.npmjs.org/ diff --git a/README.md b/README.md index 2a4f362..cf9e100 100644 --- a/README.md +++ b/README.md @@ -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 + closeBundle?: () => void | Promise + } +} +``` +### example +```js +export default defineConfig({ + plugins: [ + vitePluginClean({ + folder: 'dist', // default: dist or ['dist'] or ['dist', 'lib'] + hooks: { + buildStart() { + console.log('build start') + } + } + }) + ] +}) +``` \ No newline at end of file diff --git a/package.json b/package.json index 0d6cbdb..9b8191f 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/index.ts b/src/index.ts index 8b42c37..ab1d6b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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" @@ -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}`) ) } @@ -36,6 +39,7 @@ export default function vitePluginClean(options: PluginOptions): Plugin { } }, closeBundle() { + spinner.stop() if (hooks.closeBundle) { hooks.closeBundle() } diff --git a/vite.config.ts b/vite.config.ts index bfe4cbc..1dff7ce 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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", @@ -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", },