forked from aFarkas/webshim
/
grunt.js
124 lines (110 loc) · 3.65 KB
/
grunt.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
/*global module:false*/
module.exports = function(grunt){
// Project configuration.
grunt.initConfig({
pkg: '<json:package.json>',
meta: {
banner: '/*! v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %> */'
},
//concat is changed through webshimscombos
concat: {},
//copy and min are changed through cfgcopymin
copy: {},
min: {},
cssmin: getFiles('src', 'demos/js-webshim/minified', '**/*.css'),
uglify: {
codegen: {ascii_only: true}
}
});
grunt.registerTask('webshimscombos', 'create combos from polyfiller.js.', function() {
var done = this.async();
var combos = {};
grunt.utils.spawn({
cmd: 'phantomjs',
args: [
// PhantomJS options.
'--config={}',
// The main script file.
'build/combobuild.js',
// The temporary file used for communications.
'',
// The QUnit helper file to be injected.
'build/build.html'
]
},
function(err, result, code) {
if(!err && result.indexOf && result.indexOf('done') == -1){
try {
combos = JSON.parse(result);
} catch(er){
grunt.warn('parse error');
}
grunt.config('concat', combos);
done(code);
return;
}
// Something went horribly wrong.
grunt.verbose.or.writeln();
grunt.log.write('Running PhantomJS...').error();
if (code === 127) {
grunt.log.errorlns(
'In order for this task to work properly, PhantomJS must be ' +
'installed and in the system PATH (if you can run "phantomjs" at' +
' the command line, this task should work). Unfortunately, ' +
'PhantomJS cannot be installed automatically via npm or grunt. ' +
'See the grunt FAQ for PhantomJS installation instructions: ' +
'https://github.com/gruntjs/grunt/blob/master/docs/faq.md'
);
grunt.warn('PhantomJS not found.');
} else {
result.split('\n').forEach(grunt.log.error, grunt.log);
grunt.warn('PhantomJS exited unexpectedly with exit code ' + code + '.');
}
done(code);
});
});
grunt.registerTask('cfgcopymin', 'config min and copy tasks.', function() {
var files = getFiles('src', false, '**', 'demos/js-webshim/dev', '*.js');
var path = require('path');
var copyTask = {};
var minTask = {};
var minPath, file, found;
for(var i in files){
file = files[i];
if(grunt.file.isMatch('*.*', file)){
minPath = path.join('demos/js-webshim/minified', i);
if(grunt.file.isMatch('*.js', file)){
minTask[minPath] = file;
found = true;
}
copyTask[minPath] = file;
copyTask[path.join('demos/js-webshim/dev', i)] = file;
}
}
if(!found){
minTask[path.join('demos/js-webshim/minified', 'polyfiller.js')] = path.join('src', 'polyfiller.js');
}
grunt.config('min', minTask);
grunt.config('copy', copyTask);
});
// Default task.
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-css');
grunt.registerTask('default', 'webshimscombos concat cfgcopymin copy cssmin min');
function getFiles(srcdir, destdir, wildcard, compareDir, compareMatch) {
var path = require('path');
var fs = require('fs');
// In Nodejs 0.8.0, existsSync moved from path -> fs.
var existsSync = fs.existsSync || path.existsSync;
var files = {};
grunt.file.expand({cwd: srcdir}, wildcard).forEach(function(relpath) {
var src = path.join(srcdir, relpath);
if(!compareDir || !compareMatch || !grunt.file.isMatch(compareMatch, src) || (!existsSync(path.join(compareDir, relpath)) || grunt.file.read(src) != grunt.file.read(path.join(compareDir, relpath)))){
files[destdir === false ? relpath : path.join(destdir, relpath)] = src;
}
});
return files;
}
};