forked from guillaumepotier/Parsley.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
221 lines (198 loc) · 6.29 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
module.exports = function (grunt) {
/** LOAD Tasks **/
grunt.loadNpmTasks('grunt-replace');
grunt.loadNpmTasks('grunt-bower-cli');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-npm2bower-sync');
grunt.loadNpmTasks('grunt-docco');
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: {
dist: ['./dist']
},
sync: {
all: {
options: {
sync: ['author', 'name', 'version', 'license', 'main', 'keywords'],
from: 'package.json',
to: 'bower.json'
}
}
},
docco: {
source: {
src: ['src/parsley.js', 'src/parsley/*.js', 'src/extra/plugin/remote.js'],
options: {
output: 'doc/annotated-source/',
layout: 'parallel'
}
}
},
replace: {
annotated: {
options: {
patterns: [
{
match: /<div id="jump_page">/ig,
replacement: function () {
return '<div id="jump_page"><a class="source" href="../index.html"><<< back to documentation</a>';
}
},
{
match: /<\/body>/ig,
replacement: function () {
return '<script type="text/javascript">var _gaq=_gaq||[];_gaq.push(["_setAccount","UA-37229467-1"]);_gaq.push(["_trackPageview"]);(function(){var e=document.createElement("script");e.type="text/javascript";e.async=true;e.src=("https:"==document.location.protocol?"https://ssl":"http://www")+".google-analytics.com/ga.js";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)})();</script></body>';
}
},
{
match: 'version',
replacement: '<%= pkg.version %>'
},
]
},
files: [
{
expand: true,
flatten: true,
src: 'doc/annotated-source/*.html',
dest: 'doc/annotated-source/'
}
]
},
dist: {
options: {
patterns: [
{
match: 'name',
replacement: '<%= pkg.name.charAt(0).toUpperCase() + pkg.name.slice(1) %>'
},
{
match: 'version',
replacement: '<%= pkg.version %>'
},
{
match: 'author',
replacement: '<%= pkg.author.name %> - <<%= pkg.author.email %>>'
},
{
match: 'timestamp',
replacement: '<%= grunt.template.today() %>'
},
{
match: 'license',
replacement: '<%= pkg.license %>'
},
// Remove useless define statements that r.js add after concatenation, and not matched by convert callback
{
match: /define\("((?!\);)[\s\S])*\);/ig,
replacement: function () {
return '';
}
},
// Remove useless double line breaks
{
match: /\n\n/ig,
replacement: function () {
return "\n";
}
}
]
},
files: [
{
expand: true,
flatten: true,
src: 'dist/parsley.js',
dest: 'dist/'
}
]
}
},
requirejs: {
compile: {
options: {
// name: "./bower_components/almond/almond",
name: "parsley",
mainConfigFile: "./src/config.js",
wrap: {
startFile: "src/wrap/prepend.js",
endFile: "src/wrap/append.js"
},
optimize: 'none',
out: "dist/parsley.js",
findNestedDependencies: true,
// Avoid breaking semicolons inserted by r.js
skipSemiColonInsertion: true,
onBuildWrite: convert
}
}
},
uglify: {
options: {
report: 'gzip',
preserveComments: function (pos, node) {
return new RegExp('^!').test(node.value) && RegExp('parsley', 'i').test(node.value);
}
},
min: {
files: {
'dist/parsley.min.js': 'dist/parsley.js'
}
},
remote: {
files: {
'dist/parsley.remote.min.js': 'dist/parsley.remote.js'
}
}
},
watch: {
dev: {
files: ['src/parsley.js', 'src/parsley.css', 'src/wrap/*.js', 'src/parsley/*.js'],
tasks: ['requirejs', 'replace:dist']
}
},
concat: {
remote: {
src: ['src/extra/plugin/remote.js', 'dist/parsley.js'],
dest: 'dist/parsley.remote.js'
}
}
});
/** Tasks here **/
grunt.registerTask('default', []);
grunt.registerTask('configure', ['bower:install']);
grunt.registerTask('build-std', ['requirejs', 'replace:dist', 'uglify:min']);
grunt.registerTask('build-remote', ['concat:remote', 'uglify:remote']);
grunt.registerTask('build-annotated-source', ['docco:source', 'replace:annotated']);
grunt.registerTask('build', ['configure', 'clean:dist', 'build-std', 'build-remote', 'build-annotated-source', 'sync']);
grunt.registerTask('build-all', ['build']);
};
var rdefineEnd = /\}\);[^}\w]*$/;
function convert(name, path, contents) {
// Update original validatorjs for non-AMD implementation
if (/(dist\/validator.js)/.test(path)) {
// Makes sure the self-executing wrapper function stores a variable
contents = contents.replace("(","var Validator = (");
// Makes sure the self-executing wrapper function returns an object
var lastClosingBraceIndex = contents.lastIndexOf("}");
contents = contents.substring(0, lastClosingBraceIndex)
+ "\n\n return exports;\n}"
+ contents.substring(lastClosingBraceIndex + 1);
return contents;
}
// Ignore returns
contents = contents
.replace(/\s*return\s+[^\}]+(\}\);[^\w\}]*)$/, "$1")
// Multiple exports
.replace(/\s*exports\.\w+\s*=\s*\w+;/g, "");
// Remove define wrappers, closure ends, and empty declarations
contents = contents
.replace(/define\([^{]*?{/, "")
.replace(rdefineEnd, "\n");
return contents;
}