forked from seajs/seajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil-path.js
269 lines (207 loc) · 5.33 KB
/
util-path.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Path utilities
*/
;(function(util, config, global) {
var DIRNAME_RE = /[^?]*(?=\/.*$)/
var MULTIPLE_SLASH_RE = /([^:\/])\/\/+/g
var FILE_EXT_RE = /\.(?:css|js)$/
var ROOT_RE = /^(.*?\w)(?:\/|$)/
var VARS_RE = /\{\{([^{}]+)\}\}/g
/**
* Extracts the directory portion of a path.
* dirname('a/b/c.js') ==> 'a/b/'
* dirname('d.js') ==> './'
* @see http://jsperf.com/regex-vs-split/2
*/
function dirname(path) {
var s = path.match(DIRNAME_RE)
return (s ? s[0] : '.') + '/'
}
/**
* Canonicalizes a path.
* realpath('./a//b/../c') ==> 'a/c'
*/
function realpath(path) {
MULTIPLE_SLASH_RE.lastIndex = 0
// 'file:///a//b/c' ==> 'file:///a/b/c'
// 'http://a//b/c' ==> 'http://a/b/c'
if (MULTIPLE_SLASH_RE.test(path)) {
path = path.replace(MULTIPLE_SLASH_RE, '$1\/')
}
// 'a/b/c', just return.
if (path.indexOf('.') === -1) {
return path
}
var original = path.split('/')
var ret = [], part
for (var i = 0; i < original.length; i++) {
part = original[i]
if (part === '..') {
if (ret.length === 0) {
throw new Error('The path is invalid: ' + path)
}
ret.pop()
}
else if (part !== '.') {
ret.push(part)
}
}
return ret.join('/')
}
/**
* Normalizes an uri.
*/
function normalize(uri) {
uri = realpath(uri)
var lastChar = uri.charAt(uri.length - 1)
if (lastChar === '/') {
return uri
}
// Adds the default '.js' extension except that the uri ends with #.
// ref: http://jsperf.com/get-the-last-character
if (lastChar === '#') {
uri = uri.slice(0, -1)
}
else if (uri.indexOf('?') === -1 && !FILE_EXT_RE.test(uri)) {
uri += '.js'
}
// Remove ':80/' for bug in IE
if (uri.indexOf(':80/') > 0) {
uri = uri.replace(':80/', '/')
}
return uri
}
/**
* Parses {{xxx}} in the module id.
*/
function parseVars(id) {
if (id.indexOf('{') === -1) {
return id
}
var vars = config.vars
return id.replace(VARS_RE, function(m, key) {
return vars.hasOwnProperty(key) ? vars[key] : ''
})
}
/**
* Parses alias in the module id. Only parse the first part.
*/
function parseAlias(id) {
// #xxx means xxx is already alias-parsed.
if (id.charAt(0) === '#') {
return id.substring(1)
}
var alias = config.alias
// Only top-level id needs to parse alias.
if (alias && isTopLevel(id)) {
var parts = id.split('/')
var first = parts[0]
if (alias.hasOwnProperty(first)) {
parts[0] = alias[first]
id = parts.join('/')
}
}
return id
}
/**
* Converts the uri according to the map rules.
*/
function parseMap(uri) {
// map: [[match, replace], ...]
var map = config.map || []
if (!map.length) return uri
var ret = uri
// Apply all matched rules in sequence.
for (var i = 0; i < map.length; i++) {
var rule = map[i]
if (util.isArray(rule) && rule.length === 2) {
var m = rule[0]
if (util.isString(m) && ret.indexOf(m) > -1 ||
util.isRegExp(m) && m.test(ret)) {
ret = ret.replace(m, rule[1])
}
}
else if (util.isFunction(rule)) {
ret = rule(ret)
}
}
if (!isAbsolute(ret)) {
ret = realpath(dirname(pageUri) + ret)
}
if (ret !== uri) {
//mapCache[ret] = uri
}
return ret
}
/**
* Converts id to uri.
*/
function id2Uri(id, refUri) {
if (!id) return ''
id = parseAlias(parseVars(id))
refUri || (refUri = pageUri)
var ret
// absolute id
if (isAbsolute(id)) {
ret = id
}
// relative id
else if (isRelative(id)) {
// Converts './a' to 'a', to avoid unnecessary loop in realpath.
if (id.indexOf('./') === 0) {
id = id.substring(2)
}
ret = dirname(refUri) + id
}
// root id
else if (isRoot(id)) {
ret = refUri.match(ROOT_RE)[1] + id
}
// top-level id
else {
ret = config.base + '/' + id
}
return parseMap(normalize(ret))
}
function isAbsolute(id) {
return id.indexOf('://') > 0 || id.indexOf('//') === 0
}
function isRelative(id) {
return id.indexOf('./') === 0 || id.indexOf('../') === 0
}
function isRoot(id) {
return id.charAt(0) === '/' && id.charAt(1) !== '/'
}
function isTopLevel(id) {
var c = id.charAt(0)
return id.indexOf('://') === -1 && c !== '.' && c !== '/'
}
/**
* Normalizes pathname to start with '/'
* Ref: https://groups.google.com/forum/#!topic/seajs/9R29Inqk1UU
*/
function normalizePathname(pathname) {
if (pathname.charAt(0) !== '/') {
pathname = '/' + pathname
}
return pathname
}
var loc = global['location']
var pageUri = loc.protocol + '//' + loc.host +
normalizePathname(loc.pathname)
// local file in IE: C:\path\to\xx.js
if (pageUri.indexOf('\\') > 0) {
pageUri = pageUri.replace(/\\/g, '/')
}
util.dirname = dirname
util.realpath = realpath
util.normalize = normalize
util.parseVars = parseVars
util.parseAlias = parseAlias
util.parseMap = parseMap
util.id2Uri = id2Uri
util.isAbsolute = isAbsolute
util.isRoot = isRoot
util.isTopLevel = isTopLevel
util.pageUri = pageUri
})(seajs._util, seajs._config, this)