Skip to content

Commit

Permalink
refactor: code (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 8, 2020
1 parent b0514f2 commit 333d90b
Show file tree
Hide file tree
Showing 3 changed files with 413 additions and 401 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
"homepage": "https://github.com/css-modules/postcss-modules-values#readme",
"devDependencies": {
"coveralls": "^3.1.0",
"eslint": "^7.9.0",
"eslint": "^7.10.0",
"husky": "^4.3.0",
"jest": "^26.4.2",
"jest": "^26.5.2",
"lint-staged": "^10.4.0",
"postcss": "^8.0.7",
"postcss": "^8.1.0",
"prettier": "^2.1.2"
},
"dependencies": {
"icss-utils": "^5.0.0-rc.0"
},
"peerDependencies": {
"postcss": "^8.0.0"
"postcss": "^8.1.0"
}
}
112 changes: 58 additions & 54 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,78 @@ module.exports = () => {
prepare(result) {
const importAliases = [];
const definitions = {};
const addDefinition = (atRule) => {
let matches;

while ((matches = matchValueDefinition.exec(atRule.params))) {
let [, /*match*/ key, value] = matches;

// Add to the definitions, knowing that values can refer to each other
definitions[key] = ICSSUtils.replaceValueSymbols(value, definitions);
atRule.remove();
}
};
const addImport = (atRule) => {
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();
}
};

return {
/* Look at all the @value statements and treat them as locals or as imports */
AtRule: {
value(atRule) {
if (matchImports.exec(atRule.params)) {
addImport(atRule);
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);
}

addDefinition(atRule);
let matches;

while ((matches = matchValueDefinition.exec(atRule.params))) {
let [, /*match*/ key, value] = matches;

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

atRule.remove();
}
}
},
},
RootExit(root, postcss) {
OnceExit(root, postcss) {
/* If we have no definitions, don't continue */
if (!Object.keys(definitions).length) {
return;
}

/* Perform replacements */
ICSSUtils.replaceSymbols(root, definitions);

/* We want to export anything defined by now, but don't add it to the CSS yet or it well get picked up by the replacement stuff */
const exportDeclarations = Object.keys(definitions).map((key) =>
postcss.decl({
Expand All @@ -88,14 +100,6 @@ module.exports = () => {
})
);

/* If we have no definitions, don't continue */
if (!Object.keys(definitions).length) {
return;
}

/* Perform replacements */
ICSSUtils.replaceSymbols(root, definitions);

/* Add export rules if any */
if (exportDeclarations.length > 0) {
const exportRule = postcss.rule({
Expand Down
Loading

0 comments on commit 333d90b

Please sign in to comment.