-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (69 loc) · 1.99 KB
/
Copy pathindex.js
File metadata and controls
73 lines (69 loc) · 1.99 KB
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
/*
* @Author: daipeng7
* @Date: 2021-07-15 17:01:59
* @LastEditTime: 2024-05-22 10:50:41
* @LastEditors: DaiPeng
* @Description: iconfont rollup plugin
*/
const nodify = require('nodeify');
const path = require('path');
const fs = require('fs');
const generate = require('./generate');
const writeFiles = require('./writeFiles');
const thro_debs = require('thro-debs');
const globby = require('globby');
const defaultOptions = require('./defaultOptions');
module.exports = function rollupPluginIconfont(options = {}) {
const required = ['svgs', 'fontsOutput'];
for (const r of required) {
if (!options[r]) {
throw new Error(`Require '${r}' option`);
}
}
if (!options.cssOutput) {
options.cssOutput = path.resolve(options.fontsOutput, options.fontName + '.css');
}
options = Object.assign({}, defaultOptions, options);
const build = () => {
return generate.byGlobby(options).then(result => {
return writeFiles(result);
}).then(ret => {
console.log('iconfont + css have been built with ' + ret.glyphDatas.length + ' svg-icons.');
options.success && options.success();
return ret;
}).catch(console.error.bind(console));
};
const watch = () => {
const comileDebounce = thro_debs.debounce(800, build.bind(this));
const svgs = [].concat(options.svgs);
// 只有一个文件夹时,监视文件夹。支持新增。/ab/c/*.svg
if (svgs.length === 1) {
const dir = path.dirname(svgs[0]).replace('*.svg', '');
if (fs.existsSync(dir) && fs.statSync(dir).isDirectory()) {
fs.watch(dir, (event, filename) => {
if (filename.length > 4 && filename.slice(-4) === '.svg') {
comileDebounce();
}
});
return;
}
}
// 监视每个文件。/ab/c/**/*.svg
globby(svgs).then(files => {
files.forEach(file => {
fs.watch(file, (event, filename) => {
comileDebounce();
});
});
});
};
return {
name: 'rollupPluginIconfont',
buildStart() {
return build().then((result) => {
options.watch && watch();
return result;
});
}
};
};