Last active
March 27, 2020 21:26
-
-
Save jouni-kantola/481d87634f9455ead0397d3f01663036 to your computer and use it in GitHub Desktop.
Inline styles from PostCSS with 11ty and Liquid
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
module.exports = eleventyConfig => { | |
/* ... */ | |
eleventyConfig.addPairedShortcode("postcss", require("./shortcodes/postcss")); | |
/* ... */ | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<!-- ... --> | |
<style> | |
{% postcss %} | |
entry.css | |
{% endpostcss %} | |
{% if category %} | |
{% postcss %} | |
{{category}}.css | |
{% endpostcss %} | |
{% endif %} | |
</style> | |
<!-- ... --> | |
</html> |
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 path = require("path"); | |
const postcss = require("postcss"); | |
const fs = require("fs").promises; | |
module.exports = eleventyConfig => { | |
eleventyConfig.addPairedShortcode("postcss", async filename => { | |
const rawFilepath = path.join( | |
__dirname, | |
`../src/_includes/${filename.trim()}` | |
); | |
const code = await fs.readFile(rawFilepath); | |
return await postcss([ | |
require("precss"), | |
require("postcss-import"), | |
require("postcss-custom-selectors"), | |
require("autoprefixer"), | |
require("cssnano") | |
]) | |
.process(code, { from: rawFilepath }) | |
.then(result => result.css); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment