forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-style-entry.js
92 lines (79 loc) · 2.39 KB
/
build-style-entry.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
/**
* Build style entry of all components
*/
const fs = require('fs-extra');
const path = require('path');
const components = require('./get-components')();
const dependencyTree = require('dependency-tree');
const whiteList = ['info', 'icon', 'loading', 'cell', 'cell-group', 'button'];
const dir = path.join(__dirname, '../es');
components.forEach(component => {
// css entry
destEntryFile(component, 'index.js', '.css');
// less entry
destEntryFile(component, 'less.js', '.less');
});
function destEntryFile(component, filename, ext = '') {
const deps = analyzeDependencies(component).map(dep =>
getStyleRelativePath(component, dep, ext)
);
const esEntry = path.join(dir, component, `style/${filename}`);
const libEntry = path.join(__dirname, '../lib', component, `style/${filename}`);
const esContent = deps.map(dep => `import '${dep}';`).join('\n');
const libContent = deps.map(dep => `require('${dep}');`).join('\n');
fs.outputFileSync(esEntry, esContent);
fs.outputFileSync(libEntry, libContent);
}
// analyze component dependencies
function analyzeDependencies(component) {
const checkList = ['base'];
search(
dependencyTree({
directory: dir,
filename: path.join(dir, component, 'index.js'),
filter: path => !~path.indexOf('node_modules')
}),
component,
checkList
);
if (!whiteList.includes(component)) {
checkList.push(component);
}
return checkList.filter(item => checkComponentHasStyle(item));
}
function search(tree, component, checkList) {
Object.keys(tree).forEach(key => {
search(tree[key], component, checkList);
components
.filter(item =>
key
.replace(dir, '')
.split('/')
.includes(item)
)
.forEach(item => {
if (
!checkList.includes(item) &&
!whiteList.includes(item) &&
item !== component
) {
checkList.push(item);
}
});
});
}
function getStylePath(component, ext = '.css') {
if (component === 'base') {
return path.join(__dirname, `../es/style/base${ext}`);
}
return path.join(__dirname, `../es/${component}/index${ext}`);
}
function getStyleRelativePath(component, style, ext) {
return path.relative(
path.join(__dirname, `../es/${component}/style`),
getStylePath(style, ext)
);
}
function checkComponentHasStyle(component) {
return fs.existsSync(getStylePath(component));
}