* 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)
| 267 | * @returns {String} normalized name |
| 268 | */ |
| 269 | function normalize(name, baseName, applyMap) { |
| 270 | var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex, |
| 271 | foundMap, foundI, foundStarMap, starI, normalizedBaseParts, |
| 272 | baseParts = (baseName && baseName.split('/')), |
| 273 | map = config.map, |
| 274 | starMap = map && map['*']; |
| 275 | |
| 276 | //Adjust any relative paths. |
| 277 | if (name) { |
| 278 | name = name.split('/'); |
| 279 | lastIndex = name.length - 1; |
| 280 | |
| 281 | // If wanting node ID compatibility, strip .js from end |
| 282 | // of IDs. Have to do this here, and not in nameToUrl |
| 283 | // because node allows either .js or non .js to map |
| 284 | // to same file. |
| 285 | if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) { |
| 286 | name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, ''); |
| 287 | } |
| 288 | |
| 289 | // Starts with a '.' so need the baseName |
| 290 | if (name[0].charAt(0) === '.' && baseParts) { |
| 291 | //Convert baseName to array, and lop off the last part, |
| 292 | //so that . matches that 'directory' and not name of the baseName's |
| 293 | //module. For instance, baseName of 'one/two/three', maps to |
| 294 | //'one/two/three.js', but we want the directory, 'one/two' for |
| 295 | //this normalization. |
| 296 | normalizedBaseParts = baseParts.slice(0, baseParts.length - 1); |
| 297 | name = normalizedBaseParts.concat(name); |
| 298 | } |
| 299 | |
| 300 | trimDots(name); |
| 301 | name = name.join('/'); |
| 302 | } |
| 303 | |
| 304 | //Apply map config if available. |
| 305 | if (applyMap && map && (baseParts || starMap)) { |
| 306 | nameParts = name.split('/'); |
| 307 | |
| 308 | outerLoop: for (i = nameParts.length; i > 0; i -= 1) { |
| 309 | nameSegment = nameParts.slice(0, i).join('/'); |
| 310 | |
| 311 | if (baseParts) { |
| 312 | //Find the longest baseName segment match in the config. |
| 313 | //So, do joins on the biggest to smallest lengths of baseParts. |
| 314 | for (j = baseParts.length; j > 0; j -= 1) { |
| 315 | mapValue = getOwn(map, baseParts.slice(0, j).join('/')); |
| 316 | |
| 317 | //baseName segment has config, find if it has one for |
| 318 | //this name. |
| 319 | if (mapValue) { |
| 320 | mapValue = getOwn(mapValue, nameSegment); |
| 321 | if (mapValue) { |
| 322 | //Match, update name to the new value. |
| 323 | foundMap = mapValue; |
| 324 | foundI = i; |
| 325 | break outerLoop; |
| 326 | } |
no test coverage detected