@meng-xi/vite-plugin 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README-en.md +150 -0
  2. package/README.md +84 -138
  3. package/dist/common/index.cjs +1 -0
  4. package/dist/common/index.d.cts +109 -0
  5. package/dist/common/index.d.mts +109 -0
  6. package/dist/common/index.d.ts +109 -0
  7. package/dist/common/index.mjs +1 -0
  8. package/dist/factory/index.cjs +1 -0
  9. package/dist/factory/index.d.cts +260 -0
  10. package/dist/factory/index.d.mts +260 -0
  11. package/dist/factory/index.d.ts +260 -0
  12. package/dist/factory/index.mjs +1 -0
  13. package/dist/index.cjs +1 -3
  14. package/dist/index.d.cts +7 -282
  15. package/dist/index.d.mts +7 -282
  16. package/dist/index.d.ts +7 -282
  17. package/dist/index.mjs +1 -3
  18. package/dist/logger/index.cjs +1 -0
  19. package/dist/logger/index.d.cts +1 -0
  20. package/dist/logger/index.d.mts +1 -0
  21. package/dist/logger/index.d.ts +1 -0
  22. package/dist/logger/index.mjs +1 -0
  23. package/dist/plugins/index.cjs +1 -0
  24. package/dist/plugins/index.d.cts +210 -0
  25. package/dist/plugins/index.d.mts +210 -0
  26. package/dist/plugins/index.d.ts +210 -0
  27. package/dist/plugins/index.mjs +1 -0
  28. package/dist/shared/vite-plugin.B3PARlU9.d.cts +119 -0
  29. package/dist/shared/vite-plugin.B3PARlU9.d.mts +119 -0
  30. package/dist/shared/vite-plugin.B3PARlU9.d.ts +119 -0
  31. package/dist/shared/vite-plugin.BT1oHRKK.cjs +1 -0
  32. package/dist/shared/vite-plugin.BTKhc7n7.cjs +3 -0
  33. package/dist/shared/vite-plugin.BZqhBDYR.mjs +1 -0
  34. package/dist/shared/vite-plugin.Bn8mcCzy.cjs +3 -0
  35. package/dist/shared/vite-plugin.CY2ydccp.mjs +3 -0
  36. package/dist/shared/vite-plugin.CiHfwMiN.d.cts +91 -0
  37. package/dist/shared/vite-plugin.CiHfwMiN.d.mts +91 -0
  38. package/dist/shared/vite-plugin.CiHfwMiN.d.ts +91 -0
  39. package/dist/shared/vite-plugin.ClHiVXD6.mjs +1 -0
  40. package/dist/shared/vite-plugin.DSRKYuae.mjs +3 -0
  41. package/dist/shared/vite-plugin.DrSzERYS.cjs +1 -0
  42. package/dist/shared/vite-plugin.UkE7CdSe.d.cts +43 -0
  43. package/dist/shared/vite-plugin.UkE7CdSe.d.mts +43 -0
  44. package/dist/shared/vite-plugin.UkE7CdSe.d.ts +43 -0
  45. package/package.json +72 -57
  46. package/dist/plugins/copyFile/index.d.ts +0 -44
  47. package/dist/plugins/copyFile/index.mjs +0 -43
  48. package/dist/plugins/copyFile/type.d.ts +0 -47
  49. package/dist/plugins/copyFile/type.mjs +0 -0
  50. package/dist/plugins/injectIco/index.d.ts +0 -74
  51. package/dist/plugins/injectIco/index.mjs +0 -82
  52. package/dist/plugins/injectIco/type.d.ts +0 -115
  53. package/dist/plugins/injectIco/type.mjs +0 -0
@@ -0,0 +1,3 @@
1
+ function s(...n){const u={};for(const r of n)if(r)for(const t in r){const i=r[t],e=u[t];typeof i=="object"&&i!==null&&!Array.isArray(i)&&typeof e=="object"&&e!==null&&!Array.isArray(e)?u[t]=s(e,i):u[t]=i}return u}class o{options;currentField=null;errors=[];constructor(u){this.options=u}field(u){const r=this;return r.currentField=u,r}required(){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");return this.options[this.currentField]==null&&this.errors.push(`${String(this.currentField)} \u662F\u5FC5\u586B\u5B57\u6BB5`),this}string(){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");const u=this.options[this.currentField];return u!=null&&typeof u!="string"&&this.errors.push(`${String(this.currentField)} \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`),this}boolean(){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");const u=this.options[this.currentField];return u!=null&&typeof u!="boolean"&&this.errors.push(`${String(this.currentField)} \u5FC5\u987B\u662F\u5E03\u5C14\u7C7B\u578B`),this}number(){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");const u=this.options[this.currentField];return u!=null&&typeof u!="number"&&this.errors.push(`${String(this.currentField)} \u5FC5\u987B\u662F\u6570\u5B57\u7C7B\u578B`),this}array(){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");const u=this.options[this.currentField];return u!=null&&!Array.isArray(u)&&this.errors.push(`${String(this.currentField)} \u5FC5\u987B\u662F\u6570\u7EC4\u7C7B\u578B`),this}object(){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");const u=this.options[this.currentField];return u!=null&&typeof u!="object"&&!Array.isArray(u)&&this.errors.push(`${String(this.currentField)} \u5FC5\u987B\u662F\u5BF9\u8C61\u7C7B\u578B`),this}default(u){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");return this.options[this.currentField]==null&&(this.options[this.currentField]=u),this}custom(u,r){if(this.currentField===null)throw new Error("\u5FC5\u987B\u5148\u8C03\u7528 field() \u65B9\u6CD5\u6307\u5B9A\u8981\u9A8C\u8BC1\u7684\u5B57\u6BB5");const t=this.options[this.currentField];return t!=null&&!u(t)&&this.errors.push(r),this}validate(){if(this.errors.length>0)throw new Error(`\u914D\u7F6E\u9A8C\u8BC1\u5931\u8D25\uFF1A
2
+ ${this.errors.map(u=>`- ${u}`).join(`
3
+ `)}`);return this.options}}export{o as V,s as d};
@@ -0,0 +1 @@
1
+ "use strict";const s=require("fs"),l=require("path");function _interopDefaultCompat(u){return u&&typeof u=="object"&&"default"in u?u.default:u}const s__default=_interopDefaultCompat(s),l__default=_interopDefaultCompat(l);async function checkSourceExists(u){try{await s__default.promises.access(u,s__default.constants.F_OK)}catch(t){const e=t;throw e.code==="ENOENT"?new Error(`\u590D\u5236\u6587\u4EF6\u5931\u8D25\uFF1A\u6E90\u6587\u4EF6\u4E0D\u5B58\u5728 - ${u}`):e.code==="EACCES"?new Error(`\u590D\u5236\u6587\u4EF6\u5931\u8D25\uFF1A\u6CA1\u6709\u6743\u9650\u8BBF\u95EE\u6E90\u6587\u4EF6 - ${u}`):new Error(`\u590D\u5236\u6587\u4EF6\u5931\u8D25\uFF1A\u68C0\u67E5\u6E90\u6587\u4EF6\u65F6\u51FA\u9519 - ${u}\uFF0C\u9519\u8BEF\uFF1A${e.message}`)}}async function ensureTargetDir(u){try{await s__default.promises.mkdir(u,{recursive:!0})}catch(t){const e=t;throw e.code==="EACCES"?new Error(`\u590D\u5236\u6587\u4EF6\u5931\u8D25\uFF1A\u6CA1\u6709\u6743\u9650\u521B\u5EFA\u76EE\u6807\u76EE\u5F55 - ${u}`):new Error(`\u590D\u5236\u6587\u4EF6\u5931\u8D25\uFF1A\u521B\u5EFA\u76EE\u6807\u76EE\u5F55\u65F6\u51FA\u9519 - ${u}\uFF0C\u9519\u8BEF\uFF1A${e.message}`)}}async function readDirRecursive(u,t){const e=await s__default.promises.readdir(u,{withFileTypes:!0});let i=[];for(const F of e){const o=l__default.join(u,F.name);if(F.isDirectory()){if(i.push(o),t){const E=await readDirRecursive(o,t);i=[...i,...E]}}else i.push(o)}return i}async function shouldUpdateFile(u,t){try{const e=await s__default.promises.stat(u),i=await s__default.promises.stat(t);return e.mtimeMs>i.mtimeMs||e.size!==i.size}catch{return!0}}async function copySourceToTarget(u,t,e){const i=Date.now(),{recursive:F,overwrite:o,incremental:E=!1}=e;let w=0,m=0,D=0;if((await s__default.promises.stat(u)).isDirectory()){await ensureTargetDir(t);const a=await readDirRecursive(u,F),y=(await Promise.all(a.map(async r=>{const n=await s__default.promises.stat(r);return{path:r,isFile:n.isFile()}}))).filter(r=>r.isFile).map(r=>r.path);for(const r of y){const n=l__default.relative(u,r),p=l__default.join(t,n);await ensureTargetDir(l__default.dirname(p));let c=o;if(!c)try{await s__default.promises.access(p,s__default.constants.F_OK),c=!1}catch{c=!0}E&&c&&(c=await shouldUpdateFile(r,p)),c?(await s__default.promises.copyFile(r,p),w++):m++}D=(await Promise.all(a.map(async r=>{const n=await s__default.promises.stat(r);return{path:r,isDirectory:n.isDirectory()}}))).filter(r=>r.isDirectory).length}else{await ensureTargetDir(l__default.dirname(t));let a=o;if(!a)try{await s__default.promises.access(t,s__default.constants.F_OK),a=!1}catch{a=!0}E&&a&&(a=await shouldUpdateFile(u,t)),a?(await s__default.promises.copyFile(u,t),w++):m++}const f=Date.now()-i;return{copiedFiles:w,skippedFiles:m,copiedDirs:D,executionTime:f}}async function writeFileContent(u,t){try{await s__default.promises.writeFile(u,t,"utf-8")}catch(e){const i=e;throw i.code==="EACCES"?new Error(`\u5199\u5165\u6587\u4EF6\u5931\u8D25\uFF1A\u6CA1\u6709\u6743\u9650\u5199\u5165\u6587\u4EF6 - ${u}`):new Error(`\u5199\u5165\u6587\u4EF6\u5931\u8D25\uFF1A\u5199\u5165\u6587\u4EF6\u65F6\u51FA\u9519 - ${u}\uFF0C\u9519\u8BEF\uFF1A${i.message}`)}}function readFileSync(u){try{return s__default.readFileSync(u,"utf-8")}catch(t){const e=t;throw e.code==="EACCES"?new Error(`\u8BFB\u53D6\u6587\u4EF6\u5931\u8D25\uFF1A\u6CA1\u6709\u6743\u9650\u8BFB\u53D6\u6587\u4EF6 - ${u}`):new Error(`\u8BFB\u53D6\u6587\u4EF6\u5931\u8D25\uFF1A\u8BFB\u53D6\u6587\u4EF6\u65F6\u51FA\u9519 - ${u}\uFF0C\u9519\u8BEF\uFF1A${e.message}`)}}exports.checkSourceExists=checkSourceExists,exports.copySourceToTarget=copySourceToTarget,exports.ensureTargetDir=ensureTargetDir,exports.readDirRecursive=readDirRecursive,exports.readFileSync=readFileSync,exports.shouldUpdateFile=shouldUpdateFile,exports.writeFileContent=writeFileContent;
@@ -0,0 +1,43 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /**
4
+ * 基础插件配置
5
+ *
6
+ * @interface BasePluginOptions
7
+ */
8
+ interface BasePluginOptions {
9
+ /**
10
+ * 是否启用插件
11
+ *
12
+ * @default true
13
+ */
14
+ enabled?: boolean;
15
+ /**
16
+ * 是否启用日志
17
+ *
18
+ * @default true
19
+ */
20
+ verbose?: boolean;
21
+ /**
22
+ * 错误处理策略
23
+ *
24
+ * @default 'throw'
25
+ */
26
+ errorStrategy?: 'throw' | 'log' | 'ignore';
27
+ }
28
+ /**
29
+ * 插件选项标准化器类型
30
+ *
31
+ * @template T 目标选项类型
32
+ * @template R 原始选项类型
33
+ */
34
+ type OptionsNormalizer<T, R = any> = (raw?: R) => T;
35
+ /**
36
+ * 插件工厂函数类型
37
+ *
38
+ * @template T 插件配置类型,默认继承自 BasePluginOptions
39
+ * @template R 原始配置类型,默认与 T 相同
40
+ */
41
+ type PluginFactory<T extends BasePluginOptions = BasePluginOptions, R = T> = (options?: R) => Plugin;
42
+
43
+ export type { BasePluginOptions as B, OptionsNormalizer as O, PluginFactory as P };
@@ -0,0 +1,43 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /**
4
+ * 基础插件配置
5
+ *
6
+ * @interface BasePluginOptions
7
+ */
8
+ interface BasePluginOptions {
9
+ /**
10
+ * 是否启用插件
11
+ *
12
+ * @default true
13
+ */
14
+ enabled?: boolean;
15
+ /**
16
+ * 是否启用日志
17
+ *
18
+ * @default true
19
+ */
20
+ verbose?: boolean;
21
+ /**
22
+ * 错误处理策略
23
+ *
24
+ * @default 'throw'
25
+ */
26
+ errorStrategy?: 'throw' | 'log' | 'ignore';
27
+ }
28
+ /**
29
+ * 插件选项标准化器类型
30
+ *
31
+ * @template T 目标选项类型
32
+ * @template R 原始选项类型
33
+ */
34
+ type OptionsNormalizer<T, R = any> = (raw?: R) => T;
35
+ /**
36
+ * 插件工厂函数类型
37
+ *
38
+ * @template T 插件配置类型,默认继承自 BasePluginOptions
39
+ * @template R 原始配置类型,默认与 T 相同
40
+ */
41
+ type PluginFactory<T extends BasePluginOptions = BasePluginOptions, R = T> = (options?: R) => Plugin;
42
+
43
+ export type { BasePluginOptions as B, OptionsNormalizer as O, PluginFactory as P };
@@ -0,0 +1,43 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /**
4
+ * 基础插件配置
5
+ *
6
+ * @interface BasePluginOptions
7
+ */
8
+ interface BasePluginOptions {
9
+ /**
10
+ * 是否启用插件
11
+ *
12
+ * @default true
13
+ */
14
+ enabled?: boolean;
15
+ /**
16
+ * 是否启用日志
17
+ *
18
+ * @default true
19
+ */
20
+ verbose?: boolean;
21
+ /**
22
+ * 错误处理策略
23
+ *
24
+ * @default 'throw'
25
+ */
26
+ errorStrategy?: 'throw' | 'log' | 'ignore';
27
+ }
28
+ /**
29
+ * 插件选项标准化器类型
30
+ *
31
+ * @template T 目标选项类型
32
+ * @template R 原始选项类型
33
+ */
34
+ type OptionsNormalizer<T, R = any> = (raw?: R) => T;
35
+ /**
36
+ * 插件工厂函数类型
37
+ *
38
+ * @template T 插件配置类型,默认继承自 BasePluginOptions
39
+ * @template R 原始配置类型,默认与 T 相同
40
+ */
41
+ type PluginFactory<T extends BasePluginOptions = BasePluginOptions, R = T> = (options?: R) => Plugin;
42
+
43
+ export type { BasePluginOptions as B, OptionsNormalizer as O, PluginFactory as P };
package/package.json CHANGED
@@ -1,57 +1,72 @@
1
- {
2
- "name": "@meng-xi/vite-plugin",
3
- "type": "module",
4
- "version": "0.0.1",
5
- "private": false,
6
- "description": "一个为 Vite 提供的插件库",
7
- "readme": "README.md",
8
- "keywords": [
9
- "meng-xi",
10
- "vite",
11
- "plugin"
12
- ],
13
- "main": "./dist/index.cjs",
14
- "module": "./dist/index.mjs",
15
- "types": "./dist/index.d.ts",
16
- "exports": {
17
- ".": {
18
- "require": "./dist/index.cjs",
19
- "import": "./dist/index.mjs",
20
- "types": "./dist/index.d.ts"
21
- },
22
- "./plugins/*": {
23
- "require": "./dist/plugins/*.cjs",
24
- "import": "./dist/plugins/*.mjs",
25
- "types": "./dist/plugins/*.d.ts"
26
- }
27
- },
28
- "files": [
29
- "dist"
30
- ],
31
- "scripts": {
32
- "dev": "unbuild --watch",
33
- "build": "unbuild"
34
- },
35
- "peerDependencies": {
36
- "vite": "^5.0.0"
37
- },
38
- "dependencies": {
39
- "vite": "^5.0.0"
40
- },
41
- "devDependencies": {
42
- "@types/node": "^20.0.0",
43
- "unbuild": "^3.0.0",
44
- "typescript": "^5.2.0"
45
- },
46
- "author": {
47
- "name": "PedroQue99",
48
- "email": "ksmengfang@163.com"
49
- },
50
- "license": "MIT",
51
- "homepage": "https://github.com/MengXi-Studio/vite-plugin#readme",
52
- "repository": {
53
- "type": "git",
54
- "url": "git+https://github.com/MengXi-Studio/vite-plugin.git"
55
- },
56
- "bugs": "https://github.com/MengXi-Studio/vite-plugin/issues"
57
- }
1
+ {
2
+ "name": "@meng-xi/vite-plugin",
3
+ "type": "module",
4
+ "version": "0.0.3",
5
+ "private": false,
6
+ "description": "一个为 Vite 提供的插件库",
7
+ "keywords": [
8
+ "@meng-xi/vite-plugin",
9
+ "meng-xi",
10
+ "vite",
11
+ "plugin",
12
+ "vite-plugin"
13
+ ],
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "require": "./dist/index.cjs",
20
+ "import": "./dist/index.mjs",
21
+ "types": "./dist/index.d.ts"
22
+ },
23
+ "./common": {
24
+ "require": "./dist/common/index.cjs",
25
+ "import": "./dist/common/index.mjs",
26
+ "types": "./dist/common/index.d.ts"
27
+ },
28
+ "./factory": {
29
+ "require": "./dist/factory/index.cjs",
30
+ "import": "./dist/factory/index.mjs",
31
+ "types": "./dist/factory/index.d.ts"
32
+ },
33
+ "./logger": {
34
+ "require": "./dist/logger/index.cjs",
35
+ "import": "./dist/logger/index.mjs",
36
+ "types": "./dist/logger/index.d.ts"
37
+ },
38
+ "./plugins": {
39
+ "require": "./dist/plugins/index.cjs",
40
+ "import": "./dist/plugins/index.mjs",
41
+ "types": "./dist/plugins/index.d.ts"
42
+ }
43
+ },
44
+ "files": [
45
+ "dist",
46
+ "README.md",
47
+ "README-en.md"
48
+ ],
49
+ "scripts": {
50
+ "dev": "unbuild --watch",
51
+ "build": "unbuild"
52
+ },
53
+ "peerDependencies": {
54
+ "vite": "^5.0.0"
55
+ },
56
+ "devDependencies": {
57
+ "@types/node": "^20.0.0",
58
+ "unbuild": "^3.0.0",
59
+ "typescript": "^5.2.0"
60
+ },
61
+ "author": {
62
+ "name": "PedroQue99",
63
+ "email": "ksmengfang@163.com"
64
+ },
65
+ "license": "MIT",
66
+ "homepage": "https://github.com/MengXi-Studio/vite-plugin#readme",
67
+ "repository": {
68
+ "type": "git",
69
+ "url": "git+https://github.com/MengXi-Studio/vite-plugin.git"
70
+ },
71
+ "bugs": "https://github.com/MengXi-Studio/vite-plugin/issues"
72
+ }
@@ -1,44 +0,0 @@
1
- import { type Plugin } from 'vite';
2
- import type { CopyFileOptions } from './type';
3
- /**
4
- * 复制文件插件
5
- *
6
- * @param options - 配置参数
7
- * @returns 一个 Vite 插件实例
8
- *
9
- * @example
10
- * ```typescript
11
- * // 基本使用
12
- * copyFile({
13
- * sourceDir: 'src/assets',
14
- * targetDir: 'dist/assets'
15
- * })
16
- *
17
- * // 自定义配置
18
- * copyFile({
19
- * sourceDir: 'src/static',
20
- * targetDir: 'dist/static',
21
- * overwrite: false,
22
- * verbose: true,
23
- * recursive: false
24
- * })
25
- *
26
- * // 根据环境启用
27
- * copyFile({
28
- * sourceDir: 'src/assets',
29
- * targetDir: 'dist/assets',
30
- * enabled: process.env.NODE_ENV === 'production'
31
- * })
32
- *
33
- * // 禁用复制功能
34
- * copyFile({
35
- * sourceDir: 'src/assets',
36
- * targetDir: 'dist/assets',
37
- * enabled: false
38
- * })
39
- * ```
40
- *
41
- * @remarks
42
- * 该插件会在 Vite 构建完成后执行,将指定源目录的所有文件和子目录复制到目标目录
43
- */
44
- export declare function copyFile(options: CopyFileOptions): Plugin;
@@ -1,43 +0,0 @@
1
- import { checkSourceExists, ensureTargetDir, copySourceToTarget } from "@/utils";
2
- export function copyFile(options) {
3
- const { sourceDir, targetDir, overwrite = true, recursive = true, verbose = true, enabled = true } = options;
4
- return {
5
- // 插件名称
6
- name: "copy-file",
7
- // 插件在构建流程的最后阶段执行,确保其他构建任务完成后再进行文件复制
8
- enforce: "post",
9
- /**
10
- * Vite 构建完成后触发的钩子函数,执行文件复制操作
11
- *
12
- * @remarks
13
- * 该钩子在 Vite 构建流程的最后阶段执行,确保所有构建任务完成后再进行文件复制
14
- *
15
- * @throws 当源文件不存在、权限不足或复制过程中出现其他错误时抛出异常
16
- */
17
- async writeBundle() {
18
- if (!enabled) {
19
- if (verbose) {
20
- console.log(`\u2139 \u590D\u5236\u6587\u4EF6\u529F\u80FD\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u6267\u884C\uFF1A\u4ECE ${sourceDir} \u5230 ${targetDir}`);
21
- }
22
- return;
23
- }
24
- try {
25
- await checkSourceExists(sourceDir);
26
- await ensureTargetDir(targetDir);
27
- await copySourceToTarget(sourceDir, targetDir, { recursive, overwrite });
28
- if (verbose) {
29
- console.log(`\u2705 \u590D\u5236\u6587\u4EF6\u6210\u529F\uFF1A\u4ECE ${sourceDir} \u5230 ${targetDir}`);
30
- }
31
- } catch (err) {
32
- if (verbose) {
33
- if (err instanceof Error) {
34
- console.error(err.message);
35
- } else {
36
- console.error(`\u274C \u590D\u5236\u6587\u4EF6\u5931\u8D25\uFF1A\u672A\u77E5\u9519\u8BEF - ${sourceDir} -> ${targetDir}`, err);
37
- }
38
- }
39
- throw err;
40
- }
41
- }
42
- };
43
- }
@@ -1,47 +0,0 @@
1
- /**
2
- * 复制文件插件的配置选项接口
3
- *
4
- * @interface CopyFileOptions
5
- */
6
- export interface CopyFileOptions {
7
- /**
8
- * 源文件目录的路径
9
- *
10
- * @example 'src/assets'
11
- */
12
- sourceDir: string;
13
- /**
14
- * 目标文件目录的路径
15
- *
16
- * @example 'dist/assets'
17
- */
18
- targetDir: string;
19
- /**
20
- * 是否覆盖同名文件
21
- *
22
- * @defaultValue true
23
- * @example false
24
- */
25
- overwrite?: boolean;
26
- /**
27
- * 是否支持递归复制
28
- *
29
- * @defaultValue true
30
- * @example false
31
- */
32
- recursive?: boolean;
33
- /**
34
- * 是否显示详细日志
35
- *
36
- * @defaultValue true
37
- * @example false
38
- */
39
- verbose?: boolean;
40
- /**
41
- * 是否启用复制功能
42
- *
43
- * @defaultValue true
44
- * @example false
45
- */
46
- enabled?: boolean;
47
- }
File without changes
@@ -1,74 +0,0 @@
1
- import { type Plugin } from 'vite';
2
- import type { InjectIcoOptions } from './type';
3
- /**
4
- * 注入网站图标链接到 HTML 文件的头部
5
- *
6
- * @param options - 配置选项(字符串时视为 base)
7
- * @returns 一个 Vite 插件实例,用于在构建过程中修改 HTML 文件
8
- *
9
- * @example
10
- * ```typescript
11
- * // 基本使用
12
- * injectIco({ base: '/assets' })
13
- *
14
- * // 自定义图标
15
- * injectIco({
16
- * icons: [
17
- * { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
18
- * { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' },
19
- * { rel: 'icon', href: '/favicon-16x16.png', sizes: '16x16', type: 'image/png' }
20
- * ]
21
- * })
22
- *
23
- * // 带文件复制功能(适用于 uni-app 等框架)
24
- * injectIco({
25
- * base: '/assets',
26
- * copyOptions: {
27
- * sourceDir: 'src/assets/icons',
28
- * targetDir: 'dist/assets/icons'
29
- * }
30
- * })
31
- *
32
- * // 带完整复制配置的使用
33
- * injectIco({
34
- * base: '/assets',
35
- * copyOptions: {
36
- * sourceDir: 'src/assets/icons',
37
- * targetDir: 'dist/assets/icons',
38
- * overwrite: false,
39
- * recursive: true
40
- * }
41
- * })
42
- *
43
- * // 关闭日志输出
44
- * injectIco({
45
- * base: '/assets',
46
- * verbose: false,
47
- * copyOptions: {
48
- * sourceDir: 'src/assets/icons',
49
- * targetDir: 'dist/assets/icons'
50
- * }
51
- * })
52
- *
53
- * // 根据环境启用
54
- * injectIco({
55
- * base: '/assets',
56
- * enabled: process.env.NODE_ENV === 'production',
57
- * copyOptions: {
58
- * sourceDir: 'src/assets/icons',
59
- * targetDir: 'dist/assets/icons'
60
- * }
61
- * })
62
- *
63
- * // 禁用插件
64
- * injectIco({
65
- * base: '/assets',
66
- * enabled: false,
67
- * copyOptions: {
68
- * sourceDir: 'src/assets/icons',
69
- * targetDir: 'dist/assets/icons'
70
- * }
71
- * })
72
- * ```
73
- */
74
- export declare function injectIco(options?: InjectIcoOptions | string): Plugin;
@@ -1,82 +0,0 @@
1
- import { checkSourceExists, ensureTargetDir, copySourceToTarget, generateIconTags } from "@/utils";
2
- export function injectIco(options) {
3
- const normalizedOptions = typeof options === "string" ? { base: options } : options || {};
4
- const { verbose = true, enabled = true } = normalizedOptions;
5
- return {
6
- name: "inject-ico",
7
- /**
8
- * 转换 HTML 入口文件的钩子函数
9
- *
10
- * @param html - 原始的 HTML 内容
11
- * @returns 经过修改后的 HTML 内容,在 `</head>` 标签前注入图标链接
12
- */
13
- transformIndexHtml(html) {
14
- if (!enabled) {
15
- if (verbose) {
16
- console.log("\u2139 inject-ico: \u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165");
17
- }
18
- return html;
19
- }
20
- const iconTags = generateIconTags(normalizedOptions);
21
- if (iconTags.length === 0) {
22
- if (verbose) {
23
- console.log("\u2139 inject-ico: \u6CA1\u6709\u751F\u6210\u56FE\u6807\u6807\u7B7E\uFF0C\u8DF3\u8FC7\u6CE8\u5165");
24
- }
25
- return html;
26
- }
27
- let modifiedHtml = html;
28
- const headCloseIndex = modifiedHtml.indexOf("</head>");
29
- if (headCloseIndex !== -1) {
30
- const tagsHtml = iconTags.join("\n") + "\n";
31
- modifiedHtml = modifiedHtml.substring(0, headCloseIndex) + tagsHtml + modifiedHtml.substring(headCloseIndex);
32
- if (verbose) {
33
- console.log(`\u2705 inject-ico: \u6210\u529F\u6CE8\u5165 ${iconTags.length} \u4E2A\u56FE\u6807\u6807\u7B7E\u5230 HTML \u6587\u4EF6`);
34
- iconTags.forEach((tag) => {
35
- console.log(` - ${tag}`);
36
- });
37
- }
38
- } else {
39
- if (verbose) {
40
- console.warn("\u26A0 inject-ico: \u672A\u627E\u5230 </head> \u6807\u7B7E\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165");
41
- }
42
- }
43
- return modifiedHtml;
44
- },
45
- /**
46
- * 构建完成后执行的钩子函数,用于复制图标文件到打包目录
47
- *
48
- * @remarks
49
- * 只有当配置了 copyOptions 对象且 enabled 为 true 时才会执行复制操作
50
- *
51
- * @throws 当源文件不存在、权限不足或复制过程中出现其他错误时抛出异常
52
- */
53
- async writeBundle() {
54
- if (!enabled) {
55
- if (verbose) {
56
- console.log("\u2139 inject-ico: \u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u6587\u4EF6\u590D\u5236");
57
- }
58
- return;
59
- }
60
- const { copyOptions } = normalizedOptions;
61
- if (!copyOptions) return;
62
- const { sourceDir, targetDir, overwrite = true, recursive = true } = copyOptions;
63
- try {
64
- await checkSourceExists(sourceDir);
65
- await ensureTargetDir(targetDir);
66
- await copySourceToTarget(sourceDir, targetDir, { recursive, overwrite });
67
- if (verbose) {
68
- console.log(`\u2705 inject-ico: \u56FE\u6807\u6587\u4EF6\u590D\u5236\u6210\u529F\uFF1A\u4ECE ${sourceDir} \u5230 ${targetDir}`);
69
- }
70
- } catch (err) {
71
- if (verbose) {
72
- if (err instanceof Error) {
73
- console.error(err.message);
74
- } else {
75
- console.error(`\u274C inject-ico: \u56FE\u6807\u6587\u4EF6\u590D\u5236\u5931\u8D25\uFF1A\u672A\u77E5\u9519\u8BEF - ${sourceDir} -> ${targetDir}`, err);
76
- }
77
- }
78
- throw err;
79
- }
80
- }
81
- };
82
- }