-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenMeta.js
58 lines (52 loc) · 1.64 KB
/
genMeta.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
import fs from 'fs';
import fs_extra from 'fs-extra';
import path from 'path';
function getAllBlockComment(src) {
const reg = /\/\*[:~][\s\S]*?\*\//g;
//console.log(src);
// console.log(src.match(reg));
return src.match(reg)?.join('\n') ?? 'no comment';
}
export default function writeAFile(pluginDir = 'src/plugins', outDir = 'meta') {
return {
name: 'write-a-file',
buildStart() {
fs.mkdirSync('./' + outDir, { recursive: true });
fs_extra.emptyDirSync('./' + outDir);
},
transform: {
order: 'pre',
handler(src, id) {
//const pluginDirPath = path.resolve(__dirname, pluginDir);
const pluginDirPath = __dirname.replace(/\\/g, '/') + '/' + pluginDir;
// console.log(pluginDirPath);
// console.log(id);
// console.log(id.startsWith(pluginDirPath));
// console.log(id.split('/') , pluginDirPath.split('/'));
// console.log('');
return new Promise((resolve, reject) => {
// 确定文件是否属于pluginDir的第一级
if (!id.startsWith(pluginDirPath) || id.split('/').length !== pluginDirPath.split('/').length + 1) {
resolve({
code: src,
map: null
});
return;
}
const fileName = id.split('/').pop().split('.')[0];
fs.writeFile(`./${outDir}/${fileName}.js`, getAllBlockComment(src), (err) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve({
code: src,
map: null
});
}
})
})
}
},
}
}