-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGruntfile.js
156 lines (139 loc) · 3.78 KB
/
Gruntfile.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
function qunitVersion() {
var prepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = function() {
return '';
};
try {
return require('qunitjs').version;
}
finally {
Error.prepareStackTrace = prepareStackTrace;
}
}
module.exports = function(grunt) {
// Force use of Unix newlines
grunt.util.linefeed = '\n';
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
qunit_ver: qunitVersion(),
banner: '/*!\n' +
' * HTMLLint v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
' * Copyright 2010-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
' * Licensed under the <%= pkg.license %> license\n' +
' */\n',
browserify: {
src: {
options: {
banner: '<%= banner %>',
require: [
'./src/htmllint.js:html-minifier-lint'
]
},
src: 'src/htmllint.js',
dest: 'dist/htmllint.js'
}
},
eslint: {
grunt: {
src: 'Gruntfile.js'
},
src: {
src: ['cli.js', 'src/**/*.js']
},
tests: {
src: ['tests/*.js', 'test.js']
},
web: {
src: 'assets/master.js'
}
},
qunit: {
htmllint: ['./tests/lint', 'tests/index.html']
},
replace: {
'./index.html': [
/(<h1>.*?<span>).*?(<\/span><\/h1>)/,
'$1(v<%= pkg.version %>)$2'
],
'./tests/index.html': [
/("[^"]+\/qunit-)[0-9\.]+?(\.(?:css|js)")/g,
'$1<%= qunit_ver %>$2'
]
},
uglify: {
options: {
banner: '<%= banner %>',
compress: true,
mangle: true,
preserveComments: false,
report: 'min'
},
minify: {
files: {
'dist/htmllint.min.js': '<%= browserify.src.dest %>'
}
}
}
});
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-eslint');
function report(type, details) {
grunt.log.writeln(type + ' completed in ' + details.runtime + 'ms');
details.failures.forEach(function(details) {
grunt.log.error();
grunt.log.error(details.name + (details.message ? ' [' + details.message + ']' : ''));
grunt.log.error(details.source);
grunt.log.error('Actual:');
grunt.log.error(details.actual);
grunt.log.error('Expected:');
grunt.log.error(details.expected);
});
grunt.log[details.failed ? 'error' : 'ok'](details.passed + ' of ' + details.total + ' passed, ' + details.failed + ' failed');
return details.failed;
}
var phantomjs = require('phantomjs-prebuilt').path;
grunt.registerMultiTask('qunit', function() {
var done = this.async();
var errors = [];
function run(testType, binPath, testPath) {
grunt.util.spawn({
cmd: binPath,
args: ['test.js', testPath]
}, function(error, result) {
if (error) {
grunt.log.error(result.stderr);
grunt.log.error(testType + ' test failed to load');
errors.push(-1);
}
else {
errors.push(report(testType, JSON.parse(result.stdout)));
}
if (errors.length === 2) {
done(!errors[0] && !errors[1]);
}
});
}
run('node', process.argv[0], this.data[0]);
run('web', phantomjs, this.data[1]);
});
grunt.registerMultiTask('replace', function() {
var pattern = this.data[0];
var path = this.target;
var html = grunt.file.read(path);
html = html.replace(pattern, this.data[1]);
grunt.file.write(path, html);
});
grunt.registerTask('dist', [
'replace',
'browserify',
'uglify'
]);
grunt.registerTask('test', [
'eslint',
'dist',
'qunit'
]);
grunt.registerTask('default', 'test');
};