如何配置documentationjs解析的目录包含与排除规则?含node_modules排除方法
Great question! Configuring include/exclude rules for documentation.js is straightforward once you know where to set it up—let's break this down step by step, focusing on blocking node_modules and other directories you don't want parsed. All of this is handled via a configuration file, most commonly a .jsdoc file (though you have other options too).
1. Create or Edit Your Configuration File
First, create a .jsdoc file in your project root if you don't already have one. This is a JSON file that tells documentation.js exactly which files to process and which to ignore.
2. Define Include/Exclude Rules
Add a source section to your .jsdoc file to specify your rules. Here's a practical example tailored to exclude node_modules and other common non-source directories:
{ "source": { // Define which files/directories to INCLUDE in documentation "include": [ "src/**/*.js", "lib/**/*.js" // Add any other source directories you want documented ], // Define which files/directories to EXCLUDE "exclude": [ "node_modules/**/*", // Critical: Excludes all files in node_modules "dist/**/*", // Exclude built output "tests/**/*", // Exclude test files "docs/**/*" // Exclude existing documentation files ] } }
Key Notes on Glob Patterns:
- Use
**/*to match all files and subdirectories within a folder (e.g.,node_modules/**/*catches everything insidenode_modules). - Be specific with your
includerules to avoid accidentally pulling in unwanted files—stick to your actual source code directories.
3. Alternative: Use package.json for Configuration
If you prefer to keep all project config in one place, you can add the same rules directly to your package.json under a documentation key:
{ "name": "your-project", "version": "1.0.0", "documentation": { "source": { "include": ["src/**/*.js"], "exclude": ["node_modules/**/*", "dist/**/*"] } } }
4. Run documentation.js with Your Config
When you generate documentation, documentation.js will automatically pick up your .jsdoc file or package.json config by default. If you're using a custom-named config file (e.g., docs-config.jsdoc), specify it with the -c flag:
# Use default config (.jsdoc or package.json) documentation build -o docs # Use a custom config file documentation build -c ./docs-config.jsdoc -o docs
5. Verify Your Rules Are Working
To double-check that node_modules and other excluded directories aren't being parsed, run the lint command with your config—it will list all files that documentation.js intends to process:
documentation lint -c .jsdoc
If you see any files you don't want in the output, adjust your exclude rules and re-run the check.
内容的提问来源于stack exchange,提问作者kmiklas

