Skip to content

Commit 5146687

Browse files
committed
docs: improvements
1 parent 966840e commit 5146687

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@ There are two ways to use css-codemod.
99
First, using [npx](https://www.npmjs.com/package/npx) to execute the transform without need to explicitly install `css-codemod`.
1010

1111
```bash
12-
npx css-codemod ./src/**/*.css -t ./transform.ts
12+
npx css-codemod "./src/**/*.css" -t ./transform.ts
1313
```
1414

1515
Second, install `css-codemod` as a dependency and execute with your package manager of choice.
1616

1717
```bash
1818
# Install and execute css-codemod with npm
1919
npm i -D css-codemod
20-
./node_modules/.bin/css-codemod ./src/**/*.css -t ./transform.ts
20+
./node_modules/.bin/css-codemod "./src/**/*.css" -t ./transform.ts
2121

2222
# Or, install and execute css-codemod with yarn
2323
yarn add -D css-codemod
24-
yarn css-codemod ./src/**/*.css -t ./transform.ts
24+
yarn css-codemod "./src/**/*.css" -t ./transform.ts
2525
```
2626

2727
## Transform
2828

29-
The transform file defines the transformations to define. The transform can be written in either JavaScript or TypeScript.
29+
The transform file defines the transformation to make to each file. The transform can be written in either JavaScript or TypeScript.
30+
31+
It needs to provide a named `transform` export that is a function. This transform function will be invoked for each file that matches the files to process.
3032

3133
```ts
3234
// transform.ts
@@ -35,10 +37,8 @@ The transform file defines the transformations to define. The transform can be w
3537
// using and creating a transform function.
3638
import { Transform } from 'css-codemod';
3739

38-
// Define a named `transform` export.
39-
// Note: it's important the function is named `transform` because that's
40-
// what the tool expects.
41-
export const transform: Transform = (file, api) => {
40+
// Define a named `transform` export function.
41+
export const transform: Transform = (fileInfo, api) => {
4242
// Implement the transform.
4343
};
4444
```
@@ -71,20 +71,28 @@ css-codemod "./**/*.css"
7171

7272
### `Transform`
7373

74-
Define a transform function. This type is provided to explicitly type the exported `transform` function. In general, this should be the only type that needs to be imported. The expected return value is either a CSS string or `null`. When returned a CSS string that will be written back to the original file. When returned `null`, nothing happens and the original file is skipped.
74+
Define a transform function.
75+
76+
This type is provided to explicitly type the exported `transform` function. In general, this should be the only type that needs to be explicitly imported. The expected return value is either a string or `null`.
77+
78+
- When returned a string it will be written back to the original file. - When returned `null`, nothing happens and the original file is skipped.
7579

7680
### `TransformFileInfo`
7781

78-
The first argument passed to the `transform` function. It's an object with metadata about the current file being processed by the transform.
82+
The first argument passed to the `transform` function.
83+
84+
It's an object with metadata about the current file being processed by the transform.
7985

8086
- `path`: the resolved path of the file being transformed.
8187
- `source`: the file contents source of the file being transformed.
8288

8389
### `TransformAPI`
8490

85-
The second argument passed to the `transform` function. It's an object with helpers provided by `css-codemod` to perform transformations.
91+
The second argument passed to the `transform` function.
8692

87-
- `parse`: parse a raw CSS string into an AST. This returns the root node of the underlying abstract syntax tree to perform mutations. 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 and various helpers.
93+
It's an object with helpers provided by `css-codemod` to perform transformations.
94+
95+
- `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.
8896

8997
### Example
9098

@@ -122,6 +130,12 @@ export const transform: Transform = (fileInfo, api) => {
122130

123131
[PostCSS](https://postcss.org) is the core tool used for performing code transformations. As a result, much of it's API is re-surfaced in this toolkit and will link to it's documentation.
124132

133+
### AST Explorer
134+
135+
[AST Explorer](https://astexplorer.net) is recommended when working on transforms. Change the language to "CSS" and the parser to "postcss" to see the underlying abstract syntax tree for a given snippet of CSS. This makes it much easier to understand the transformations that need to be made.
136+
125137
## Motivation
126138

127139
**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.
140+
141+
Read [CSS codemods with PostCSS](https://www.skovy.dev/blog/css-codemods-with-postcss) for a conceptual overview of how this toolkit works.

0 commit comments

Comments
 (0)