amd-name-resolver
Advanced tools
Comparing version 1.0.0 to 1.2.0
87
index.js
@@ -1,6 +0,15 @@ | ||
var throwOnRootAccess = true; | ||
var ensurePosix = require('ensure-posix-path'); | ||
/** | ||
* Configures the module resolver. Empty options will reset to default settings | ||
* @typedef {Object} Options | ||
* @property {Bool} throwOnRootAccess whether to throw on | ||
* @property {String} moduleRoot to use for each of the resolved modules | ||
* @param {Options} options | ||
* @returns {Function} configured module resolver | ||
*/ | ||
function resolveModules(options) { | ||
options = options || {}; | ||
options || (options = {}); | ||
var throwOnRootAccess = true; | ||
var moduleRoot; | ||
@@ -11,40 +20,62 @@ if (options.throwOnRootAccess === false) { | ||
return moduleResolve; | ||
} | ||
if (typeof options.moduleRoot === 'string') { | ||
moduleRoot = options.moduleRoot; | ||
} | ||
function moduleResolve(_child, _name) { | ||
var child = ensurePosix(_child); | ||
var name = ensurePosix(_name); | ||
if (child.charAt(0) !== '.') { return child; } | ||
/** | ||
* Module resolver for AMD transpiling (Babel) | ||
* @param {String} _child child path of module to resolve | ||
* @param {String} _name name path of module to resolve | ||
* @returns {String} resolved module path | ||
*/ | ||
function moduleResolve(_child, _name) { | ||
var child = ensurePosix(_child); | ||
var name = ensurePosix(_name); | ||
if (child.charAt(0) !== '.') { | ||
return child; | ||
} | ||
var parts = child.split('/'); | ||
var nameParts = name.split('/'); | ||
var parentBase = nameParts.slice(0, -1); | ||
var parts = child.split('/'); | ||
var nameParts = name.split('/'); | ||
var parentBase = nameParts.slice(0, -1); | ||
// Add moduleRoot if not already present | ||
if (moduleRoot && nameParts[0] !== moduleRoot) { | ||
parentBase = moduleRoot.split('/').concat(parentBase); | ||
} | ||
for (var i = 0, l = parts.length; i < l; i++) { | ||
var part = parts[i]; | ||
for (var i = 0, l = parts.length; i < l; i++) { | ||
var part = parts[i]; | ||
if (part === '..') { | ||
if (parentBase.length === 0) { | ||
if (throwOnRootAccess) { | ||
throw new Error('Cannot access parent module of root'); | ||
} else { | ||
continue; | ||
if (part === '..') { | ||
if (parentBase.length === 0) { | ||
if (throwOnRootAccess) { | ||
throw new Error('Cannot access parent module of root'); | ||
} else { | ||
continue; | ||
} | ||
} | ||
parentBase.pop(); | ||
} else if (part === '.') { | ||
continue; | ||
} else { | ||
parentBase.push(part); | ||
} | ||
parentBase.pop(); | ||
} else if (part === '.') { | ||
continue; | ||
} else { parentBase.push(part); } | ||
} | ||
return parentBase.join('/'); | ||
} | ||
return parentBase.join('/'); | ||
// parallel API - enable parallel babel transpilation for the moduleResolve function | ||
moduleResolve._parallelBabel = { | ||
requireFile: __filename, | ||
buildUsing: 'resolveModules', | ||
params: options | ||
}; | ||
return moduleResolve; | ||
} | ||
// parallel API - enable parallel babel transpilation for the moduleResolve function | ||
moduleResolve._parallelBabel = { requireFile: __dirname, useMethod: 'moduleResolve' }; | ||
module.exports = { | ||
moduleResolve: moduleResolve, | ||
moduleResolve: resolveModules(), | ||
resolveModules: resolveModules | ||
}; |
{ | ||
"name": "amd-name-resolver", | ||
"version": "1.0.0", | ||
"version": "1.2.0", | ||
"description": "AMD module name resolver algorithm", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2753
69
1