Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export the plugin name #173

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 69 additions & 63 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,75 +305,81 @@ module.exports = (opts = {}) => {
let preserveEmpty = opts.preserveEmpty

return {
Once(root) {
root.walkAtRules(rootRuleName, node => {
normalizeRootRule(node)
root[hasRootRule] = true
})
},

postcssPlugin: 'postcss-nested',

RootExit(root) {
if (!root[hasRootRule]) return

root.walkAtRules(rootRuleName, unwrapRootRule)
root[hasRootRule] = false
},

Rule(rule) {
let unwrapped = false
let after = rule
let copyDeclarations = false
let declarations = []

rule.each(child => {
switch (child.type) {
case 'atrule':
[after, declarations] = pickAndClearDeclarations(rule.selector, declarations, after)

if (child.name === rootRuleName) {
unwrapped = true
atruleChilds(rule, child, true, child[rootRuleMergeSel])
after = breakOut(child, after)
} else if (bubble[child.name]) {
copyDeclarations = true
unwrapped = true
atruleChilds(rule, child, true)
after = breakOut(child, after)
} else if (unwrap[child.name]) {
copyDeclarations = true
unwrapped = true
atruleChilds(rule, child, false)
after = breakOut(child, after)
} else if (copyDeclarations) {
declarations.push(child)
}

break
case 'decl':
if (copyDeclarations) {
declarations.push(child)
prepare() {
return {
Once(root) {
root.walkAtRules(rootRuleName, node => {
normalizeRootRule(node)
root[hasRootRule] = true
})
},

postcssPlugin: 'postcss-nested',

RootExit(root) {
if (!root[hasRootRule]) return

root.walkAtRules(rootRuleName, unwrapRootRule)
root[hasRootRule] = false
},

Rule(rule) {
let unwrapped = false
let after = rule
let copyDeclarations = false
let declarations = []

rule.each(child => {
switch (child.type) {
case 'atrule':
[after, declarations] = pickAndClearDeclarations(rule.selector, declarations, after)

if (child.name === rootRuleName) {
unwrapped = true
atruleChilds(rule, child, true, child[rootRuleMergeSel])
after = breakOut(child, after)
} else if (bubble[child.name]) {
copyDeclarations = true
unwrapped = true
atruleChilds(rule, child, true)
after = breakOut(child, after)
} else if (unwrap[child.name]) {
copyDeclarations = true
unwrapped = true
atruleChilds(rule, child, false)
after = breakOut(child, after)
} else if (copyDeclarations) {
declarations.push(child)
}

break
case 'decl':
if (copyDeclarations) {
declarations.push(child)
}

break
case 'rule':
[after, declarations] = pickAndClearDeclarations(rule.selector, declarations, after)

copyDeclarations = true
unwrapped = true
child.selectors = mergeSelectors(rule, child)
after = breakOut(child, after)

break
}
})

break
case 'rule':
[after, declarations] = pickAndClearDeclarations(rule.selector, declarations, after)
pickAndClearDeclarations(rule.selector, declarations, after, false)

copyDeclarations = true
unwrapped = true
child.selectors = mergeSelectors(rule, child)
after = breakOut(child, after)

break
if (unwrapped && preserveEmpty !== true) {
rule.raws.semicolon = true
if (rule.nodes.length === 0) rule.remove()
}
}
})

pickAndClearDeclarations(rule.selector, declarations, after, false)

if (unwrapped && preserveEmpty !== true) {
rule.raws.semicolon = true
if (rule.nodes.length === 0) rule.remove()
}
}
}
Expand Down
Loading