Skip to content

Adding SRI support to codeorigin.jquery.com #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ var _ = require( "underscore" ),
http = require( "http" );

grunt.loadNpmTasks( "grunt-jquery-content" );
grunt.loadNpmTasks( "grunt-sri" );

grunt.initConfig({
wordpress: (function() {
var config = require( "./config" );
config.dir = "dist/wordpress";
return config;
})()
})(),
sri: {
generate: {
src: [
"cdn/**/*.js",
"cdn/**/*.css"
],
options: {
algorithms: ["sha256"],
dest: "./sri-directives.json"
}
}
}
});

grunt.registerTask( "build-index", function() {
Expand Down Expand Up @@ -265,15 +278,25 @@ grunt.registerTask( "build-index", function() {
};
}

var sriHashes = require("./sri-directives.json");
Handlebars.registerHelper( "release", function( prefix, release ) {
var html = prefix + " " + release.version + " - " +
"<a href='/" + release.filename + "'>uncompressed</a>";
var sri = function(filename) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be defined outside of the helper so that it's only ever defined once.

var hashes = sriHashes["@cdn/" + filename]["hashes"];
return "sha256-" + hashes["sha256"];
};

var href = function(key, label) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say this should move out too, and the value from release should be provided instead of just the key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon moving this out, I decided that a seperate sri function seemed overkill as there was no reuse. I collapsed href and sri in to a single function.

label = (label === undefined ? key : label);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

label = label || key

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this line would go away if the above is implemented (label would no longer be optional).

return "<a class='open-sri-modal' href='/" + release[key] + "' data-hash='" + sri(release[key]) + "'>" + label + "</a>";
};

var html = prefix + " " + release.version + " - " + href("filename", "uncompressed");

if ( release.minified ) {
html += ", <a href='/" + release.minified + "'>minified</a>";
html += ", " + href("minified");
}
if ( release.packed ) {
html += ", <a href='/" + release.packed + "'>packed</a>";
html += ", " + href("packed");
}

return new Handlebars.SafeString( html );
Expand Down Expand Up @@ -360,7 +383,7 @@ grunt.registerTask( "reload-listings", function() {
});
});

grunt.registerTask( "build", [ "build-index" ] );
grunt.registerTask( "build", [ "sri:generate", "build-index" ] );
grunt.registerTask( "deploy", [ "wordpress-deploy", "reload-listings" ] );

};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"grunt": "0.4.5",
"grunt-jquery-content": "3.0.0",
"grunt-sri": "0.0.5",
"semver": "2.0.11",
"underscore": "1.5.1",
"handlebars": "1.0.12"
Expand Down
1 change: 1 addition & 0 deletions sri-directives.json

Large diffs are not rendered by default.