This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
forked from mistic100/jQuery-QueryBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
299 lines (262 loc) · 9.02 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
module.exports = function(grunt) {
// list available modules and languages
var modules = {},
langs = {},
js_files_to_load = [],
css_files_to_load = [],
files_for_standalone = [
'bower_components/microevent-mistic100/microevent.js',
'bower_components/jquery-extendext/jQuery.extendext.js',
'dist/query-builder.js'
],
loaded_modules = [],
loaded_lang = '';
grunt.file.expand('src/plugins/*/plugin.js')
.forEach(function(f) {
modules[f.split('/')[2]] = f;
});
grunt.file.expandMapping('src/i18n/*.js', '', {
flatten: true, ext: ''
})
.forEach(function(f) {
langs[f.dest] = f.src[0];
});
// parse 'modules' parameter
var arg_modules = grunt.option('modules');
if (typeof arg_modules === 'string') {
arg_modules.split(',').forEach(function(m) {
m = m.trim();
if (modules[m]) {
js_files_to_load.push(modules[m]);
loaded_modules.push(m);
}
else if (m !== 'none') {
grunt.fail.warn('Module '+ m +' unknown');
}
});
}
else if (arg_modules === undefined) {
for (var m in modules) {
js_files_to_load.push(modules[m]);
loaded_modules.push(m);
}
}
// parse 'lang' parameter
var arg_lang = grunt.option('lang');
if (typeof arg_lang === 'string') {
if (langs[arg_lang]) {
if (arg_lang != 'en') {
js_files_to_load.push(langs[arg_lang]);
}
loaded_lang = arg_lang;
}
else {
grunt.fail.warn('Lang '+ arg_lang +' unknown');
}
}
// get css files for loaded mofules
js_files_to_load.forEach(function(js_file) {
var css_file = js_file.replace(/js$/, 'css');
if (grunt.file.exists(css_file)) {
css_files_to_load.push(css_file);
}
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
banner:
'/*!\n'+
' * jQuery QueryBuilder <%= pkg.version %>\n'+
' * Copyright 2014-<%= grunt.template.today("yyyy") %> Damien "Mistic" Sorel (http://www.strangeplanet.fr)\n'+
' * Licensed under MIT (http://opensource.org/licenses/MIT)\n'+
' */',
// copy i18n
copy: {
i18n: {
files: [{
expand: true,
flatten: true,
src: ['src/i18n/*.js'],
dest: 'dist/i18n'
}]
}
},
// copy src
concat: {
options: {
separator: '\n',
stripBanners: {
block: true
}
},
css: {
src: ['src/query-builder.css'].concat(css_files_to_load),
dest: 'dist/query-builder.css',
options: {
banner: '<%= banner %>\n',
// remove sections comments
process: function(src) {
return src.replace(/\/\* [^\r\n]* \*\/\r?\n/g, '');
}
}
},
js: {
src: ['src/query-builder.js'].concat(js_files_to_load),
dest: 'dist/query-builder.js',
options: {
// remove wrappers, use strict, jshint directives, sections comments
process: function(src) {
return src
.replace(/\(function\(\$\){\r?\n/g, '')
.replace(/\r?\n}\(jQuery\)\);/g, '')
.replace(/[ \t]*"use strict";\r?\n/g, '')
.replace(/\/\*jshint [a-z:]+ \*\/\r?\n/g, '')
.replace(/\r?\n( *\/\/ [^\r\n]*\r?\n)+ *\/\/ =+/g, '');
}
}
}
},
// add AMD wrapper
wrap: {
dist: {
src: ['dist/query-builder.js'],
dest: '',
options: {
separator: '',
wrapper: function() {
var wrapper = grunt.file.read('src/.wrapper.js').split('@@js\n');
if (loaded_modules.length) {
wrapper[0] = '// Modules: ' + loaded_modules.join(', ') + '\n' + wrapper[0];
}
if (loaded_lang.length) {
wrapper[0] = '// Language: ' + loaded_lang + '\n' + wrapper[0];
}
wrapper[0] = grunt.template.process('<%= banner %>\n\n') + wrapper[0];
return wrapper;
}
}
}
},
// compress js
uglify: {
options: {
banner: '<%= banner %>\n',
mangle: { except: ['$'] }
},
dist: {
files: {
'dist/query-builder.min.js': [
'dist/query-builder.js'
],
'dist/query-builder.standalone.min.js': [
'dist/query-builder.standalone.js'
]
}
}
},
// compress css
cssmin: {
options: {
banner: '<%= banner %>',
keepSpecialComments: 0
},
dist: {
files: {
'dist/query-builder.min.css': [
'dist/query-builder.css'
]
}
}
},
// jshint tests
jshint: {
lib: {
files: {
src: ['src/query-builder.js'].concat(js_files_to_load)
}
}
},
// qunit test suite
qunit: {
all: ['tests/*.html']
}
});
// from https://github.com/brianreavis/selectize.js/blob/master/Gruntfile.js
grunt.registerTask('build_standalone', '', function() {
var files = [],
modules = [];
// get sources with named definitions
for (var i=0, n=files_for_standalone.length; i<n; i++) {
var path = files_for_standalone[i],
name = path.match(/([^\/]+?).js$/)[1],
source = grunt.file.read(path);
source = source.replace(/define\((.*?)factory\);/, 'define(\'' + name + '\', $1factory);');
modules.push(source);
}
// write output
path = 'dist/query-builder.standalone.js';
grunt.file.write(path, modules.join('\n\n'));
grunt.log.writeln('Built "' + path + '".');
});
// list the triggers and changes in core code
grunt.registerTask('describe_triggers', '', function() {
var triggers = {};
core = grunt.file.read('src/query-builder.js').split('\n').forEach(function(line, i) {
var matches = /(?:this|that)\.(trigger|change)\('(\w+)'[^)]*\);/.exec(line);
if (matches !== null) {
triggers[matches[2]] = triggers[matches[2]] || {
name: matches[2],
type: matches[1],
usages: []
};
triggers[matches[2]].usages.push([i, matches[0].slice(0,-1)]);
}
});
grunt.log.writeln('\nTriggers in QueryBuilder:\n');
for (var t in triggers) {
grunt.log.write((triggers[t].name)['cyan']);
grunt.log.writeln(' (' + triggers[t].type + ')');
triggers[t].usages.forEach(function(line) {
grunt.log.write('+-- ');
grunt.log.write((':' + line[0])['red']);
grunt.log.writeln(' ' + line[1]);
});
grunt.log.write('\n');
}
});
// display avilable modules
grunt.registerTask('list_modules', '', function() {
grunt.log.writeln('\nAvailable QueryBuilder plugins:\n');
for (var m in modules) {
grunt.log.write(m['cyan']);
if (grunt.file.exists(modules[m].replace(/js$/, 'css'))) {
grunt.log.write(' + CSS');
}
grunt.log.write('\n');
}
grunt.log.writeln('\nAvailable QueryBuilder languages:\n');
for (var l in langs) {
if (l !== 'en') {
grunt.log.writeln(l['cyan']);
}
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-wrap');
grunt.registerTask('default', [
'copy',
'concat',
'wrap',
'build_standalone',
'uglify',
'cssmin'
]);
grunt.registerTask('test', [
'qunit',
'jshint'
]);
};