Comparing version
{ | ||
"name": "alga-css", | ||
"version": "1.0.0-gold-4", | ||
"version": "1.0.0-gold-5", | ||
"description": "Alga CSS is a scope-first CSS toolkit for quickly mix or compose CSS references and share CSS properties between components", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -35,3 +35,6 @@ const postcss = require('postcss') | ||
for(let dnode of rnode.nodes) { | ||
defineObj[dnode.prop] = dnode.value | ||
defineObj[dnode.prop] = { | ||
value: dnode.value, | ||
source: dnode.source | ||
} | ||
} | ||
@@ -48,49 +51,64 @@ component[componentName][param] = Object.assign({}, component[componentName][param], defineObj) | ||
const defineObj = {} | ||
defineObj[param] = {} | ||
defineObj[param] = { | ||
value: {}, | ||
source: rnode.source | ||
} | ||
for(let dnode of rnode.nodes) { | ||
// Extracting content of provide | ||
if(dnode.type === 'decl' && dnode.prop === 'ref') { | ||
defineObj[param] = Object.assign({}, defineObj[param], reference(dnode.value, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, reference(dnode, refOpt)) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('ref-')) { | ||
let splitRefs = dnode.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = dnode.value | ||
defineObj[param] = Object.assign({}, defineObj[param], splitRefsObj) | ||
splitRefsObj[camelDash(splitRefs)] = { | ||
value: dnode.value, | ||
source: dnode.source | ||
} | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, splitRefsObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('props-')) { | ||
let splitProps = dnode.prop.split('-')[1] | ||
let splitPropsObj = {} | ||
splitPropsObj[camelDash(splitProps)] = '{'+dnode.value+'}' | ||
defineObj[param] = Object.assign({}, defineObj[param], splitPropsObj) | ||
splitPropsObj[camelDash(splitProps)] = { | ||
value: '{'+dnode.value+'}', | ||
source: dnode.source | ||
} | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, splitPropsObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('screen-')) { | ||
let screenObj = {} | ||
screenObj[dnode.prop] = Object.assign({}, screenObj[dnode.prop], reference(dnode.value, refOpt)) | ||
defineObj[param] = Object.assign({}, defineObj[param], screenObj) | ||
screenObj[dnode.prop] = Object.assign({}, screenObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, screenObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('state-')) { | ||
let stateObj = {} | ||
stateObj[dnode.prop] = Object.assign({}, stateObj[dnode.prop], reference(dnode.value, refOpt)) | ||
defineObj[param] = Object.assign({}, defineObj[param], stateObj) | ||
stateObj[dnode.prop] = Object.assign({}, stateObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, stateObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('prefers-')) { | ||
let prefersObj = {} | ||
prefersObj[dnode.prop] = Object.assign({}, prefersObj[dnode.prop], reference(dnode.value, refOpt)) | ||
defineObj[param] = Object.assign({}, defineObj[param], prefersObj) | ||
prefersObj[dnode.prop] = Object.assign({}, prefersObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, prefersObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('if-')) { | ||
let conditionalObj = {} | ||
conditionalObj[dnode.prop] = Object.assign({}, conditionalObj[dnode.prop], reference(dnode.value, refOpt)) | ||
defineObj[param] = Object.assign({}, defineObj[param], conditionalObj) | ||
conditionalObj[dnode.prop] = Object.assign({}, conditionalObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, conditionalObj) | ||
} else if(dnode.type === 'atrule' && dnode.name === 'if' && 'nodes' in dnode) { | ||
const paramConditional = node.params.split('in') | ||
const paramConditional = node.params.split(/is|has/) | ||
const valueConditional = paramConditional[0]?.trim() || '' | ||
const propsConditional = paramConditional[1]?.trim() || '' | ||
let conditionalObj = {} | ||
let conditionalObj = { | ||
value: {}, | ||
source: node.source | ||
} | ||
for(let condVal of dnode.nodes) { | ||
if(condVal.type === 'decl' && condVal.prop === 'ref') { | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], reference(condVal, refOpt)) | ||
conditionalObj.value['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj.value['if-'+propsConditional+'-'+valueConditional], reference(condVal, refOpt)) | ||
} else if(condVal.type === 'decl' && condVal.prop.startsWith('ref-')) { | ||
let splitRefs = condVal.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = condVal.value | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], splitRefsObj) | ||
splitRefsObj[camelDash(splitRefs)] = { | ||
value: condVal.value, | ||
source: condVal.source | ||
} | ||
conditionalObj.value['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj.value['if-'+propsConditional+'-'+valueConditional], splitRefsObj) | ||
} | ||
} | ||
defineObj[param] = Object.assign({}, defineObj[param], conditionalObj) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, conditionalObj) | ||
} | ||
@@ -109,3 +127,3 @@ } | ||
const arrowParams = param.split(/\(|\)/g) | ||
param = component[componentName][arrowParams[0]][arrowParams[1]] | ||
param = component[componentName][arrowParams[0]][arrowParams[1]].value | ||
} | ||
@@ -115,5 +133,3 @@ let defineObj = {} | ||
defineObj['body'] = [] | ||
defineObj['sourceBody'] = [] | ||
defineObj['content'] = {} | ||
defineObj['sourceContent'] = {} | ||
let index = 1 | ||
@@ -123,3 +139,2 @@ for(let dnode of rnode.nodes) { | ||
defineObj['content'][randId] = [] | ||
defineObj['sourceContent'][randId] = [] | ||
if(dnode.type === 'atrule' && dnode.name === 'use') { | ||
@@ -131,3 +146,2 @@ if('params' in dnode && dnode.params !== '') { | ||
defineObj['content'][randId].push(component[componentName]['modules'][dnode.params.trim()][dnode.params.trim()]['body']) | ||
defineObj['sourceContent'][randId].push(component[componentName]['modules'][dnode.params.trim()][dnode.params.trim()]['sourceBody']) | ||
} | ||
@@ -139,3 +153,3 @@ } else if(dnode.type === 'atrule' && dnode.name === 'keyframes') { | ||
const arrowDnodeParams = paramDnode.split(/\(|\)/g) | ||
paramDnode = component[componentName][arrowDnodeParams[0]][arrowDnodeParams[1]] | ||
paramDnode = component[componentName][arrowDnodeParams[0]][arrowDnodeParams[1]].value | ||
} | ||
@@ -152,3 +166,2 @@ let keyframeDefineObj = {} | ||
defineObj['content'][randId].push(keyframeDefineObj) | ||
defineObj['sourceContent'][randId].push(dnode.source) | ||
} | ||
@@ -158,5 +171,3 @@ } else if(dnode.type === 'atrule' && dnode.name === 'if') { | ||
let ifDefineObj = {} | ||
let ifDefineSource = {} | ||
ifDefineObj['@if '+dnode.params.trim()] = [] | ||
ifDefineSource['@if '+dnode.params.trim()] = [] | ||
for(let ifnode of dnode.nodes) { | ||
@@ -168,8 +179,5 @@ let ifRecursiveDefineObj = recursive(ifnode, { | ||
ifDefineObj['@if '+dnode.params.trim()].push(ifRecursiveDefineObj.body) | ||
ifDefineSource['@if '+dnode.params.trim()].push(ifRecursiveDefineObj.sourceBody) | ||
} | ||
ifDefineObj['@if '+dnode.params.trim()] = ifDefineObj['@if '+dnode.params.trim()].flat() | ||
ifDefineSource['@if '+dnode.params.trim()] = ifDefineSource['@if '+dnode.params.trim()].flat() | ||
defineObj['content'][randId].push(ifDefineObj) | ||
defineObj['sourceContent'][randId].push(ifDefineSource) | ||
} | ||
@@ -182,11 +190,8 @@ } else { | ||
defineObj['content'][randId].push(recursiveDefineObj.body) | ||
defineObj['sourceContent'][randId].push(recursiveDefineObj.sourceBody) | ||
} | ||
defineObj['content'][randId] = defineObj['content'][randId].flat() | ||
defineObj['sourceContent'][randId] = defineObj['sourceContent'][randId].flat() | ||
index++ | ||
} | ||
defineObj['body'] = Object.values(defineObj['content']).flat() | ||
defineObj['sourceBody'] = Object.values(defineObj['sourceContent']).flat() | ||
component[componentName][param] = Object.assign({}, component[componentName][param], defineObj) | ||
@@ -193,0 +198,0 @@ } else if(rnode.type === 'atrule' && rnode.name === 'use') { |
@@ -6,3 +6,2 @@ const postcss = require('postcss') | ||
const declaration = (body, defs, opts) => { | ||
const source = defs.source | ||
const refs = defs.refs | ||
@@ -17,7 +16,4 @@ const props = defs.props | ||
for(let i = 0;i < body.length;i++) { | ||
const item = body[i] | ||
const sourceItem = source[i] | ||
for(let item of body) { | ||
const itemKey = Object.keys(item)[0] | ||
const sourceItemKey = sourceItem ? Object.keys(sourceItem)[0] : undefined | ||
if(itemKey.startsWith('@if ')) { | ||
@@ -29,7 +25,7 @@ const itemValue = Object.values(item)[0] | ||
if( | ||
(typeof props === 'object' && props !== null && splitKey[0].trim() in props && props[splitKey[0].trim()] === splitKey[1].trim()) || | ||
(typeof refs === 'object' && refs !== null && splitKey[0].trim() in refs && refs[splitKey[0].trim()] === splitKey[1].trim()) | ||
(typeof props === 'object' && props !== null && splitKey[0].trim() in props && props[splitKey[0].trim()].value === splitKey[1].trim()) || | ||
(typeof refs === 'object' && refs !== null && splitKey[0].trim() in refs && refs[splitKey[0].trim()].value === splitKey[1].trim()) | ||
) { | ||
ruleArray.push([ | ||
...declaration(itemValue, defs, opts) | ||
...declaration((itemValue?.value || itemValue), defs, opts) | ||
]) | ||
@@ -40,7 +36,7 @@ } | ||
if( | ||
(typeof props === 'object' && props !== null && splitKey[0].trim() in props && props[splitKey[0].trim()].replaceAll(' ', '').split(',').filter(i => i !== '').includes(splitKey[1].trim())) || | ||
(typeof refs === 'object' && refs !== null && splitKey[0].trim() in refs && refs[splitKey[0].trim()].replaceAll(' ', '').split(',').filter(i => i !== '').includes(splitKey[1].trim())) | ||
(typeof props === 'object' && props !== null && splitKey[0].trim() in props && props[splitKey[0].trim()].value.replaceAll(' ', '').split(',').filter(i => i !== '').includes(splitKey[1].trim())) || | ||
(typeof refs === 'object' && refs !== null && splitKey[0].trim() in refs && refs[splitKey[0].trim()].value.replaceAll(' ', '').split(',').filter(i => i !== '').includes(splitKey[1].trim())) | ||
) { | ||
ruleArray.push([ | ||
...declaration(itemValue, defs, opts) | ||
...declaration((itemValue?.value || itemValue), defs, opts) | ||
]) | ||
@@ -50,12 +46,8 @@ } | ||
} else if(itemKey.startsWith('@keyframes ')) { | ||
const newAtRule = postcss.atRule({ name: 'keyframes', params: itemKey.replace('@keyframes ', '').trim(), source: sourceItemKey }) | ||
const itemValue = Object.values(item)[0] | ||
const newAtRule = postcss.atRule({ name: 'keyframes', params: itemKey.replace('@keyframes ', '').trim(), source: Object.values(itemValue[0])[0].source }) | ||
newAtRule.append([...declaration(itemValue, defs, opts)]) | ||
ruleArray.push(newAtRule) | ||
} else { | ||
const itemValues = Object.entries(item[itemKey]) | ||
let sourceItemValues = undefined | ||
if(sourceItemKey) { | ||
sourceItemValues = sourceItem[sourceItemKey] ? Object.entries(sourceItem[sourceItemKey]) : undefined | ||
} | ||
const itemValues = Object.entries(item[itemKey].value) | ||
let selectorItemKey = itemKey | ||
@@ -69,3 +61,3 @@ if(selectorItemKey.includes('refs(') || selectorItemKey.includes('props(')) { | ||
const arrowValues = i.split('===') | ||
i = defs[arrowValues[0]][arrowValues[1]] || `${arrowValues[0]}-${arrowValues[1]}` | ||
i = defs[arrowValues[0]][arrowValues[1]].value || `${arrowValues[0]}-${arrowValues[1]}` | ||
} | ||
@@ -75,9 +67,9 @@ return i | ||
} | ||
const newRule = postcss.rule({ selector: selectorItemKey, source: sourceItemKey }) | ||
const newRule = postcss.rule({ selector: selectorItemKey, source: item[itemKey].source }) | ||
for(let [key, val] of itemValues) { | ||
const sourceItemVal = sourceItemValues ? sourceItemValues[key] : undefined | ||
if(typeof val === 'string') { | ||
const sourceItemVal = val.source | ||
if(typeof val.value === 'string') { | ||
if(key.trim().startsWith('inject-')) { | ||
for(let [keyInject, valInject] of Object.entries(provide[props[val]])) { | ||
let declVal = postcss.decl({ prop: keyInject.trim(), value: valInject.trim(), source: sourceItemVal }) | ||
for(let [keyInject, valInject] of Object.entries(provide[props[val.value].value].value)) { | ||
let declVal = postcss.decl({ prop: keyInject.trim(), value: valInject.value, source: valInject.source }) | ||
newRule.append(declVal) | ||
@@ -87,11 +79,11 @@ } | ||
let declVal = undefined | ||
if(val.trim().startsWith('{') && val.trim().endsWith('}')) { | ||
let newDeclVal = val.replace('{', '').replace('}', '').trim() | ||
if(val.value.trim().startsWith('{') && val.value.trim().endsWith('}')) { | ||
let newDeclVal = val.value.replace('{', '').replace('}', '').trim() | ||
const splitDeclVal = newDeclVal.split(/\(|\)|\s|,/g).filter(i => i !== '') | ||
if(Number(splitDeclVal.length) === 1) { | ||
declVal = postcss.decl({ prop: key.trim(), value: props[newDeclVal], source: sourceItemVal }) | ||
declVal = postcss.decl({ prop: key.trim(), value: props[newDeclVal].value, source: props[newDeclVal].source }) | ||
} else { | ||
for(let splittedDecl of splitDeclVal) { | ||
if(props[splittedDecl]) { | ||
newDeclVal = newDeclVal.replaceAll(splittedDecl, props[splittedDecl]) | ||
if(props[splittedDecl]?.value) { | ||
newDeclVal = newDeclVal.replaceAll(splittedDecl, props[splittedDecl].value) | ||
} | ||
@@ -102,6 +94,6 @@ } | ||
} else { | ||
declVal = postcss.decl({ prop: key.trim(), value: val.split(' ').map(i => { | ||
declVal = postcss.decl({ prop: key.trim(), value: val.value.split(' ').map(i => { | ||
if(i.startsWith('refs(') || i.startsWith('props(')) { | ||
const arrowValues = i.split(/\(|\)/g) | ||
i = defs[arrowValues[0]][arrowValues[1]] || i | ||
i = defs[arrowValues[0]][arrowValues[1]].value || i | ||
} | ||
@@ -157,3 +149,3 @@ return i | ||
const arrowValues = i.split('===') | ||
i = defs[arrowValues[0]][arrowValues[1]] || `${arrowValues[0]}-${arrowValues[1]}` | ||
i = defs[arrowValues[0]][arrowValues[1]].value || `${arrowValues[0]}-${arrowValues[1]}` | ||
} | ||
@@ -165,14 +157,14 @@ return i | ||
for(let [key, val] of Object.entries(itemValue)) { | ||
if(typeof val === 'string') { | ||
if(typeof val.value === 'string') { | ||
let declVal = undefined | ||
if(val.trim().startsWith('{') && val.trim().endsWith('}')) { | ||
declVal = postcss.decl({ prop: key.trim(), value: props[val.replace('{', '').replace('}', '').trim()], source: entryVal.source }) | ||
if(val.value.trim().startsWith('{') && val.value.trim().endsWith('}')) { | ||
declVal = postcss.decl({ prop: key.trim(), value: props[val.value.replace('{', '').replace('}', '').trim()].value, source: props[val.value.replace('{', '').replace('}', '').trim()].source }) | ||
} else { | ||
declVal = postcss.decl({ prop: key.trim(), value: val.split(' ').map(i => { | ||
declVal = postcss.decl({ prop: key.trim(), value: val.value.split(' ').map(i => { | ||
if(i.startsWith('refs(') || i.startsWith('props(')) { | ||
const arrowValues = i.split(/\(|\)/g) | ||
i = defs[arrowValues[0]][arrowValues[1]] || i | ||
i = defs[arrowValues[0]][arrowValues[1]].value || i | ||
} | ||
return i | ||
}).join(' ').trim(), source: entryVal.source }) | ||
}).join(' ').trim(), source: val.source }) | ||
} | ||
@@ -200,3 +192,3 @@ newRule.append(declVal) | ||
const arrowValues = i.split('===') | ||
i = defs[arrowValues[0]][arrowValues[1]] || `${arrowValues[0]}-${arrowValues[1]}` | ||
i = defs[arrowValues[0]][arrowValues[1]].value || `${arrowValues[0]}-${arrowValues[1]}` | ||
} | ||
@@ -208,14 +200,14 @@ return i | ||
for(let [key, val] of Object.entries(itemValue)) { | ||
if(typeof val === 'string') { | ||
if(typeof val.value === 'string') { | ||
let declVal = undefined | ||
if(val.trim().startsWith('{') && val.trim().endsWith('}')) { | ||
declVal = postcss.decl({ prop: key.trim(), value: props[val.replace('{', '').replace('}', '').trim()], source: entryVal.source }) | ||
if(val.value.trim().startsWith('{') && val.value.trim().endsWith('}')) { | ||
declVal = postcss.decl({ prop: key.trim(), value: props[val.value.replace('{', '').replace('}', '').trim()].value, source: props[val.value.replace('{', '').replace('}', '').trim()].source }) | ||
} else { | ||
declVal = postcss.decl({ prop: key.trim(), value: val.split(' ').map(i => { | ||
declVal = postcss.decl({ prop: key.trim(), value: val.value.split(' ').map(i => { | ||
if(i.startsWith('refs(') || i.startsWith('props(')) { | ||
const arrowValues = i.split(/\(|\)/g) | ||
i = defs[arrowValues[0]][arrowValues[1]] || i | ||
i = defs[arrowValues[0]][arrowValues[1]].value || i | ||
} | ||
return i | ||
}).join(' ').trim(), source: entryVal.source }) | ||
}).join(' ').trim(), source: val.source }) | ||
} | ||
@@ -222,0 +214,0 @@ newRule.append(declVal) |
@@ -43,3 +43,6 @@ const postcss = require('postcss') | ||
for(let dnode of rnode.nodes) { | ||
defineObj[dnode.prop] = dnode.value | ||
defineObj[dnode.prop] = { | ||
value: dnode.value, | ||
source: dnode.source | ||
} | ||
} | ||
@@ -50,50 +53,70 @@ component[componentName][param] = Object.assign({}, component[componentName][param], defineObj) | ||
const param = rnode.params.trim() | ||
const refOpt = { | ||
...opts, | ||
refs: component[componentName]['refs'] || {}, | ||
props: component[componentName]['props'] || {} | ||
} | ||
const defineObj = {} | ||
defineObj[param] = {} | ||
defineObj[param] = { | ||
value: {}, | ||
source: rnode.source | ||
} | ||
for(let dnode of rnode.nodes) { | ||
// Extracting content of provide | ||
if(dnode.type === 'decl' && dnode.prop === 'ref') { | ||
defineObj[param] = Object.assign({}, defineObj[param], reference(dnode.value)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, reference(dnode, refOpt)) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('ref-')) { | ||
let splitRefs = dnode.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = dnode.value | ||
defineObj[param] = Object.assign({}, defineObj[param], splitRefsObj) | ||
splitRefsObj[camelDash(splitRefs)] = { | ||
value: dnode.value, | ||
source: dnode.source | ||
} | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, splitRefsObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('props-')) { | ||
let splitProps = dnode.prop.split('-')[1] | ||
let splitPropsObj = {} | ||
splitPropsObj[camelDash(splitProps)] = '{'+dnode.value+'}' | ||
defineObj[param] = Object.assign({}, defineObj[param], splitPropsObj) | ||
splitPropsObj[camelDash(splitProps)] = { | ||
value: '{'+dnode.value+'}', | ||
source: dnode.source | ||
} | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, splitPropsObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('screen-')) { | ||
let screenObj = {} | ||
screenObj[dnode.prop] = Object.assign({}, screenObj[dnode.prop], reference(dnode.value)) | ||
defineObj[param] = Object.assign({}, defineObj[param], screenObj) | ||
screenObj[dnode.prop] = Object.assign({}, screenObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, screenObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('state-')) { | ||
let stateObj = {} | ||
stateObj[dnode.prop] = Object.assign({}, stateObj[dnode.prop], reference(dnode.value)) | ||
defineObj[param] = Object.assign({}, defineObj[param], stateObj) | ||
stateObj[dnode.prop] = Object.assign({}, stateObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, stateObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('prefers-')) { | ||
let prefersObj = {} | ||
prefersObj[dnode.prop] = Object.assign({}, prefersObj[dnode.prop], reference(dnode.value)) | ||
defineObj[param] = Object.assign({}, defineObj[param], prefersObj) | ||
prefersObj[dnode.prop] = Object.assign({}, prefersObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, prefersObj) | ||
} else if(dnode.type === 'decl' && dnode.prop.startsWith('if-')) { | ||
let conditionalObj = {} | ||
conditionalObj[dnode.prop] = Object.assign({}, conditionalObj[dnode.prop], reference(dnode.value)) | ||
defineObj[param] = Object.assign({}, defineObj[param], conditionalObj) | ||
conditionalObj[dnode.prop] = Object.assign({}, conditionalObj[dnode.prop], reference(dnode, refOpt)) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, conditionalObj) | ||
} else if(dnode.type === 'atrule' && dnode.name === 'if' && 'nodes' in dnode) { | ||
const paramConditional = node.params.split('in') | ||
const paramConditional = node.params.split(/is|has/) | ||
const valueConditional = paramConditional[0]?.trim() || '' | ||
const propsConditional = paramConditional[1]?.trim() || '' | ||
let conditionalObj = {} | ||
let conditionalObj = { | ||
value: {}, | ||
source: node.source | ||
} | ||
for(let condVal of dnode.nodes) { | ||
if(condVal.type === 'decl' && condVal.prop === 'ref') { | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], reference(condVal)) | ||
conditionalObj.value['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj.value['if-'+propsConditional+'-'+valueConditional], reference(condVal, refOpt)) | ||
} else if(condVal.type === 'decl' && condVal.prop.startsWith('ref-')) { | ||
let splitRefs = condVal.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = condVal.value | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], splitRefsObj) | ||
splitRefsObj[camelDash(splitRefs)] = { | ||
value: condVal.value, | ||
source: condVal.source | ||
} | ||
conditionalObj.value['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj.value['if-'+propsConditional+'-'+valueConditional], splitRefsObj) | ||
} | ||
} | ||
defineObj[param] = Object.assign({}, defineObj[param], conditionalObj) | ||
defineObj[param].value = Object.assign({}, defineObj[param].value, conditionalObj) | ||
} | ||
@@ -112,3 +135,3 @@ } | ||
const arrowParams = param.split(/\(|\)/g) | ||
param = component[componentName][arrowParams[0]][arrowParams[1]] | ||
param = component[componentName][arrowParams[0]][arrowParams[1]].value | ||
} | ||
@@ -118,5 +141,3 @@ let defineObj = {} | ||
defineObj['body'] = [] | ||
defineObj['sourceBody'] = [] | ||
defineObj['content'] = {} | ||
defineObj['sourceContent'] = {} | ||
let index = 1 | ||
@@ -126,3 +147,2 @@ for(let dnode of rnode.nodes) { | ||
defineObj['content'][randId] = [] | ||
defineObj['sourceContent'][randId] = [] | ||
if(dnode.type === 'atrule' && dnode.name === 'use') { | ||
@@ -134,3 +154,2 @@ if('params' in dnode && dnode.params !== '') { | ||
defineObj['content'][randId].push(component[componentName]['modules'][dnode.params.trim()][dnode.params.trim()]['body']) | ||
defineObj['sourceContent'][randId].push(component[componentName]['modules'][dnode.params.trim()][dnode.params.trim()]['sourceBody']) | ||
} | ||
@@ -142,3 +161,3 @@ } else if(dnode.type === 'atrule' && dnode.name === 'keyframes') { | ||
const arrowDnodeParams = paramDnode.split(/\(|\)/g) | ||
paramDnode = component[componentName][arrowDnodeParams[0]][arrowDnodeParams[1]] | ||
paramDnode = component[componentName][arrowDnodeParams[0]][arrowDnodeParams[1]].value | ||
} | ||
@@ -155,3 +174,2 @@ let keyframeDefineObj = {} | ||
defineObj['content'][randId].push(keyframeDefineObj) | ||
defineObj['sourceContent'][randId].push(dnode.source) | ||
} | ||
@@ -161,5 +179,3 @@ } else if(dnode.type === 'atrule' && dnode.name === 'if') { | ||
let ifDefineObj = {} | ||
let ifDefineSource = {} | ||
ifDefineObj['@if '+dnode.params.trim()] = [] | ||
ifDefineSource['@if '+dnode.params.trim()] = [] | ||
for(let ifnode of dnode.nodes) { | ||
@@ -171,8 +187,5 @@ let ifRecursiveDefineObj = recursive(ifnode, { | ||
ifDefineObj['@if '+dnode.params.trim()].push(ifRecursiveDefineObj.body) | ||
ifDefineSource['@if '+dnode.params.trim()].push(ifRecursiveDefineObj.sourceBody) | ||
} | ||
ifDefineObj['@if '+dnode.params.trim()] = ifDefineObj['@if '+dnode.params.trim()].flat() | ||
ifDefineSource['@if '+dnode.params.trim()] = ifDefineSource['@if '+dnode.params.trim()].flat() | ||
defineObj['content'][randId].push(ifDefineObj) | ||
defineObj['sourceContent'][randId].push(ifDefineSource) | ||
} | ||
@@ -185,11 +198,8 @@ } else { | ||
defineObj['content'][randId].push(recursiveDefineObj.body) | ||
defineObj['sourceContent'][randId].push(recursiveDefineObj.sourceBody) | ||
} | ||
defineObj['content'][randId] = defineObj['content'][randId].flat() | ||
defineObj['sourceContent'][randId] = defineObj['sourceContent'][randId].flat() | ||
index++ | ||
} | ||
defineObj['body'] = Object.values(defineObj['content']).flat() | ||
defineObj['sourceBody'] = Object.values(defineObj['sourceContent']).flat() | ||
component[componentName][param] = Object.assign({}, component[componentName][param], defineObj) | ||
@@ -196,0 +206,0 @@ } else if(rnode.type === 'atrule' && rnode.name === 'use') { |
const screen = require('../configs/screen.js') | ||
const camelDash = require('../helpers/camelDash.js') | ||
const reference = require('./reference.js') | ||
const source = require('./source.js') | ||
const selector = require('./selector.js') | ||
@@ -11,55 +10,73 @@ | ||
const recursiveObj = {} | ||
recursiveObj[param] = {} | ||
recursiveObj[param] = { | ||
value: {}, | ||
source: root.source | ||
} | ||
if('nodes' in root && Array.isArray(root.nodes) && root.nodes.length >= 1) { | ||
for(let node of root.nodes) { | ||
if(node.type === 'decl' && node.prop === 'ref') { | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], reference(node.value, opt)) | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, reference(node, opt)) | ||
} else if(node.type === 'decl' && node.prop.startsWith('ref-')) { | ||
let splitRefs = node.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = node.value | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], splitRefsObj) | ||
splitRefsObj[camelDash(splitRefs)] = { | ||
value: node.value, | ||
source: node.source | ||
} | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, splitRefsObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('props-')) { | ||
let splitProps = node.prop.split('-')[1] | ||
let splitPropsObj = {} | ||
splitPropsObj[camelDash(splitProps)] = '{'+node.value+'}' | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], splitPropsObj) | ||
splitPropsObj[camelDash(splitProps)] = { | ||
value: '{'+node.value+'}', | ||
source: node.source | ||
} | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, splitPropsObj) | ||
} else if(node.type === 'decl' && node.prop === 'inject') { | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], opt.provide[node.value]) | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, opt.provide[node.value].value) | ||
} else if(node.type === 'decl' && node.prop === 'inject-props') { | ||
let injectPropsObj = {} | ||
injectPropsObj['inject-'+node.value] = node.value | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], injectPropsObj) | ||
injectPropsObj['inject-'+node.value] = { | ||
value: node.value, | ||
source: node.source | ||
} | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, injectPropsObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('screen-')) { | ||
let screenObj = {} | ||
screenObj[node.prop] = Object.assign({}, screenObj[node.prop], reference(node.value, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], screenObj) | ||
screenObj[node.prop] = Object.assign({}, screenObj[node.prop], reference(node, opt)) | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, screenObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('state-')) { | ||
let stateObj = {} | ||
stateObj[node.prop] = Object.assign({}, stateObj[node.prop], reference(node.value, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], stateObj) | ||
stateObj[node.prop] = Object.assign({}, stateObj[node.prop], reference(node, opt)) | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, stateObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('prefers-')) { | ||
let prefersObj = {} | ||
prefersObj[node.prop] = Object.assign({}, prefersObj[node.prop], reference(node.value, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], prefersObj) | ||
prefersObj[node.prop] = Object.assign({}, prefersObj[node.prop], reference(node, opt)) | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, prefersObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('if-')) { | ||
let conditionalObj = {} | ||
conditionalObj[node.prop] = Object.assign({}, conditionalObj[node.prop], reference(node.value, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], conditionalObj) | ||
conditionalObj[node.prop] = Object.assign({}, conditionalObj[node.prop], reference(node, opt)) | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, conditionalObj) | ||
} else if(node.type === 'atrule' && node.name === 'if' && 'nodes' in node) { | ||
const paramConditional = node.params.split('in') | ||
const paramConditional = node.params.split(/is|has/) | ||
const valueConditional = paramConditional[0]?.trim() || '' | ||
const propsConditional = paramConditional[1]?.trim() || '' | ||
let conditionalObj = {} | ||
let conditionalObj = { | ||
value: {}, | ||
source: node.source | ||
} | ||
for(let condVal of node.nodes) { | ||
if(condVal.type === 'decl' && condVal.prop === 'ref') { | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], reference(condVal, opt)) | ||
conditionalObj.value['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj.value['if-'+propsConditional+'-'+valueConditional], reference(condVal, opt)) | ||
} else if(condVal.type === 'decl' && condVal.prop.startsWith('ref-')) { | ||
let splitRefs = condVal.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = condVal.value | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], splitRefsObj) | ||
splitRefsObj[camelDash(splitRefs)] = { | ||
value: condVal.value, | ||
source: condVal.source | ||
} | ||
conditionalObj.value['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj.value['if-'+propsConditional+'-'+valueConditional], splitRefsObj) | ||
} | ||
} | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], conditionalObj) | ||
recursiveObj[param].value = Object.assign({}, recursiveObj[param].value, conditionalObj) | ||
} else if(node.type === 'rule') { | ||
@@ -76,76 +93,4 @@ for(let par of param.split(',')) { | ||
function recursiveSource(root, prm, opt = {}) { | ||
let param = (root.type === 'rule') ? selector(root, prm) : '' | ||
const recursiveArr = [] | ||
const recursiveObj = {} | ||
recursiveObj[param] = {} | ||
if('nodes' in root && Array.isArray(root.nodes) && root.nodes.length >= 1) { | ||
for(let node of root.nodes) { | ||
if(node.type === 'decl' && node.prop === 'ref') { | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], source(node.value, root.source, opt)) | ||
} else if(node.type === 'decl' && node.prop.startsWith('ref-')) { | ||
let splitRefs = node.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = root.source | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], splitRefsObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('props-')) { | ||
let splitProps = node.prop.split('-')[1] | ||
let splitPropsObj = {} | ||
splitPropsObj[camelDash(splitProps)] = root.source | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], splitPropsObj) | ||
} else if(node.type === 'decl' && node.prop === 'inject') { | ||
const optProvideSource = {} | ||
for(let optProv of Object.keys(opt.provide[node.value])) { | ||
optProvideSource[optProv] = root.source | ||
} | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], optProvideSource) | ||
} else if(node.type === 'decl' && node.prop === 'inject-props') { | ||
let injectPropsObj = {} | ||
injectPropsObj['inject-'+node.value] = root.source | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], injectPropsObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('screen-')) { | ||
let screenObj = {} | ||
screenObj[node.prop] = Object.assign({}, screenObj[node.prop], source(node.value, root.source, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], screenObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('state-')) { | ||
let stateObj = {} | ||
stateObj[node.prop] = Object.assign({}, stateObj[node.prop], source(node.value, root.source, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], stateObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('prefers-')) { | ||
let prefersObj = {} | ||
prefersObj[node.prop] = Object.assign({}, prefersObj[node.prop], source(node.value, root.source, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], prefersObj) | ||
} else if(node.type === 'decl' && node.prop.startsWith('if-')) { | ||
let conditionalObj = {} | ||
conditionalObj[node.prop] = Object.assign({}, conditionalObj[node.prop], source(node.value, root.source, opt)) | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], conditionalObj) | ||
} else if(node.type === 'atrule' && node.name === 'if' && 'nodes' in node) { | ||
const paramConditional = node.params.split('in') | ||
const valueConditional = paramConditional[0]?.trim() || '' | ||
const propsConditional = paramConditional[1]?.trim() || '' | ||
let conditionalObj = {} | ||
for(let condVal of node.nodes) { | ||
if(condVal.type === 'decl' && condVal.prop === 'ref') { | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], source(condVal, node.source, opt)) | ||
} else if(condVal.type === 'decl' && condVal.prop.startsWith('ref-')) { | ||
let splitRefs = condVal.prop.split('-')[1] | ||
let splitRefsObj = {} | ||
splitRefsObj[camelDash(splitRefs)] = node.source | ||
conditionalObj['if-'+propsConditional+'-'+valueConditional] = Object.assign({}, conditionalObj['if-'+propsConditional+'-'+valueConditional], splitRefsObj) | ||
} | ||
} | ||
recursiveObj[param] = Object.assign({}, recursiveObj[param], conditionalObj) | ||
} else if(node.type === 'rule') { | ||
for(let par of param.split(',')) { | ||
recursiveArr.push(recursiveSource(node, par.trim(), opt)) | ||
} | ||
} | ||
} | ||
recursiveArr.unshift(recursiveObj) | ||
} | ||
return recursiveArr | ||
} | ||
module.exports = (node, opt = {}) => { | ||
return { body: Array.from(recursiveFunc(node, '', opt)).flat(Infinity), sourceBody: Array.from(recursiveSource(node, '', opt)).flat(Infinity)} | ||
return { body: Array.from(recursiveFunc(node, '', opt)).flat(Infinity) } | ||
} |
@@ -10,3 +10,3 @@ const preset = require('../configs/preset.js') | ||
const refs = ref.split(' ').filter(i => i !== '') | ||
const refs = ref.value.split(' ').filter(i => i !== '') | ||
for(let rf of refs) { | ||
@@ -27,3 +27,6 @@ const props = rf.trim().split('-').filter(i => i !== '') | ||
const refObj = {} | ||
refObj[camelDash(props[0])] = value(props[1], refOpt) | ||
refObj[camelDash(props[0])] = { | ||
value: value(props[1], refOpt), | ||
source: ref.source | ||
} | ||
@@ -30,0 +33,0 @@ references = Object.assign({}, references, refObj) |
@@ -79,3 +79,3 @@ const chokidar = require('chokidar'); | ||
for(let node of rule.nodes) { | ||
config.components[param]['props'][node.prop] = node.value | ||
config.components[param]['props'][node.prop].value = node.value | ||
} | ||
@@ -87,3 +87,2 @@ } | ||
{ | ||
source: config.components[param][name]['sourceBody'], | ||
refs: config.components[param]['refs'], | ||
@@ -149,3 +148,2 @@ props: config.components[param]['props'], | ||
{ | ||
source: config.components[param][name]['sourceBody'], | ||
refs: config.components[param]['refs'], | ||
@@ -152,0 +150,0 @@ props: config.components[param]['props'], |
127799
-4.15%50
-1.96%1656
-4.22%