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