-
Notifications
You must be signed in to change notification settings - Fork 4
/
builder.js
230 lines (185 loc) · 6.1 KB
/
builder.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
/** @license MIT License (c) copyright Pieter Vanderwerff */
/**
* wire-rjs-builder plugin
* Builder plugin for requirejs (r.js)
* see: http://github.com/jrburke/requirejs for details
*
* Supports requirejs version 2.1.9+
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*/
(function(define) {
define( function() {
var defaultModuleRegex, defaultSpecRegex, defaultWireName, replaceIdsRegex, removeCommentsRx, specCache;
// default dependency regex
defaultWireName = 'wire';
defaultModuleRegex = /(\.module|\.create|\$plugins\[\])$/;
defaultSpecRegex = /(\.wire|\.spec)$/;
replaceIdsRegex = /(define)\s*\(\s*(?:\s*["']([^"']*)["']\s*,)?(?:\s*\[([^\]]*)\]\s*,)?/;
removeCommentsRx = /\/\*[\s\S]*?\*\/|\/\/.*?[\n\r]/g;
specCache = {};
// Using special require.nodeRequire, something added by r.js.
if ( require.nodeRequire ) {
var fs = require.nodeRequire('fs');
}
return {
load: analyze,
write: writeSpec
};
function analyze(resourceId, require, load, config) {
var specIds, childSpecRegex, moduleRegex, wirePluginName, filePath;
moduleRegex = defaultModuleRegex;
childSpecRegex = defaultSpecRegex;
wirePluginName = defaultWireName;
// Get config values
if (config) {
if (config.moduleRegex) { moduleRegex = new RegExp(config.moduleRegex); }
if (config.childSpecRegex) { childSpecRegex = new RegExp(config.childSpecRegex); }
if (config.wirePluginName) { wirePluginName = new RegExp(config.wirePluginName); }
}
// Grab the spec module id, *or comma separated list of spec module ids*
// Split in case it's a comma separated list of spec ids
specIds = resourceId.split(',');
if (specIds.length > 1) {
require(specIds.map(function(id) { return wirePluginName + '!' + id; } ), function() {
load();
});
return;
}
// get all the specs
filePath = ensureExtension(require.toUrl(resourceId), 'js');
getFile(filePath, function(contents) {
var spec, dependencies;
load.fromText(contents);
spec = require(resourceId);
dependencies = processSpec(spec);
specCache[ resourceId ] = {
dependencies: dependencies.runtime,
path: filePath
};
require(dependencies.build);
});
// For each spec id, add the spec itself as a dependency, and then
// scan the spec contents to find all modules that it needs (e.g.
// "module" and "create")
function processSpec(spec) {
var runtime_deps, build_deps;
runtime_deps = [];
build_deps = [];
scanObj(spec);
function scanObj(obj, path) {
// Scan all keys. This might be the spec itself, or any sub-object-literal
// in the spec.
for (var name in obj) {
if (obj.hasOwnProperty(name)) {
scanItem(obj[name], createPath(path, name));
}
}
}
function scanItem(it, path) {
// Determine the kind of thing we're looking at
// 1. If it's a string, and the key is module or create, then assume it
// is a moduleId, and add it as a dependency.
// 2. If it's an object or an array, scan it recursively
if (typeof it === 'string') {
if (isDep(path)) {
// Get module def
addDependency(it);
} else if (isWireDep(path)) {
addChildSpec(it);
}
} else if (isStrictlyObject(it)) {
// Descend into subscope
scanObj(it, path);
} else if (Array.isArray(it)) {
// Descend into array
var arrayPath = path + '[]';
it.forEach(function(arrayItem) {
scanItem(arrayItem, arrayPath);
});
}
}
function addDependency(moduleId) {
var mod = moduleId;
if (moduleId.indexOf( './' ) === 0 ) {
// relative path, assume its relative to spec we're loading (resourceId)
var split = resourceId.split('/');
// last element is the spec name, take it out
split.pop();
// remove ./ and join two together
var moduleIdWithoutDotSlash = moduleId.slice(2);
split.push(moduleIdWithoutDotSlash);
mod = split.join( '/' );
}
runtime_deps.push(mod);
build_deps.push(mod);
}
function addChildSpec(moduleId) {
runtime_deps.push(moduleId);
build_deps.push(wirePluginName + '!' + moduleId);
}
function isDep(path) {
return moduleRegex.test(path);
}
function isWireDep(path) {
return childSpecRegex.test(path);
}
function createPath(path, name) {
return path ? (path + '.' + name) : name;
}
function isStrictlyObject(it) {
return (it && Object.prototype.toString.call(it) == '[object Object]');
}
return {
runtime: runtime_deps,
build: build_deps
};
}
}
function writeSpec(wireId, resourceId, write) {
var specIds, spec;
specIds = resourceId.split(',');
if (specIds.length > 1) {
return;
}
spec = specCache[resourceId];
if (!spec) {
throw new Error('Spec of "' + resourceId + '" was not previously processed');
}
getFile(spec.path, function (contents) {
write.asModule(resourceId, injectIds(contents, resourceId, spec.dependencies));
} );
}
function ensureExtension(id, ext) {
return id.lastIndexOf('.' + ext) <= id.lastIndexOf('/')
? id + '.' + ext
: id;
}
function injectIds (moduleText, absId, moduleIds) {
// note: replaceIdsRegex removes commas, parens, and brackets
return moduleText.replace(removeCommentsRx, '').replace(replaceIdsRegex, function (m, def, mid, depIds) {
// merge deps, but not args since they're not referenced in module
if (depIds) {
moduleIds = moduleIds.concat(depIds);
}
moduleIds = moduleIds.map(quoted).join(', ');
if (moduleIds) {
moduleIds = '[' + moduleIds + '], ';
}
return def + '(' + quoted(absId) + ', ' + moduleIds;
});
}
function quoted (id) {
return '\'' + id + '\'';
}
function getFile (url, cb) {
var file = fs.readFileSync(url, 'utf8');
//Remove BOM (Byte Mark Order) from utf8 files if it is there.
if (file.indexOf('\uFEFF') === 0) {
file = file.substring(1);
}
cb(file);
}
});
}(typeof define === 'function' ? define : function(factory) { module.exports = factory(); }));