* Given a relative module name, like ./something, normalize it to * a real name that can be mapped to a path. * @param {String} name the relative name * @param {String} baseName a real name that the name arg is relative * to. * @param {Boolean} applyMap a
(name, baseName, applyMap)
| 257 | * @returns {String} normalized name |
| 258 | */ |
| 259 | function normalize(name, baseName, applyMap) { |
| 260 | var pkgName, pkgConfig, mapValue, nameParts, i, j, nameSegment, |
| 261 | foundMap, foundI, foundStarMap, starI, |
| 262 | baseParts = baseName && baseName.split('/'), |
| 263 | normalizedBaseParts = baseParts, |
| 264 | map = config.map, |
| 265 | starMap = map && map['*']; |
| 266 | |
| 267 | //Adjust any relative paths. |
| 268 | if (name && name.charAt(0) === '.') { |
| 269 | //If have a base name, try to normalize against it, |
| 270 | //otherwise, assume it is a top-level require that will |
| 271 | //be relative to baseUrl in the end. |
| 272 | if (baseName) { |
| 273 | if (getOwn(config.pkgs, baseName)) { |
| 274 | //If the baseName is a package name, then just treat it as one |
| 275 | //name to concat the name with. |
| 276 | normalizedBaseParts = baseParts = [baseName]; |
| 277 | } else { |
| 278 | //Convert baseName to array, and lop off the last part, |
| 279 | //so that . matches that 'directory' and not name of the baseName's |
| 280 | //module. For instance, baseName of 'one/two/three', maps to |
| 281 | //'one/two/three.js', but we want the directory, 'one/two' for |
| 282 | //this normalization. |
| 283 | normalizedBaseParts = baseParts.slice(0, baseParts.length - 1); |
| 284 | } |
| 285 | |
| 286 | name = normalizedBaseParts.concat(name.split('/')); |
| 287 | trimDots(name); |
| 288 | |
| 289 | //Some use of packages may use a . path to reference the |
| 290 | //'main' module name, so normalize for that. |
| 291 | pkgConfig = getOwn(config.pkgs, (pkgName = name[0])); |
| 292 | name = name.join('/'); |
| 293 | if (pkgConfig && name === pkgName + '/' + pkgConfig.main) { |
| 294 | name = pkgName; |
| 295 | } |
| 296 | } else if (name.indexOf('./') === 0) { |
| 297 | // No baseName, so this is ID is resolved relative |
| 298 | // to baseUrl, pull off the leading dot. |
| 299 | name = name.substring(2); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | //Apply map config if available. |
| 304 | if (applyMap && map && (baseParts || starMap)) { |
| 305 | nameParts = name.split('/'); |
| 306 | |
| 307 | for (i = nameParts.length; i > 0; i -= 1) { |
| 308 | nameSegment = nameParts.slice(0, i).join('/'); |
| 309 | |
| 310 | if (baseParts) { |
| 311 | //Find the longest baseName segment match in the config. |
| 312 | //So, do joins on the biggest to smallest lengths of baseParts. |
| 313 | for (j = baseParts.length; j > 0; j -= 1) { |
| 314 | mapValue = getOwn(map, baseParts.slice(0, j).join('/')); |
| 315 | |
| 316 | //baseName segment has config, find if it has one for |
no test coverage detected