forked from aFarkas/webshim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
350 lines (322 loc) · 9.27 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
/*global module:false*/
module.exports = function(grunt){
var DISTPATH = "js-webshim";
var MINPATH = DISTPATH+"/minified";
var DEVPATH = DISTPATH+"/dev";
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
//concat is changed through webshimscombos
concat: {
options: {
separator: ';'
},
demo: {
src: ['demos/demo-js/src/prism.js', 'demos/demo-js/src/behavior.js'],
dest: 'demos/demo-js/demo.js'
},
cfgs: {
src: ['demos/demos/cfgs/assets/jquery.deserialize.js', 'demos/demos/cfgs/assets/configurator.js'],
dest: 'demos/demos/cfgs/assets/cfg.js'
}
},
//copy and uglify are changed through cfgcopymin
copy: {
main: {},
archive: {
files: [{src: 'webshims-stable.zip', dest: 'webshims-<%= pkg.version %>.zip'}]
}
},
compress: {
main: {
options: {
archive: 'webshims-stable.zip'
},
files: [
{expand: true, cwd: 'demos/', src: ['**'], dest: 'demos/', filter: function(filepath){
if(filepath.indexOf('demos/js-webshim') == 0 || filepath.indexOf('.tmp') != -1){
return false;
}
return true;
}},
{expand: true, cwd: 'js-webshim/', src: ['**'], dest: 'js-webshim/'}
]
}
},
clean: {
build: {
src: ['js-webshim/', 'src/shims/combos', 'webshims-*.zip']
}
},
cssmin: getFiles('src', MINPATH, '**/*.css'),
csslint: {
css: {
options: {
'adjoining-classes': false,
'outline-none': false,
'box-model': false,
'compatible-vendor-prefixes': false,
'box-sizing': false,
'overqualified-elements': false,
ids: false,
'qualified-headings': false,
'unique-headings': false,
important: false
},
src: ['src/**/*.css']
}
},
jshint: {
strict: {
options: {
eqnull: true
},
src: ['src/*.js', 'src/shims/*.js', 'src/jme/*.js', 'src/i18n/*.js', '!src/shims/promise.js', '!src/shims/sizzle.js', '!src/shims/picture.js']
}
},
sass: {
options: {
style: 'expanded',
sourcemap: 'none'
},
dist: {
files:[{
expand : true,
cwd : 'src/shims/styles/scss',
src : ['*.scss'],
dest : 'src/shims/styles/',
ext : '.css'
}]
},
themes: {
files:[{
expand : true,
cwd : 'demos/demos/themes/',
src : ['**/*.scss'],
dest : 'demos/demos/themes/',
ext : '.css'
}]
},
jme: {
files:[{
expand : true,
cwd : 'src/shims/jme',
src : ['*.scss'],
dest : 'src/shims/jme',
ext : '.css'
}]
}
},
/*not used for build but might be really helpfull for production*/
/* should we create a npm grunt task???*/
optimizePolyfiller: {
options: {
src: 'js-webshim/dev/', //required
features: 'forms',
dest: 'polyfiller-custom.js',
//should existing uglify be extended to uglify custom polyfiller? default: false
uglify: true,
//should initially loaded files inlined into polyfiller? default: false
inlineInitFiles: true,
/*
//only in case inlineInitFiles is true
*/
//which lang or langs are used on page?
lang: 'fr it',
//forms feature option default: false
customMessages: false,
//forms-ext feature option default: false
replaceUI: false,
//is swfobject not used on site default: true
includeSwfmini: true
}
},
bytesize: {
all: {
src: [
MINPATH+'/polyfiller.js'
]
}
},
uglify: {
options: {
beautify: {
ascii_only : true
},
preserveComments: 'some',
compress: {
global_defs: {
"WSDEBUG": false
},
dead_code: true
}
},
demo: {
src: 'demos/demo-js/demo.js',
dest: 'demos/demo-js/demo.js'
},
cfgs: {
src: 'demos/demos/cfgs/assets/cfg.js',
dest: 'demos/demos/cfgs/assets/cfg.js'
}
},
watch: {
sass: {
files: ['src/**/*.scss'],
tasks: ['sass']
},
themes: {
files: ['demos/demos/themes/**/*.scss'],
tasks: ['sass:themes']
},
css: {
files: ['src/**/*.css'],
tasks: ['cfgcopymin', 'copy']
},
js: {
files: ['src/**/*.js'],
tasks: ['webshimscombos', 'concat', 'cfgcopymin', 'copy']
},
demos: {
files: ['demos/demo-js/src/**/*.js'],
tasks: ['concat:demo']
},
cfgs: {
files: ['demos/demos/cfgs/assets/**/*.js'],
tasks: ['concat:cfgs']
}
}
});
grunt.registerTask('versiontest', 'test the current version of build', function(){
var version = grunt.file.readJSON(DEVPATH+'/shims/combos/comboinfo.json').version;
var packageinfos = ['package.json', 'bower.json', 'webshims.jquery.json'];
if(!/^\d+\.\d+\.\d+[\-RC\d]*$/.test(version)){
grunt.warn('version "'+ version +'" is not valid');
}
for(var i = 0; i < packageinfos.length; i++){
if(grunt.file.readJSON(packageinfos[i]).version != version){
grunt.warn('version in"'+ packageinfos[i] +'" does not match file-version');
}
}
});
grunt.registerTask('webshimscombos', 'create combos from polyfiller.js.', function() {
var phantomjs = require('phantomjs');
var done = this.async();
var combos = {};
grunt.util.spawn({
cmd: phantomjs.path,
args: [
// The main script file.
'build/combobuild.js',
// The QUnit helper file to be injected.
'build/build.html'
]
},
function(err, result, code) {
var concatCfg;
result = result.toString();
if(!err && result.indexOf && result.indexOf('done') == -1){
try {
combos = JSON.parse(result);
} catch(er){
grunt.warn('parse error');
}
concatCfg = grunt.config('concat');
if(concatCfg){
concatCfg.combos = {files: combos.files};
} else {
concatCfg = combos.files;
}
//grunt.warn(JSON.stringify(concatCfg))
grunt.config('concat', concatCfg);
grunt.file.write(DEVPATH+'/shims/combos/comboinfo.json', result);
done(code);
return;
}
// Something went horribly wrong.
grunt.verbose.or.writeln();
grunt.log.write('Running PhantomJS...').error();
if (code === 127) {
grunt.warn('PhantomJS not found.');
} else {
result.split('\n').forEach(grunt.log.error, grunt.log);
grunt.warn('PhantomJS exited unexpectedly with exit code ' + code + '.');
}
done(code);
});
});
grunt.registerTask('cfgcopymin', 'config min and copy tasks.', function() {
var files = getFiles('src', false, '**', 'demos/js-webshim/dev', '*.js');
var path = require('path');
var copyTask = {};
var minTask = {};
var minPath, file, found;
var exclude = /(\.scss|config\.rb|\.txt|\.php|\.zip)$/;
for(var i in files){
file = files[i];
if(grunt.file.isFile(file)){
minPath = path.join(MINPATH, i);
if(/\.js$/.test(file)){
minTask[minPath] = [file];
found = true;
}
if(! exclude.test(file) ){
copyTask[minPath] = [file];
}
copyTask[path.join(DEVPATH, i)] = [file];
}
}
if(!found){
minTask[path.join(MINPATH, 'polyfiller.js')] = path.join('src', 'polyfiller.js');
}
var uglifyCfg = grunt.config('uglify');
var copyCfg = grunt.config('copy');
uglifyCfg.dist = { 'files': minTask };
copyCfg.main = { 'files': copyTask };
grunt.config('uglify', uglifyCfg);
grunt.config('copy', copyCfg);
});
grunt.registerTask('versionreplace', 'replace version', function() {
var reg = /<span class=\"ws-version\">[\d\.\-a-zA-Z]+<\/span>/g;
var version = grunt.config('pkg').version;
var replace = '<span class="ws-version">'+ version +'</span>';
var newUrl = 'https://github.com/aFarkas/webshim/archive/'+ version +'.zip';
var regUrl = /https:\/\/github\.com\/aFarkas\/webshim\/archive\/(\d+\.\d+\.\d+[\-RC\d]*)\.zip/g;
grunt.file.expand({cwd: 'demos/', matchBase: true}, '*.html').forEach(function(path) {
var code = grunt.file.read('demos/'+path);
code = code.replace(reg, replace);
code = code.replace(regUrl, newUrl);
grunt.file.write('demos/'+path, code);
});
});
// Default task.
grunt.loadTasks('grunt-tasks/');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-css');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-bytesize');
grunt.loadNpmTasks('grunt-contrib-csslint');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('default', ['webshimscombos', 'concat', 'cfgcopymin', 'copy:main', 'sass', 'cssmin', 'uglify', 'versionreplace', 'bytesize']);
grunt.registerTask('dev', ['webshimscombos', 'concat', 'cfgcopymin', 'copy:main', 'sass', 'watch']);
grunt.registerTask('release', ['versiontest', 'clean', 'default', 'bytesize']);
function getFiles(srcdir, destdir, wildcard, compareDir, compareMatch) {
var path = require('path');
var fs = require('fs');
// In Nodejs 0.8.0, existsSync moved from path -> fs.
var existsSync = fs.existsSync || path.existsSync;
var files = {};
grunt.file.expand({cwd: srcdir}, wildcard).forEach(function(relpath) {
var src = path.join(srcdir, relpath);
if(!compareDir || !compareMatch || !grunt.file.isMatch(compareMatch, src) || (!existsSync(path.join(compareDir, relpath)) || grunt.file.read(src) != grunt.file.read(path.join(compareDir, relpath)))){
files[destdir === false ? relpath : path.join(destdir, relpath)] = src;
}
});
return files;
}
};