Skip to content

Commit

Permalink
✨ feat: vite构建前清理上一次构建产物
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuuuuyu committed Jul 24, 2024
1 parent 66d23aa commit 9c874b9
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist/
pnpm-lock.yaml
package-lock.json
.DS_Store
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# registry=https://registry.npmjs.org/
21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no">
<link rel="icon" href="/favicon.ico">
<title>Axios</title>
</head>

<body>
<noscript>
<strong>We're sorry but blank doesn't work properly without JavaScript enabled. Please enable it to
continue.</strong>
</noscript>
<div id="app"></div>
</body>

</html>
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "vite-plugin-clean",
"version": "0.0.2",
"description": "",
"main": "dist/vite-plugin-clean.umd.js",
"module": "dist/vite-plugin-clean.es.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"build": "vite build"
},
"files": [
"dist/*"
],
"keywords": [
"vite",
"build",
"plugin",
"clean"
],
"author": "于智勇|[email protected]",
"license": "ISC",
"dependencies": {
"typescript": "^5.5.4",
"vite": "^5.3.4"
},
"devDependencies": {
"@types/node": "^20.14.11",
"chalk": "^5.3.0",
"path": "^0.12.7",
"rollup-plugin-polyfill-node": "^0.13.0",
"vite-plugin-dts": "4.0.0-beta.1"
}
}
45 changes: 45 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Plugin } from "vite"
import path from "path"
import chalk from "chalk"

import { PluginOptions } from "./types"
import { deleteFolderRecursive, log } from "./utils"

export default function vitePluginClean(options: PluginOptions): Plugin {
const { folder = "dist", hooks = {} } = options || {}
if (typeof folder !== "string" && !Array.isArray(folder)) {
throw new Error(
chalk.blue.bgRed.bold(
"Invalid value for folder: must be a string or an array of strings."
)
)
}
const folders = typeof folder === "string" ? [folder] : folder

return {
name: "vite-plugin-clean",
async buildStart() {
log(
chalk.blue("[vite:clean]"),
chalk.green(`Start cleaning: ${folders.join(", ")}`)
)
for (const folder of folders) {
const folderPath = path.resolve(process.cwd(), folder)
await deleteFolderRecursive(folderPath)
log(
chalk.blue("[vite:clean]"),
chalk.green.bold(`Successfully deleted: ${folderPath}`)
)
}
if (hooks.buildStart) {
await hooks.buildStart()
}
},
closeBundle() {
if (hooks.closeBundle) {
hooks.closeBundle()
}
},
}
}

7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type PluginOptions = {
folder: string | string[]
hooks?: {
buildStart?: () => void | Promise<void>
closeBundle?: () => void | Promise<void>
}
}
31 changes: 31 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import path from "path"
import fs from "fs/promises"

export const log = console.log
export async function deleteFolderRecursive(folderPath: string) {
try {
// 检查文件夹是否存在
await fs.stat(folderPath)

const files = await fs.readdir(folderPath)
await Promise.all(
files.map(async file => {
const curPath = path.join(folderPath, file)
const stat = await fs.stat(curPath)
if (stat.isDirectory()) {
await deleteFolderRecursive(curPath)
} else {
await fs.unlink(curPath)
}
})
)
await fs.rmdir(folderPath)
} catch (error) {
if ((error as { code?: string }).code === "ENOENT") {
return
} else {
console.error(`Error deleting folder: ${folderPath}`, error)
}
}
}

3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import chalk from 'chalk';

console.log(chalk.blue('Hello, world!'));
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true, // 生成 .d.ts 文件
"outDir": "dist" // 输出目录
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist"
]
}
38 changes: 38 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { defineConfig } from "vite"
import polyfillNode from "rollup-plugin-polyfill-node"
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",
}),
dts({
entryRoot: "src",
outputDir: "dist/types",
tsConfigFilePath: "./tsconfig.json",
}),
],
build: {
lib: {
entry: path.resolve(__dirname, "src/index.ts"),
name: "VitePluginClean",
fileName: format => `vite-plugin-clean.${format}.js`,
},
rollupOptions: {
plugins: [polyfillNode()],
external: ["fs/promises"],
output: {
globals: {
"fs/promises": "fs/promises",
},
exports: "named",
},
},
},
})

0 comments on commit 9c874b9

Please sign in to comment.