# postcss-es-modules
[Postcss](https://postcss.org/) plugin which transform css files in to the .js/.ts css modules.
What is this? For example, you have the following CSS:
```css
/* ./component.module.css */
.article {
font-size: 16px;
}
.title {
font-size: 24px;
}
.title:hover {
color: red;
}
```
After the transformation it will become like this:
```javascript
/* ./component.module.css.js */
// File generated by the postcss-es-modules plugin. Please do not modify it !!!
import { injectStyles } from 'css-es-modules';
const key = '77d26384-99a7-48e0-9f08-cd25a85864fb';
const css =`._article_9u0vb_1 {
font-size: 16px;
}
._title_9u0vb_9 {
font-size: 24px;
}
._title_9u0vb_9:hover {
color: red;
}
`;
const styles = {
get ['article']() { injectStyles(key, css); return '_article_9u0vb_1 article'; },
get ['title']() { injectStyles(key, css); return '_title_9u0vb_9 title'; },
inject() { injectStyles(key, css); }
};
export default styles;
```
Then that code can be referred from your component.
```javascript
/* ./component.jsx */
import styles from './component.module.css.js';
export const Component = () => (
)
```
## Description
This plugin allows you to write the styles in *css/sass/less/...* syntax and transform it on the
build time in the efficient *js/ts* code which can be simple referred from your component/application
code. That code is responsible for attach the styles into the DOM (or to SSR pipeline) in most possible efficient way.
This plugin also generates scoped class names, so you can be sure that your components styles will not leak
in to the other parts of the application.
## Features
* **Zero** runtime dependencies (on eject or embed mode, pleas look at the options)
* Framework agnostic
* Javascript and Typescript support
* Server side rendering
* Lazy, on demand, instant or none styles injection
* Rich (optional) configuration
## Installation
```bash
npm i postcss postcss-es-modules --save-dev
```
## Usage
Configure the postcss to use the plugin:
```javascript
// postcss.config.js
const { postcssEsModules } = require('postcss-es-modules');
module.exports = (ctx) => ({
plugins: [
postcssEsModules({
// options
}),
]
})
```
> Please remember that this plugin is generating **non css syntax**, so it should be last on the list
> of the used plugins.
> This plugin is internally using *postcss-modules* plugin, so you do not have to add it by self.
### With the postcss-cli
```bash
postcss src/**/*.module.css --dir src --base src --ext css.js
```
This command will generate the `.js` files by the post-cli, directly to your src directory.
### With the postcss-cli and typescript
```javascript
// postcss.config.js
const { postcssEsModules } = require('postcss-es-modules');
module.exports = (ctx) => ({
plugins: [
postcssEsModules({ inject: { scriptType: 'ts' } }),
]
})
```
```bash
postcss src/**/*.module.css --dir src --base src --ext css.ts
```
### With the webpack/rollup/...
There is nothing unique to use of this plugin with bundlers.
### With the webpack/rollup/... and typescript
If you are using typescript, and you do not want to generate es css modules ahead but just
let the bundler do the job, for solving typescript compilation errors please add global declaration
to your project, like that:
```typescript
/* ./global.d.ts */
declare module "*.css" {
type Styles = { [className: string]: string; } & { inject(): void; }
export const styles: Styles;
export const key: string;
export const css: string;
export default styles;
}
```
This will say to the compiler that each `*.css` import should be mapped to declared type.
### Other usage examples
You can find more examples [here](https://majo44.github.io/postcss-es-modules/#/examples/).
## Options
Here is the list of all available options, for more details please go to [Recipes](#recipes).
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `inject` | `object` | - | The configuration fo the styles injection |
| `inject.useNounce` | `string` | - | The style nounce key |
| `inject.useConstructableStylesheet` | `boolean` | true | Use Constructable Stylesheet for styles injection to DOM if the browser supports Constructable Stylesheet |
| `inject.useStyleTag` | `boolean` | true | Use \