forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamdify.js
More file actions
140 lines (134 loc) · 4.59 KB
/
amdify.js
File metadata and controls
140 lines (134 loc) · 4.59 KB
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
steal('steal/build', 'steal/build/pluginify', function(steal) {
var contents = {},
modules = {},
inexcludes = function(excludes, src) {
for(var i = 0; i < excludes.length; i++) {
if(src.indexOf(excludes[i]) !== -1) {
return true;
}
}
return false;
},
/**
* Creates a variable name from a filename or AMD module name.
*
* @param {String} name The name of the AMD module or file
* @return {String} The variable name
*/
variableName = function(name) {
var start = name.lastIndexOf('/') + 1,
end = name.lastIndexOf('.') !== -1 ? name.lastIndexOf('.') : name.length;
return '__' + name.substring(start, end).replace(/\./g, '_');
},
/**
* Returns a steal.File instance from a filename or AMD module name.
*
* @param name
* @param suffix
* @return {*}
*/
getFile = function(name, suffix) {
var suffix = suffix || '.js', file = name;
if(name.indexOf(suffix, name.length - suffix.length) === -1) {
file = file + suffix;
}
return steal.File(file);
},
/**
* Returns a list of steal dependencies for a given file and caches
* the plain content.
*
* @param {String} file The JavaScript file to load
* @param {Array} excludes A list of dependencies to exclude
* @param {Object} options Options
* @param {Function} callback A callback getting passed an array
* of steals
*/
getDependencies = function(file, excludes, options, callback) {
steal.build.open("steal/rhino/empty.html", {
startFile : file,
skipCallbacks: true
}, function(opener){
var ret = [];
opener.each(function(stl, text){
if(!inexcludes(excludes || [], stl.rootSrc)) {
// Add the parsed content to cache
if(!contents[stl.rootSrc]) {
contents[stl.rootSrc] = steal.build.pluginify.content(stl, options, text);
}
ret.push(stl);
}
});
callback(ret);
}, null);
},
/**
* Creates the actual module recursively
*
* @param {String} name The name of the main module file
* @param {Array} excludes A list of files to exclude
* @param {Object} options The options to use
*/
createModule = function(name, excludes, options) {
getDependencies(name, excludes, options, function(steals) {
var content,
dependencies = [],
names = [],
nameMap = options.names || {},
map = options.map || {},
where = getFile(options.out + (map[name] || name));
print(' > ' + name + ' -> ' + (map[name] || name));
steals.forEach(function(stl) {
var current = (map[stl.rootSrc] || stl.rootSrc);
if(stl.rootSrc !== name) { // Don't include the current file
if(!modules[stl.rootSrc]) {
createModule(stl.rootSrc, excludes, options);
}
dependencies.push("'" + current + "'");
names.push(nameMap[current] || variableName(current));
}
});
content = "define([" +
dependencies.join(',') +
'], function(' +
names.join(', ') +
') { \n' +
(contents[name] || (' return ' + (options.global || '{}'))) +
';\n})';
modules[name] = content;
steal.File(where.dir()).mkdirs();
where.save(content);
});
};
/**
* Creates a set of AMD modules recursively. The `map` options contain a mapping from Steal
* rootSrc filenames to AMD module names. For examples:
*
* { "jquery/dom/compare/compare.js" : "jquerypp/compare" }
*
* Will map "jquery/dom/compare/compare.js" to "jquerypp/compare.js" in the output folder
* and all dependencies as well (e.g. dependent files would `define(['jquery/compare'], ...)`.
* By default it will use the Steal rootSrc name.
* The `names` mapping can be used to map AMD module names to variable names passed to the
* pluginified function. By default this will be the filename without extension, `__` prefixed and
* `.` converted to `_` (looking like `define(['jquery/compare`], function(__compare) { ... })`).
*
* @param {String} source The root JavaScript source file name to generate the modules from.
* @param {Object} options The options for generating AMD modules.
* The following options will be used:
*
* - `out` - The output folder
* - `excludes` - An array of files to exclude (must be the full Steal rootSrc)
* - `map` - A mapping from full Steal rootSrc filenames to the AMD module name.
* Any missing folders will be created automatically.
* - `names` - A mapping from AMD module names (as set in `map` or the default)
* to parameter variable names.
* - `global` - The global option passed to pluginify
*/
steal.build.amdify = function(source, options) {
var out = options.out;
print('Creating AMD modules for ' + source + " in " + options.out);
steal.File(out).mkdirs();
createModule(source, options.exclude || {}, options);
}
});