-
A common task is to add cache-busting hashes to asset filenames, such as requested here: This could work well with Here is a sample Eleventy config, based on the documentation, where I try to add MD5 hashing in the output filename for SCSS files: const path = require("node:path");
const crypto = require('node:crypto');
function getHash(content, length = 10) {
const hash = crypto.createHash('md5');
hash.update(content);
return hash.digest('hex').substr(0, length);
}
/* SASS support */
const scss_hashes = {};
config.addTemplateFormats('scss');
config.addExtension('scss', {
outputFileExtension: "css",
getData: inputPath => ({ _md5: scss_hashes[inputPath] }),
compileOptions: {
cache: false,
permalink: function(permalink, inputPath) {
if (path.basename(inputPath).startsWith("_")) {
return false;
}
return data => {
const { filePathStem, outputFileExtension } = data.page;
return filePathStem + data._md5 + `.${outputFileExtension}`;
};
}
},
compile: function(content, inputPath) {
const result = sass.compileString(content, {
loadPaths: [
path.dirname(inputPath) || '.',
this.config.dir.includes
]
});
scss_hashes[inputPath] = getHash(content);
return data => result.css;
}
}); But Would it make sense to execute |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
A possible workaround involves processing the SCSS file at the config.addTemplateFormats('scss');
config.addExtension('scss', {
read: false,
getData: async function(inputPath) {
const { css } = sass.compile(inputPath);
return {
_content: css,
_hash: getHash(css)
};
},
compileOptions: {
permalink: function(permalink, inputPath) {
if (path.basename(inputPath).startsWith("_")) {
return false;
}
return data => `${data.page.filePathStem}.${data._hash}.css`;
}
},
compile: () => data => data._content
}); |
Beta Was this translation helpful? Give feedback.
A possible workaround involves processing the SCSS file at the
getData()
level: