-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildHash.js
78 lines (67 loc) · 2.2 KB
/
buildHash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* ASSET HASH FOR CSS AND JS FILES
*
* Adds hash numberS to the end of css and js files in the dist/ folder.
* Also adds the hash numbers to "src/_data/version.json" file to be used in base.njk like so:
*
* {% set cssHash = version.css || "" %}
* <link rel="stylesheet" href="/assets/css/main{{ cssHash }}.css">
*
* {% set jsHash = version.js || '' %}
* <script src="/assets/js/main{{ jsHash }}.js"></script>
*
* Inspired from eleventy-parcel starter
* https://github.com/mbarker84/eleventy-parcel
*/
const fs = require('fs')
const util = require('util')
const md5 = require('md5')
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)
let cssHash
let jsHash
const readCssFile = () => {
return readFile('dist/assets/css/main.css', (err, data) => {
if (err) console.log(err)
})
}
const readJsFile = () => {
return readFile('dist/assets/js/main.js', (err, data) => {
if (err) console.log(err)
})
}
readCssFile()
.then((data) => {
cssHash = md5(data).slice(0, 16)
console.log('Hashes:')
console.log(cssHash)
})
.then(() => {
readJsFile()
.then((data) => {
jsHash = md5(data).slice(0, 16)
console.log(jsHash)
})
.then(() => {
const versionObject = {
css: cssHash,
js: jsHash
}
writeFile('src/_data/version.json', JSON.stringify(versionObject), (err) => {
if (err) throw err
console.log('The file has been saved!');
})
console.log('Files: ')
console.log(`${JSON.stringify(versionObject)} > src/_data/version.json`)
})
.then(() => {
fs.rename('dist/assets/css/main.css', `dist/assets/css/main${cssHash}.css`, function(err) {
if ( err ) return console.log('ERROR: ' + err)
console.log(`dist/assets/css/main.css > dist/assets/css/main${cssHash}.css`)
})
fs.rename('dist/assets/js/main.js', `dist/assets/js/main${jsHash}.js`, function(err) {
if ( err ) return console.log('ERROR: ' + err)
console.log(`dist/assets/js/main.js > dist/assets/js/main${jsHash}.js`)
})
})
})