[bugfix] Optimize component dependency analyze when build style entry (#224)

* [bugfix] Checkbox border render error in weixin browser

* [bugfix] TreeSelect dependency path error

* [bugfix] Swipe should clear autoplay timer when destroyed

* [bugfix] Optimize component dependency analyze when build style entry

* merge

* update yarn.lock
This commit is contained in:
neverland
2017-10-20 02:00:31 -05:00
committed by GitHub
parent 45e134fa7e
commit 46a0d7e49f
6 changed files with 391 additions and 98 deletions
+23 -24
View File
@@ -1,43 +1,42 @@
/**
* 生成每个组件目录下的 style 入口
* Build style entry of all components
*/
const fs = require('fs-extra');
const path = require('path');
const components = require('./get-components')();
const source = require('../../lib/vant');
const dependencyTree = require('dependency-tree');
components.forEach(componentName => {
const dependencies = analyzeDependencies(componentName);
const styleDir = path.join(__dirname, '../../lib/', componentName, '/style');
const libDir = path.resolve(__dirname, '../../lib');
const dependencies = analyzeDependencies(componentName, libDir);
const styleDir = path.join(libDir, componentName, '/style');
const content = dependencies.map(component => `require('../../vant-css/${component}.css');`);
fs.outputFileSync(path.join(styleDir, './index.js'), content.join('\n'));
});
// 递归分析组件依赖
// 样式引入顺序:基础样式, 组件依赖样式,组件本身样式
function analyzeDependencies(componentName) {
// Analyze component dependencies
function analyzeDependencies(componentName, libDir) {
const dependencies = dependencyTree({
directory: libDir,
filename: path.resolve(libDir, componentName, 'index.js'),
filter: path => path.indexOf('vant/lib/') !== -1
})
const checkList = ['base'];
const search = component => {
const componentSource = source[toPascal(component)];
if (componentSource && componentSource.components) {
Object.keys(componentSource.components).forEach(name => {
name = name.replace('van-', '');
if (checkList.indexOf(name) === -1) {
search(name);
}
});
}
if (checkList.indexOf(component) === -1) {
checkList.push(component);
}
}
search(componentName);
search(dependencies, checkList);
return checkList.filter(component => checkComponentHasStyle(component));
}
// 判断组件是否有样式
function search(tree, checkList) {
tree && Object.keys(tree).forEach(key => {
search(tree[key], checkList);
const component = key.split('/vant/lib/')[1].replace('/index.js', '');
if (checkList.indexOf(component) === -1) {
checkList.push(component);
}
});
}
function checkComponentHasStyle(componentName) {
const cssPath = path.join(__dirname, '../../lib/vant-css/', `${componentName}.css`);
return fs.existsSync(cssPath);