-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
495 lines (449 loc) · 15.1 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* jshint node: true */
module.exports = function(grunt) {
"use strict";
var CONST = {
dest: ['target/dict/','target/build/'],
dest_css: ['target/dict/default/','target/build/default/']
};
// Project configuration.
grunt.initConfig({
// Metadata.
pkg: grunt.file.readJSON('package.json'),
banner: '/*! DICT, a second screen bookmarklet for your browser.\n' +
'* v<%= pkg.version %>\n' +
'* Copyright <%= grunt.template.today("yyyy")%> <%= pkg.author %> \n' +
'* <%= pkg.licenses%>\n' +
'*/\n',
// Task configuration.
clean: {
dist: ['target/dict/','target/build/']
},
// Check javascript error
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
src: {
src: ['dict/**/*.js','lib/jquery.*.js']
}
},
// Check CSS error
csslint: {
strict: {
options: {
// a value of 2 will set it to become an error.
import: 2
},
src: ['dict/_resource/css/*.css']
},
lax: {
options: {
// a value of false ignores the rule,
// Otherwise all rules are considered warnings.
import: false
},
src: ['dict/_resource/css/*.css']
}
},
// Build DICT
concat: {
options: {
stripBanners: false
},
// Bookmark for browser (Only a loader for ui.js)
dict_bookmarklet: {
src: ['dict/_cmn/dict.util.share.js',
'dict/bml/loader.js'],
dest: 'target/build/<%= pkg.name %>_bookmarklet.js'
},
dict_userscript: {
src: ['dict/bml/loader_userscript.js',
"<%= concat.dict_bookmarklet.src %>" ],
dest: 'target/dict/<%= pkg.name %>_userscript.js'
},
// Dict UI (Also is a loader for proxy.js)
dict_ui: {
src: [
// lib
'lib/jquery.js',
'lib/jquery.cookie.js', // TODO:Will be delete for a while.
'lib/json2.js',
'lib/jstorage.js',
'lib/jquery.selection.js',
'lib/jquery.disablescroll.js',
'lib/jquery.hoverIntent.js',
'lib/jquery.plaintext.js',
'lib/tooltip/jquery.tipsy.js',
'lib/jwe/jquery.windows-engine.js',
// share
'dict/_cmn/conf.js',
'dict/_cmn/dict.util.js',
'dict/_cmn/dict.util.share.js',
// main
'dict/bml/dict.ui.start.js',
'dict/bml/dict.ui.js',
'dict/bml/dict.ui.selection.js',
'dict/bml/dict.ui.plaintext.js',
'dict/bml/dict.ui.navi.js',
'dict/bml/dict.ui.end.js',
],
dest: 'target/build/<%= pkg.name %>_ui.js'
},
// Dict iframe (Formatters for each sites)
dict_proxy: {
src: [
// lib
'lib/jquery.js',
'lib/jquery.cookie.js', // TODO:Will be delete for a while.
'lib/json2.js',
'lib/jstorage.js',
'lib/jquery.jsonp.js',
'lib/jquery.highlight.js',
'lib/bootstrap/js/bootstrap.js',
// share
'dict/_cmn/conf.js',
'dict/_cmn/dict.util.js',
// loader
'dict/pxy/dict.proxy.loader.js',
'dict/pxy/loaders/common.js',
'dict/pxy/loaders/common.cache.js',
//'dict/pxy/loaders/dict.load.gae.js', // Use yql
//'dict/pxy/loaders/util.gae_lb.js',
'dict/pxy/loaders/dict.load.google.js',
'dict/pxy/loaders/dict.load.yql.js',
'dict/pxy/loaders/dict.load.xiaod.js',
// formatter
'dict/pxy/dict.proxy.formatter.js',
'dict/pxy/formaters/common.js', // common
'dict/pxy/formaters/dict.formatter.*.js', // core
// Plugins: Priority defines with order bellow.
'dict/pxy/formaters/plugins/dict.formatter.weblio.js',
'dict/pxy/formaters/plugins/dict.formatter.wiki.js',
'dict/pxy/formaters/plugins/dict.formatter.ewords.js',
// Dropins < Plugins(priority): load by alphabet order.
'dict/pxy/formaters/dropins/dict.formatter.*.js',
// Analyze result from search engine
'dict/pxy/dict.proxy.dispatcher.js',
'dict/pxy/dict.proxy.navi.js',
'dict/pxy/dict.proxy.modal-dialog.js',
// main (MUST defined at last)
'dict/pxy/dict.proxy.js',
],
dest: 'target/build/<%= pkg.name %>_proxy.js'
},
// CSS
dict_ui_css: {
src: [
'lib/jwe/jquery.windows-engine.css',
'lib/tooltip/tipsy.css',
'dict/_resource/css/dict.ui.css',
],
dest: 'target/build/<%= pkg.name %>_ui.css'
},
dict_proxy_css: {
src: [
'lib/bootstrap/css/bootstrap.css',
'dict/_resource/css/dict.proxy.css',
],
dest: 'target/build/<%= pkg.name %>_proxy.css'
}
},
// Css minify for release
cssmin: {
compress: {
files: {
'target/dict/<%= pkg.name %>_ui.css':['<%= concat.dict_ui_css.dest %>'],
'target/dict/<%= pkg.name %>_proxy.css':['<%= concat.dict_proxy_css.dest %>'],
},
}
},
// Remove console log for release
removelogging: {
dict_ui: {
src: "<%= concat.dict_ui.dest %>",
dest: "target/build/<%= pkg.name %>_ui_clean.js",
options: {
// Use default
}
},
dict_proxy: {
src: "<%= concat.dict_proxy.dest %>",
dest: "target/build/<%= pkg.name %>_proxy_clean.js",
options: {
// Use default
}
}
},
// Javascript minify for release
uglify: {
options: {
preserveComments: 'some',
},
// Bookmarklet: No sourceMap, No stripBanner!
dict_bookmarklet: {
src: ['<%= concat.dict_bookmarklet.dest %>'],
dest: 'target/dict/<%= pkg.name %>_bookmarklet.js'
},
// Change for old browsers: "//# sourceMappingURL" to "//@ sourceMappingURL"
// Or fix it in "node_modules\grunt-contrib-uglify"
dict_ui: {
options: {
banner: '<%= banner %>',
sourceMap: getMapPath,
sourceMappingURL: '<%= pkg.name %>_ui.map',
sourceMapIncludeSources: true,
sourceMapPrefix: 2,
sourceMapRoot: '/build'
},
files: {'target/dict/<%= pkg.name %>_ui.js': ['<%= concat.dict_ui.dest %>'],}
},
dict_proxy: {
options: {
banner: '<%= banner %>',
sourceMap: getMapPath, // where to output sourcemap
// If not set, it will be NG: sources="target/dict/dict_proxy.map" in map
sourceMappingURL: '<%= pkg.name %>_proxy.map',
sourceMapIncludeSources: true, // Not work???
// 1) + 2) = /build/dict_proxy.map (source path)
sourceMapPrefix: 2, // 1) target/dict/dict_proxy.map --> /dict_proxy.map (remove target/dict)
sourceMapRoot: '/build', // 2) As source & minified file not in same folder. Source in `/build`
},
files: {
'target/dict/<%= pkg.name %>_proxy.js': ['<%= concat.dict_proxy.dest %>'],
},
/* // Same as above
files: [{
expand: true,
cwd : 'target/build/', // Remove 'target/' in map files
src : ['<%= pkg.name %>_proxy.js'],
dest: 'target/dict/',
filter: 'isFile',
}],
*/
},
dict_ui_clean: {
options: {
banner: '<%= banner %>',
},
src: ['<%= removelogging.dict_ui.dest %>'],
dest: 'target/dict/<%= pkg.name %>_ui_clean.js'
},
dict_proxy_clean: {
options: {
banner: '<%= banner %>',
},
src: ['<%= removelogging.dict_proxy.dest %>'],
dest: 'target/dict/<%= pkg.name %>_proxy_clean.js'
},
},
/* Start a default static web server(Need build)
connect: {
// grunt connect:server:keepalive
server: {
options: {
protocol : 'http',
port : 8443,
keepalive: true,
base: './target'
}
},
},*/
// Copy static resources
copy: {
main: {
files: [
//{src: ['lib/jwe/default/*'], dest: 'lib/pkg/default/', filter: 'isFile'}, // includes files in path
//{src: ['path/**'], dest: 'dest/'}, // includes files in path and its subdirs
//{expand: true, cwd: 'path/', src: ['**'], dest: 'dest/'}, // makes all src relative to cwd
//{expand: true, flatten: true, src: ['path/**'], dest: 'dest/', filter: 'isFile'} // flattens results to a single level
// Copy images
// For test
{expand:true, cwd: 'dict/bml/test/' ,src: ['*'], dest: 'target/build/test/'},
{expand:true, src: ['lib/**'], dest: 'target/'},
]
},
html: {
files: [{expand:true, cwd: 'dict/bml/test/' ,src: ['*'], dest: 'target/build/test/'}]
.concat(copyToMulti('dict/pxy/', '*.html', CONST.dest ))
},
css: {
files: [].concat(copyToMulti('dict/_resource/css/', 'dropins/*', CONST.dest ))
},
img: {
files: [].concat(copyToMulti('lib/jwe/default/', '*_mid.gif', CONST.dest_css))
.concat(copyToMulti('dict/_resource/sprite/','*.png', CONST.dest_css))
.concat(copyToMulti('dict/_resource/img/', ['*.*'], CONST.dest_css))
.concat(copyToMulti('dict/_resource/', '*.ico', CONST.dest))
}
},
/*
qunit: {
options: {
inject: 'js/tests/unit/phantom.js'
},
files: ['js/tests/*.html']
},
validation: {
options: {
reset: true,
},
files: {
src: ['_gh_pages/*.html']
}
},
*/
spritepacker: {
default_options: {
options: {
// Path to the template for generating metafile:
template: 'dict/_resource/sprite/dict-sprite.styl.tpl',
// Destination metafile:
destCss: 'dict/_resource/sprite/dict-sprite.styl',
// Base URL for sprite image, used in template
baseUrl: 'default/',
padding: 2,
},
files: {
'dict/_resource/sprite/sprites.png': ['<%= watch.img_sprite.files %>']
}
}
},
stylus: {
compile: {
options: {
paths: ['dict/_resource/sprite/'],
import : ['dict-sprite'],
},
files: {
'dict/_resource/css/dict.proxy.css': ['dict/_resource/css/dict.proxy.styl' ],
'dict/_resource/css/dict.ui.css': ['dict/_resource/css/dict.ui.styl' ],
'lib/jwe/jquery.windows-engine.css': ['lib/jwe/jquery.windows-engine.styl'],
}
}
},
watch: {
options: {
livereload: true
},
script: {
files: ['dict/**/*.js',
'lib/**/*',
'Gruntfile.js'
],
tasks: ['jshint','dist-dev']
},
css_sprite: {
files : [
'dict/_resource/css/*.styl',
'lib/jwe/jquery.windows-engine.styl',
],
tasks: ['stylus','dist-css']
},
img_sprite: {
files : [
'lib/jwe/default/icons/*.*',
'dict/_resource/img/icons/*.*'
],
tasks: ['stylus','dist-css','copy']
},
html: {
files: ['dict/**/*.html'],
tasks: ['copy:html']
},
// test: {
// files: '<%= jshint.test.src %>',
// tasks: ['jshint:test']
// }
},
htmlbuild: {
dist: {
src: 'target/dict/proxy.html',
dest: 'target/dict/proxy-min.html',
options: {
beautify: false,// Keep tab
relative: true,
scripts: {
dict_proxy: 'target/dict/dict_proxy.js',
},
styles: {
dict_proxy: 'target/dict/dict_proxy.css',
},
data: {
// Data to pass to templates
version: '<%= pkg.version %>',
},
}
}
},
}); // End grunt.initConfig
// A shortcut for copy file to multi dirs at once (google keyword: grunt copy multi dest) :
// {expand:true, cwd: 'lib/jwe/default/', src: ['*_mid.gif'], dest: 'target/dict/default/' },
// {expand:true, cwd: 'lib/jwe/default/', src: ['*_mid.gif'], dest: 'target/build/default/'},
function copyToMulti(p_cwd, p_src, p_dests) {
var arr = [];
for (var i in p_dests) {
arr.push({expand:true, cwd:p_cwd, src: p_src, dest: p_dests[i]});
}
return arr;
}
function getMapPath(path) { return path.replace(/.js/,".map")}
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-csslint');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-stylus');
//grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-sprite-packer');
grunt.loadNpmTasks("grunt-remove-logging");
grunt.loadNpmTasks('grunt-html-build');
// TODO
//grunt.loadNpmTasks('grunt-imagine');
//grunt.loadNpmTasks('grunt-contrib-qunit');
//grunt.loadNpmTasks('grunt-html-validation');
// Docs HTML validation task
//grunt.registerTask('validate-docs', ['jekyll', 'validation']);
// Test task.
// var testSubtasks = ['jshint', 'qunit', 'validate-docs'];
var testSubtasks = ['dist'];
grunt.registerTask('test', testSubtasks);
// JS build task.
grunt.registerTask('dist-js', ['jshint','removelogging','uglify']);
grunt.registerTask('dist-js-dev', ['jshint']);
// CSS build task.
grunt.registerTask('dist-css', ['cssmin']); // 'sprite' should manually because it depends imagick
// HTML build task.
grunt.registerTask('dist-html', ['htmlbuild']);
// Sprite
grunt.registerTask('sprite', ['spritepacker','stylus']);
// Full build task.
grunt.registerTask('dist', ['clean', 'copy','concat', 'dist-css', 'dist-js', 'dist-html']);
grunt.registerTask('dist-dev', ['clean', 'copy','concat', 'dist-css', 'dist-js-dev', 'dist-html']);
// Default task.
grunt.registerTask('default', ['dist']);
// task for building customizer
grunt.registerTask('build-min', 'Modify html files using minimized scripts/css.', function () {
var fs = require('fs')
function getFiles(type) {
var files = {}
fs.readdirSync(type)
.filter(function (path) {
return new RegExp('\\.' + type + '$').test(path)
})
.forEach(function (path) {
return files[path] = fs.readFileSync(type + '/' + path, 'utf8')
})
return 'var __' + type + ' = ' + JSON.stringify(files) + '\n'
}
var customize = fs.readFileSync('test.html', 'utf-8')
var files = '<!-- generated -->\n<script id="files">\n' + getFiles('js') + getFiles('less') + '<\/script>\n<!-- /generated -->'
fs.writeFileSync('customize.html', customize.replace(/<!-- generated -->(.|[\n\r])*<!-- \/generated -->/, files))
});
};