UNPKG

nimble-ui-pc

Version:

基础vue ui库 配置化组件 后台管理系统组件

106 lines (104 loc) 3.53 kB
let formatRegText = require('nimble-lib/transform/formatRegText'); /** * 获取包的key * * @param {*} dir 目录 * @param {*} replacePre 替换的前缀 * @param {*} defPrefix 默认前缀 * @returns {String} */ module.exports = function (dir, replacePre, defPrefix) { let key = dir || ''; if (replacePre) { // 处理前缀 [{ reg: new RegExp('/?' + formatRegText(replacePre), 'i'), content: '' }, { reg: /(\/|\\)?index(\.esm)?\.(j|t)sx?$/i, content: '' }, { key: 'components/', prefix: '', content(_path) { // ui库使用 // console.log(_path, $1); let res = _path; let arr; if (_path && typeof _path === 'string') { arr = _path.split('/'); if (arr && arr.length >= 2) { let _name = 'Nup'; // if (/^T/ig.test(_name)) { // _name = (_name.toLowerCase()).replace(/^[a-zA-Z]/g, $1 => $1.toUpperCase()); // } else { // _name = ''; // } res = 'modules/' + _name + (arr.slice(-1).join('/')).replace(/^[a-zA-Z]/g, ($1) => $1.toUpperCase()); } } return res; } }, { key: 'services/', prefix: 'S' }, { key: 'filters/', prefix: 'F' }, { key: 'directives/', prefix: 'D' }, { key: 'mixins/', prefix: 'M' }, { key: 'utils/', prefix: 'U' }, { key: 'modules/', prefix: '', children: [{ reg: /^(utils|helpers)?\//gi, content: '' }, { reg: /^([^/\\]+\/)*([A-Z_]+)(\/.*)*/g, content($0, $1, $2) { // // 导出模块根目录下的常量 return $2; } }] }].forEach((item) => { // key = key && key.replace(item.reg || new RegExp('^' + formatRegText(item.key) + '(\.*)', 'i'), (name, _path) => { // _path = _path && _path.replace(/^[a-zA-Z]/g, ($1) => $1.toUpperCase()); // return (defPrefix || '') + item.prefix + (_path || ''); // }); key = replace(key, item); if (item.children && item.children.length) { item.children.forEach((_item) => { key = replace(key, _item); }); } }); if (!(new RegExp('^' + formatRegText(key) + '(\\/|\\\\)?', 'i').test(defPrefix || ''))) { key = key ? (defPrefix || '') + key : ''; } // key = key && key.replace(/^(utils|helpers)?\//gi, ''); } return key; }; /** * 替换方法 * * @param {*} key 路径取出的key * @param {*} item 替换的规则项 * @returns {String} */ function replace(key, item) { let content = item.content; if (typeof content === 'undefined') { content = (name, _path) => { _path = _path && _path.replace(/^[a-zA-Z]/g, ($1) => $1.toUpperCase()); return item.prefix + (_path || ''); }; } let res = key && key.replace(item.reg || new RegExp('^' + formatRegText(item.key) + '(.*)', 'i'), content); return res || ''; }