-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcdnify.js
57 lines (47 loc) · 1.64 KB
/
cdnify.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
'use strict';
var path = require('path');
var eachAsync = require('each-async');
var googlecdn = require('google-cdn');
var bowerConfig = require('bower').config;
var chalk = require('chalk');
module.exports = function (grunt) {
grunt.registerMultiTask('cdnify', 'Replace scripts with refs to the Google CDN', function () {
// collect files
var files = grunt.file.expand({ filter: 'isFile' }, this.data.html);
var compJson = grunt.file.readJSON('bower.json');
var options = this.options({
cdn: 'google'
});
// Strip the leading path segment off, e.g. `app/bower_components` ->
// `bower_components`
var bowerDirBits = bowerConfig.directory.split(path.sep);
bowerDirBits.shift();
var componentsPath = bowerDirBits.join(path.sep);
grunt.log
.writeln('Going through ' + grunt.log.wordlist(files) + ' to update script refs');
files = files.map(function (filepath) {
return {
path: filepath,
body: grunt.file.read(filepath)
};
});
eachAsync(files, function (file, index, cbInner) {
var content = file.body;
content = googlecdn(content, compJson, {
componentsPath: componentsPath,
cdn: options.cdn
}, function (err, content, replacements) {
if (err) {
return cbInner(err);
}
if (replacements.length > 0) {
replacements.forEach(function (replacement) {
grunt.log.writeln(chalk.green('✔ ') + replacement.from + chalk.gray(' changed to ') + replacement.to);
});
}
grunt.file.write(file.path, content);
cbInner();
});
}, this.async());
});
};