Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

css-module-deps

reducejs12MIT3.0.0

walk the css dependency graph to generate a stream of json output

css, module-deps, dependencies, dependency, graph, reduce-css

readme

css-module-deps

version status coverage dependencies devDependencies node

Walk the css dependency graph to generate a stream of json output.

Example

var Deps = require('..')
var path = require('path')
var url = require('postcss-custom-url')
var atImport = require('postcss-simple-import')
var vars = require('postcss-advanced-variables')
var JSONStream = require('JSONStream')

var fixtures = path.resolve.bind(path, __dirname, 'src')

var stream = Deps({
  atRuleName: 'external',
  basedir: fixtures(),
  processor: [ atImport(), url(), vars() ],
})
stream.end({ file: './import-and-deps.css' })

stream.pipe(
  JSONStream.stringify(false, null, null, 2)
)
.pipe(process.stdout)

Directory structure:

⌘ tree example/src
example/src
├── import-and-deps.css
├── import-url.css
└── node_modules
    ├── helper
    │   └── vars.css
    └── sprites
        └── dialog
            ├── index.css
            └── sp-dialog.png

import-and-deps.css:

@external "./import-url";
@import "helper/vars";

.import-and-deps {
  color: $red;
}

import-url.css

@import "sprites/dialog";
.importUrl{}

helper/vars.css:

$red: #FF0000;

sprites/dialog/index.css:

.dialog {
  background: url(sp-dialog.png)
}

output:

⌘ node example/deps.js
{
  "file": "/Users/zoubin/usr/src/self/css-module-deps/example/src/import-url.css",
  "source": ".dialog {\n  background: url(node_modules/sprites/dialog/sp-dialog.png)\n}\n.importUrl{}\n\n",
  "deps": {},
  "id": "/Users/zoubin/usr/src/self/css-module-deps/example/src/import-url.css"
}
{
  "file": "/Users/zoubin/usr/src/self/css-module-deps/example/src/import-and-deps.css",
  "source": ".import-and-deps {\n  color: #FF0000;\n}\n\n",
  "deps": {
    "./import-url": "/Users/zoubin/usr/src/self/css-module-deps/example/src/import-url.css"
  },
  "id": "/Users/zoubin/usr/src/self/css-module-deps/example/src/import-and-deps.css"
}

stream = Deps(opts)

Return an object stream that expects { file: ... } objects as input, and produces objects for every dependency from a recursive module traversal as output.

opts

resolve

Specify how to resolve a file path

Type: Function

Receives the string to be resolved, and an option object with basedir.

Should return a promise, or the absolute path.

noParse

Specify which files to skip parsing.

Type: Array

Passed to multimatch to do matching.

atRuleName

Specify the name of at-rules to declare a dependency.

Type: String

Default: deps

Dependencies are declared through the @deps at-rule by default.

transform

Used to transform each file in the dependency graph.

Type: Function, Array

Signature: fn(result)

Return a promise to make it asynchronous.

result is an instance of Result.

processor

Specify postcss plugins to transform css file contents.

Type: Array

basedir

Type: String

Used to resolve input filenames.

dependenciesFilter

To filter the resolved dependencies.

Type: Function

Signature: var newDeps = dependenciesFilter(deps, file)

Result

var r = new Result(row)

Each row has the following fields:

  • file: file path
  • source: file contents

Read or modify r.root (the AST object) or r.css to do transformations.

Usually, you do not have to access both.

r.from is the file path.

r is an emitter.

Events

stream.on('file', file => {})

Before readFile called.

stream.on('transform', (result, file) => {})

Before applying transforms.

result is an Result.

changelog

v3.0.0 (2016-02-19)

v2.2.1 (2016-01-05)

v2.2.0 (2015-12-08)

v2.1.0 (2015-12-04)

  • [559bfd4] Add processor option for postcss-plugins

  • [e8a93f1] Fix: pass the from option when parse result.root

  • [f5eb58c] Readme for dependenciesFilter

  • [dd76ed9] CHANGELOG

v2.0.0 (2015-12-03)

  • [d27e19d] Refactor. Breaking changes.
* Exports a normal function rather than a constructor
* `atDeps` is deprecated by `atRuleName`
* `processor` is deprecated by `transform`
* `noParse` only supports [`multimatch`](https://github.com/sindresorhus/multimatch) inputs.
  • [a337edf] Support dependenciesFilter.
* Use ES5.
* Add option `dependenciesFilter`. Allow to modify css dependency graph.
* Change most of instance methods to private functions, so that they do not mess.

v1.1.0 (2015-10-28)

  • [723a795] remove unused dependencies

  • [a488ea8] update task-tape

  • [588e428] add more tests, readme

  • [f986fec] fix option processor, support arguments

  • [8e50a8b] add test for option cache

  • [29dd242] collect processors from upstream

  • [5328489] refactor code. run processors

  • [ec9413f] support option noParse

  • [dcbd75d] fix test processor

v1.0.0 (2015-10-20)

v0.0.2 (2015-10-20)