postcss-modules-values
Advanced tools
Comparing version 4.0.0-rc.3 to 4.0.0-rc.4
@@ -6,2 +6,10 @@ # Change Log | ||
## [4.0.0-rc.4](https://github.com/postcss-modules-local-by-default/compare/v4.0.0-rc.3...v4.0.0-rc.4) - 2020-10-08 | ||
### Fixes | ||
- perf | ||
- compatibility with empty custom properties | ||
- works with `options.createImportedName` | ||
## [4.0.0-rc.3](https://github.com/postcss-modules-local-by-default/compare/v4.0.0-rc.2...v4.0.0-rc.3) - 2020-10-08 | ||
@@ -8,0 +16,0 @@ |
{ | ||
"name": "postcss-modules-values", | ||
"version": "4.0.0-rc.3", | ||
"version": "4.0.0-rc.4", | ||
"description": "PostCSS plugin for CSS Modules to pass arbitrary values between your module files", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
114
src/index.js
@@ -6,13 +6,12 @@ "use strict"; | ||
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/; | ||
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g; | ||
const matchValueDefinition = /(?:\s+|^)([\w-]+):?(.*?)$/; | ||
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/; | ||
let options = {}; | ||
let importIndex = 0; | ||
let createImportedName = | ||
(options && options.createImportedName) || | ||
((importName /*, path*/) => | ||
`i__const_${importName.replace(/\W/g, "_")}_${importIndex++}`); | ||
module.exports = (options) => { | ||
let importIndex = 0; | ||
let createImportedName = | ||
(options && options.createImportedName) || | ||
((importName /*, path*/) => | ||
`i__const_${importName.replace(/\W/g, "_")}_${importIndex++}`); | ||
module.exports = () => { | ||
return { | ||
@@ -25,61 +24,68 @@ postcssPlugin: "postcss-modules-values", | ||
return { | ||
/* Look at all the @value statements and treat them as locals or as imports */ | ||
AtRule: { | ||
value(atRule) { | ||
if (matchImports.exec(atRule.params)) { | ||
const matches = matchImports.exec(atRule.params); | ||
const matches = atRule.params.match(matchImports); | ||
if (matches) { | ||
let [, /*match*/ aliases, path] = matches; | ||
if (matches) { | ||
let [, /*match*/ aliases, path] = matches; | ||
// We can use constants for path names | ||
if (definitions[path]) { | ||
path = definitions[path]; | ||
} | ||
// We can use constants for path names | ||
if (definitions[path]) { | ||
path = definitions[path]; | ||
} | ||
const imports = aliases | ||
.replace(/^\(\s*([\s\S]+)\s*\)$/, "$1") | ||
.split(/\s*,\s*/) | ||
.map((alias) => { | ||
const tokens = matchImport.exec(alias); | ||
const imports = aliases | ||
.replace(/^\(\s*([\s\S]+)\s*\)$/, "$1") | ||
.split(/\s*,\s*/) | ||
.map((alias) => { | ||
const tokens = matchImport.exec(alias); | ||
if (tokens) { | ||
const [ | ||
, | ||
/*match*/ theirName, | ||
myName = theirName, | ||
] = tokens; | ||
const importedName = createImportedName(myName); | ||
definitions[myName] = importedName; | ||
return { theirName, importedName }; | ||
} else { | ||
throw new Error( | ||
`@import statement "${alias}" is invalid!` | ||
); | ||
} | ||
}); | ||
if (tokens) { | ||
const [, /*match*/ theirName, myName = theirName] = tokens; | ||
const importedName = createImportedName(myName); | ||
definitions[myName] = importedName; | ||
return { theirName, importedName }; | ||
} else { | ||
throw new Error(`@import statement "${alias}" is invalid!`); | ||
} | ||
}); | ||
importAliases.push({ path, imports }); | ||
importAliases.push({ path, imports }); | ||
atRule.remove(); | ||
} | ||
} else { | ||
if (atRule.params.indexOf("@value") !== -1) { | ||
result.warn("Invalid value definition: " + atRule.params); | ||
} | ||
atRule.remove(); | ||
let matches; | ||
return; | ||
} | ||
while ((matches = matchValueDefinition.exec(atRule.params))) { | ||
let [, /*match*/ key, value] = matches; | ||
if (atRule.params.indexOf("@value") !== -1) { | ||
result.warn("Invalid value definition: " + atRule.params); | ||
} | ||
// Add to the definitions, knowing that values can refer to each other | ||
definitions[key] = ICSSUtils.replaceValueSymbols( | ||
value, | ||
definitions | ||
); | ||
let [, key, value] = `${atRule.params}${atRule.raws.between}`.match( | ||
matchValueDefinition | ||
); | ||
atRule.remove(); | ||
} | ||
const normalizedValue = value.replace(/\/\*((?!\*\/).*?)\*\//g, ""); | ||
if (normalizedValue.length === 0) { | ||
result.warn("Invalid value definition: " + atRule.params); | ||
atRule.remove(); | ||
return; | ||
} | ||
let isOnlySpace = /^\s+$/.test(normalizedValue); | ||
if (!isOnlySpace) { | ||
value = value.trim(); | ||
} | ||
// Add to the definitions, knowing that values can refer to each other | ||
definitions[key] = ICSSUtils.replaceValueSymbols( | ||
value, | ||
definitions | ||
); | ||
atRule.remove(); | ||
}, | ||
@@ -86,0 +92,0 @@ }, |
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
9313
113