-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGruntfile.js
More file actions
203 lines (196 loc) · 6.82 KB
/
Gruntfile.js
File metadata and controls
203 lines (196 loc) · 6.82 KB
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
var fs = require('fs');
module.exports = function(grunt) {
// Bower filenames:
bowerRcFilename = '.bowerrc';
bowerRcFilenameDefault = 'component.json';
// Define root paths to 3party assets and this app's custom assets:
var gruntBowerDir = './grunt-bower-lib';
var customAssetsDir = './assets';
// Order critical list of 3rd party & custom dependencies,
// list in correct order (i.e. jQuery first for 3rd party)
// and paths relative to the Bower ("./components/")" directory.
var bowerJs = [];
var bowerCss = [];
var bowerRc = {};
if (grunt.file.exists(bowerRcFilename)) {
bowerRc = grunt.file.readJSON(bowerRcFilename);
}
if (!bowerRc.json) {
bowerRc.json = bowerRcFilenameDefault;
}
var bowerComps = grunt.file.readJSON(bowerRc.json);
String.prototype.endsWith = function(suffix) {
return this.toLowerCase().indexOf(suffix, this.length - suffix.length) !== -1;
};
var isJavascript = function(filename) {
return filename.endsWith('.js');
};
var isCss = function(filename) {
return filename.endsWith('.css');
};
var getFilename = function(path) {
return path.split('\\').pop().split('/').pop();
};
var getPrefixMapFn = function(comp) {
return function (path) {
return gruntBowerDir + '/' + comp + '/' + path;
};
};
Object.keys(bowerComps.dependencies).forEach(function(comp) {
if (bowerComps.exportsOverride && bowerComps.exportsOverride[comp]) {
// This Bower component was overridden to specify exact files to
// include from the library. Additionally we will infer the order
// that those files should be included in steps such as concat
// which ultimately affects the order the code is presented to
// the browser. So if it's critical to have one file come after another
// (say you want bootstrap.css to come before bootstrap-responsive.css)
// include them in that order in the exportsOverride field of
// component.json.
Object.keys(bowerComps.exportsOverride[comp]).forEach(function(targetDir) {
var srcFiles = bowerComps.exportsOverride[comp][targetDir];
var jsFiles = srcFiles.filter(isJavascript);
var cssFiles = srcFiles.filter(isCss);
var prefixMapFn = getPrefixMapFn(comp);
if (jsFiles && jsFiles.length > 0) {
bowerJs = bowerJs.concat(jsFiles.map(getFilename).map(prefixMapFn));
}
if (cssFiles && cssFiles.length > 0) {
bowerCss = bowerCss.concat(cssFiles.map(getFilename).map(prefixMapFn));
}
});
} else {
bowerJs.push(gruntBowerDir + '/' + comp + '/*.js');
bowerCss.push(gruntBowerDir + '/' + comp + '/*.css');
}
});
// Initialize Grunt configuration:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: {
prod: {
src: ['./public/dist/']
},
dev: {
src: ['./public/dist-dev/']
}
},
bower: {
install: {
options: {
targetDir: gruntBowerDir
}
}
},
jshint: {
// define the files to lint
files: ['Gruntfile.js'],
// configure JSHint (documented at http://www.jshint.com/docs/)
options: {
globals: {
jQuery: true,
console: true,
module: true
}
}
},
concat: {
options: {
stripBanners: false,
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */\n\n'
},
'3party-js': {
src: bowerJs,
dest: gruntBowerDir + '/3party/3party.js'
},
'3party-css': {
src: bowerCss,
dest: gruntBowerDir + '/3party/3party.css'
},
'custom-js': {
src: [
customAssetsDir + '/js/*.js'
],
dest: gruntBowerDir + '/<%= pkg.name %>/<%= pkg.name %>.js'
},
'custom-css': {
src: [
customAssetsDir + '/css/*.css'
],
dest: gruntBowerDir + '/<%= pkg.name %>/<%= pkg.name %>.css'
}
},
min: {
dist: {
files: {
'./public/dist/js/3party.min.js': gruntBowerDir + '/3party/3party.js',
'./public/dist/js/<%= pkg.name %>.min.js': gruntBowerDir + '/<%= pkg.name %>/<%= pkg.name %>.js'
}
}
},
cssmin: {
dist: {
files: {
'./public/dist/css/3party.min.css': gruntBowerDir + '/3party/3party.css',
'./public/dist/css/<%= pkg.name %>.min.css': gruntBowerDir + '/<%= pkg.name %>/<%= pkg.name %>.css'
}
}
},
copy: {
prod: {
files: [
{expand: true, cwd: customAssetsDir + '/img/', src: ['**'], dest: './public/dist/img/', filter: 'isFile'},
{expand: true, cwd: customAssetsDir + '/ico/', src: ['**'], dest: './public/dist/ico/', filter: 'isFile'},
{expand: true, cwd: customAssetsDir + '/fonts/', src: ['**'], dest: './public/dist/fonts/', filter: 'isFile'}
]
},
dev: {
files: [
{expand: true, cwd: gruntBowerDir + '/3party/', src: ['3party.js'], dest: './public/dist-dev/js/', filter: 'isFile'},
{expand: true, cwd: gruntBowerDir + '/3party/', src: ['3party.css'], dest: './public/dist-dev/css/', filter: 'isFile'},
{expand: true, cwd: gruntBowerDir + '/<%= pkg.name %>/', src: ['<%= pkg.name %>.js'], dest: './public/dist-dev/js/', filter: 'isFile'},
{expand: true, cwd: gruntBowerDir + '/<%= pkg.name %>/', src: ['<%= pkg.name %>.css'], dest: './public/dist-dev/css/', filter: 'isFile'},
{expand: true, cwd: customAssetsDir + '/img/', src: ['**'], dest: './public/dist-dev/img/', filter: 'isFile'},
{expand: true, cwd: customAssetsDir + '/ico/', src: ['**'], dest: './public/dist-dev/ico/', filter: 'isFile'},
{expand: true, cwd: customAssetsDir + '/fonts/', src: ['**'], dest: './public/dist-dev/fonts/', filter: 'isFile'}
]
}
},
watch: {
files: [
'<%= jshint.files %>', // Gruntfile.js - TODO: apply to custom assets & node scripts
'package.json', // NPM modules
bowerRc.json, // Bower modules
'assets/**'
],
tasks: ['dev']
}
});
// Load libs
grunt.loadNpmTasks('grunt-bower-task');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-yui-compressor');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-clean');
// Register the default tasks
grunt.registerTask('default', [
'clean',
'bower:install',
'jshint',
'concat',
'min',
'cssmin',
'copy:prod'
]
);
// Developer tasks (not for production):
grunt.registerTask('dev', [
'clean:dev',
'bower:install',
'jshint',
'concat',
'copy:dev'
]);
};