postcss-modules-scope
Advanced tools
Comparing version 3.1.0 to 3.1.1
{ | ||
"name": "postcss-modules-scope", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "A CSS Modules transform to extract export statements from local-scope classes", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -7,3 +7,19 @@ "use strict"; | ||
function getSingleLocalNamesForComposes(root) { | ||
function isNestedRule(rule) { | ||
if (!rule.parent || rule.parent.type === "root") { | ||
return false; | ||
} | ||
if (rule.parent.type === "rule") { | ||
return true; | ||
} | ||
return isNestedRule(rule.parent); | ||
} | ||
function getSingleLocalNamesForComposes(root, rule) { | ||
if (isNestedRule(rule)) { | ||
throw new Error(`composition is not allowed in nested rule \n\n${rule}`); | ||
} | ||
return root.nodes.map((node) => { | ||
@@ -95,3 +111,3 @@ if (node.type !== "selector" || node.nodes.length !== 1) { | ||
function exportScopedName(name, rawName, node) { | ||
function exportScopedName(name, rawName, node, needExport = true) { | ||
const scopedName = generateScopedName( | ||
@@ -112,2 +128,6 @@ rawName ? rawName : name, | ||
if (!needExport) { | ||
return scopedName; | ||
} | ||
exports[key] = exports[key] || []; | ||
@@ -122,6 +142,6 @@ | ||
function localizeNode(node) { | ||
function localizeNode(node, needExport = true) { | ||
switch (node.type) { | ||
case "selector": | ||
node.nodes = node.map(localizeNode); | ||
node.nodes = node.map((item) => localizeNode(item, needExport)); | ||
return node; | ||
@@ -133,3 +153,4 @@ case "class": | ||
node.raws && node.raws.value ? node.raws.value : null, | ||
node | ||
node, | ||
needExport | ||
), | ||
@@ -142,3 +163,4 @@ }); | ||
node.raws && node.raws.value ? node.raws.value : null, | ||
node | ||
node, | ||
needExport | ||
), | ||
@@ -153,3 +175,3 @@ }); | ||
quoteMark: "'", | ||
value: exportScopedName(node.value), | ||
value: exportScopedName(node.value, null, null, needExport), | ||
}); | ||
@@ -165,3 +187,3 @@ } | ||
function traverseNode(node) { | ||
function traverseNode(node, needExport = true) { | ||
switch (node.type) { | ||
@@ -174,3 +196,3 @@ case "pseudo": | ||
const selector = localizeNode(node.first, node.spaces); | ||
const selector = localizeNode(node.first, needExport); | ||
// move the spaces that were around the pseudo selector to the first | ||
@@ -198,3 +220,3 @@ // non-container node | ||
case "selector": { | ||
node.each(traverseNode); | ||
node.each((item) => traverseNode(item, needExport)); | ||
break; | ||
@@ -204,3 +226,3 @@ } | ||
case "class": | ||
if (exportGlobals) { | ||
if (needExport && exportGlobals) { | ||
exports[node.value] = [node.value]; | ||
@@ -229,3 +251,6 @@ } | ||
rule.walkDecls(/composes|compose-with/i, (decl) => { | ||
const localNames = getSingleLocalNamesForComposes(parsedSelector); | ||
const localNames = getSingleLocalNamesForComposes( | ||
parsedSelector, | ||
decl.parent | ||
); | ||
const classes = decl.value.split(/\s+/); | ||
@@ -306,2 +331,21 @@ | ||
root.walkAtRules(/scope$/i, (atRule) => { | ||
atRule.params = atRule.params | ||
.split("to") | ||
.map((item) => { | ||
const selector = item.trim().slice(1, -1).trim(); | ||
const localMatch = /^\s*:local\s*\((.+?)\)\s*$/.exec(selector); | ||
if (!localMatch) { | ||
return `(${selector})`; | ||
} | ||
let parsedSelector = selectorParser().astSync(selector); | ||
return `(${traverseNode(parsedSelector, false).toString()})`; | ||
}) | ||
.join(" to "); | ||
}); | ||
// If we found any :locals, insert an :export rule | ||
@@ -308,0 +352,0 @@ const exportedNames = Object.keys(exports); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15012
311