Skip to content

Instantly share code, notes, and snippets.

@ItsMeAra
Forked from daviddarnes/.eleventy.js
Created February 1, 2022 16:41
Show Gist options
  • Save ItsMeAra/49b58cdb68ad513b2c3d51e61026d8e0 to your computer and use it in GitHub Desktop.
Save ItsMeAra/49b58cdb68ad513b2c3d51e61026d8e0 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",
},
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment