@antora/user-require-helper
Advanced tools
Comparing version 1.0.0 to 2.0.0
'use strict' | ||
const expandPath = require('@antora/expand-path-helper') | ||
const { extname, isAbsolute, sep } = require('path') | ||
const findIndexOfAnchorSep = | ||
sep === '\\' | ||
? ((rx, noMatch) => (str) => (str.match(rx) || noMatch).index)(/:(?![/\\])/, { index: -1 }) | ||
: (str) => str.indexOf(':') | ||
const isPath = | ||
sep === '\\' | ||
? (str) => !(Math.floor(str.indexOf('./') / 2) && Math.floor(str.indexOf('.\\') / 2)) || isAbsolute(str) | ||
: (str) => !Math.floor(str.indexOf('./') / 2) || str.charAt() === '/' | ||
const [indexOfAnchorSep, isPath, { extname, sep }] = ((path) => [ | ||
path === path.posix | ||
? (str) => str.indexOf(':') | ||
: ((rx, noMatch) => (str) => (str.match(rx) || noMatch).index)(/:(?![/\\])/, { index: -1 }), | ||
path === path.posix | ||
? (str) => !Math.floor(str.indexOf('./') / 2) || str.charAt() === '/' | ||
: (str) => !(Math.floor(str.indexOf('./') / 2) && Math.floor(str.indexOf('.\\') / 2)) || path.isAbsolute(str), | ||
path, | ||
])(require('path')) | ||
@@ -28,5 +28,8 @@ /** | ||
* @param {String} request - The module or path to require. Must be a non-empty string. | ||
* @param {Object} [context={}] | ||
* @param {String} [context.base='~+'] - The path from which to resolve a relative module name or path. | ||
* @param {String} [context.dot='.'] - The path from which to resolve a dot relative module name or path. | ||
* @param {Object} [context={}] - Named parameters that control how the request is resolved. | ||
* @param {String} [context.base='~+'] - The base directory from which to resolve a relative module | ||
* name or path. | ||
* @param {String} [context.cwd=process.cwd()] - The absolute path to use as the working directory. | ||
* @param {String} [context.dot='.'] - The directory to use in place of a leading dot segment in the | ||
* module name or path. | ||
* @param {Array<String>} [context.path=[]] - The paths to pass to `require.resolve` when a bare | ||
@@ -39,4 +42,4 @@ * module name is specified. If this value is empty, no paths are passed to `require.resolve`. | ||
*/ | ||
function userRequire (request, args) { | ||
return require(userResolve(request, args)) | ||
const userRequire = (request, context) => { | ||
return require(userResolve(request, context)) | ||
} | ||
@@ -47,7 +50,7 @@ | ||
* | ||
* See userRequire for details. | ||
* Refer to userRequire for the list of parameters. | ||
* | ||
* @returns {String} The absolute path resolved from the module name or path request. | ||
*/ | ||
const userResolve = (userRequire.resolve = (request, { base, dot, paths = [], resolve = require.resolve } = {}) => { | ||
userRequire.resolve = (request, { base, cwd, dot, paths = [], resolve = require.resolve } = {}) => { | ||
if (typeof request !== 'string') { | ||
@@ -57,5 +60,5 @@ throw new TypeError(`The "request" argument must be of type string. Received type ${typeof request}`) | ||
if (!request) return resolve(request) | ||
if (extname(request + '_')) return resolve(expandPath(request, base, dot)) | ||
const anchorSepIdx = findIndexOfAnchorSep(request) | ||
if (!~anchorSepIdx && isPath(request)) return resolve(expandPath(request, base, dot)) | ||
if (extname(request + '_')) return resolve(expandPath(request, { base, cwd, dot })) | ||
const anchorSepIdx = indexOfAnchorSep(request) | ||
if (!~anchorSepIdx && isPath(request)) return resolve(expandPath(request, { base, cwd, dot })) | ||
const lastChar = request.charAt(request.length - 1) | ||
@@ -68,6 +71,6 @@ if (lastChar === '/' || lastChar === sep) request = request.substr(0, request.length - 1) | ||
case '': | ||
paths = [expandPath('.', undefined, base)] // FIXME ideally this would be expandPath('', base) | ||
paths = [expandPath('', { base })] | ||
break | ||
case '.': | ||
paths = [expandPath('.', undefined, dot)] | ||
paths = [expandPath('.', { dot })] | ||
break | ||
@@ -78,3 +81,3 @@ case '^': | ||
default: | ||
paths = [expandPath(anchor, base, dot)] | ||
paths = [expandPath(anchor, { base, cwd, dot })] | ||
} | ||
@@ -89,4 +92,6 @@ } | ||
} | ||
}) | ||
} | ||
const userResolve = userRequire.resolve | ||
module.exports = userRequire |
{ | ||
"name": "@antora/user-require-helper", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A helper function to resolve a module name or path in relation to the specified context and require it.", | ||
@@ -22,3 +22,3 @@ "license": "MPL-2.0", | ||
"dependencies": { | ||
"@antora/expand-path-helper": "~1.0" | ||
"@antora/expand-path-helper": "~2.0" | ||
}, | ||
@@ -32,3 +32,3 @@ "devDependencies": { | ||
"eslint-plugin-node": "~11.1", | ||
"eslint-plugin-promise": "~4.3", | ||
"eslint-plugin-promise": "~5.1", | ||
"mocha": "~8.4", | ||
@@ -35,0 +35,0 @@ "nyc": "~15.1", |
# @antora/user-require-helper | ||
Resolve the module name or path in relation to the specified context and require it (using `userRequire`) or return it (using `userRequire.resolve`). | ||
A Node.js module that provides helper functions to resolve the module name or path in relation to the specified context and require it (using `userRequire`) or return it (using `userRequire.resolve`). | ||
The expanded path is system dependent. | ||
Developed for use in Antora. | ||
@@ -10,14 +12,14 @@ The purpose of this function is to require a JavaScript module (specified as a module name or path request) from the user's environment. | ||
This behavior can be altered using `require.resolve`, which is precisely the functionality this helper encapsulates. | ||
The described behavior can be altered using `require.resolve`, which is precisely the functionality this helper encapsulates. | ||
Building on the behavior of `require.resolve`, this function honor prefixes recognized by the *@antora/expand-path-helper* module to anchor the location of the requested module name or path to the resolved context specified in the request. | ||
See *Usage* for examples. | ||
For example, the `.:` or `./` prefix will look for a module name or path, respectively, relative to the specified dot argument. | ||
(See Usage for examples). | ||
If a context is not specified, and the request argument is a module name, this function will use the function specified by the resolve argument (which defaults to the built-in `require.resolve` function) along with the paths argument, if specified. | ||
In other words, if a bare module name is given, the helper resolves the module relative to the specified paths, if specified, otherwise to the environment of the calling script (thus ignoring the start and dot arguments). | ||
In other words, if a bare module name is given, the helper resolves the module relative to the specified paths, if specified, otherwise to the environment of the calling script (thus ignoring the base and dot arguments). | ||
The function inspects the request argument to differentiate between a module name and a path. | ||
If the request argument has a file extension, it is resolved as a path. | ||
Additionally, if the request argument begins with a dot (`./`) or dot dot (`../`) segment, and it does not contain a colon (`:`), it is resolved as a path, _even if it doesn't have a file extension_. | ||
Otherwise, it is resolved as a module name. | ||
If the request argument has a file extension, it's resolved as a path. | ||
Additionally, if the request argument begins with a dot (`./`) or dot dot (`../`) segment, and it does not contain a colon (`:`), it's resolved as a path, _even if it doesn't have a file extension_. | ||
Otherwise, it's resolved as a module name. | ||
@@ -41,18 +43,20 @@ TIP: If the request is for a module name that contains a file extension (e.g., `highlight.js`), append `/` to force it to be treated as a module name (e.g., `highlight.js/`). | ||
```js | ||
function userRequire (id, { base, dot, paths = [], resolve = require.resolve } = {}) | ||
function userRequire (id, { base, cwd, dot, paths = [], resolve = require.resolve } = {}) | ||
``` | ||
Resolve the module name or path using `userRequire.resolve` and require it. | ||
Resolves the module name or path using `userRequire.resolve` and requires it. | ||
If the resolved module name or path cannot be found on the local filesystem, a `MODULE_NOT_FOUND` error is thrown. | ||
* `request` `<string>` - The module name or path to resolve. | ||
This argument is required and must be a non-empty string. | ||
* `context` `<Object>` - Options that control how the request is resolved. | ||
All options are optional. | ||
* `context.base` `<string>` - The path from which to resolve a relative module name or path. | ||
This parameter is required and must be a non-empty string. | ||
* `context` `<Object>` - Named parameters that control how the request is resolved. | ||
All named parameters are optional. | ||
* `context.base` `<string>` - The base directory from which to resolve a relative module name or path. | ||
Passed on to `expandPath`, which assigns the default value `~+`. | ||
* `context.dot` `<string>` - The path from which to resolve a dot relative module name or path. | ||
* `context.cwd` `<string>` - The absolute directory to use as the working directory instead of the current working directory. | ||
Passed on to `expandPath`, which assigns the current working directory as the default value. | ||
* `context.dot` `<string>` - The directory to use in place of a leading dot (`.`) segment in the module name or path. | ||
Passed on to `expandPath`, which assigns the default value `.`. | ||
* `context.paths` `<string[]>` - The paths to pass to `require.resolve` when a bare module name is specified. | ||
If this value is empty, no paths are passed to `require.resolve`. | ||
If this argument is empty, no paths are passed to `require.resolve`. | ||
* `context.resolve` `<Function>` - The resolve function to use to resolve a module name or path request. | ||
@@ -62,6 +66,6 @@ Defaults to the built-in `require.resolve` function. | ||
```js | ||
function userRequire.resolve (request, { base, dot, paths = [], resolve = require.resolve } = {}) | ||
function userRequire.resolve (request, { base, cwd, dot, paths = [], resolve = require.resolve } = {}) | ||
``` | ||
Resolve the module name or path in relation to the specified context and return it. | ||
Resolves the module name or path in relation to the specified context and returns it. | ||
If the resolved module name or path cannot be found on the local filesystem, a `MODULE_NOT_FOUND` error is thrown. | ||
@@ -68,0 +72,0 @@ |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
29406
85
159
28384
+ Added@antora/expand-path-helper@2.0.0(transitive)
- Removed@antora/expand-path-helper@1.0.0(transitive)