[bugfix] build style entry failed (#1282)

This commit is contained in:
neverland 2018-06-14 09:39:21 +08:00 committed by GitHub
parent 00351a564f
commit 8343d7e3e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,42 +6,56 @@ const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const components = require('./get-components')(); const components = require('./get-components')();
const dependencyTree = require('dependency-tree'); const dependencyTree = require('dependency-tree');
const SEP = path.sep; const whiteList = ['icon', 'loading', 'cell', 'button'];
const dir = path.join(__dirname, '../es');
function build(folder, isESModule) { components.forEach(component => {
const dir = path.resolve(__dirname, '../', folder); const deps = analyzeDependencies(component);
components.forEach(componentName => { const esEntry = path.join(dir, component, 'style/index.js');
const content = analyzeDependencies(componentName, dir) const libEntry = path.join(__dirname, '../lib', component, 'style/index.js');
.map(component => isESModule ? `import '../../vant-css/${component}.css';` : `require('../../vant-css/${component}.css');`); const esContent = deps.map(dep => `import '../../vant-css/${dep}.css';`).join('\n');
fs.outputFileSync(path.join(dir, componentName, './style/index.js'), content.join('\n')); const libContent = deps.map(dep => `require('../../vant-css/${dep}.css');`).join('\n');
});
// Analyze component dependencies fs.outputFileSync(esEntry, esContent);
function analyzeDependencies(componentName, dir) { fs.outputFileSync(libEntry, libContent);
const checkList = ['base']; });
const whiteList = ['icon', 'loading', 'cell', 'button'];
search(dependencyTree({ // analyze component dependencies
function analyzeDependencies(component) {
const checkList = ['base'];
search(
dependencyTree({
directory: dir, directory: dir,
filename: path.resolve(dir, componentName, 'index.js'), filename: path.join(dir, component, 'index.js'),
filter: path => path.indexOf(`vant${SEP}${folder}${SEP}`) !== -1 filter: path => !~path.indexOf('node_modules')
}), checkList, whiteList); }),
return checkList.filter(component => checkComponentHasStyle(component)); component,
checkList
);
if (!whiteList.includes(component)) {
checkList.push(component);
} }
function search(tree, checkList, whiteList) { return checkList.filter(item => checkComponentHasStyle(item));
tree && Object.keys(tree).forEach(key => {
search(tree[key], checkList, whiteList);
const component = key.split(`${SEP}vant${SEP}${folder}${SEP}`)[1].replace(`${SEP}index.js`, '').replace(`mixins${SEP}`, '');
if (checkList.indexOf(component) === -1 && whiteList.indexOf(component) === -1) {
checkList.push(component);
}
});
}
function checkComponentHasStyle(componentName) {
return fs.existsSync(path.join(__dirname, `../${folder}/vant-css/`, `${componentName}.css`));
}
} }
build('es', true); function search(tree, component, checkList) {
build('lib'); 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 checkComponentHasStyle(component) {
return fs.existsSync(
path.join(__dirname, `../es/vant-css/`, `${component}.css`)
);
}