Last active
February 14, 2022 02:10
-
-
Save daviddarnes/8d70d7b8eaee474bcb19e30fc45e63ff to your computer and use it in GitHub Desktop.
Compile JavaScript and Sass in Eleventy using UglifyJS and Sass lib
This file contains 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 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
Eleventy docs for adding custom languages https://www.11ty.dev/docs/languages/custom/