@antora/user-require-helper

中文文档
3.0.0发布于 2024/11/29MPL-2.0CommonJS近 30 天下载 18.2万

A helper function to resolve a module name or path in relation to the specified context and require it.

原始使用文档
文档目录6 节
  1. Install
  2. API
  3. Usage
  4. userRequire.resolve
  5. userRequire
  6. Copyright and License

@antora/user-require-helper

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.

The purpose of this function is to require a JavaScript module (specified as a module name or path request) from the user’s environment. The require function in Node.js resolves module names and paths in the environment of the calling script (i.e., the application code). That means it will not find modules in the local node_modules folder (unless the calling script is also located inside that folder) or local relative paths. This assumption is not well suited for user-supplied code.

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. 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 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’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.

💡 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/).

Install

$ npm i @antora/user-require-helper

API

function userRequire (id[, context])

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, an Error with a MODULE_NOT_FOUND code is thrown.

  • request <string> - The module name or path to resolve. This parameter is required and must not be empty.
  • context <Object> - Named parameters that control how the request is resolved. All named parameters are optional. Default: {}.
    • base <string> - The base directory from which to resolve a relative module name or path. Passed on to expandPath, which assigns the default value ~+. Default: ~+.
    • 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. Default: process.cwd().
    • 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 .. Default: ..
    • paths <string[]> - The paths to pass to require.resolve when a bare module name is specified. If this argument is empty, no paths are passed to require.resolve (which means it uses the built-in paths). Default: [].
    • context.resolve <Function> - The resolve function to use to resolve a module name or path request. Defaults to the built-in require.resolve function. Default: require.resolve.
function userRequire.resolve (request[, context])

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, an Error with the code MODULE_NOT_FOUND is thrown.

See userRequire for description of parameters.

Usage

userRequire.resolve

The userRequire.resolve function returns a system-dependent path. Here we show the results when used on a Unix-like operating system.

const userRequire = require('@antora/user-require-helper')

// request is a path

userRequire.resolve('/foo/bar.js')
//=> /foo/bar.js

userRequire.resolve('~/foo/bar.js')
//=> $HOME/foo/bar.js

userRequire.resolve('foo/bar.js')
//=> $PWD/foo/bar.js

userRequire.resolve('foo/bar.js', { base: '/base/dir' })
//=> /base/dir/foo/bar.js

userRequire.resolve('./foo/bar.js')
//=> $PWD/foo/bar.js

userRequire.resolve('./foo/bar.js', { dot: '/dot/dir' })
//=> /dot/dir/foo/bar.js

userRequire.resolve('~+/foo/bar.js')
//=> $PWD/foo/bar.js

// request is a module name

userRequire.resolve('/path/to/node_modules/foo-bar')
//=> /path/to/node_modules/foo-bar/main.js

userRequire.resolve('foo-bar')
//=> /calling/script/dir/node_modules/foo-bar/main.js

userRequire.resolve('foo-bar', { paths: ['/other/dir'] })
//=> /other/dir/node_modules/foo-bar/main.js

userRequire.resolve('^:foo-bar')
//=> /calling/script/dir/node_modules/foo-bar/main.js

userRequire.resolve('^:foo-bar', { paths: ['/other/dir'] })
//=> /calling/script/dir/node_modules/foo-bar/main.js

userRequire.resolve(':foo-bar')
//=> $PWD/node_modules/foo-bar/main.js

userRequire.resolve(':foo-bar', { base: '/base/dir' })
//=> /base/dir/node_modules/foo-bar/main.js

userRequire.resolve('.:foo-bar')
//=> $PWD/node_modules/foo-bar/main.js

userRequire.resolve('.:foo-bar', { dot: '/dot/dir' })
//=> /dot/dir/node_modules/foo-bar/main.js

userRequire.resolve('./subdir:foo-bar', { dot: '/dot/dir' })
//=> /dot/dir/subdir/node_modules/foo-bar/main.js

userRequire.resolve('~+:foo-bar')
//=> $PWD/node_modules/foo-bar/main.js

userRequire.resolve('foo-bar.js/')
//=> /calling/script/dir/node_modules/foo-bar.js/main.js

userRequire

Uses userRequire.resolve to resolve the path and returns the object exported by that path by requiring it.

const userRequire = require('@antora/user-require-helper')

const exportedObject = userRequire('./foo/bar.js', { dot: '/dot/dir' })

Copyright (C) 2021-present by OpenDevise Inc. and the individual contributors to Antora.

Use of this software is granted under the terms of the Mozilla Public License Version 2.0 (MPL-2.0).

版本与发布记录

@antora/user-require-helper 共发布 3 个版本,首次发布于 2021/07/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2024/11/291.9 年前
2.0.02021/07/175 年前
1.0.02021/07/125 年前

关于 @antora/user-require-helper 的常见问题

@antora/user-require-helper 是什么?

A helper function to resolve a module name or path in relation to the specified context and require it.

如何安装 @antora/user-require-helper?

在项目目录运行 npm install @antora/user-require-helper,或使用 pnpm add / yarn add / bun add。

@antora/user-require-helper 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/antora__user-require-helper 是否存在。

@antora/user-require-helper 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@antora/user-require-helper 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@antora/user-require-helper 可以商用吗?

@antora/user-require-helper 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@antora/user-require-helper 的下载量有多少?

根据 npm 官方下载统计,@antora/user-require-helper 在 2026/10/06 – 2026/11/04 这 30 天内被下载 181,946 次,日均约 6,065 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@antora/user-require-helper 还在维护吗?

最新版本 3.0.0 发布于 2024/11/29(1.9 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/07/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。