Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ezhlobo/babel-plugin-transform-jsx-css-modules

Transform className attributes in JSX to reference CSS Modules
https://github.com/ezhlobo/babel-plugin-transform-jsx-css-modules

Last synced: 9 days ago
JSON representation

Transform className attributes in JSX to reference CSS Modules

Awesome Lists containing this project

README

        

# babel-plugin-transform-jsx-css-modules

Transforms `className` attributes in JSX to get css-modules' references.

[![npm version](https://img.shields.io/npm/v/babel-plugin-transform-jsx-css-modules.svg?longCache)](https://www.npmjs.com/package/babel-plugin-transform-jsx-css-modules) [![CI Status](https://img.shields.io/circleci/project/github/ezhlobo/babel-plugin-transform-jsx-css-modules/master.svg?longCache)](https://circleci.com/gh/ezhlobo/babel-plugin-transform-jsx-css-modules/tree/master)

**Note:** It does not turn on CSS Modules in your project, it assumes that you made it yourself (via webpack and css-loader for example).

- [Example](#example)
- [Usage](#usage)
- [Options](#options)

## Example

```jsx
import './styles.css'

const Component = () => (


Hello World


I'm an example!



)
```

Will be transpiled into

```jsx
import __CSSM__ from './styles.css'

const Component = () => (


Hello World


I'm an example!



)
```

## Usage

1. Install plugin

```shell
npm install --save-dev babel-plugin-transform-jsx-css-modules
```

2. Add plugin to your `.babelrc` file

```json
{
"plugins": [
"transform-jsx-css-modules"
]
}
```

## Options

| Name | Type | Default | Description
| - | - | - | -
| [`pathToStyles`](#pathtostyles) | `RegExp` | `/^\.\/styles\.css$/` | It specifies what imports should be transformed
**Note:** Escape symbols if you specify it as a string

### `pathToStyles`

If you set it to `/^\.\/module\.scss$/` it will handle imports which start and end with `./module.scss`:

```json
{
"plugins": [
["transform-jsx-css-modules", {
"pathToStyles": "^\\.\\/module\\.scss$"
}]
]
}
```

```jsx
import './module.scss'
```

Will be transformed into:

```jsx
import __CSSM__ from './module.scss'
```