What is precinct?
The precinct npm package is used to find dependencies in JavaScript and other file types. It helps in identifying the modules that a given file depends on, which can be useful for various tasks such as dependency analysis, bundling, and more.
What are precinct's main functionalities?
JavaScript Dependency Detection
This feature allows you to detect dependencies in JavaScript files. By passing the content of a JavaScript file to the precinct function, it returns an array of dependencies.
const precinct = require('precinct');
const content = 'const foo = require("foo");';
const dependencies = precinct(content);
console.log(dependencies); // ['foo']
CSS Dependency Detection
This feature allows you to detect dependencies in CSS files. By specifying the type as 'css', precinct can parse and return the dependencies in CSS files.
const precinct = require('precinct');
const content = '@import "foo.css";';
const dependencies = precinct(content, { type: 'css' });
console.log(dependencies); // ['foo.css']
TypeScript Dependency Detection
This feature allows you to detect dependencies in TypeScript files. By specifying the type as 'ts', precinct can parse and return the dependencies in TypeScript files.
const precinct = require('precinct');
const content = 'import foo from "foo";';
const dependencies = precinct(content, { type: 'ts' });
console.log(dependencies); // ['foo']
Other packages similar to precinct
madge
Madge is a JavaScript library that can create graphs of your module dependencies. It supports ES6, CommonJS, and AMD modules. Compared to precinct, madge provides more visualization features and can generate dependency graphs.
dependency-tree
Dependency-tree is a library for generating a dependency tree from a file or directory. It supports various module formats including ES6, CommonJS, and AMD. Unlike precinct, dependency-tree focuses on creating a hierarchical tree structure of dependencies.
detective
Detective is a library for finding the dependencies of a JavaScript file. It supports both CommonJS and ES6 modules. Detective is similar to precinct in its core functionality but is more focused on JavaScript and does not support other file types like CSS or TypeScript.
Precinct
Unleash the detectives
npm install precinct
Uses the appropriate detective to find the dependencies of a file or its AST.
Supports: AMD, CommonJS, and ES6 modules.
Also supports SASS dependencies via detective-sass.
- Does not support a SASS AST
Usage
var precinct = require('precinct');
var content = fs.readFileSync('myFile.js', 'utf8');
var deps = precinct(content);
Finding SASS dependencies:
var content = fs.readFileSync('styles.scss', 'utf8');
var deps = precinct(content, 'sass');
If you just want to pass in a filepath and get the dependencies:
var paperwork = require('precinct').paperwork;
var deps = paperwork('myFile.js');
Options
precinct.paperwork(filename, options)
includeCore
: (default: true) set to false
to exclude core Node dependencies from the list of dependencies.
License
MIT