Skip to content

feat: export css variables to a separate file #11

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,18 @@ You can easily disable the module by giving it a value of `false`

```js
{
outputToFile: true; // default: false
outputFilePath: './path/to/file.css'; // default: null
postcssEachVariables: true; // default: false
}
```

### outputToFile
This option can be set to `true` if you want to extract the `:root {}` variables into a separate file so that they are not apart of the tailwind.css output.

### outputFilePath
This option is the relative path to the file you wish to write to. By specifiying the path eg. `./dist/css/variables.css`, the plugin will create that file and populate it with the `:root {}` variables.

### postcssEachVariables

this option will let the plugin generate CSS variables as an array of colors, screens, fonts and text sizes as this example
Expand Down
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require('fs');

module.exports = function(customVariableName, opts) {
return ({ addComponents, config }) => {
const varModules = {
Expand Down Expand Up @@ -94,7 +96,20 @@ module.exports = function(customVariableName, opts) {
let root = {
':root': rootArray
};
addComponents(root);

if (options.outputToFile && options.outputFilePath) {
const cssVariables = Object.entries(rootArray);
let outputString = ":root {\n";
cssVariables.forEach((variable) => outputString += `\t${variable[0]}: ${variable[1]};\n`);
outputString += "}\n";

fs.writeFile(options.outputFilePath, outputString, (err) => {
if (err) throw err;
console.log(`Created file: ${options.outputFilePath}`);
});
} else {
addComponents(root);
}
};
};

Expand Down