Skip to content

Commit

Permalink
Merge pull request #4 from jbgraug/master
Browse files Browse the repository at this point in the history
added -e, --etag argument to inject filenames + ?etag=hash to avoid undesired caching in new deployments
  • Loading branch information
jbgraug authored Aug 22, 2017
2 parents 85fd3f2 + 309c947 commit b26e724
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
21 changes: 19 additions & 2 deletions postbuild
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ const path = require('path');
const glob = require('glob');
const replaceStream = require('replacestream');
const program = require('commander');
const crypto = require('crypto');

let cssFiles;
let jsFiles;
let revision;
let removeCondition;

function appendETagSHAToFilename(filename){
if (program.etag) {
let sha = crypto.createHash('sha');
let fContent = fs.readFileSync(filename).toString();
sha.update(fContent);
return `${filename}?etag=${sha.digest('hex').toString()}`;
} else {
return filename;
}
}

const replaceFunc = replacement => {
return function() {
return replacement === undefined ? arguments[0] : replacement;
Expand All @@ -38,6 +50,7 @@ program
.option('-r, --remove <remove>', 'Remove condition')
.option('-g, --ignore <path>', 'Prefix to remove from the injected filenames')
.option('-H, --hash', 'Inject git hash of current commit')
.option('-e, --etag', 'appends "?etag=fileHash" to every import (link, script) to avoid undesired caching in new deployments')
.parse(process.argv);

if(!program.input) {
Expand Down Expand Up @@ -68,19 +81,21 @@ if(program.js) {
if(glob.hasMagic(js)) {
jsFiles = glob.sync(js)
.map(file => removePrefix(file, program.ignore))
.map(file => appendETagSHAToFilename(file))
.map(file => `<script src="${file}"></script>`)
.join('\n');
}
else if(fs.lstatSync(js).isDirectory()) {
jsFiles = fs.readdirSync(js)
.filter(file => file.substr(-3) === '.js')
.map(file => removePrefix(`${js}/${file}`, program.ignore))
.map(file => appendETagSHAToFilename(file))
.map(file => `<script src="${file}"></script>`)
.join('\n');
}
else if(fs.lstatSync(js).isFile()) {
let file = removePrefix(js, program.ignore);
jsFiles = `<script src="${file}"></script>`;
jsFiles = `<script src="${appendETagSHAToFilename(file)}"></script>`;
}
}
catch(e) {
Expand All @@ -95,19 +110,21 @@ if(program.css) {
if(glob.hasMagic(css)) {
cssFiles = glob.sync(css)
.map(file => removePrefix(file, program.ignore))
.map(file => appendETagSHAToFilename(file))
.map(file => `<link rel="stylesheet" href="${file}">`)
.join('\n');
}
else if(fs.lstatSync(css).isDirectory()) {
cssFiles = fs.readdirSync(css)
.filter(file => file.substr(-4) === '.css')
.map(file => removePrefix(`${css}/${file}`, program.ignore))
.map(file => appendETagSHAToFilename(file))
.map(file => `<link rel="stylesheet" href="${file}">`)
.join('\n');
}
else if(fs.lstatSync(css).isFile()) {
let file = removePrefix(css, program.ignore);
cssFiles = `<link rel="stylesheet" href="${file}">`;
cssFiles = `<link rel="stylesheet" href="${appendETagSHAToFilename(file)}">`;
}
}
catch(e) {
Expand Down
24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ This way you can replace the regular files used in a development environment wit
-r, --remove <remove> Remove condition
-g, --ignore <path> Prefix to remove from the injected filenames
-H, --hash Inject git hash of current commit
-e, --etag Appends "?etag=fileHash" to every import (link, script) to avoid undesired caching in new deployments

### Examples

Expand Down Expand Up @@ -134,6 +135,29 @@ results in:

*Notice that `vendor.js` is not injected.*

If the -e, --etag is specified the output will be

```html
<!DOCTYPE html>
<html>
<head>
<title>Home</title>

<!-- inject:css -->
<link rel="stylesheet" href="dist/css/styles.1234abc.css?etag=e997365235369248a234b1c343ac41">
<!-- endinject -->

</head>
<body>

<!-- inject:js -->
<script src="dist/js/build.4567def.js?etag=9fffaf9de332d9848ab34bbc3434d34341"></script>
<!-- endinject -->

</body>
</html>
```

### Tests

Run `npm test` to run the tests.
Expand Down

0 comments on commit b26e724

Please sign in to comment.