You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-14Lines changed: 42 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,31 @@
1
1
# :snake: css-codemod
2
2
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.
4
4
5
-
Powered by [PostCSS](https://postcss.org) so the existing tooling and community can be leveraged.
5
+
Powered by [PostCSS](https://postcss.org):
6
6
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.
9
10
10
11
## Install
11
12
12
13
There are a few ways to use css-codemod.
13
14
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):
// Implement the transform. See below for more details on the API.
79
79
};
80
80
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.
83
82
// export const parser = ...;
83
+
84
+
// Optionally define a named `plugins` export to configure PostCSS plugins.
85
+
// export const plugins = [...];
84
86
```
85
87
86
88
## API
@@ -110,13 +112,30 @@ It's an object with helpers provided by `css-codemod` to perform transformations
110
112
111
113
-`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.
112
114
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
+
113
129
### Example
114
130
115
131
```ts
116
132
// transform.ts
117
133
118
134
import { Transform } from'css-codemod';
135
+
// Example PostCSS syntax extension. This isn't required.
**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.
165
193
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