-
Notifications
You must be signed in to change notification settings - Fork 23
/
Gruntfile.js
119 lines (105 loc) · 3.25 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
var requirejs = require('requirejs')
, config = requirejs('./config.js')
, debug = false
, color = require('color')
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
testacular: {
unit: {
configFile: 'testacular.conf.js',
singleRun: true,
browsers: ['PhantomJS']
}
},
watch: {
src: {
files: ['js/app/*.js', 'css/**/*.css'],
tasks: ['build']
}
}
});
// Plug-ins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('gruntacular');
// Tasks
grunt.registerTask('default', ['server']);
grunt.registerTask('server', ['_server', 'watch']);
grunt.registerTask('test', ['testacular']);
// Set debug mode
grunt.registerTask('debug', function() {
debug = true
})
// Development Server
grunt.registerTask('_server', 'Start a custom static web server.', function() {
var express = require('express');
var fs = require('fs')
var mdir = __dirname
var app = express()
app.use(function(req, res, next) {
if (false == debug) return next()
if ('/css/style.min.css' == req.url) return res.redirect('/css/main.css')
if ('/js/app.min.js' == req.url) return res.redirect('/js/app/main.js')
next()
})
app.use('/js', express.static(mdir + '/js'))
app.use('/css', express.static(mdir + '/css'))
app.use('/img', express.static(mdir + '/img'))
app.use('/locales', express.static(mdir + '/locales'))
app.use('/templates', express.static(mdir + '/templates'))
app.get('/config.js', function(req, res) {
fs.readFile(mdir + '/config.js', 'utf8', function(error, file) {
res.contentType('application/javascript')
res.send(file)
});
});
app.get('*', function(req, res) {
fs.readFile(mdir + '/index.html', 'utf8', function(error, file) {
res.send(file)
})
});
var port = config.port || 3000
app.listen(port)
console.log('Listening on port ' + port + '...')
});
// Build server
grunt.registerTask('build', 'Build the compressed javascript and CSS files', function() {
var done = this.async()
var requirejs = require('requirejs')
var tasks = ['css', 'javascript']
// Compress CSS
var cssConfig = {
cssIn: './css/main.css',
out: './css/style.min.css',
optimizeCss: 'standard'
}
requirejs.optimize(cssConfig, function(log) {
grunt.log.writeln(("Application CSS optimisation complete").green)
tasks.pop()
if (0 == tasks.length) return done()
}, function(error) {
done(new Error("\nError optimizing CSS: " + error))
})
// Compress JavaScript
var mainConfig = {
baseUrl: './js/app',
name: 'main',
inlineText: true,
out: './js/app.min.js',
mainConfigFile: './js/app/main.js',
optimizeAllPluginResources: true,
optimize: 'uglify2',
generateSourceMaps: true,
preserveLicenseComments: false,
exclude: [ 'config' ]
}
requirejs.optimize(mainConfig, function (log) {
grunt.log.writeln(("Main JavaScript optimisation complete").green)
tasks.pop()
if (0 == tasks.length) return done()
}, function(error) {
grunt.log.writeln("\nError optimizing JavaScript: " + error)
done(new Error("\nError optimizing JavaScript: " + error))
})
})
}