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
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.
30
32
31
33
```ts
32
34
// transform.ts
@@ -35,10 +37,8 @@ The transform file defines the transformations to define. The transform can be w
35
37
// using and creating a transform function.
36
38
import { Transform } from'css-codemod';
37
39
38
-
// Define a named `transform` export.
39
-
// Note: it's important the function is named `transform` because that's
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.
75
79
76
80
### `TransformFileInfo`
77
81
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.
79
85
80
86
-`path`: the resolved path of the file being transformed.
81
87
-`source`: the file contents source of the file being transformed.
82
88
83
89
### `TransformAPI`
84
90
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.
86
92
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.
[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.
124
132
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
+
125
137
## Motivation
126
138
127
139
**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