1 Star 1 Fork 1

舞我王/jQuery-QueryBuilder

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Gruntfile.js 19.07 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
var deepmerge = require('deepmerge');
module.exports = function(grunt) {
require('time-grunt')(grunt);
require('jit-grunt')(grunt, {
scsslint: 'grunt-scss-lint'
});
grunt.util.linefeed = '\n';
function removeJshint(src) {
return src
.replace(/\/\*jshint [a-z:]+ \*\/\r?\n\r?\n?/g, '')
.replace(/\/\*jshint -[EWI]{1}[0-9]{3} \*\/\r?\n\r?\n?/g, '');
}
function process_lang(file, src, wrapper) {
var lang = file.split(/[\/\.]/)[2];
var content = JSON.parse(src);
wrapper = wrapper || ['', ''];
grunt.config.set('lang_locale', content.__locale || lang);
grunt.config.set('lang_author', content.__author);
var header = grunt.template.process('<%= langBanner %>');
loaded_plugins.forEach(function(p) {
var plugin_file = 'src/plugins/' + p + '/i18n/' + lang + '.json';
if (grunt.file.exists(plugin_file)) {
content = deepmerge(content, grunt.file.readJSON(plugin_file));
}
});
return header
+ '\n\n'
+ wrapper[0]
+ 'QueryBuilder.regional[\'' + lang + '\'] = '
+ JSON.stringify(content, null, 2)
+ ';\n\n'
+ 'QueryBuilder.defaults({ lang_code: \'' + lang + '\' });'
+ wrapper[1];
}
var all_plugins = {},
all_langs = {},
loaded_plugins = [],
loaded_langs = [],
js_core_files = [
'src/main.js',
'src/defaults.js',
'src/core.js',
'src/public.js',
'src/data.js',
'src/template.js',
'src/model.js',
'src/utils.js',
'src/jquery.js'
],
js_files_to_load = js_core_files.slice(),
all_js_files = js_core_files.slice(),
js_files_for_standalone = [
'bower_components/jquery-extendext/jQuery.extendext.js',
'bower_components/doT/doT.js',
'dist/js/query-builder.js'
];
(function() {
// list available plugins and languages
grunt.file.expand('src/plugins/**/plugin.js')
.forEach(function(f) {
var n = f.split('/')[2];
all_plugins[n] = f;
});
grunt.file.expand('src/i18n/*.json')
.forEach(function(f) {
var n = f.split(/[\/\.]/)[2];
all_langs[n] = f;
});
// fill all js files
for (var p in all_plugins) {
all_js_files.push(all_plugins[p]);
}
// parse 'plugins' parameter
var arg_plugins = grunt.option('plugins');
if (typeof arg_plugins === 'string') {
arg_plugins.replace(/ /g, '').split(',').forEach(function(p) {
if (all_plugins[p]) {
js_files_to_load.push(all_plugins[p]);
loaded_plugins.push(p);
}
else {
grunt.fail.warn('Plugin ' + p + ' unknown');
}
});
}
else if (arg_plugins === undefined) {
for (var p in all_plugins) {
js_files_to_load.push(all_plugins[p]);
loaded_plugins.push(p);
}
}
// default language
js_files_to_load.push('.temp/i18n/en.js');
loaded_langs.push('en');
// parse 'lang' parameter
var arg_langs = grunt.option('languages');
if (typeof arg_langs === 'string') {
arg_langs.replace(/ /g, '').split(',').forEach(function(l) {
if (all_langs[l]) {
if (l !== 'en') {
js_files_to_load.push(all_langs[l].replace(/^src/, '.temp').replace(/json$/, 'js'));
loaded_langs.push(l);
}
}
else {
grunt.fail.warn('Language ' + l + ' unknown');
}
});
}
}());
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' +
' */',
langBanner:
'/*!\n' +
' * jQuery QueryBuilder <%= pkg.version %>\n' +
' * Locale: <%= lang_locale %>\n' +
'<% if (lang_author) { %> * Author: <%= lang_author %>\n<% } %>' +
' * Licensed under MIT (http://opensource.org/licenses/MIT)\n' +
' */',
// serve folder content
connect: {
dev: {
options: {
host: '0.0.0.0',
port: 9000,
livereload: true
}
}
},
// watchers
watch: {
options: {
livereload: true
},
js: {
files: ['src/*.js', 'src/plugins/**/plugin.js'],
tasks: ['build_js']
},
css: {
files: ['src/scss/*.scss', 'src/plugins/**/plugin.scss'],
tasks: ['build_css']
},
lang: {
files: ['src/i18n/*.json', 'src/plugins/**/i18n/*.json'],
tasks: ['build_lang']
},
example: {
files: ['examples/**'],
tasks: []
}
},
// open example
open: {
dev: {
path: 'http://localhost:<%= connect.dev.options.port%>/examples/index.html'
}
},
// copy SASS files
copy: {
sass_core: {
files: [{
expand: true,
flatten: true,
src: ['src/scss/*.scss'],
dest: 'dist/scss'
}]
},
sass_plugins: {
files: loaded_plugins.map(function(name) {
return {
src: 'src/plugins/' + name + '/plugin.scss',
dest: 'dist/scss/plugins/' + name + '.scss'
};
})
}
},
concat: {
// concat all JS
js: {
src: js_files_to_load,
dest: 'dist/js/query-builder.js',
options: {
stripBanners: false,
separator: '\n\n',
process: function(src) {
return removeJshint(src).replace(/\r\n/g, '\n');
}
}
},
// create standalone version
js_standalone: {
src: js_files_for_standalone,
dest: 'dist/js/query-builder.standalone.js',
options: {
stripBanners: false,
separator: '\n\n',
process: function(src, file) {
var name = file.match(/([^\/]+?).js$/)[1];
return removeJshint(src)
.replace(/\r\n/g, '\n')
.replace(/define\((.*?)\);/, 'define(\'' + name + '\', $1);');
}
}
},
// compile language files with AMD wrapper
lang: {
files: Object.keys(all_langs).map(function(name) {
return {
src: 'src/i18n/' + name + '.json',
dest: 'dist/i18n/query-builder.' + name + '.js'
};
}),
options: {
process: function(src, file) {
var wrapper = grunt.file.read('src/i18n/.wrapper.js').replace(/\r\n/g, '\n').split(/@@js\n/);
return process_lang(file, src, wrapper);
}
}
},
// compile language files without wrapper
lang_temp: {
files: Object.keys(all_langs).map(function(name) {
return {
src: 'src/i18n/' + name + '.json',
dest: '.temp/i18n/' + name + '.js'
};
}),
options: {
process: function(src, file) {
return process_lang(file, src);
}
}
},
// add banner to CSS files
css: {
options: {
banner: '<%= banner %>\n\n',
},
files: [{
expand: true,
src: ['dist/css/*.css', 'dist/scss/*.scss'],
dest: ''
}]
}
},
wrap: {
// add AMD wrapper and banner
js: {
src: ['dist/js/query-builder.js'],
dest: '',
options: {
separator: '',
wrapper: function() {
var wrapper = grunt.file.read('src/.wrapper.js').replace(/\r\n/g, '\n').split(/@@js\n/);
if (loaded_plugins.length) {
wrapper[0] = '// Plugins: ' + loaded_plugins.join(', ') + '\n' + wrapper[0];
}
if (loaded_langs.length) {
wrapper[0] = '// Languages: ' + loaded_langs.join(', ') + '\n' + wrapper[0];
}
wrapper[0] = grunt.template.process('<%= banner %>\n\n') + wrapper[0];
return wrapper;
}
}
},
// add plugins SASS imports
sass: {
src: ['dist/scss/default.scss'],
dest: '',
options: {
separator: '',
wrapper: function() {
return ['', loaded_plugins.reduce(function(wrapper, name) {
if (grunt.file.exists('dist/scss/plugins/' + name + '.scss')) {
wrapper += '\n@import \'plugins/' + name + '\';';
}
return wrapper;
}, '\n')];
}
}
}
},
// parse scss
sass: {
options: {
sourcemap: 'none',
style: 'expanded'
},
dist: {
files: [{
expand: true,
flatten: true,
src: ['dist/scss/*.scss'],
dest: 'dist/css',
ext: '.css',
rename: function(dest, src) {
return dest + '/query-builder.' + src;
}
}]
}
},
// compress js
uglify: {
options: {
banner: '<%= banner %>\n\n',
mangle: { except: ['$'] }
},
dist: {
files: [{
expand: true,
flatten: true,
src: ['dist/js/*.js', '!dist/js/*.min.js'],
dest: 'dist/js',
ext: '.min.js',
extDot: 'last'
}]
}
},
// compress css
cssmin: {
dist: {
files: [{
expand: true,
flatten: true,
src: ['dist/css/*.css', '!dist/css/*.min.css'],
dest: 'dist/css',
ext: '.min.css',
extDot: 'last'
}]
}
},
// clean build dir
clean: {
temp: ['.temp']
},
// jshint tests
jshint: {
lib: {
options: {
jshintrc: '.jshintrc'
},
src: js_files_to_load
}
},
// jscs tests
jscs: {
lib: {
options: {
config: '.jscsrc'
},
src: js_files_to_load
}
},
// scss tests
scsslint: {
lib: {
options: {
colorizeOutput: true,
config: '.scss-lint.yml'
},
src: ['src/**/*.scss']
}
},
// inject all source files and test modules in the test file
'string-replace': {
test: {
src: 'tests/index.html',
dest: 'tests/index.html',
options: {
replacements: [{
pattern: /(<!-- qunit:imports -->)(?:[\s\S]*)(<!-- \/qunit:imports -->)/m,
replacement: function(match, m1, m2) {
var scripts = '\n';
js_core_files.forEach(function(file) {
scripts += '<script src="../' + file + '" data-cover></script>\n';
});
scripts += '\n';
for (var p in all_plugins) {
scripts += '<script src="../' + all_plugins[p] + '" data-cover></script>\n';
}
return m1 + scripts + m2;
}
}, {
pattern: /(<!-- qunit:modules -->)(?:[\s\S]*)(<!-- \/qunit:modules -->)/m,
replacement: function(match, m1, m2) {
var scripts = '\n';
grunt.file.expand('tests/*.module.js').forEach(function(file) {
scripts += '<script src="../' + file + '"></script>\n';
});
return m1 + scripts + m2;
}
}]
}
}
},
// qunit test suite
qunit: {
all: {
options: {
urls: ['tests/index.html?coverage=true'],
noGlobals: true
}
}
},
// save LCOV files
qunit_blanket_lcov: {
all: {
files: [{
expand: true,
src: ['src/*.js', 'src/plugins/**/plugin.js']
}],
options: {
dest: '.coverage-results/all.lcov'
}
}
},
// coveralls data
coveralls: {
options: {
force: true
},
all: {
src: '.coverage-results/all.lcov',
}
}
});
// list the triggers and changes
grunt.registerTask('describe_triggers', 'List QueryBuilder triggers.', function() {
var triggers = {};
var total = 0;
for (var f in all_js_files) {
grunt.file.read(all_js_files[f]).split(/\r?\n/).forEach(function(line, i) {
var matches = /(e = )?(?:this|that)\.(trigger|change)\('(\w+)'([^)]*)\);/.exec(line);
if (matches !== null) {
triggers[matches[3]] = {
name: matches[3],
type: matches[2],
file: all_js_files[f],
line: i,
args: matches[4].slice(2),
prevent: !!matches[1]
};
total++;
}
});
}
grunt.log.write('\n');
for (var t in triggers) {
grunt.log.write(t['cyan'] + ' ' + triggers[t].type['magenta']);
if (triggers[t].prevent) grunt.log.write(' (*)'['yellow']);
grunt.log.write('\n');
grunt.log.writeln(' ' + (triggers[t].file + ':' + triggers[t].line)['red'] + ' ' + triggers[t].args);
grunt.log.write('\n');
}
grunt.log.writeln((total + ' Triggers in QueryBuilder.')['cyan']['bold']);
});
// list all possible thrown errors
grunt.registerTask('describe_errors', 'List QueryBuilder errors.', function() {
var errors = {};
var total = 0;
for (var f in all_js_files) {
grunt.file.read(all_js_files[f]).split(/\r?\n/).forEach(function(line, i) {
var matches = /Utils\.error\('(\w+)', '([^)]+)'([^)]*)\);/.exec(line);
if (matches !== null) {
(errors[matches[1]] = errors[matches[1]] || []).push({
type: matches[1],
message: matches[2],
file: all_js_files[f],
line: i,
args: matches[3].slice(2).split(', ')
});
total++;
}
});
}
grunt.log.write('\n');
for (var e in errors) {
grunt.log.writeln((e + 'Error')['cyan']);
errors[e].forEach(function(error) {
var message = error.message.replace(/{([0-9]+)}/g, function(m, i) {
return error.args[parseInt(i)]['yellow'];
});
grunt.log.writeln(' ' + (error.file + ':' + error.line)['red']);
grunt.log.writeln(' ' + message);
});
grunt.log.write('\n');
}
grunt.log.writeln((total + ' Errors in QueryBuilder.')['cyan']['bold']);
});
// display available modules
grunt.registerTask('list_modules', 'List QueryBuilder plugins and languages.', function() {
grunt.log.writeln('\nAvailable QueryBuilder plugins:\n');
for (var p in all_plugins) {
grunt.log.write(p['cyan']);
if (grunt.file.exists(all_plugins[p].replace(/js$/, 'scss'))) {
grunt.log.write(' + CSS');
}
grunt.log.write('\n');
}
grunt.log.writeln('\nAvailable QueryBuilder languages:\n');
for (var l in all_langs) {
if (l !== 'en') {
grunt.log.writeln(l['cyan']);
}
}
});
grunt.registerTask('build_js', [
'concat:lang_temp',
'concat:js',
'wrap:js',
'concat:js_standalone',
'uglify',
'clean:temp'
]);
grunt.registerTask('build_css', [
'copy:sass_core',
'copy:sass_plugins',
'wrap:sass',
'sass',
'cssmin',
'concat:css'
]);
grunt.registerTask('build_lang', [
'concat:lang'
]);
grunt.registerTask('default', [
'build_lang',
'build_js',
'build_css'
]);
grunt.registerTask('test', [
'jshint',
'jscs',
'scsslint',
'default',
'string-replace:test',
'qunit_blanket_lcov',
'qunit'
]);
grunt.registerTask('serve', [
'default',
'open',
'connect',
'watch'
]);
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/ahwangbing/jQuery-QueryBuilder.git
git@gitee.com:ahwangbing/jQuery-QueryBuilder.git
ahwangbing
jQuery-QueryBuilder
jQuery-QueryBuilder
master

搜索帮助