-
Notifications
You must be signed in to change notification settings - Fork 2
/
glob.js
65 lines (61 loc) · 1.81 KB
/
glob.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
/**
* glob.js
* A RequireJS plugin for loading multiple files that match a glob pattern.
* See https://github.com/OpenWebStack/requirejs-glob for details
*
* Copyright 2013 Dave Geddes
* @geddski heygeddski@gmail.com
* MIT License
*/
define(function(){
var buildFiles = {};
return {
/**
* load matched files, using the server to help find matches
*/
load:function(name, req, load, config) {
var files;
var globFrom = config.glob;
if (!globFrom) throw new Error('glob.js plugin requires the "glob" config setting');
// during the build we load the actual file contents in Node
if (config.isBuild){
var glob = require.nodeRequire('requirejs-glob');
files = glob.contents(name, globFrom);
buildFiles[name] = [];
files.forEach(function(file){
buildFiles[name].push(file);
});
load(files);
}
else{
// in the browser we just request matching files from the Node web service
var xhr = new XMLHttpRequest();
xhr.open('GET', 'requirejs-glob?glob=' + name + '&from=' + globFrom);
xhr.send();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4){
files = JSON.parse(xhr.responseText);
//load all the files with RequireJS
req(files, function(){
load(files);
});
}
};
}
},
/**
* write contents of matched files during the build
*/
write:function(pluginName, name, write) {
if (name in buildFiles) {
//write the glob module
write('define("glob!'+name+'");');
//write the contents of the matched files
var files = buildFiles[name];
files.forEach(function(file){
write(file);
});
}
}
};
});