Find the dependencies of a CSS file (PostCSS dialect)
Supports @import and @value ... from. Absolute and protocol-relative URLs are automatically filtered out.
The AST is generated using postcss and postcss-values-parser.
npm install detective-postcsspostcss must be installed separately as a peer dependency:
npm install postcssconst fs = require('node:fs');
const detective = require('detective-postcss');
const content = fs.readFileSync('styles.css', 'utf8');
// Returns an array of imported file paths (e.g. ['foo.css', 'bar.css'])
const dependencies = detective(content);
// Also include url() references (images, fonts, etc.) found in declarations
const allDependencies = detective(content, { url: true });TypeScript / ESM:
import detective = require('detective-postcss');
const dependencies = detective(content);
const allDependencies = detective(content, { url: true });| Parameter | Type | Required | Description |
|---|---|---|---|
src |
string |
Yes | CSS source code to analyse |
options.url |
boolean |
No | When true, also extracts url() references from declarations (e.g. background, src, cursor). Defaults to false. |
Returns string[] - the list of local dependency paths found in the source.
Throws detective.MalformedCssError if src cannot be parsed.
| Syntax | Example | Detected by default |
|---|---|---|
@import "file.css" |
@import "theme.css" |
yes |
@import url(file.css) |
@import url(print.css) |
yes |
@value x from "file.css" |
@value primary from 'colors.css' |
yes |
url() in declarations |
background: url(bg.png) |
only with { url: true } |
Absolute URLs (https://...) and protocol-relative URLs (//...) are always ignored.
This is the CSS (PostCSS dialect) counterpart to:
- detective-cjs - CommonJS
- detective-amd - AMD
- detective-es6 - ES modules
- detective-sass - Sass
- detective-scss - SCSS
- Ensure CI is green on
main. - Preview what would be included in the package without publishing:
npm pack --dry-run. - Bump the version following semver (this also creates the
vX.Y.Ztag):npm version <patch|minor|major>. - Push the commit and tag:
git push --follow-tags. - Create (or draft) a GitHub release from that
vX.Y.Ztag, then Publish it. - Publishing the release triggers npm-publish (
release.published), which runsnpm ciandnpm publish --provenance.