@11ty/dependency-tree 中文文档教程
dependency-tree
返回节点 JavaScript 文件依赖项的本地路径的无序数组(它或其任何依赖项 require
的所有内容)。
dependency-tree
package 的缩减功能(更快)替代品,仅适用于库存节点JS。 Eleventy 使用它来查找 JavaScript 文件的依赖项,以监视更改以重新运行 Eleventy 的构建。
Big Huge Caveat
⚠ 这个插件的一个重要警告是它需要这个文件来构建一个依赖树。 因此,如果您的模块有副作用并且您不希望它执行,请不要使用它!
Installation
npm install --save-dev @11ty/dependency-tree
Features
- Ignores
node_modules
- Or, use
nodeModuleNamesOnly
to return a list of node_modules packages (added in v2.0.0) - Ignores Node’s built-ins (e.g.
path
) - Handles circular dependencies (Node does this too)
Usage
// my-file.js
// if my-local-dependency.js has dependencies, it will include those too
const test = require("./my-local-dependency.js");
// ignored, is a built-in
const path = require("path");
const DependencyTree = require("@11ty/dependency-tree");
DependencyTree("./my-file.js");
// returns ["./my-local-dependency.js"]
allowNotFound
const DependencyTree = require("@11ty/dependency-tree");
DependencyTree("./this-does-not-exist.js"); // throws an error
DependencyTree("./this-does-not-exist.js", { allowNotFound: true });
// returns []
nodeModuleNamesOnly
(在 v2.0.0 中添加)导航所有本地文件并返回所需的唯一包名称(不是文件名)列表。
const DependencyTree = require("@11ty/dependency-tree");
DependencyTree("./this-does-not-exist.js", { nodeModuleNamesOnly: true });
// returns []
dependency-tree
Returns an unordered array of local paths to dependencies of a node JavaScript file (everything it or any of its dependencies require
s).
Reduced feature (faster) alternative to the dependency-tree
package that only works with stock node JS. This is used by Eleventy to find dependencies of a JavaScript file to watch for changes to re-run Eleventy’s build.
Big Huge Caveat
⚠ A big caveat to this plugin is that it will require the file in order to build a dependency tree. So if your module has side effects and you don’t want it to execute—do not use this!
Installation
npm install --save-dev @11ty/dependency-tree
Features
- Ignores
node_modules
- Or, use
nodeModuleNamesOnly
to return a list of node_modules packages (added in v2.0.0) - Ignores Node’s built-ins (e.g.
path
) - Handles circular dependencies (Node does this too)
Usage
// my-file.js
// if my-local-dependency.js has dependencies, it will include those too
const test = require("./my-local-dependency.js");
// ignored, is a built-in
const path = require("path");
const DependencyTree = require("@11ty/dependency-tree");
DependencyTree("./my-file.js");
// returns ["./my-local-dependency.js"]
allowNotFound
const DependencyTree = require("@11ty/dependency-tree");
DependencyTree("./this-does-not-exist.js"); // throws an error
DependencyTree("./this-does-not-exist.js", { allowNotFound: true });
// returns []
nodeModuleNamesOnly
(Added in v2.0.0) Navigates all the local files and returns a list of unique package names (not file names) required.
const DependencyTree = require("@11ty/dependency-tree");
DependencyTree("./this-does-not-exist.js", { nodeModuleNamesOnly: true });
// returns []