Skip to content

Commit 03b11b4

Browse files
committed
Add a simple script to generate SRI hashes for our assets.
1 parent 0f17d53 commit 03b11b4

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

build/generate-sri.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
3+
/*!
4+
* Script to generate SRI hashes for use in our docs.
5+
* Remember to use the same vendor files as the CDN ones,
6+
* otherwise the hashes won't match!
7+
*
8+
* Copyright 2017 The Bootstrap Authors
9+
* Copyright 2017 Twitter, Inc.
10+
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
11+
*/
12+
13+
'use strict'
14+
15+
const fs = require('fs')
16+
const path = require('path')
17+
const sriToolbox = require('sri-toolbox')
18+
const sh = require('shelljs')
19+
const sed = sh.sed
20+
21+
sh.config.fatal = true
22+
23+
const configFile = path.join(__dirname, '..', '_config.yml')
24+
25+
// Array of objects which holds the files to generate SRI hashes for.
26+
// `file` is the path from the root folder
27+
// `configPropertyName` is the _config.yml variable's name of the file
28+
const files = [
29+
{
30+
file: 'dist/css/bootstrap.min.css',
31+
configPropertyName: 'css_hash'
32+
},
33+
{
34+
file: 'dist/js/bootstrap.min.js',
35+
configPropertyName: 'js_hash'
36+
},
37+
{
38+
file: 'assets/js/vendor/jquery-slim.min.js',
39+
configPropertyName: 'jquery_hash'
40+
},
41+
{
42+
file: 'assets/js/vendor/popper.min.js',
43+
configPropertyName: 'popper_hash'
44+
}
45+
]
46+
47+
files.forEach((file) => {
48+
fs.readFile(file.file, 'utf8', (err, data) => {
49+
if (err) {
50+
throw err
51+
}
52+
53+
const integrity = sriToolbox.generate({
54+
algorithms: ['sha384']
55+
}, data)
56+
57+
console.log(`${file.configPropertyName}: ${integrity}`)
58+
59+
sed('-i', new RegExp(`(\\s${file.configPropertyName}:\\s+"|')(\\S+)("|')`), '$1' + integrity + '$3', configFile)
60+
})
61+
})

build/ship.sh

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ printf "\n${magenta}Compile latest CSS and JS...${end}"
4040
printf "\n${magenta}=======================================================\n${end}"
4141
npm run dist
4242

43+
# Generate the SRI hashes
44+
printf "\n${magenta}=======================================================${end}"
45+
printf "\n${magenta}Generate the SRI hashes...${end}"
46+
printf "\n${magenta}=======================================================\n${end}"
47+
npm run release-sri
48+
4349
# Compress the dist files
4450
printf "\n${magenta}=======================================================${end}"
4551
printf "\n${magenta}Compressing the dist files...${end}"

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"docs-upload-preview": "build/upload-preview.sh",
5656
"docs-workbox-precache": "node build/workbox.js",
5757
"maintenance-dependencies": "ncu -a -x jquery && npm update && bundle update && shx echo \"Manually update assets/js/vendor/*, js/tests/vendor/* and .travis.yml\"",
58+
"release-sri": "node build/generate-sri.js",
5859
"release-version": "node build/change-version.js",
5960
"release-zip": "cd dist/ && zip -r9 bootstrap-$npm_package_version-dist.zip * && shx mv bootstrap-$npm_package_version-dist.zip ..",
6061
"dist": "npm-run-all --parallel css js",
@@ -106,6 +107,7 @@
106107
"rollup-plugin-node-resolve": "^3.0.0",
107108
"shelljs": "^0.7.8",
108109
"shx": "^0.2.2",
110+
"sri-toolbox": "^0.2.0",
109111
"stylelint": "^8.2.0",
110112
"stylelint-config-recommended-scss": "^2.0.0",
111113
"stylelint-config-standard": "^17.0.0",

0 commit comments

Comments
 (0)