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

chore(release): 4.0.0-rc.4 #14

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [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

### BREAKING CHANGE
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"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",
"main": "src/index.js",
"files": [
Expand Down
124 changes: 65 additions & 59 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,89 @@
const ICSSUtils = require("icss-utils");

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 {
postcssPlugin: "postcss-modules-values",
prepare(result) {
const importAliases = [];
const definitions = {};

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);

if (matches) {
let [, /*match*/ aliases, path] = matches;

// 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);

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 });

atRule.remove();
}
} else {
if (atRule.params.indexOf("@value") !== -1) {
result.warn("Invalid value definition: " + atRule.params);
const matches = atRule.params.match(matchImports);

if (matches) {
let [, /*match*/ aliases, path] = matches;

// We can use constants for path names
if (definitions[path]) {
path = definitions[path];
}

let matches;
const imports = aliases
.replace(/^\(\s*([\s\S]+)\s*\)$/, "$1")
.split(/\s*,\s*/)
.map((alias) => {
const tokens = matchImport.exec(alias);

while ((matches = matchValueDefinition.exec(atRule.params))) {
let [, /*match*/ key, value] = matches;
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!`);
}
});

// Add to the definitions, knowing that values can refer to each other
definitions[key] = ICSSUtils.replaceValueSymbols(
value,
definitions
);
importAliases.push({ path, imports });

atRule.remove();
}
atRule.remove();

return;
}

if (atRule.params.indexOf("@value") !== -1) {
result.warn("Invalid value definition: " + atRule.params);
}

let [, key, value] = `${atRule.params}${atRule.raws.between}`.match(
matchValueDefinition
);

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();
},
},
OnceExit(root, postcss) {
Expand Down
Loading