Skip to content

Commit 7467dc2

Browse files
committed
docs: add more plugin docs
1 parent f61b0cf commit 7467dc2

1 file changed

Lines changed: 42 additions & 14 deletions

File tree

README.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
# :snake: css-codemod
22

3-
css-codemod is a toolkit for running codemods (a.k.a. transforms) over many CSS files.
3+
css-codemod is a toolkit for running codemods over many CSS files to transform code.
44

5-
Powered by [PostCSS](https://postcss.org) so the existing tooling and community can be leveraged.
5+
Powered by [PostCSS](https://postcss.org):
66

7-
- Any [PostCSS syntax parser and stringifier](https://github.com/postcss/postcss/blob/main/docs/syntax.md) can be added.
8-
- Any [PostCSS plugin](https://github.com/postcss/postcss/blob/main/docs/plugins.md) can be added,
7+
- Any [PostCSS syntax parser and stringifier](https://github.com/postcss/postcss/blob/main/docs/syntax.md) can be added. This extends support for additional syntaxes like SASS and LESS.
8+
- Any [PostCSS plugin](https://github.com/postcss/postcss/blob/main/docs/plugins.md) can be added. This allows running any plugin as a one-off transform. This can be useful if you want to run a plugin once and remove it from a build tool.
9+
- Any [PostCSS helpers](https://postcss.org/api/) for working with nodes and the abstract syntax tree can be used to transform CSS.
910

1011
## Install
1112

1213
There are a few ways to use css-codemod.
1314

14-
First, using [npx](https://www.npmjs.com/package/npx) to execute the transform without need to explicitly install `css-codemod`.
15+
First, using [npx](https://www.npmjs.com/package/npx):
1516

1617
```bash
1718
npx css-codemod "./src/**/*.css" -t ./transform.ts
1819
```
1920

20-
Second, install `css-codemod` as a dependency and execute with your package manager of choice.
21+
Second, install and execute `css-codemod` with a package manager:
2122

2223
```bash
2324
yarn add -D css-codemod
2425
yarn css-codemod "./src/**/*.css" -t ./transform.ts
2526
```
2627

27-
Third, install `css-codemod` globally.
28+
Third, globally install `css-codemod`:
2829

2930
```
3031
yarn add -g css-codemod
@@ -74,13 +75,14 @@ import { Transform } from 'css-codemod';
7475

7576
// Define a named `transform` export function.
7677
export const transform: Transform = (fileInfo, api) => {
77-
// Implement the transform.
78-
// See below for more details on the API.
78+
// Implement the transform. See below for more details on the API.
7979
};
8080

81-
// Optionally defined a named `parser` export to configure the PostCSS parser.
82-
// Docs: https://postcss.org/api/#parser
81+
// Optionally define a named `parser` export to configure the PostCSS parser.
8382
// export const parser = ...;
83+
84+
// Optionally define a named `plugins` export to configure PostCSS plugins.
85+
// export const plugins = [...];
8486
```
8587

8688
## API
@@ -110,13 +112,30 @@ It's an object with helpers provided by `css-codemod` to perform transformations
110112

111113
- `parse`: parse a raw CSS string into an AST. This returns the root node of the underlying abstract syntax tree. Transformations can be made by making direct mutations to the underlying node. This is performed with [PostCSS](https://postcss.org/) so the returned node is a PostCSS [Root](https://postcss.org/api/#root) node. Refer to the [PostCSS API documentation](https://postcss.org/api/) for documentation on nodes and various helpers.
112114

115+
### `parser`
116+
117+
Define the [PostCSS parser](https://postcss.org/api/#parser) to use when parsing the CSS files. This is useful for adding support for additional syntaxes.
118+
119+
To configure, export `parser` with a PostCSS parser from the transform file.
120+
121+
Note: if you define a `parser` than you almost always want to pass the `stringifier` from the same package to `root.toString(stringifier)`. This will guarantee the output is properly formatted using the same syntax.
122+
123+
### `plugins`
124+
125+
Define [PostCSS plugins]() to use when parsing the CSS files. This is useful for running plugins one-off, for example to upgrade syntax or perform other transformations already provided as plugins. Creating a custom plugin is one way that transform logic can be shared with other PostCSS tools and `css-codemod`. If you only want to share the codemod, then creating a transform file and sharing it is another option that requires less setup from others.
126+
127+
To configure, export `plugins` with an array of PostCSS plugins from the transform file.
128+
113129
### Example
114130

115131
```ts
116132
// transform.ts
117133

118134
import { Transform } from 'css-codemod';
135+
// Example PostCSS syntax extension. This isn't required.
119136
import { parse, stringify } from 'postcss-scss';
137+
// Example PostCSS plugin. This isn't required.
138+
import calc from 'postcss-calc';
120139

121140
export const transform: Transform = (fileInfo, api) => {
122141
// Convert the file source into an AST using the provided helper.
@@ -139,6 +158,7 @@ export const transform: Transform = (fileInfo, api) => {
139158

140159
// Convert the mutated AST back into a string.
141160
// Since a string is returned this will be written back to the file.
161+
//
142162
// Note: in this example the `postcss-scss` package is used to add
143163
// SCSS syntax support. The stringifier is passed when we call `toString` to
144164
// re-output valid SCSS syntax.
@@ -147,8 +167,16 @@ export const transform: Transform = (fileInfo, api) => {
147167

148168
// Note: in this example the `postcss-scss` package is used to add SCSS syntax support.
149169
// This configures PostCSS to correctly parse SCSS syntax.
150-
// Docs: https://postcss.org/api/#parser
170+
// API docs: https://postcss.org/api/#parser
171+
// Syntax docs: https://github.com/postcss/postcss/blob/main/docs/syntax.md
151172
export const parser = parse;
173+
174+
// Note: in this example the `postcss-calc` package is used to compute `calc` expressions.
175+
// This is used only as an example. Say you wanted to simplify all the complex `calc`
176+
// expressions? Or, maybe you want to remove a plugin from the build pipeline and run it once?
177+
// API docs: https://postcss.org/api/#acceptedplugin
178+
// Plugin docs: https://github.com/postcss/postcss/blob/main/docs/plugins.md
179+
export const plugins = [calc({})];
152180
```
153181

154182
### PostCSS
@@ -161,6 +189,6 @@ export const parser = parse;
161189

162190
## Motivation
163191

164-
**css-codemod** is inspired by tools like [`jscodeshift`](https://github.com/facebook/jscodeshift) to streamline CSS transformations whether it be an evolving codebase, or adopting newer syntax.
192+
css-codemod is inspired by tools like [`jscodeshift`](https://github.com/facebook/jscodeshift) to streamline CSS transformations whether it be an evolving codebase, or adopting newer syntax.
165193

166-
Read [CSS codemods with PostCSS](https://www.skovy.dev/blog/css-codemods-with-postcss) for a conceptual overview of how this toolkit works.
194+
Read [CSS codemods with PostCSS](https://www.skovy.dev/blog/css-codemods-with-postcss) for a conceptual overview of how this toolkit works and the initial motivation.

0 commit comments

Comments
 (0)