Skip to content

Instantly share code, notes, and snippets.

@OwenMelbz
Last active June 14, 2020 13:54
Show Gist options
  • Save OwenMelbz/427c83156014de2fec74eb6b482623c2 to your computer and use it in GitHub Desktop.
Save OwenMelbz/427c83156014de2fec74eb6b482623c2 to your computer and use it in GitHub Desktop.
Compile Tailwind CSS Programatically
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)
})
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)
})
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)
})
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