forked from kissyteam/kissy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.js
More file actions
200 lines (182 loc) · 5.65 KB
/
Copy pathpatch.js
File metadata and controls
200 lines (182 loc) · 5.65 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
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
/**
* patch for nodejs
* @author yiminghe@gmail.com
* @require jsdom (npm install jsdom) ,path
* @description emulate browser environment and rewrite loader
*/
/**
* emulate browser and exports kissy
*/
(function () {
var jsdom = require("jsdom").jsdom;
document = jsdom("<html><head></head><body></body></html>");
window = document.createWindow();
location = window.location;
navigator = window.navigator;
window.document = document;
/**
* note : this === exports !== global
*/
KISSY = exports.KISSY = window.KISSY = exports;
KISSY.Env = {
host:window
};
})();
/**
* rewrite loader
*/
(function (S) {
var path = require("path");
function mix(d, s) {
for (var i in s) {
if (s.hasOwnProperty(i)) {
d[i] = s[i];
}
}
}
mix(S, {
Env:{
mods:{
}
},
configs:{
packages:function (cfgs) {
var ps = S.__packages = S.__packages || {};
for (var i = 0; i < cfgs.length; i++) {
var cfg = cfgs[i], p;
ps[cfg.name] = cfg;
if ((p = cfg.path) && !p.match(/\/$/)) {
p += "/";
}
cfg.path = p;
}
}
}
});
var mods = S.Env.mods;
mix(S, {
Config:{
base:__dirname.replace(/\\/g, "/") + "/"
},
add:function (name, def, cfg) {
if (S.isFunction(name)) {
cfg = def;
def = name;
name = this.currentModName;
}
mods[name] = {
name:name,
fn:def
};
S.mix(mods[name], cfg);
},
_getPath:function (modName) {
this.__packages = this.__packages || {};
var packages = this.__packages;
var pName = "";
for (var p in packages) {
if (packages.hasOwnProperty(p)) {
if (startsWith(modName, p)) {
if (p.length > pName) {
pName = p;
}
}
}
}
var base = (packages[pName] && packages[pName].path) || this.Config.base;
return base + modName;
},
require:function (moduleName) {
var mod = mods[moduleName];
var re = S['onRequire'] && S['onRequire'](mod);
if (re !== undefined) return re;
return mod && mod.value;
},
_attach:function (modName) {
var modPath = this._getPath(modName);
var mod = mods[modName];
if (!mod) {
this.currentModName = modName;
if (endsWith(modPath, ".css")) {
var link = document.createElement("link");
link.href = modPath;
link.rel = 'stylesheet';
document.head.appendChild(link);
mods[modName] = {
attached:1
};
} else {
require(modPath);
}
}
mod = mods[modName];
if (mod.attached) return;
mod.requires = mod.requires || [];
var requires = mod.requires;
normalDepModuleName(modName, requires);
var deps = [this];
for (var i = 0; i < requires.length; i++) {
this._attach(requires[i]);
deps.push(this.require(requires[i]));
}
mod.value = mod.fn.apply(null, deps);
mod.attached = true;
},
use:function (modNames, callback) {
modNames = modNames.replace(/\s+/g, "").split(',');
indexMapping(modNames);
var self = this;
var deps = [this];
S.each(modNames, function (modName) {
self._attach(modName);
deps.push(self.require(modName));
});
callback && callback.apply(null, deps);
}
});
//http://wiki.commonjs.org/wiki/Packages/Mappings/A
//如果模块名以 / 结尾,自动加 index
function indexMapping(names) {
for (var i = 0; i < names.length; i++) {
if (names[i].match(/\/$/)) {
names[i] += "index";
}
}
return names;
}
function startsWith(str, prefix) {
return str.lastIndexOf(prefix, 0) == 0;
}
function endsWith(str, suffix) {
var ind = str.length - suffix.length;
return ind >= 0 && str.indexOf(suffix, ind) == ind;
}
function normalDepModuleName(moduleName, depName) {
if (!depName) {
return depName;
}
if (S.isArray(depName)) {
for (var i = 0; i < depName.length; i++) {
depName[i] = normalDepModuleName(moduleName, depName[i]);
}
return depName;
}
var ret;
if (startsWith(depName, "../") || startsWith(depName, "./")) {
var anchor = moduleName.replace(/[^/]*$/, "");
if (!endsWith(anchor, "/")) {
anchor += "/";
}
// x/y/z -> x/y/
// note in window:
// path.normalize("x/./y") == "x\\y\\"
ret = path.normalize(anchor + depName);
} else if (depName.indexOf("./") != -1
|| depName.indexOf("../") != -1) {
ret = path.normalize(depName);
} else {
ret = depName;
}
return ret.replace(/\\/g, "/");
}
})(KISSY);