-
Notifications
You must be signed in to change notification settings - Fork 12
/
Gruntfile.js
151 lines (138 loc) · 3.92 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
module.exports = function(grunt) {
grunt.initConfig({
// clean directory
clean: {
build: 'build/'
},
// copy directory
copy: {
build: {
expand: true,
src: 'static/**',
dest: 'build/'
}
},
// revision javascript and css files
filerev: {
options: {
algorithm: 'md5',
length: 8
},
js: {
src: [
// exclude lib directory
'build/static/js/*.js',
'build/static/js/collections/**/*.js',
'build/static/js/config/**/*.js',
'build/static/js/helper/**/*.js',
'build/static/js/models/**/*.js',
'build/static/js/views/**/*.js',
]
},
css: {
src: 'build/static/css/**/*.css',
},
},
// replace filenames with revisioned file names
filerev_replace: {
options: {
assets_root: 'build',
},
html: {
src: 'build/static/index.html',
},
},
// filerev_replace does not replace data-main argument supplied to
// requirejs, doing that here
replace: {
requirejs: {
src: 'build/static/index.html',
overwrite: true,
replacements: [{
// replace data-main with the revisioned file name
from: 'static/js/setup',
to: function(match) {
var k, v;
for(k in grunt.filerev.summary) {
v = grunt.filerev.summary[k];
if(k.indexOf('static/js/setup') !== -1) {
// remove prefix 'build/' and suffix '.js'
return v.slice('build/'.length, -1*'.js'.length);
}
}
return match;
}
}],
},
},
// minify javascript files
uglify: {
options: {
sourceMap: true,
sourceMapIncludeSources: true,
},
js: {
files: [{
expand: true,
src: ['build/static/js/**/*.js'],
dest: '', // overwrite original files
}]
},
},
// minify css files
cssmin: {
css: {
files: [{
expand: true,
src: ['build/static/css/**/*.css'],
dest: '', // overwrite original files
}]
}
},
});
// Configure requirejs paths to serve revved files
grunt.registerTask('require-paths', '', function() {
var config = concatConfig();
var file = findMainConfigFile();
grunt.file.write(file, config);
return;
// concat revved config and original config
function concatConfig() {
var config = 'require.config({ paths: {';
var k, v;
for(k in grunt.filerev.summary) {
v = grunt.filerev.summary[k];
if(k.indexOf('.js') !== -1) {
// remove prefix 'build/static/js/' and suffix '.js'
k = k.slice('build/static/js/'.length, -1*'.js'.length);
v = v.slice('build/static/js/'.length, -1*'.js'.length);
config += "'" + k + "'" + ":" + "'" + v + "'" + ", \n";
}
}
config += '}});';
// add original config
config += '\n' + grunt.file.read('static/js/setup.js');
return config;
}
// find the main config file for requirejs
function findMainConfigFile() {
var k, v;
for(k in grunt.filerev.summary) {
v = grunt.filerev.summary[k];
// setup.js is the main config file for requirejs
if(k.indexOf('setup.js') !== -1) {
return v;
}
}
}
});
// load modules
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-filerev');
grunt.loadNpmTasks('grunt-filerev-replace');
grunt.loadNpmTasks('grunt-text-replace');
grunt.registerTask('default', ['clean', 'copy', 'filerev', 'filerev_replace', 'replace', 'require-paths', 'uglify', 'cssmin']);
}