This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathversion.js
137 lines (110 loc) · 4.8 KB
/
version.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* grunt-assets-wp
* https://github.com/roots/grunt-assets-wp
*
* Copyright (c) 2014 Hariadi Hinta
* Licensed under the MIT license.
*/
'use strict';
var fs = require('fs'),
path = require('path'),
crypto = require('crypto'),
async = require('async');
module.exports = function(grunt) {
grunt.registerMultiTask('version', 'WordPress assets revving', function() {
var options = this.options({
encoding: 'utf8',
algorithm: 'md5',
format: false,
minify: true,
minifyname: 'min',
length: 8,
rename: false,
querystring: {},
manifest: false,
summaryOnly: false
});
var manifest = grunt.manifest || {}, summary = {};
options.minifyname = '.' + options.minifyname;
var querystring = (options.querystring.style && options.querystring.script) ? true : false;
async.forEach(this.files, function (files, next) {
files.src.filter(function(file) {
if (!grunt.file.exists(file)) {
grunt.log.warn('Source file "' + file + '" not found.');
return false;
} else {
return true;
}
}).map(function(file) {
var basename = path.basename,
original = basename(file),
isMinify = (options.minify || options.minifyname.indexOf(original) !== -1) ? true : false,
name = original.replace(options.minifyname, ''),
content = grunt.file.read(file),
hash = crypto.createHash(options.algorithm).update(content, options.encoding).digest('hex'),
suffix = hash.slice(0, options.length),
ext = path.extname(file),
namepart = path.basename(name, ext),
newName = options.format ?
[suffix, basename(file, ext), ext.slice(1)].join('.') :
(isMinify) ?
[basename(name, ext), suffix + options.minifyname, ext.slice(1)].join('.') :
[basename(file, ext), suffix, ext.slice(1)].join('.');
manifest.dest = (typeof options.manifest === 'string' || options.manifest instanceof String) ? options.manifest : path.dirname(files.dest) + '/manifest.json';
summary = { path: file, hash: suffix };
// Get target, find and change references assets to new hashed.
var wpcontent = grunt.file.read(files.dest), match, re;
if (querystring) {
if (ext === '.css') {
re = new RegExp('(wp_enqueue_style\\(' + '\''+ options.querystring.style +'\'' + ',(\\s*[^,]+,){2})\\s*[^\\)]+\\);');
newName = '$1 ' + '\''+ suffix +'\'' + ');';
summary.handle = options.querystring.style;
} else if (ext === '.js') {
re = new RegExp('(wp_register_script\\(' + '\''+ options.querystring.script +'\'' + ',(\\s*[^,]+,){2})\\s*[^,]+,\\s*([^\\)]+)\\);');
newName = '$1 ' + '\''+ suffix +'\'' + ', ' + '$3);';
summary.handle = options.querystring.script;
}
manifest.querystring = true;
// Only rename the source if manifest disable
if (!options.manifest) {
wpcontent = wpcontent.replace(re, newName);
grunt.log.writeln(' ' + files.dest.grey + ' update to ' + name.green + ' ('+ suffix.grey +')');
}
} else {
// Copy/rename file base on hash and format
var resultPath = path.resolve(path.dirname(file), newName);
if (options.rename) {
fs.renameSync(file, resultPath);
} else {
grunt.file.copy(file, resultPath);
}
// Make sure $media for css and $ver for js is null
// Ref: wp_enqueue_style( $handle, $src, $deps, $ver, $media )
wpcontent = wpcontent.replace('\''+ hash +'\'', 'null').replace('\''+ suffix +'\'', 'null');
ext = isMinify ? options.minifyname + ext : ext;
match = options.format ?
new RegExp('[a-z0-9]{' + options.length + '}.' + original, 'g') :
new RegExp(namepart + '.[a-z0-9]{' + options.length + '}' + ext, 'g');
re = ( match.test(wpcontent) ) ? match : new RegExp(original, 'g');
wpcontent = wpcontent.replace(re, newName);
var status = (options.rename) ? ' rename' : ' change';
grunt.log.writeln(' ' + file.grey + status + ' to ' + newName.green);
summary.path = path.dirname(file) + '/' + newName;
}
if(typeof summary !== 'undefined'){
manifest[file] = summary;
}
if (!options.summaryOnly) {
grunt.file.write(files.dest, wpcontent);
}
});
next();
}, this.async());
if (options.manifest) {
grunt.file.write(manifest.dest, JSON.stringify(manifest, null, 2));
grunt.log.writeln(' Manifest file save to ' + manifest.dest.green);
}
// expose to other task
grunt.manifest = manifest;
});
};