# rollup-plugin-css-export

> Extracts and exports all CSS files that have been imported as a module, preserving or not the original path of the asset.

Latest version **0.2.0** (published 2021-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install rollup-plugin-css-export
pnpm add rollup-plugin-css-export
yarn add rollup-plugin-css-export
bun add rollup-plugin-css-export
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2021-08-18 |
| First published | 2021-08-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 20.2 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Sébastien Mischler |
| Maintainers | skarab |
| Keywords | rollup, plugin, css, style, import, export |

## Links

- npm: https://www.npmjs.com/package/rollup-plugin-css-export
- Repository: https://github.com/skarab42/rollup-plugin-css-export
- Issues: https://github.com/skarab42/rollup-plugin-css-export/issues
- npm.io page: https://npm.io/package/rollup-plugin-css-export

## Dependencies (4)

- [tslib](https://npm.io/package/tslib.md) ^2.3.1
- [postcss](https://npm.io/package/postcss.md) 8.3.6
- [postcss-import](https://npm.io/package/postcss-import.md) 14.0.2
- [@rollup/pluginutils](https://npm.io/package/@rollup/pluginutils.md) ^4.1.1

## 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

- 0.2.0 (latest) — 2021-08-18

## README

# rollup-plugin-css-export 
[![integration](https://github.com/skarab42/rollup-plugin-css-export/actions/workflows/integration.yml/badge.svg)](https://github.com/skarab42/rollup-plugin-css-export/actions/workflows/integration.yml)

Extracts and exports all CSS files that have been imported as a module, preserving or not the original path of the asset.

In addition to this a `styles` property can be added to the `metadata` of each entry that imports one or more CSS files.

> The `styles` property contains a list of paths to the generated assets which can, for example, be used by other plugins to generate HTML template.

# Installation

With yarn:

```bash
yarn add --dev rollup-plugin-css-export
```

With npm:

```bash
npm install --save-dev rollup-plugin-css-export
```

> If you want to import libraries directly from `node_modules` (e.g. [normalize.css](https://www.npmjs.com/package/normalize.css)) you may need to install the [@rollup/plugin-node-resolve](https://www.npmjs.com/package/@rollup/plugin-node-resolve) plugin.

# Options

| Name     | Type                                         | Default     | Description                                                                                                      |
| -------- | -------------------------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------- |
| metaKey? | string \| Symbol                             | `undefined` | The name of the metadata key. When the value is `nullish` the plugin will not process or export any metadata.    |
| include? | String \| RegExp \| Array[...String\|RegExp] | `**/*.css`  | See [pluginutils](https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter) for more info |
| exclude? | String \| RegExp \| Array[...String\|RegExp] | `undefined` | See [pluginutils](https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter) for more info |

# Basic example

`red.js`, `blue.js` and `purple.js` can be found at https://github.com/skarab42/rollup-plugin-css-export/tree/main/test/fixtures.

```js
import css from "rollup-plugin-css-export";

export default {
  input: ["src/red.js", "src/blue.js", "src/purple.js"],
  output: {
    dir: "dist",
    // preserveModules: true,
  },
  plugins: [css()],
};
```

Outputs with `preserveModules = false`

```
📦dist
 ┣ 📂assets
 ┃ ┣ 📜blue-8e2a6dc2.css
 ┃ ┣ 📜red-443842c2.css
 ┃ ┗ 📜reset-be7c786b.css
 ┣ 📜blue.js
 ┣ 📜purple.js
 ┗ 📜red.js
```

Outputs with `preserveModules = true`

```
📦dist
 ┣ 📂assets
 ┃ ┣ 📂lib
 ┃ ┃ ┗ 📜reset-5af228c6.css
 ┃ ┣ 📜blue-8e2a6dc2.css
 ┃ ┗ 📜red-443842c2.css
 ┣ 📜blue.js
 ┣ 📜purple.js
 ┗ 📜red.js
```

# Accessing metadata from another plugin

Metadata are not directly accessible in the bundle. You have to retrieve the extended information from the module with the `this.getModuleInfo` method.

```js
import css from "rollup-plugin-css-export";

const metaKey = Symbol("styles");

export default {
  input: ["src/red.js", "src/blue.js", "src/purple.js"],
  output: {
    dir: "dist",
    // preserveModules: true,
  },
  plugins: [
    css({ metaKey }),
    {
      name: "your-plugin",
      generateBundle(_, bundle) {
        Object.entries(bundle).forEach(([id, entry]) => {
          if (entry.isEntry) {
            const info = this.getModuleInfo(entry.facadeModuleId);
            console.log("META:", id, info.meta[metaKey]);
          }
        });
      },
    },
  ],
};
```

Outputs with `preserveModules = false`

```bash
.../red.js    [ 'assets/reset-be7c786b.css', 'assets/red-443842c2.css' ]
.../blue.js   [ 'assets/reset-be7c786b.css', 'assets/blue-8e2a6dc2.css' ]
.../purple.js [ 'assets/reset-be7c786b.css', 'assets/red-443842c2.css', 'assets/blue-8e2a6dc2.css' ]
```

Outputs with `preserveModules = true`

```bash
.../red.js    [ 'assets/lib/reset-be7c786b.css', 'assets/red-443842c2.css' ]
.../blue.js   [ 'assets/lib/reset-be7c786b.css', 'assets/blue-8e2a6dc2.css' ]
.../purple.js [ 'assets/lib/reset-be7c786b.css', 'assets/red-443842c2.css', 'assets/blue-8e2a6dc2.css' ]
```

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