babel-plugin-zoom-react-ui
Advanced tools
Comparing version 0.1.1 to 0.1.2
230
index.js
@@ -15,6 +15,6 @@ let types = require('babel-types'); | ||
let checkScope = (relyUrl,mapKeys ) => { | ||
for(let keyword of mapKeys){ | ||
if (relyUrl === keyword || relyUrl.indexOf(`${keyword}/`) === 0) { | ||
return true; | ||
let checkScope = (relyUrl, mapKeys) => { | ||
for (let keyword of mapKeys) { | ||
if (relyUrl === keyword || relyUrl.indexOf(`${keyword}/`) === 0) { | ||
return true; | ||
} | ||
@@ -27,113 +27,108 @@ } | ||
let getHumpNo=(str)=>{ | ||
let no=0; | ||
let lg = str.length; | ||
let i = 0; | ||
let temp; | ||
for(;i<lg;i+=1){ | ||
temp=str[i]; | ||
if (i === 0 ){ | ||
if (temp.toUpperCase() === temp){ | ||
no += 1; | ||
let getHumpNo = str => { | ||
let no = 0; | ||
let lg = str.length; | ||
let i = 0; | ||
let temp; | ||
for (; i < lg; i += 1) { | ||
temp = str[i]; | ||
if (i === 0) { | ||
if (temp.toUpperCase() === temp) { | ||
no += 1; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
if (temp.toUpperCase() === temp) { | ||
no += 1; | ||
} | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
else{ | ||
if(temp.toUpperCase()===temp){ | ||
no+=1; | ||
} | ||
} | ||
} | ||
return no; | ||
return no; | ||
}; | ||
let humpToBar=(str)=>{ | ||
let humpToBar = str => { | ||
let lg = str.length; | ||
let i = 0; | ||
let temp; | ||
let ind=0; | ||
for(;i<lg;i+=1){ | ||
temp=str[i]; | ||
if(i!==0&&(temp.toUpperCase()===temp)){ | ||
ind=i; | ||
let ind = 0; | ||
for (; i < lg; i += 1) { | ||
temp = str[i]; | ||
if (i !== 0 && temp.toUpperCase() === temp) { | ||
ind = i; | ||
} | ||
} | ||
let left=str.slice(0,ind); | ||
let right=str.slice(ind); | ||
return left.toLowerCase()+'-'+right.toLowerCase(); | ||
let left = str.slice(0, ind); | ||
let right = str.slice(ind); | ||
return left.toLowerCase() + '-' + right.toLowerCase(); | ||
}; | ||
let getPrefixName=(str)=>{ | ||
let lg = str.length; | ||
let getPrefixName = str => { | ||
let lg = str.length; | ||
let i = 0; | ||
let temp; | ||
// let ind=0; | ||
for(;i<lg;i+=1){ | ||
temp=str[i]; | ||
if(temp==='-'){ | ||
return {prefix:str.slice(0,i),name:str.slice(i+1)}; | ||
for (; i < lg; i += 1) { | ||
temp = str[i]; | ||
if (temp === '-') { | ||
return { prefix: str.slice(0, i), name: str.slice(i + 1) }; | ||
} | ||
} | ||
return ; | ||
return; | ||
}; | ||
let antdExcludeLess = ['col', 'icon', 'popconfirm', 'row', 'version']; | ||
// let antdExcludeLess = ['col', 'icon', 'popconfirm', 'row', 'version']; | ||
let dispatchCustom = (opt)=>(componentName, pathArray,useTheme)=> { | ||
if(pathArray.length>2){ | ||
return; | ||
let dispatchCustom = opt => (componentName, pathArray, useTheme) => { | ||
if (pathArray.length > 2) { | ||
return; | ||
} | ||
let { map, ifHump = true } = opt; | ||
let prefixComponent = pathArray[0]; | ||
if (pathArray.length > 1) { | ||
//@components/Button,not import {Button} from '@components'; | ||
componentName = pathArray[1]; | ||
} | ||
let name; | ||
let humpNo = getHumpNo(componentName); | ||
if (typeof humpNo === 'number' && ifHump === true) { | ||
if (humpNo === 1) { | ||
name = componentName.toLowerCase(); | ||
} else if (humpNo > 1) { | ||
name = humpToBar(componentName); | ||
} | ||
let {map,ifHump=true}=opt; | ||
let prefixComponent=pathArray[0]; | ||
if (pathArray.length > 1) {//@components/Button,not import {Button} from '@components'; | ||
componentName = pathArray[1]; | ||
} | ||
let name; | ||
let humpNo=getHumpNo(componentName); | ||
if((typeof humpNo==='number')&&ifHump===true){ | ||
if(humpNo===1){ | ||
name = componentName.toLowerCase(); | ||
} | ||
else if(humpNo>1){ | ||
name=humpToBar(componentName); | ||
} | ||
} | ||
else{ | ||
name=componentName; | ||
} | ||
let pathTp='css'; | ||
} else { | ||
name = componentName; | ||
} | ||
let pathTp = `lib/${name}/style/css`; | ||
if(useTheme===true){ | ||
if (antdExcludeLess.indexOf(name) !== -1) { | ||
pathTp = 'css'; | ||
} else { | ||
pathTp = 'index.less'; | ||
} | ||
} | ||
if (useTheme === true) { | ||
pathTp = `lib/${name}/style`; | ||
// if (antdExcludeLess.indexOf(name) !== -1) { | ||
// pathTp = `lib/${name}/style/css`; | ||
// } else { | ||
// pathTp = `lib/${name}/style`; | ||
// } | ||
} | ||
if(prefixComponent==='zoom-react-ui'){ | ||
return { | ||
js: { rely: `@zoom/zoom-react-ui/lib/${name}`, name: componentName },//where render js path,and name | ||
css: { rely: `@zoom/zoom-react-ui/lib/${name}/style/${pathTp}` }//which render css | ||
}; | ||
} | ||
else if(prefixComponent==='antd'){ | ||
return { | ||
js: { rely: `antd/lib/${name}` }, //where render js path,and name | ||
css: { rely: `antd/lib/${name}/style/${pathTp}` } //which render css | ||
}; | ||
} | ||
else if((typeof map==='object')&&(prefixComponent in map)){ | ||
let prefixName=getPrefixName(componentName); | ||
return map[prefixComponent]({name:componentName,changedName:name,prefixName}); | ||
} | ||
}; | ||
if (prefixComponent === 'zoom-react-ui') { | ||
return { | ||
js: { rely: `@zoom/zoom-react-ui/lib/${name}`, name: componentName }, //where render js path,and name | ||
css: { rely: `@zoom/zoom-react-ui/${pathTp}` } //which render css | ||
}; | ||
} else if (prefixComponent === 'antd') { | ||
return { | ||
js: { rely: `antd/lib/${name}` }, //where render js path,and name | ||
css: { rely: `antd/${pathTp}` } //which render css | ||
}; | ||
} else if (typeof map === 'object' && prefixComponent in map) { | ||
let prefixName = getPrefixName(componentName); | ||
return map[prefixComponent]({ | ||
name: componentName, | ||
changedName: name, | ||
prefixName | ||
}); | ||
} | ||
}; | ||
module.exports = function() { | ||
@@ -144,7 +139,7 @@ return { | ||
let relyUrl = path.node.source.value; | ||
let customOpts = state.opts||{}; | ||
let customOpts = state.opts || {}; | ||
// let opts = importOption; | ||
let {map={},useTheme=false}=customOpts; | ||
let mapKeys=Object.keys(map); | ||
mapKeys=[].concat(mapKeys,['zoom-react-ui','antd']); | ||
let { map = {}, useTheme = false } = customOpts; | ||
let mapKeys = Object.keys(map); | ||
mapKeys = [].concat(mapKeys, ['zoom-react-ui', 'antd']); | ||
// let components = opts.components || {}; | ||
@@ -155,9 +150,9 @@ let dispatchRes = dispatchCustom(customOpts); | ||
} | ||
let relyArr= relyUrl.split('/').filter((val) => { | ||
if (val) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
let componentsArr=relyArr; | ||
let relyArr = relyUrl.split('/').filter(val => { | ||
if (val) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
let componentsArr = relyArr; | ||
let imports = handleImport(path); | ||
@@ -167,8 +162,10 @@ let defaultImports = handleDefaultImport(path); | ||
defaultImports.forEach((importObj) => { | ||
defaultImports.forEach(importObj => { | ||
let componentName = importObj.local.name; | ||
let newRelyObj = dispatchRes(componentName, componentsArr,useTheme); | ||
let newRelyObj = dispatchRes(componentName, componentsArr, useTheme); | ||
//now parse | ||
if(!newRelyObj){return;} | ||
if (!newRelyObj) { | ||
return; | ||
} | ||
let { css, js } = newRelyObj; | ||
@@ -179,3 +176,2 @@ | ||
); | ||
@@ -189,11 +185,13 @@ if (typeof css === 'object') { | ||
imports.forEach((importObj) => { | ||
imports.forEach(importObj => { | ||
let componentName = importObj.imported.name; | ||
let localComponentName = importObj.local.name; | ||
let newRelyObj = dispatchRes(componentName, componentsArr,useTheme); | ||
//now parse | ||
if(!newRelyObj){return;} | ||
let newRelyObj = dispatchRes(componentName, componentsArr, useTheme); | ||
//now parse | ||
if (!newRelyObj) { | ||
return; | ||
} | ||
let { css, js } = newRelyObj; | ||
let newImportObj = importObj; | ||
if (relyArr.length === 1&&!js.name) { | ||
if (relyArr.length === 1 && !js.name) { | ||
//for @comp.../Button,will not change import source | ||
@@ -205,5 +203,5 @@ //not double layer | ||
} | ||
if(js.name){ | ||
newImportObj.imported.name=js.name; | ||
newImportObj.local.name=localComponentName; | ||
if (js.name) { | ||
newImportObj.imported.name = js.name; | ||
newImportObj.local.name = localComponentName; | ||
} | ||
@@ -216,3 +214,3 @@ transforms.push( | ||
); | ||
if (typeof css === 'object') { | ||
@@ -219,0 +217,0 @@ transforms.push( |
{ | ||
"name": "babel-plugin-zoom-react-ui", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
7410
202