Skip to content
Snippets Groups Projects
Commit 9575c136 authored by David Sveningsson's avatar David Sveningsson
Browse files

add assetDir option

parent 1616ec82
Branches
Tags
No related merge requests found
......@@ -26,6 +26,13 @@ nunjucks: {
## Options
### `assetDir`
- Type: `string`
- Default: `"public/assets"`
Source directory to read assets from.
## Filters
### `assetUrl` filter
......
......@@ -2,7 +2,9 @@ const crypto = require('crypto');
const execSync = require('child_process').execSync;
const fs = require('fs');
const defaults = {};
const defaults = {
assetDir: 'public/assets',
};
function wrapper(opt){
const options = Object.assign({}, defaults, opt);
......@@ -26,20 +28,20 @@ function wrapper(opt){
}
function assetHash(asset){
const filename = `public/assets/${asset}`;
if (fs.existsSync(filename)){
const filename = getAsset(asset);
if (filename){
const data = fs.readFileSync(filename);
const hash = crypto.createHash('md5').update(data).digest('hex');
return `assets/${asset}?${hash}`;
} else {
console.log(`${filename} does not exist when trying to calculate asset hash.`);
console.log(`${options.assetDir}/${asset} does not exist when trying to calculate asset hash.`);
return `assets/${asset}`;
}
}
function sri(asset, algorithm){
const filename = `public/assets/${asset}`;
if (fs.existsSync(filename)){
const filename = getAsset(asset);
if (filename){
if (process.env.NODE_ENV === 'production'){ /* set in .gitlab-ci.yml */
const data = fs.readFileSync(filename);
const hash = crypto.createHash(algorithm).update(data).digest('base64');
......@@ -48,10 +50,15 @@ function wrapper(opt){
return ''; /* SRI skipped for local builds */
}
} else {
console.log(`${filename} does not exist when trying to calculate SRI.`);
console.log(`${options.assetDir}/${asset} does not exist when trying to calculate SRI.`);
return '';
}
}
function getAsset(asset){
const filename = `${options.assetDir}/${asset}`;
return fs.existsSync(filename) ? filename : null;
}
}
module.exports = wrapper;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment