Skip to content
This repository has been archived by the owner on Sep 15, 2020. It is now read-only.

Commit

Permalink
Switch to grunt-sri-hash.
Browse files Browse the repository at this point in the history
Due to cheerio which grunt-sri-hash is using, we need to make `doctype` lowercase again ourselves hence the grunt-doctype addition.
  • Loading branch information
XhmikosR committed Oct 14, 2018
1 parent 787d823 commit 97e7871
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 71 deletions.
83 changes: 27 additions & 56 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ module.exports = function(grunt) {
]
},

sri_hash: {
dist: {
options: {
algorithm: 'sha384',
assetsDir: '<%= dirs.dest %>'
},
expand: true,
cwd: '<%= dirs.dest %>',
dest: '<%= dirs.dest %>',
src: ['**/*.html', '**/*.php']
}
},

// This is because grunt-sri-hash is using cheerio which
// makes `doctype` uppercase...
doctype: {
dist: {
expand: true,
cwd: '<%= dirs.dest %>',
dest: '<%= dirs.dest %>',
src: ['**/*.html', '**/*.php']
}
},

connect: {
options: {
hostname: 'localhost',
Expand Down Expand Up @@ -280,58 +304,6 @@ module.exports = function(grunt) {
require('load-grunt-tasks')(grunt, { scope: 'dependencies' });
require('time-grunt')(grunt);

grunt.registerTask('generate-sri', 'Generate SRI hashes for our assets.', function () {

function sriDigest(filePattern) {
var sriToolbox = require('sri-toolbox');
var matches = grunt.file.expand({ filter: 'isFile' }, filePattern);
var match = matches[0]; // `grunt.file.expand` returns an array
var matchCount = matches.length;
var integrity = '';

if (matchCount === 0) {
grunt.fail.fatal('Generating SRI failed; didn\'t find any matches!');
} else if (matchCount > 1) {
// shouldn't really happen since we clean the '_site' directory
grunt.fail.fatal('Generating SRI failed; Found more than one matches!');
}

try {
integrity = sriToolbox.generate({ algorithms: ['sha384'] }, grunt.file.read(match));
} catch (err) {
grunt.log.error(err);
grunt.fail.fatal('Generating SRI hash failed.');
}

grunt.log.ok('Generated SRI hash for ' + match.cyan + '.');
return integrity;

}

var packCssIntegrity = sriDigest('_site/assets/css/pack.*.css');
var packJsIntegrity = sriDigest('_site/assets/js/pack.*.js');
var jqueryPackJsIntegrity = sriDigest('_site/assets/js/jquery-pack.*.js');

grunt.config('includereplace', {
dist: {
options: {
globals: {
packCssIntegrity: packCssIntegrity,
packJsIntegrity: packJsIntegrity,
jqueryPackJsIntegrity: jqueryPackJsIntegrity
}
},
files: [{
src: ['**/*.html', '**/*.php'],
dest: '<%= dirs.dest %>/',
expand: true,
cwd: '<%= dirs.dest %>/'
}]
}
});

});

var buildTasks = [
'clean',
'jekyll',
Expand All @@ -343,8 +315,8 @@ module.exports = function(grunt) {
'uglify',
'filerev',
'usemin',
'generate-sri',
'includereplace',
'sri_hash',
'doctype',
'htmlmin'
];

Expand All @@ -366,8 +338,7 @@ module.exports = function(grunt) {
'postcss',
'filerev',
'usemin',
'generate-sri',
'includereplace'
'sri_hash'
]);

grunt.registerTask('server', [
Expand Down
106 changes: 96 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
"grunt-contrib-htmlmin": "^3.0.0",
"grunt-contrib-uglify": "^4.0.0",
"grunt-contrib-watch": "^1.1.0",
"grunt-doctype": "^2.0.1",
"grunt-eslint": "^21.0.0",
"grunt-filerev": "^2.3.1",
"grunt-html": "^9.3.0",
"grunt-include-replace": "^5.0.0",
"grunt-jekyll": "^0.4.7",
"grunt-postcss": "^0.9.0",
"grunt-purgecss": "^1.0.0",
"grunt-sri-hash": "^1.1.1",
"grunt-usemin": "^3.1.1",
"load-grunt-tasks": "^4.0.0",
"postcss-combine-duplicated-selectors": "^6.0.4",
"sri-toolbox": "^0.2.0",
"time-grunt": "^1.4.0"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions source/_includes/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ <h4><span class="fa fa-exclamation-circle" aria-hidden="true"></span> Warning!</
</div>
</noscript>

<script src="/assets/js/pack.js" integrity="@@packJsIntegrity" crossorigin="anonymous" async></script>
<script src="/assets/js/jquery-pack.js" integrity="@@jqueryPackJsIntegrity" crossorigin="anonymous" async></script>
<script src="/assets/js/pack.js" async></script>
<script src="/assets/js/jquery-pack.js" async></script>

</body>
</html>
2 changes: 1 addition & 1 deletion source/_includes/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<meta name="description" content="{{ meta_description }}">
<meta name="generator" content="Jekyll v{{ jekyll.version }}">

<link rel="stylesheet" href="/assets/css/pack.css" integrity="@@packCssIntegrity" crossorigin="anonymous">
<link rel="stylesheet" href="/assets/css/pack.css">

<link rel="apple-touch-icon" href="/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
Expand Down

0 comments on commit 97e7871

Please sign in to comment.