forked from cedricpinson/osgjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
578 lines (470 loc) · 14.3 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
'use strict';
/* global process */
var fs = require( 'fs' );
var path = require( 'path' );
var webpackConfig = require( './webpack.config.js' );
var extend = require( 'extend' );
var glob = require( 'glob' );
var jshintrc = JSON.parse( fs.readFileSync( './.jshintrc' ).toString() );
// Base paths used by the tasks.
// They always have to finish with a '/'.
//
var SOURCE_PATH = 'sources/';
var BUILD_PATH = 'builds/';
var DIST_PATH = path.join( BUILD_PATH, 'dist/' );
var DOCS_PATH = path.join( BUILD_PATH, 'docs/' );
// Utility functions
//
var find = function ( cwd, pattern ) {
if ( typeof pattern === 'undefined' ) {
pattern = cwd;
cwd = undefined;
}
var isEntity = function ( pathname ) {
if ( cwd ) pathname = path.join( cwd, pathname );
return !fs.lstatSync( pathname ).isDirectory();
};
var options = {};
if ( cwd )
options.cwd = cwd;
return glob.sync( pattern, options ).filter( isEntity );
};
// get source file once and for all, caching results.
var srcFiles = find( SOURCE_PATH, '**/*.js' ).map( function ( pathname ) {
return pathname;
} );
// Used to store all Grunt tasks
//
var gruntTasks = {};
// ## Top-level configurations
//
( function () {
//lint
gruntTasks.jshint = {
options: jshintrc
};
//build/bundle
gruntTasks.copy = {
options: {}
};
gruntTasks.clean = {
options: {}
};
// docs
gruntTasks.plato = {};
gruntTasks.docco = {};
//tests
gruntTasks.qunit = {};
gruntTasks.connect = {};
// static website
gruntTasks.gitclone = {};
gruntTasks.gitpush = {};
gruntTasks.shell = {};
gruntTasks.gitcommit = {};
// duck the camel. (case)
gruntTasks[ 'wintersmith_compile' ] = {};
} )();
// ## Webpack
//
// Build OSGJS with webpack
//
( function () {
var webpack = require( 'webpack' );
// var ExtractTextPlugin = require( 'extract-text-webpack-plugin' );
var targets = {
build: {
entry: {
OSG: [ './sources/OSG.js' ],
tests: [ './tests/tests.js' ]
},
devtool: 'source-map',
module: {
loaders: [ {
test: /\.js$/,
loader: 'webpack-strip-block'
} ]
}
},
builddebug: {
entry: {
OSG: [ './sources/OSG.js' ],
tests: [ './tests/tests.js' ]
},
devtool: 'eval-source-map'
},
buildrelease: {
devtool: null,
output: {
path: DIST_PATH,
filename: '[name].min.js',
libraryTarget: 'umd',
library: 'OSG'
},
loaders: [
{ test: /\.js$/, loader : 'webpack-strip-block' }
],
// additional plugins for this specific mode
plugins: [
new webpack.optimize.UglifyJsPlugin( {
sourceMap: false
} )
]
},
docs: {
entry: './sources/OSG.js',
output: {
path: DOCS_PATH,
filename: 'OSG.js'
}
}
};
gruntTasks.webpack = {
options: webpackConfig,
build: targets.build,
buildrelease: targets.buildrelease,
builddebug: targets.builddebug,
docs: targets.docs,
watch: {
entry: targets.build.entry,
devtool: targets.build.devtool,
// use webpacks watcher
// You need to keep the grunt process alive
watch: true,
keepalive: true
}
};
} )();
// ## JSHint
//
// Will check the Gruntfile and every "*.js" file in the "statics/sources/" folder.
//
( function () {
gruntTasks.jshint.self = {
options: {
node: true
},
src: [ 'Gruntfile.js' ]
};
gruntTasks.jshint.sources = {
options: {
globals: {
define: true,
require: true
}
},
src: srcFiles.filter( function ( pathName ) {
return pathName.indexOf( 'vendors' ) === -1;
} ).map( function ( pathname ) {
return path.join( SOURCE_PATH, pathname );
} )
};
// add another output from envvar to have better error tracking in emacs
if ( process.env.GRUNT_EMACS_REPORTER !== undefined ) {
gruntTasks.jshint.sources.options.reporter = process.env.GRUNT_EMACS_REPORTER;
}
} )();
( function () {
gruntTasks.jsbeautifier = {
default: {
src: [ 'sources/**/*.js', 'examples/**/*.js', '!examples/vendors/*.js' ],
options: {
config: './.jsbeautifyrc'
}
},
check: {
src: [ 'sources/**/*.js', 'examples/**/*.js', '!examples/vendors/*.js' ],
// config: './.jsbeautifyrc',
options: {
mode: 'VERIFY_ONLY',
config: './.jsbeautifyrc'
}
}
};
} )();
var generateVersionFile = function () {
var pkg = JSON.parse( fs.readFileSync( 'package.json' ) );
var content = [
'define( [], function () {',
' return {',
' name: \'' + pkg.name + '\',',
' version: \'' + pkg.version + '\',',
' author: \'' + pkg.author + '\'',
' };',
'} );',
''
];
fs.writeFileSync( path.join( SOURCE_PATH, 'version.js' ), content.join( '\n' ) );
};
// ## Clean
//
( function () {
gruntTasks.clean.staticWeb = {
src: [ path.join( BUILD_PATH, 'web' ) ]
};
} )();
// ## Docco
//
( function () {
gruntTasks.docco = {
singleDoc: {
src: path.join( DOCS_PATH, 'OSG.js' ),
options: {
output: 'docs/annotated-source'
}
},
docs: {
src: srcFiles.map( function ( pathname ) {
return path.join( SOURCE_PATH, pathname );
} ),
options: {
layout: 'classic',
output: 'docs/annotated-source'
}
}
};
} )();
// ## Plato
( function () {
gruntTasks.plato = {
options: {
// Task-specific options go here.
},
main: {
files: {
'docs/analysis': srcFiles.map( function ( pathname ) {
return path.join( SOURCE_PATH, pathname );
} )
}
}
};
} )();
// ## Qunit and connect
//
( function () {
// qunit using connect
gruntTasks.qunit = {
all: {
options: {
timeout: 30000,
urls: [
'http://127.0.0.1:9001/tests/index.html'
]
}
}
};
// will start a server on port 9001 with root directory at the same level of
// the grunt file
var currentDirectory = path.dirname( path.resolve( './Gruntfile.js', './' ) );
gruntTasks.connect = {
server: {
options: {
port: 9001,
hostname: '127.0.0.1'
}
},
dist: {
options: {
port: 9000,
directory: currentDirectory,
hostname: '0.0.0.0',
open: true,
middleware: function ( connect, options, middlewares ) {
// inject a custom middleware into the array of default middlewares
middlewares.unshift( function ( req, res, next ) {
var ext = path.extname( req.url );
if ( ext === '.gz' ) {
res.setHeader( 'Content-Type', 'text/plain' );
res.setHeader( 'Content-Encoding', 'gzip' );
}
return next();
} );
return middlewares;
}
}
}
};
} )();
// ## Copy
// (explicit because windows doesn't support symlinks)
( function () {
gruntTasks.copyto = {
main: {
files: [
//Hammer:
{
cwd: './',
src: 'examples/vendors/hammer-2.0.4.js',
dest: 'examples/vendors/hammer.js'
},
//Bluebird:
{
cwd: './',
src: 'examples/vendors/bluebird-2.9.25.js',
dest: 'examples/vendors/bluebird.js'
},
//es5-shim:
{
cwd: './',
src: 'tests/vendors/es5-shim.js',
dest: 'examples/vendors/es5-shim.js'
},
//es6-shim:
{
cwd: './',
src: 'tests/vendors/es6-shim.js',
dest: 'examples/vendors/es6-shim.js'
}
]
}
};
} )();
// ## WinterSmith:
// (static site gen for osgjs.org)
( function () {
gruntTasks[ 'wintersmith_compile' ] = {
build: {
options: {
config: './website/web/config.json',
output: path.join( BUILD_PATH, 'web' )
}
},
preview: {
options: {
action: 'preview',
config: './website/web/config.json',
output: path.join( BUILD_PATH, 'web' )
}
}
};
} )();
( function () {
gruntTasks.copy = {
staticWeb: {
files: [ {
expand: true,
src: [ 'sources/**' ],
dest: path.join( BUILD_PATH, 'web/' )
}, {
expand: true,
src: [ 'docs/**' ],
dest: path.join( BUILD_PATH, 'web/' )
}, {
expand: true,
src: [ 'examples/**' ],
dest: path.join( BUILD_PATH, 'web/' )
}, {
expand: true,
src: [ 'tests/**' ],
dest: path.join( BUILD_PATH, 'web/' )
}, {
expand: true,
cwd: 'builds',
src: [ 'dist/**' ],
dest: path.join( BUILD_PATH, 'web/builds/' )
}, {
expand: true,
cwd: 'builds',
src: [ 'active/**' ],
dest: path.join( BUILD_PATH, 'web/builds/' )
} ]
}
};
} )();
// ## git:
// (static site upload)
( function () {
//git clone -b my-branch [email protected]:cedricpinson/osgjs.git
gruntTasks.gitclone = {
staticWeb: {
options: {
branch: 'gh-pages',
repository: '[email protected]:cedricpinson/osgjs.git',
directory: path.join( BUILD_PATH, 'web' )
//, depth: -1 // cannot push from a shallow clone
}
}
};
// missing add --all
gruntTasks.shell = {
staticWeb: {
options: {
execOptions: {
cwd: path.join( BUILD_PATH, 'web' )
}
},
command: 'git add -A -v'
}
};
gruntTasks.gitcommit = {
staticWeb: {
options: {
branch: 'gh-pages',
repository: '[email protected]:cedricpinson/osgjs.git',
message: 'website update to latest develop',
cwd: path.join( BUILD_PATH, 'web' ),
verbose: true
}
},
files: {
src: ''
}
};
gruntTasks.gitpush = {
staticWeb: {
options: {
branch: 'gh-pages',
repository: '[email protected]:cedricpinson/osgjs.git',
cwd: path.join( BUILD_PATH, 'web' ),
verbose: true
}
}
};
} )();
module.exports = function ( grunt ) {
var distFullPath = path.normalize( path.join( __dirname, DIST_PATH ) );
grunt.file.mkdir( distFullPath );
grunt.initConfig( extend( {
pkg: grunt.file.readJSON( 'package.json' )
}, gruntTasks ) );
generateVersionFile();
// grunt.event.on('qunit.testStart', function (name) {
// grunt.log.ok("Running test: " + name);
// });
// grunt.event.on('qunit.log', function (result, actual, expected, message, source) {
// if ( !result ) {
// if ( expected !== undefined ) {
// grunt.log.error('failed ' + message + ' ' + source );
// } else {
// grunt.log.error('actual: ' + actual + ' expected: ' + expected + ' ,failed ' + message );
// }
// }
// });
grunt.loadNpmTasks( 'grunt-release' );
grunt.loadNpmTasks( 'grunt-contrib-connect' );
grunt.loadNpmTasks( 'grunt-contrib-qunit' );
grunt.loadNpmTasks( 'grunt-docco' );
grunt.loadNpmTasks( 'grunt-plato' );
grunt.loadNpmTasks( 'grunt-contrib-jshint' );
grunt.loadNpmTasks( 'grunt-contrib-copy' );
grunt.loadNpmTasks( 'grunt-contrib-clean' );
grunt.loadNpmTasks( 'grunt-jsbeautifier' );
// windows not supporting link in git repo
grunt.loadNpmTasks( 'grunt-copy-to' );
//static site
grunt.loadNpmTasks( 'grunt-wintersmith-compile' );
grunt.loadNpmTasks( 'grunt-git' );
grunt.loadNpmTasks( 'grunt-shell' );
grunt.loadNpmTasks( 'grunt-webpack' );
grunt.registerTask( 'watch', [ 'webpack:watch' ] );
grunt.registerTask( 'check', [ 'jsbeautifier:check', 'jshint:self', 'jshint:sources' ] );
grunt.registerTask( 'beautify', [ 'jsbeautifier:default' ] );
grunt.registerTask( 'test', [ 'connect:server', 'qunit:all' ] );
grunt.registerTask( 'docs', [ 'webpack:docs', 'docco' ] );
grunt.registerTask( 'build', [ 'copyto', 'webpack:build' ] );
grunt.registerTask( 'build-release', [ 'copyto', 'webpack:buildrelease' ] );
grunt.registerTask( 'build-debug', [ 'copyto', 'webpack:builddebug' ] );
grunt.registerTask( 'default', [ 'check', 'build' ] );
grunt.registerTask( 'serve', [ 'build', 'connect:dist:keepalive' ] );
grunt.registerTask( 'website_only', [ 'clean:staticWeb', 'gitclone:staticWeb', 'copy:staticWeb', 'wintersmith_compile:build', 'shell:staticWeb', 'gitcommit:staticWeb', 'gitpush:staticWeb' ] );
grunt.registerTask( 'website', [ 'default', 'docs', 'website_only' ] );
};