Skip to content

Instantly share code, notes, and snippets.

@daviddarnes
Last active February 14, 2022 02:10
Show Gist options
  • Save daviddarnes/8d70d7b8eaee474bcb19e30fc45e63ff to your computer and use it in GitHub Desktop.
Save daviddarnes/8d70d7b8eaee474bcb19e30fc45e63ff to your computer and use it in GitHub Desktop.
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