forked from stealjs/steal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system-format-steal.js
169 lines (139 loc) · 4.64 KB
/
system-format-steal.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
(function(){
var filename = function(uri){
var lastSlash = uri.lastIndexOf("/"),
matches = ( lastSlash == -1 ? uri : uri.substr(lastSlash+1) ).match(/^[\w-\s\.]+/);
return matches ? matches[0] : "";
};
var ext = function(uri){
var fn = filename(uri);
var dot = fn.lastIndexOf(".");
if(dot !== -1) {
return fn.substr(dot+1);
} else {
return "";
}
};
var pluginCache = {};
var normalize = function(name, loader){
// Detech if this name contains a plugin part like: app.less!steal/less
// and catch the plugin name so that when it is normalized we do not perform
// Steal's normalization against it.
var pluginIndex = name.lastIndexOf('!');
var pluginPart = "";
if (pluginIndex != -1) {
// argumentName is the part before the !
var argumentName = name.substr(0, pluginIndex);
var pluginName = name.substr(pluginIndex + 1) || argumentName.substr(argumentName.lastIndexOf('.') + 1);
pluginPart = "!" + pluginName;
pluginCache[pluginName] = true;
// Set the name to the argument name so that we can normalize it alone.
name = argumentName;
} else if(pluginCache[name]) {
// This is a plugin so just return the name unnormalized.
return name;
}
var last = filename(name),
extension = ext(name);
// if the name ends with /
if( name[name.length -1] === "/" ) {
return name+filename( name.substr(0, name.length-1) ) + pluginPart;
} else if( !/^(\w+(?:s)?:\/\/|\.|file|\/)/.test(name) &&
// and doesn't end with a dot
last.indexOf(".") === -1
) {
return name+"/"+last + pluginPart;
} else {
if(extension === "js") {
return name.substr(0, name.lastIndexOf(".")) + pluginPart;
} else {
return name + pluginPart;
}
}
};
// AMD Module Format Detection RegEx
// define([.., .., ..], ...)
// define(varName); || define(function(require, exports) {}); || define({})
var stealRegEx = /(?:^\s*|[}{\(\);,\n\?\&]\s*)steal\s*\(\s*((?:"[^"]+"\s*,|'[^']+'\s*,\s*)*)/;
function prepareDeps(deps, meta) {
// remove duplicates
for (var i = 0; i < deps.length; i++)
if ([].lastIndexOf.call(deps, deps[i]) != i)
deps.splice(i--, 1);
return deps;
};
var addFormat = function(loader){
function makeRequire(parentName, deps, depsNormalized) {
return function(names, callback, errback) {
if (typeof names == 'string' && indexOf.call(deps, names) != -1)
return loader.getModule(depsNormalized[indexOf.call(deps, names)]);
return require(names, callback, errback, { name: parentName });
};
};
function prepareExecute(depNames, load) {
var meta = load.metadata;
var deps = [];
for (var i = 0; i < depNames.length; i++) {
var module = loader.get(depNames[i]);
if (module.__useDefault) {
module = module['default'];
}
else if (!module.__esModule) {
// compatibility -> ES6 modules must have a __esModule flag
// we clone the module object to handle this
var moduleClone = { __esModule: true };
for (var p in module)
moduleClone[p] = module[p];
module = moduleClone;
}
deps[i] = module;
}
var module, exports;
return {
deps: deps,
module: module || exports && { exports: exports }
};
}
loader.formats.unshift('steal');
loader.format.steal = {
detect: function(load) {
return !!load.source.match(stealRegEx);
},
deps: function(load) {
var global = loader.global;
var deps = [];
var meta = load.metadata;
var oldSteal = global.steal;
global.steal = function(){
for( var i = 0; i < arguments.length; i++ ) {
if (typeof arguments[i] == 'string') {
deps.push( arguments[i] );
} else {
meta.factory = arguments[i];
}
}
};
loader.__exec(load);
global.steal = oldSteal;
// deps not defined for an AMD module that defines a different name
deps = deps || [];
deps = prepareDeps(deps, meta);
global.define = undefined;
meta.deps = deps;
return deps;
},
execute: function(depNames, load ) {
if (!load.metadata.factory)
return;
var execs = prepareExecute(depNames, load);
return load.metadata.factory.apply(loader.global, execs.deps) || execs.module && execs.module.exports;
},
normalize: function(name, refererName, refererAddress, baseNormalize){
return baseNormalize(normalize(name, this), refererName, refererAddress);
}
};
return loader;
};
if(typeof System !== "undefined") {
addFormat(System);
}
})();