Last active
June 14, 2020 13:54
-
-
Save OwenMelbz/427c83156014de2fec74eb6b482623c2 to your computer and use it in GitHub Desktop.
Compile Tailwind CSS Programatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const postcss = require('postcss') | |
const tailwind = require('tailwindcss') | |
const source = ` | |
@tailwind base; | |
@tailwind components; | |
@tailwind utilities; | |
` | |
postcss([tailwind()]) | |
.process(source, { from: undefined }) | |
.then(({ css }) => { | |
fs.writeFileSync(`${__dirname}/styles.css`, css) | |
}) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const postcss = require('postcss') | |
const tailwind = require('tailwindcss') | |
const source = ` | |
@tailwind components; | |
@tailwind utilities; | |
` | |
function toJson(node, json) { | |
node.each(node => json[node.prop] = node.value); | |
return json; | |
} | |
function cssToJson(root, cssMap) { | |
root.each(node => { | |
if (node.type == "rule" && node.selectors && node.nodes) { | |
cssMap[node.selectors.join(" ").replace('.', '')] = toJson(node, {}); | |
} | |
}); | |
} | |
postcss([tailwind()]) | |
.process(source, { from: undefined }) | |
.then(({ root }) => { | |
const json = {}; | |
cssToJson(root, json); | |
fs.writeFileSync(`${__dirname}/styles.json`, JSON.stringify(json, null, 4)) | |
}) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const postcss = require('postcss') | |
const tailwind = require('tailwindcss') | |
const source = ` | |
@tailwind components; | |
@tailwind utilities; | |
` | |
function toJson(node, json) { | |
node.each(node => json[node.prop] = node.value); | |
return json; | |
} | |
function cssToJson(root, cssMap) { | |
root.each(node => { | |
if (node.type == "rule" && node.selectors && node.nodes) { | |
cssMap[node.selectors.join(" ").replace('.', '')] = toJson(node, {}); | |
} | |
}); | |
} | |
postcss([tailwind()]) | |
.process(source, { from: undefined }) | |
.then(({ root }) => { | |
const json = {}; | |
cssToJson(root, json); | |
fs.writeFileSync(`${__dirname}/styles.mjs`, `export default ${JSON.stringify(json, null, 4)};`) | |
}) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const postcss = require('postcss') | |
const tailwind = require('tailwindcss') | |
const source = ` | |
@tailwind components; | |
@tailwind utilities; | |
` | |
function toJson(node, json) { | |
node.each(node => json[node.prop] = node.value); | |
return json; | |
} | |
function cssToJson(root, cssMap) { | |
root.each(node => { | |
if (node.type == "rule" && node.selectors && node.nodes) { | |
cssMap[node.selectors.join(" ").replace('.', '')] = toJson(node, {}); | |
} | |
}); | |
} | |
postcss([tailwind()]) | |
.process(source, { from: undefined }) | |
.then(({ root }) => { | |
const json = {}; | |
cssToJson(root, json); | |
fs.writeFileSync(`${__dirname}/styles.js`, `module.exports = ${JSON.stringify(json, null, 4)};`) | |
}) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment