This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
forked from FremyCompany/css-regions-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
248 lines (199 loc) · 6.74 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
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
'use strict';
module.exports = function(grunt) {
// Import the dependencies
var url = require('url');
// Transform a require-uri into a canonical uri
function resolveModuleUri(baseUri, relativeModuleUri) {
// determine the url kind
var indexOfSeparator = -1;
if ((indexOfSeparator=relativeModuleUri.indexOf(':')) != -1) {
var moduleUri = 'src/'+relativeModuleUri.substr(0,indexOfSeparator)+'/'+relativeModuleUri.substr(indexOfSeparator+1);
} else {
var moduleUri = url.resolve(baseUri, relativeModuleUri);
}
// potentially append .js
if(!grunt.file.exists(moduleUri)) { moduleUri = moduleUri + '.js'; }
// return the final value
return moduleUri;
}
// Data storage
var pkg = grunt.file.readJSON('package.json');
var code_wrapper_start = (
"\n"+
"!(function() { 'use strict';"+"\n"+
" var module = { exports:{} };"+"\n"+
" var require = (function() { var modules = {}; var require = function(m) { return modules[m]; }; require.define = function(m) { modules[m]=module.exports; module.exports={}; }; return require; })();"
);
var code_wrapper_separator = "\n\n////////////////////////////////////////\n\n";
code_wrapper_start += code_wrapper_separator;
var code_wrapper_end = (
"\n\n" + "window.cssPolyfills = { require: require };" + "\n\n" + "})();"
);
// Project configuration.
grunt.initConfig({
//
// Metadata:
//
pkg: pkg,
banner:
'/*! <%= pkg.name.toUpperCase() %> - v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd") %>' + '<%= pkg.homepage ? " - " + pkg.homepage : "" %>' + ' - Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +' <%= _.map(pkg.licenses, "type").join("- and ") %>-Licensed !*/\n',
code_wrapper_start:
code_wrapper_start,
code_wrapper_end:
code_wrapper_end,
code_wrapper_separator:
code_wrapper_separator,
//
// Task configuration:
//
findreq: {
root: '<%= pkg.main =>'
},
concat: {
bin: {
src: ['<%= pkg.main %>'],
dest: 'bin/<%= pkg.name %>.js',
options: {
banner: '<%= banner %><%= code_wrapper_start %>',
footer: '<%= code_wrapper_end %>',
separator: '<%= code_wrapper_separator %>',
stripBanners: true,
sourceMap: true,
process: function(src, filepath) {
return src.replace(/(?:(?:\/\/|\/\*)[-_=a-zA-Z0-9 ]*)?require\((\'.*?\'|\"".*?\"")\)/g, function(str, match) {
if(str.indexOf('require')==0) {
var relativeModuleUri = match.substr(1, match.length-2);
var moduleUri = resolveModuleUri(filepath, relativeModuleUri);
return "require('"+moduleUri+"')";
} else {
return str;
}
})+"\nrequire.define('" + filepath + "');";
}
}
},
doc: {
src: ['doc/**/*.ts'],
dest: 'doc/README.md',
options: {
old_banner: '<%= ""+(function() { var source = grunt.file.read("doc/README.md"), reg = /(\\r?\\n){3}/; reg=reg.exec(source)[0]; return source.substr(0, reg.lastIndex); })() %>',
banner: '<%= grunt.file.read("doc/README[header].md") %>',
separator: '',
stripBanners: false,
sourceMap: false,
process: function(src, filepath) {
if(filepath == "doc/requirements.ts") { return; }
try {
var fileurl = filepath.replace(/^(\.\/)?doc\/?/,'./'); grunt.log.writeln(fileurl);
var filename = /\/([^\/]+)\.ts/i.exec(filepath)[1]; grunt.log.writeln(filename);
var firstComment = /(?: |\t)\*(?: |\t)(.*?)(\r?\n)/.exec(src)[1].trimRight(); grunt.log.writeln(firstComment);
src = src.replace(/^(?:.|\r|\n)*?\/\/\/\/ EXAMPLES [\/\r\n ]+/,'/');
var extractExamples = /\/\*+ (.*?)(?:\*+\/)[\r\n]+function ?[a-zA-Z0-9_]*\([a-zA-Z0-9_, ]*\) *\{[\r\n\t ]*\r?\n((.|\r|\n)*?)([\r\n]+\})/g;
var example, examples=''; while(example = extractExamples.exec(src)) {
examples = examples + '\r\n### '+example[1].trim() + '\r\n\r\n```javascript\r\n\t'+example[2].trim()+'\r\n```\r\n';
}
return (
"\r\n\r\n"+
"["+filename.toUpperCase()+"]("+fileurl+")\r\n"+
"===================================\r\n"+
firstComment+"\r\n"+
examples
);
} catch (ex) {
grunt.log.writeln('[ERROR] Generating doc for ' + filepath + ' failled with message: ' + ex.message);
return '';
}
}
}
}
},
uglify: {
options: {
banner: '<%= banner %>',
sourceMap: true
},
bin: {
src: '<%= concat.bin.dest %>',
dest: 'bin/<%= pkg.name %>.min.js'
},
},
nodeunit: {
files: ['test/**/*_test.js']
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
src: {
options: {
jshintrc: '.jshintrc'
},
src: ['src/**/*.js']
},
test: {
src: ['test/**/*.js']
},
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
src: {
files: '<%= jshint.src.src %>',
tasks: ['jshint:src', 'nodeunit']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'nodeunit']
},
},
});
// These plugins provide some necessary tasks.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
// Define the special 'findreq' task
grunt.registerTask('findreq', 'Automatically determines the order in which the source files have to be concatened', function() {
var modules = [];
var importModule = function(rootUri, baseUri, relativeModuleUri) {
// find out the canonical name of the module
var moduleUri = resolveModuleUri(baseUri, relativeModuleUri);
if(!grunt.file.exists(moduleUri)) { moduleUri += '.js'; }
grunt.log.writeln('importing: '+moduleUri);
// check that the module hasn't already been loaded before
if(modules.indexOf(moduleUri) >= 0) { return; }
// load the file content from the disk
var fileContent = grunt.file.read(moduleUri);
// find all dependencies
var dependencyPattern = /(?:(?:\/\/|\/\*)[-_=a-zA-Z0-9 ]*)?require\((\'.*?\'|\"".*?\"")\)/g;
var match; while(match = dependencyPattern.exec(fileContent)) {
// check we are not in a comment
if(match[0].indexOf('require') == 0) {
// get the relative uri
match = match[1]; match = match.substr(1, match.length-2);
grunt.log.writeln('depencendy: '+match);
// import the module
importModule(rootUri, moduleUri, match);
}
}
// mark the module as ready
modules.push(moduleUri);
}
importModule(pkg.main, '.', pkg.main);
grunt.config.merge({
concat: {
bin: {
src: modules
}
}
});
});
// Default task.
grunt.registerTask('default', [ /*'jshint', 'nodeunit', */'findreq', 'concat', 'uglify']);
};