-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (95 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* @Author: Cc-Mac [email protected]
* @Date: 2022-07-07 13:12:24
* @LastEditors: Cc-Mac [email protected]
* @LastEditTime: 2022-07-08 10:36:40
* @FilePath: /svelte-hiprint/packages/vite-copy-plugin/index.js
* @Description: vite 复制文件/文件夹 plugin
*/
const path = require("path");
const fs = require("fs-extra");
const separator = process.platform == "win32" ? "\\" : "/";
// 是否是绝对路径
const isAbs = (url) => {
return path.isAbsolute(url);
};
// 是否是目录
const isDir = (url) => {
const pathEnd = url.substring(url.lastIndexOf(separator));
return !pathEnd.includes(".");
};
// 是否是文件
const isFile = (url) => {
const pathEnd = url.substring(url.lastIndexOf(separator));
return pathEnd.includes(".");
};
// 创建目录
const mkdir = (url) => {
if (isDir(url)) {
if (!fs.existsSync(url)) {
fs.mkdirsSync(url);
}
} else {
const parentUrl = path.resolve(url, "..");
if (!fs.existsSync(parentUrl)) {
fs.mkdirsSync(parentUrl);
}
}
};
const copy = function (item) {
try {
if (fs.existsSync(item.from)) {
const from = fs.statSync(item.from);
if (from.isDirectory() && isDir(item.to)) {
mkdir(item.to);
fs.copySync(item.from, item.to);
} else {
if (from.isFile() && isDir(item.to)) {
mkdir(item.to);
const fileName = item.from.substring(
item.from.lastIndexOf(separator)
);
const filePath = `${item.to}${fileName}`;
fs.copySync(item.from, filePath);
} else if (from.isFile() && isFile(item.to)) {
fs.copySync(item.from, item.to);
}
}
console.log(
`vite-copy-plugin: copy "${item.origin.from}" to "${item.origin.to}" success!`
);
}
} catch (err) {
console.error(
`vite-copy-plugin: copy "${item.origin.from}" to "${item.origin.to}" error!`
);
console.error(err);
}
};
module.exports = function (options) {
let viteConfig;
let list;
return {
name: "vite-copy-plugin",
apply: "build",
configResolved: (config) => {
viteConfig = config;
const root = viteConfig.root;
const outDir = viteConfig.build.outDir;
list = options.map((item) => {
const to = item.to || outDir;
item.to = to;
return {
origin: item,
from: isAbs(item.from) ? item.from : path.resolve(root, item.from),
to: isAbs(to) ? item.to : path.resolve(root, to),
};
});
},
closeBundle: () => {
list.forEach((item) => {
copy(item);
});
},
};
};