Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compile JavaScript and Sass in Eleventy using UglifyJS and Sass lib
const terser = require("terser");
const sass = require("sass");
module.exports = (eleventyConfig) => {
// Compile Sass
eleventyConfig.addTemplateFormats("scss");
eleventyConfig.addExtension("scss", {
outputFileExtension: "css",
compile: function (contents, inputPath) {
return (data) => {
let ret = sass.compile(inputPath, { style: "compressed" });
return ret.css;
};
},
});
// Compile Js
eleventyConfig.addTemplateFormats("js");
eleventyConfig.addExtension("js", {
outputFileExtension: "js",
compile: function (contents, inputPath) {
if (inputPath.startsWith(`./src/_`)) return;
return async (data) => {
let ret = await terser.minify(contents);
return ret.code;
};
},
});
return {
dir: {
input: "src",
output: "dist",
},
};
};
@daviddarnes
Copy link
Author

Eleventy docs for adding custom languages https://www.11ty.dev/docs/languages/custom/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment