forked from gskinner/regexr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
222 lines (197 loc) · 4.72 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
var path = require("path");
var uglify = require("uglify-js");
var folderMount = function folderMount(connect, point) {
return connect.static(path.resolve(point));
};
module.exports = function (grunt) {
/*
Load all the tasks we need
Usually we use uglifyJS for code minification.
However uglify breaks the Unicode characters Codemirror uses in its RegEx expressions,
whereas yui does not.
*/
grunt.loadNpmTasks("grunt-yui-compressor");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-sass");
grunt.loadNpmTasks("grunt-contrib-connect");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-htmlmin");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadTasks("tasks/");
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
deployFolder: "build/",
// YUI-min
min: {
options: {
report: false
},
build: {
src: getScripts().yui,
dest: "<%= deployFolder %>js/yui.min.js"
}
},
uglify: {
options: {
compress: {
global_defs: {
DEBUG: false
},
dead_code: true
}
},
build: {
files: {
"<%= deployFolder %>js/uglify.min.js":getScripts().uglify,
"<%= deployFolder %>js/regExWorker.template.js":"js/regExWorker.template.js"
}
}
},
concat: {
build: {
src: ["<%= deployFolder %>js/yui.min.js", "<%= deployFolder %>js/uglify.min.js"],
dest: "<%= deployFolder %>js/scripts.min.js",
}
},
sass: {
build:{
options: {
compass: true,
style: "compact", // Can be nested, compact, compressed, expanded
precision: 2,
},
files: {
"css/regexr.css":"scss/regexr.scss"
}
}
},
watchSass: {
run: {
options: {
compass: true,
// In prep-tasks, we change the style for a build, to nested;
style: "nested", // Can be nested, compact, compressed, expanded
"line-numbers": true,
precision: 2,
},
files: {
"css/regexr.css":"scss/regexr.scss"
}
}
},
cssmin: {
options: {
report: false
},
build: {
src: "css/regexr.css",
dest: "<%= deployFolder %>css/regexr.css"
}
},
copy: {
build: {
files: [
{
expand: true,
src:[
"assets/**",
"php/**",
"*.ico",
".htaccess",
"manifest.json"
],
dest: "<%= deployFolder %>"
}
]
}
},
connect: {
build: {
options: {
hostname: "*",
keepalive:true,
middleware: function (connect, options) {
return [folderMount(connect, grunt.config.get("deployFolder"))]
}
}
}
},
htmlmin: {
options: {
removeComments: true,
collapseWhitespace: true
},
build: {
files:[{src: "<%= deployFolder %>index.html.tmp", dest: "<%= deployFolder %>index.html"}],
}
},
clean: {
build: ["<%= deployFolder %>!(v1|.git|php|sitemap.txt|*.md)**"],
postBuild: ["<%= deployFolder %>**/*.tmp", "<%= deployFolder %>js/yui.min.js", "<%= deployFolder %>js/uglify.min.js"]
}
});
/**
* Runs the index.html file through grunts template system.
*
*/
grunt.registerTask("parse-index", function (type) {
var templateFile = grunt.file.read("index.html");
var indexJs = minifyJS(grunt.file.read("js/index.template.js"));
var buildIndexTag = "\n"+indexJs+"";
var output = grunt.template.process(templateFile, {data:{build:true, index:buildIndexTag, noCache:Date.now()}})
//Write a temp html file, the htmlmin task will minify it to index.html
grunt.file.write(grunt.config.get("deployFolder")+"index.html.tmp", output);
});
grunt.registerTask("build", [
"clean:build",
"sass",
"cssmin",
"min",
"uglify",
"concat",
"parse-index",
"htmlmin",
"copy",
"clean:postBuild",
"connect:build"
]);
/**
* Loads our scripts.json file.
*
*/
function getScripts() {
var scripts = grunt.file.readJSON("scripts.json");
var missing = [];
for (var n in scripts) {
var arr = scripts[n];
arr.forEach(function(item, index, array) {
if (!grunt.file.exists(item)) {
missing.push(n+": "+item);
}
});
}
if (missing.length) {
// \x07 == beep sound in the terminal.
grunt.log.warn("\x07Missing ", missing.length + " scripts.\n\t" + missing.join("\n\t"));
}
return scripts;
}
/**
* Utility function.
* Returns an minified version of a javascript string, file or files.
*
* @param script {String|Array} Either a Javascript string,
* or an array of paths to Javascript files.
*
* Returns a minified version of the script(s) passed in.
*/
function minifyJS(script) {
var uglifyConfig = {};
if (typeof script == "string") {
uglifyConfig.fromString = true;
}
var result = uglify.minify(script, uglifyConfig);
return result.code;
}
};