-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGruntfile.js
More file actions
261 lines (231 loc) · 6.05 KB
/
Copy pathGruntfile.js
File metadata and controls
261 lines (231 loc) · 6.05 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
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
/**
* @description The Gruntfile.js - build file. Tragets are:
* - grunt test
* - grunt package // package for production
* - options --concat, --minify, --no-concat, --no-minify
* - grunt // test, package
*
* @author Fahim Farook
*
*/
module.exports = function (grunt) {
"use strict";
// display the elapsed execution time of grunt tasks
require("time-grunt")(grunt);
// load grunt plugins automatically by looking at task names
// so no need explicit loading. i.e. grunt.loadNpmTasks('grunt-contrib-clean');
// task -- plugin matching rules
// - <task> -> <task>
// - <task> -> grunt-<task>
// - <task> -> grunt-contrib-<task>
require("jit-grunt")(grunt, {
// for tasks whose name doesn't match the plugin name, load it explicitly.
useminPrepare: "grunt-usemin",
replace: "grunt-text-replace"
});
// build properties
var config = (function () {
var build = grunt.file.readJSON(".fsm4jsrc");
if (!build) {
grunt.fail.fatal([".fsm4js doesn't exists"]);
}
// optimize
var defaults = {
js: {
minify: true,
concat: true
}
};
if (!build.optimize) {
grunt.log.error([".fsm4jsrc doesn't define optimize. Command line options or defaults will be used."]);
build.optimize = defaults;
} else {
for (var prop in defaults) {
if (!build.optimize[prop]) {
build.optimize[prop] = {};
}
}
}
// dirs
if (!(build.src && build.test && build.dist && build.temp)) {
grunt.fail.fatal([".fsm4js doesn't define mandatory directories: src, test, dist, temp"]);
}
// .bowerrc
var bower = grunt.file.readJSON(".bowerrc");
if (bower) {
build.bower = bower;
} else {
build.bower = {
directory: "bower_components"
};
}
// verify bower.json
bower = grunt.file.readJSON("bower.json");
if (!bower || !bower.main) {
grunt.fail.fatal(["bower.json or main section in bower.json is missing. bower.json=" + bower]);
}
// verify package.json
var pkg = grunt.file.readJSON("package.json");
if (!pkg || !pkg.name) {
grunt.fail.fatal(["package.json or name in package.json is missing. package.json=" + pkg]);
}
// routes
var routes = {};
routes["/" + build.bower.directory] = "./" + build.bower.directory;
build.routes = routes;
return build;
}());
// configuring grunt...
grunt.initConfig({
// settings
config: config,
// replace name and versions on package.json and bower.json
replace: {
version: {
src: ["package.json", "bower.json", "<%= config.temp %>/**/*.js"],
overwrite: true,
replacements: [{
from: "@@VERSION",
to: "<%= config.version %>"
}, {
from: /"version"(\s+)?:[^,]+/g,
to: "\"version\": \"<%= config.version %>\""
}]
}
},
// delete files/ directories
clean: {
dist: {
files: [{
dot: true, // .tmp
src: [
"<%= config.dist %>",
"<%= config.temp %>"
]
}]
},
temp: ["<%= config.temp %>"]
},
// copy resources to dist or tmp
copy: {
// copy js files
// from: src, to: temp
// to allow minification (without requirejs)
js_src_temp: {
expand: true,
dot: true,
cwd: "<%= config.src %>",
src: "**/*.js",
dest: "<%= config.temp %>"
},
// copy js files
// from: temp, to: dist
js_temp_dist: {
expand: true,
dot: true,
cwd: "<%= config.temp %>",
src: "**/*.js",
dest: "<%= config.dist %>"
}
},
concat: {
target: {
// to concat files in order, put them in the required order.
// last file must be excluded first.
src: ["<%= config.concatOrder %>"],
dest: "<%= config.temp %>/<%= config.name %>.min.js"
}
},
// minify js files
uglify: {
target: {
options: {
mangle: false
},
files: [{
expand: true,
cwd: "<%= config.temp %>",
src: ["**/*.js"],
dest: "<%= config.dist %>"
}]
}
},
// validate javascript
eslint: {
options: {
configFile: ".eslintrc" // where to find eslint configurations
},
target: [
"Gruntfile.js",
"<%= config.src %>/**/*.js",
"<%= config.test %>/**/*.js",
"!<%= config.bower.directory %>/**/*.js"
]
},
// test unit tests in browser
karma: {
unit: {
configFile: "karma.conf.js",
files: [
{src: ["<%= config.bower.directory %>/**/*.js"]},
{src: ["<%= config.src %>/fsm4js-core/fsm4js-core.js"]},
{src: ["<%= config.src %>/**/*.js"]},
{src: ["<%= config.test %>/**/*.js"]}
]
}
}
});
// usage: grunt test
grunt.registerTask("test", function () {
if (grunt.option("coverage")) {
grunt.config("karma.unit.preprocessors", {
"<%= config.src %>/**/*.js": "coverage"
});
grunt.config("karma.unit.singleRun", true);
}
grunt.task.run(["karma"]);
});
// usage
// grunt package - package for production. Concat and minify based on settings provided in .zahrawirc
// grunt package --concat-js - Overrides settings provided in .zahrawirc
// grunt package --minify-js - Overrides settings provided in .zahrawirc
// grunt package --no-concat-js - Overrides settings provided in .zahrawirc
// grunt package --no-minify-js - Overrides settings provided in .zahrawirc
grunt.registerTask("package", function () {
// do configs
var options = [
"concat-js",
"minify-js",
"no-concat-js",
"no-minify-js"
];
for (var i = 0, len = options.length; i < len; i++) {
if (grunt.option(options[i])) {
var opts = options[i].split("-");
var val = opts[0] === "no" ? false : true;
var type = opts[opts.length - 1]; // js or css
var action = opts[opts.length - 2]; // concat or minify
config.optimize[type][action] = val;
}
}
var tasks = ["clean"]; // both .tmp and dist
if (config.optimize.js.concat) {
tasks.push("concat");
} else {
tasks.push("copy:js_src_temp"); // 2.a
}
tasks.push("replace");
if (config.optimize.js.minify) { // minify
tasks.push("uglify");
} else {
tasks.push("copy:js_temp_dist");
}
grunt.task.run(tasks);
});
// usage: grunt
grunt.registerTask("default", [
"newer:eslint",
"test",
"package"
]);
};