@meng-xi/vite-plugin 0.0.1 → 0.0.2

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.
@@ -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
- }
@@ -1,115 +0,0 @@
1
- /**
2
- * 图标配置项接口
3
- *
4
- * @interface Icon
5
- */
6
- interface Icon {
7
- /**
8
- * 图标关系类型
9
- */
10
- rel: string;
11
- /**
12
- * 图标 URL
13
- */
14
- href: string;
15
- /**
16
- * 图标尺寸
17
- */
18
- sizes?: string;
19
- /**
20
- * 图标 MIME 类型
21
- */
22
- type?: string;
23
- }
24
- /**
25
- * 图标文件复制配置选项接口
26
- *
27
- * @interface CopyOptions
28
- */
29
- interface CopyOptions {
30
- /**
31
- * 图标源文件目录,用于复制图标到打包目录
32
- *
33
- * @example 'src/assets/icons'
34
- */
35
- sourceDir: string;
36
- /**
37
- * 图标目标目录(打包目录),用于复制图标到打包目录
38
- *
39
- * @example 'dist/assets/icons'
40
- */
41
- targetDir: string;
42
- /**
43
- * 是否覆盖同名文件
44
- *
45
- * @defaultValue true
46
- * @example false
47
- */
48
- overwrite?: boolean;
49
- /**
50
- * 是否支持递归复制
51
- *
52
- * @defaultValue true
53
- * @example false
54
- */
55
- recursive?: boolean;
56
- }
57
- /**
58
- * 注入网站图标链接的配置选项接口
59
- *
60
- * @interface InjectIcoOptions
61
- */
62
- export interface InjectIcoOptions {
63
- /**
64
- * 图标文件的基础路径,默认为根路径 `/`
65
- *
66
- * @defaultValue `/`
67
- * @example '/assets'
68
- */
69
- base?: string;
70
- /**
71
- * 图标的完整 URL,如果提供则优先使用(覆盖 base + favicon.ico)
72
- *
73
- * @example 'https://example.com/favicon.ico'
74
- */
75
- url?: string;
76
- /**
77
- * 自定义的完整 link 标签 HTML,如果提供则优先使用(覆盖 url 和 base)
78
- *
79
- * @example '<link rel="icon" href="/favicon.svg" type="image/svg+xml" />'
80
- */
81
- link?: string;
82
- /**
83
- * 自定义图标数组,支持多种图标格式和尺寸
84
- *
85
- * @example
86
- * [
87
- * { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
88
- * { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' },
89
- * { rel: 'icon', href: '/favicon-16x16.png', sizes: '16x16', type: 'image/png' }
90
- * ]
91
- */
92
- icons?: Icon[];
93
- /**
94
- * 是否显示详细日志
95
- *
96
- * @defaultValue true
97
- * @example false
98
- */
99
- verbose?: boolean;
100
- /**
101
- * 是否启用图标注入和文件复制功能
102
- *
103
- * @defaultValue true
104
- * @example false
105
- */
106
- enabled?: boolean;
107
- /**
108
- * 图标文件复制配置选项
109
- *
110
- * @remarks
111
- * 当此对象存在时,才会开启图标文件复制功能
112
- */
113
- copyOptions?: CopyOptions;
114
- }
115
- export {};
File without changes