Repository navigation
Expand file tree
/
Copy pathGruntfile.js
More file actions
270 lines (246 loc) · 10.7 KB
/
Copy pathGruntfile.js
File metadata and controls
270 lines (246 loc) · 10.7 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
module.exports = function(grunt) {
var srcCore = [
'src/utils.js',
'src/input.js',
'src/input/*.js',
'src/touchaction.js',
'src/recognizer.js',
'src/recognizers/*.js',
'src/hammer.js',
'src/manager.js',
'src/expose.js'
];
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
usebanner: {
taskName: {
options: {
position: 'top',
banner:
'/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' +
' * <%= pkg.homepage %>\n' +
' *\n' +
' * Copyright (c) <%= grunt.template.today("yyyy") %> Hammer.js contributors;\n' +
' * Licensed under the <%= pkg.license %> license */',
linebreak: true
},
files: {
src: ['./hammer.js', './hammer.min.js']
}
}
},
concat: {
build: {
src: ['src/hammer.prefix.js'].concat(srcCore, ['src/hammer.suffix.js']),
dest: 'hammer.js'
}
},
uglify: {
min: {
options: {
report: 'gzip',
compress: {
arrows: false
},
sourceMap: true,
sourceMapName: 'hammer.min.js.map'
},
files: {
'hammer.min.js': ['hammer.js']
}
}
},
connect: {
server: {
options: {
hostname: '127.0.0.1',
port: 8000
}
}
}
});
grunt.registerTask('string-replace', 'Replace {{PKG_VERSION}} in hammer.js', function() {
var pkg = grunt.config('pkg');
var content = grunt.file.read('hammer.js');
grunt.file.write('hammer.js', content.replace(/\{\{PKG_VERSION\}\}/g, pkg.version));
});
grunt.registerTask('build-test', 'Build tests/build.js exposing internal symbols on window.$H', function() {
var files = grunt.file.expand(srcCore);
var body = files.map(function(filepath) {
return grunt.file.read(filepath);
}).join('\n');
var names = {};
var re = /^(?:var|function)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm;
var match;
while ((match = re.exec(body)) !== null) {
names[match[1]] = true;
}
var exportsCode = Object.keys(names).map(function(name) {
return 'if (typeof ' + name + ' !== \'undefined\') { $H.' + name + ' = ' + name + '; }';
}).join('\n');
var wrapped =
'(function(window, document, exportName, undefined) {\n' +
'\'use strict\';\n' +
'var $H = window.$H = {};\n' +
body + '\n' +
exportsCode + '\n' +
'})(window, document, \'Hammer\');\n';
grunt.file.write('tests/build.js', wrapped);
});
grunt.registerTask('qunit', 'Run QUnit unit tests in headless Chrome via Puppeteer', function() {
var done = this.async();
var puppeteer = require('puppeteer');
var url = 'http://127.0.0.1:8000/tests/unit/index.html';
puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
}).then(function(browser) {
return browser.newPage().then(function(page) {
var failures = [];
return page.exposeFunction('__gruntQUnitLog', function(details) {
if (!details.result) {
failures.push(details);
}
}).then(function() {
return page.exposeFunction('__gruntQUnitDone', function(details) {
browser.close().then(function() {
if (failures.length) {
failures.forEach(function(f) {
grunt.log.error(
'[' + f.module + '] ' + f.name + ': ' +
(f.message || 'failed') +
' (expected: ' + f.expected +
', actual: ' + f.actual + ')'
);
});
}
if (details.failed > 0 || details.total === 0) {
grunt.log.error(
'QUnit: ' + details.passed + '/' + details.total +
' assertions passed, ' + details.failed + ' failed (' + details.runtime + 'ms)'
);
done(false);
} else {
grunt.log.ok(
'QUnit: ' + details.passed + '/' + details.total +
' assertions passed, 0 failed (' + details.runtime + 'ms)'
);
done();
}
});
});
}).then(function() {
return page.evaluateOnNewDocument(function() {
// Match legacy PhantomJS touch-only test environment used by Hammer Simulator
delete window.PointerEvent;
Object.defineProperty(window, 'QUnit', {
configurable: true,
set: function(val) {
Object.defineProperty(window, 'QUnit', {
value: val,
writable: true,
enumerable: true,
configurable: true
});
val.log(function(details) {
if (!details.result) {
window.__gruntQUnitLog({
result: false,
module: details.module,
name: details.name,
message: details.message,
expected: String(details.expected),
actual: String(details.actual)
});
}
});
val.done(function(details) {
window.__gruntQUnitDone(details);
});
}
});
});
}).then(function() {
page.on('pageerror', function(err) {
grunt.log.error('Page error: ' + err.message);
});
return page.goto(url, {waitUntil: 'load', timeout: 30000});
});
}).catch(function(err) {
return browser.close().then(function() {
throw err;
});
});
}).catch(function(err) {
grunt.log.error(err.stack || err.message);
done(false);
});
});
grunt.registerTask('lint', 'Lint source and build files with ESLint', function() {
var done = this.async();
var ESLint = require('eslint').ESLint;
var eslint = new ESLint();
var targets = ['src', 'Gruntfile.js', 'eslint.config.js'];
if (grunt.file.exists('hammer.min.js')) {
targets.unshift('hammer.min.js');
}
if (grunt.file.exists('hammer.js')) {
targets.unshift('hammer.js');
}
eslint.lintFiles(targets).then(function(results) {
return eslint.loadFormatter('stylish').then(function(formatter) {
var output = formatter.format(results);
if (output) {
grunt.log.writeln(output);
}
var errorCount = results.reduce(function(sum, r) {
return sum + r.errorCount;
}, 0);
if (errorCount > 0) {
grunt.log.error('ESLint found ' + errorCount + ' error(s).');
done(false);
} else {
grunt.log.ok('ESLint passed (' + results.length + ' files checked).');
done();
}
});
}).catch(function(err) {
grunt.log.error(err.stack || err.message);
done(false);
});
});
grunt.registerTask('test-ssr', 'Verify hammer.js loads cleanly in Node.js without window or document', function() {
var vm = require('vm');
var files = grunt.file.expand(['src/hammer.prefix.js'].concat(srcCore, ['src/hammer.suffix.js']));
var code = files.map(function(filepath) {
return grunt.file.read(filepath);
}).join('\n');
// 1. CommonJS / Node.js SSR without window, document, navigator, or self
var cjsModule = {exports: {}};
vm.runInNewContext(code, {
module: cjsModule,
exports: cjsModule.exports,
setTimeout: setTimeout,
clearTimeout: clearTimeout
});
if (typeof cjsModule.exports !== 'function' || typeof cjsModule.exports.Manager !== 'function') {
grunt.fail.fatal('SSR CommonJS load failed to export Hammer');
}
// 2. Bare runtime without window, document, navigator, or module
vm.runInNewContext(code, {
setTimeout: setTimeout,
clearTimeout: clearTimeout
});
grunt.log.ok('SSR load passed (CommonJS and bare runtime without window/document).');
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-banner');
grunt.registerTask('default', ['connect:server:keepalive']);
grunt.registerTask('dist', ['concat', 'string-replace', 'uglify:min', 'usebanner']);
grunt.registerTask('build', ['dist', 'lint', 'test-ssr', 'build-test', 'connect:server', 'qunit']);
grunt.registerTask('test', ['concat', 'string-replace', 'lint', 'test-ssr', 'build-test', 'connect:server', 'qunit']);
grunt.registerTask('test-travis', ['build']);
};