-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
205 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,5 @@ | ||
node_modules | ||
dist/ | ||
pnpm-lock.yaml | ||
package-lock.json | ||
.DS_Store |
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 @@ | ||
# registry=https://registry.npmjs.org/ |
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,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> |
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,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" | ||
} | ||
} |
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,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() | ||
} | ||
}, | ||
} | ||
} | ||
|
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,7 @@ | ||
export type PluginOptions = { | ||
folder: string | string[] | ||
hooks?: { | ||
buildStart?: () => void | Promise<void> | ||
closeBundle?: () => void | Promise<void> | ||
} | ||
} |
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,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) | ||
} | ||
} | ||
} | ||
|
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 @@ | ||
import chalk from 'chalk'; | ||
|
||
console.log(chalk.blue('Hello, world!')); |
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,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" | ||
] | ||
} |
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 @@ | ||
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", | ||
}, | ||
}, | ||
}, | ||
}) | ||
|