# modular-css-core

> A streamlined reinterpretation of CSS Modules

Latest version **15.0.0** (published 2018-08-28) · MIT license · 764 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install modular-css-core
pnpm add modular-css-core
yarn add modular-css-core
bun add modular-css-core
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 15.0.0 |
| Published | 2018-08-28 |
| First published | 2017-02-18 |
| Weekly downloads | 764 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 15 |
| Unpacked size | 75 KB |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 289 |
| Author | Pat Cavit |
| Maintainers | tivac |
| Keywords | css, css-modules, modular-css, postcss |

## Links

- npm: https://www.npmjs.com/package/modular-css-core
- Repository: https://github.com/tivac/modular-css
- Homepage: https://github.com/tivac/modular-css#readme
- Issues: https://github.com/tivac/modular-css/issues
- npm.io page: https://npm.io/package/modular-css-core

## Dependencies (15)

- [postcss](https://npm.io/package/postcss.md) ^7.0.0
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2
- [postcss-url](https://npm.io/package/postcss-url.md) ^8.0.0
- [unique-slug](https://npm.io/package/unique-slug.md) ^2.0.0
- [resolve-from](https://npm.io/package/resolve-from.md) ^4.0.0
- [lodash.invert](https://npm.io/package/lodash.invert.md) ^4.3.0
- [p-each-series](https://npm.io/package/p-each-series.md) ^1.0.0
- [lodash.foreach](https://npm.io/package/lodash.foreach.md) ^4.5.0
- [true-case-path](https://npm.io/package/true-case-path.md) ^1.0.2
- [lodash.findlast](https://npm.io/package/lodash.findlast.md) ^4.6.0
- [dependency-graph](https://npm.io/package/dependency-graph.md) ^0.7.1
- [lodash.mapvalues](https://npm.io/package/lodash.mapvalues.md) ^4.6.0
- [escape-string-regexp](https://npm.io/package/escape-string-regexp.md) ^1.0.5
- [postcss-value-parser](https://npm.io/package/postcss-value-parser.md) ^3.3.0
- [postcss-selector-parser](https://npm.io/package/postcss-selector-parser.md) ^4.0.0

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 15.0.0 (latest) — 2018-08-28
- 5.0.0-rc1 (next) — 2017-05-12
- 4.0.1 (lerna) — 2017-02-18
- 14.4.0 — 2018-08-10
- 14.3.0 — 2018-08-09
- 14.2.1 — 2018-08-01
- 14.2.0 — 2018-07-25
- 14.1.0 — 2018-07-18
- 14.0.1 — 2018-07-16
- 14.0.0 — 2018-07-13
- 13.0.0 — 2018-07-13
- 12.1.3 — 2018-07-05
- 12.1.2 — 2018-07-05
- 12.1.0 — 2018-06-26
- 12.0.2 — 2018-06-22
- … 33 more at https://npm.io/package/modular-css-core/versions

## README

modular-css-core  [![NPM Version](https://img.shields.io/npm/v/modular-css-core.svg)](https://www.npmjs.com/package/modular-css-core) [![NPM License](https://img.shields.io/npm/l/modular-css-core.svg)](https://www.npmjs.com/package/modular-css-core) [![NPM Downloads](https://img.shields.io/npm/dm/modular-css-core.svg)](https://www.npmjs.com/package/modular-css-core)
===========

<p align="center">
    <a href="https://gitter.im/modular-css/modular-css"><img src="https://img.shields.io/gitter/room/modular-css/modular-css.svg" alt="Gitter" /></a>
</p>

The core functionality of [`modular-css`](https://npmjs.com/modular-css) exposed as a JS API.

## Install

`$ npm i modular-css-core`

## Usage

Instantiate a new `Processor` instance, call it's `.file(<path>)` or `.string(<name>, <contents>)` methods, and then use the returned Promise to get access to the results/output.

```js
var Processor = require("modular-css-core"),
    processor = new Processor({
        // See "API Options" for valid options to pass to the Processor constructor
    });

// Add entries, either from disk using .file() or as strings with .string()
Promise.all([
    processor.file("./entry.css").then(function(result) {
        // result now contains
        //  .exports - Scoped selector mappings
        //  .files - metadata about the file hierarchy
    }),
    processor.string("./fake-file.css", ".class { color: red; }")
])
.then(function() {
    // Once all files are added, use .output() to get at the rewritten CSS
    return processor.output();
})
.then(function(result) {
    // Output CSS lives on the .css property
    result.css;
    
    // Source map (if requested) lives on the .map property
    result.map;
});
```

### Processor Options

#### `before`

Specify an array of PostCSS plugins to be run against each file before it is processed.

```js
new Processor({
    before : [ require("postcss-import") ]
});
```
#### `after`

Specify an array of PostCSS plugins to be run after files are processed, but before they are combined. Plugin will be passed a `to` and `from` option.

**By default** [`postcss-url`](https://www.npmjs.com/package/postcss-url) is used in `after` mode.

```js
new Processor({
    after : [ require("postcss-someplugin") ]
});
```

#### `done`

Specify an array of PostCSS plugins to be run against the complete combined CSS.

```js
new Processor({
    done : [ require("cssnano")()]
});
```

#### `map`

Enable source map generation. Can also be passed to `.output()`.

**Default**: `false`

```js
new Processor({
    map : true
});
```

#### `cwd`

Specify the current working directory for this Processor instance, used when resolving `composes`/`@value` rules that reference other files.

**Default**: `process.cwd()`

```js
new Processor({
    cwd : path.join(process.cwd(), "/sub/dir")
})
```

#### `namer`

Specify a function (that takes `filename` & `selector` as arguments to produce scoped selectors.

**Default**: Function that returns `"mc" + unique-slug(<file>) + "_" + selector`

```js
new Processor({
    namer : function(file, selector) {
        return file.replace(/[:\/\\ .]/g, "") + "_" + selector;
    }
});
```

#### `resolvers`

If you want to provide your own file resolution logic you can pass an array of resolver functions. Each resolver function receives three arguments:

- `src`, the file that included `file`
- `file, the file path being included by `src`
- `resolve`, the default resolver function

Resolver functions should either return an absolute path or a falsey value. They must also be synchronous.

**Default**: See [/src/lib/resolve.js](/src/lib/resolve.js) for the default implementation.

```js
new Processor({
    resolvers : [
        (src, file, resolve) => ...,
        require("modular-css-resolvepaths")(
            "./some/other/path"
        )
    ]
})
```

#### `exportGlobals`

Enable exporting `:global` identifiers.

**Default**: true

```js
new Processor({
    exportDefaults: false
})
```

```css
/* exportGlobals: true */
.a {}
:global(.b) {}

/* Outputs
{
    "a" : "mc12345_a",
    "b" : "b"
}
*/

/* exportGlobals: false */
.a {}
:global(.b) {}

/* Outputs
{
    "a" : "mc12345_a"
}
*/
```

---
_Source: https://npm.io/package/modular-css-core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
