Skip to content

Commit 887f581

Browse files
committed
docs: fix CJS guidance for Node >=20.19 (use require), align Parser signature, and convert remaining Stringify examples to ESM
1 parent af27d8e commit 887f581

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Note: This package is ESM‑only. Use `import` in Node.js. In CommonJS on Node.j
3333
// CommonJS (Node >= 20.19.0)
3434
const { parse } = require('postcss-values-parser');
3535
```
36+
```
3637
3738
## Benefits
3839

docs/Parser.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
The parser converts CSS value strings into an Abstract Syntax Tree (AST). It uses [css-tree](https://github.com/csstree/csstree) under the hood, then maps css-tree nodes to this package’s node classes.
44

5-
## parse(css)
5+
## parse(css, options?)
66

7-
Converts a CSS value string into an AST with a `Root` node.
7+
Converts a CSS value string into an AST with a `Root` node. The optional `options` argument is accepted for forward‑compatibility in v7 but is currently ignored.
88

99
### Parameters
1010

@@ -13,6 +13,7 @@ Converts a CSS value string into an AST with a `Root` node.
1313
Type: `string` (required)
1414

1515
Any valid CSS value string, such as:
16+
1617
- `10px solid red`
1718
- `calc(100% - 20px)`
1819
- `rgba(255, 0, 0, 0.5)`
@@ -68,12 +69,14 @@ Unknown or unrecognized node types are parsed as `Word` nodes to ensure the pars
6869
#### Source mapping
6970

7071
The parser preserves source locations from the original CSS string, including:
72+
7173
- Line and column positions
7274
- Start and end offsets
7375
- Original source text
7476

7577
```js
7678
import { parse } from 'postcss-values-parser';
79+
7780
const root = parse('calc(100px + 20%)');
7881
// Each node maintains source position information
7982
```
@@ -103,7 +106,7 @@ try {
103106
Thrown when the parsed AST is invalid or empty:
104107

105108
```js
106-
import { parse, AstError } from 'postcss-values-parser';
109+
import { AstError, parse } from 'postcss-values-parser';
107110

108111
try {
109112
const root = parse('');
@@ -122,9 +125,9 @@ The parser creates nodes using the NodeOptions interface:
122125

123126
```typescript
124127
interface NodeOptions {
125-
node?: CssNode; // Original css-tree node
126-
value?: string; // String value
127-
parent?: any; // Parent node
128+
node?: CssNode; // Original css-tree node
129+
value?: string; // String value
130+
parent?: any; // Parent node
128131
}
129132
```
130133

@@ -157,6 +160,7 @@ console.log(calcFunc.nodes.length); // Contains parsed parameters
157160
## Browser and Environment Support
158161

159162
The parser works in all environments where css-tree is supported:
163+
160164
- Node.js (all supported versions)
161165
- Modern browsers (ES2015+)
162166
- Webpack/Rollup bundled applications
@@ -168,7 +172,7 @@ The parser works in all environments where css-tree is supported:
168172

169173
```js
170174
const root = parse('10px solid red');
171-
console.log(root.nodes.map(n => n.type)); // ['numeric', 'word', 'word']
175+
console.log(root.nodes.map((n) => n.type)); // ['numeric', 'word', 'word']
172176
```
173177

174178
### Function Parsing
@@ -184,7 +188,7 @@ console.log(func.isColor); // true
184188

185189
```js
186190
const root = parse('calc(100% - 20px) url("bg.jpg") center/cover');
187-
root.walkFuncs(func => {
191+
root.walkFuncs((func) => {
188192
console.log(`Function: ${func.name}`);
189193
});
190194
```

docs/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ Please see the [Exports](./Exports.md) documentation for further information.
1616

1717
Parsing is powered by [css-tree](https://github.com/csstree/csstree). Nodes in this package extend PostCSS `Node`/`Container`/`Root` so the API feels familiar, but there is no PostCSS parser involved.
1818

19-
> Note: This package is ESM‑only. Use `import` syntax in Node.js. If you must use CommonJS, load it via dynamic import: `const mod = await import('postcss-values-parser')`.
19+
> Note: This package is ESM‑only. Prefer `import` in Node.js. For CommonJS on Node >= 20.19.0 you can use:
20+
>
21+
> ```js
22+
> // CommonJS (Node >= 20.19.0)
23+
> const { parse } = require('postcss-values-parser');
24+
> ```
25+
>
26+
> On older Node versions, use dynamic import instead.
27+
>
28+
> ```js
29+
> // CommonJS (older Node)
30+
> (async () => {
31+
> const { parse } = await import('postcss-values-parser');
32+
> })();
33+
> ```
2034
2135
## Nodes
2236

docs/Stringify.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The string representation of the node.
8282
### Example Usage
8383

8484
```js
85-
import { parse, nodeToString } from 'postcss-values-parser';
85+
import { nodeToString, parse } from 'postcss-values-parser';
8686

8787
const root = parse('10px solid red');
8888
const numericNode = root.nodes[0];
@@ -103,7 +103,7 @@ interface Stringifier {
103103
### Example Custom Stringifier
104104

105105
```js
106-
const { parse } = require('postcss-values-parser');
106+
import { parse } from 'postcss-values-parser';
107107

108108
// Custom stringifier that uppercases all word values
109109
const upperCaseStringifier = (node, builder) => {
@@ -113,7 +113,7 @@ const upperCaseStringifier = (node, builder) => {
113113
builder(node.value + node.unit);
114114
} else if (node.nodes) {
115115
// Handle container nodes
116-
node.nodes.forEach(child => {
116+
node.nodes.forEach((child) => {
117117
upperCaseStringifier(child, builder);
118118
});
119119
} else {
@@ -131,7 +131,7 @@ console.log(root.toString(upperCaseStringifier)); // '10pxSOLIDRED'
131131
All nodes have a `toString()` method that accepts an optional stringifier parameter:
132132

133133
```js
134-
const { parse } = require('postcss-values-parser');
134+
import { parse } from 'postcss-values-parser';
135135

136136
const root = parse('calc(100px + 20%)');
137137

@@ -149,7 +149,7 @@ console.log(root.toString(customStringifier));
149149
The stringify function can preserve original formatting and spacing when nodes maintain source mapping information:
150150

151151
```js
152-
const { parse } = require('postcss-values-parser');
152+
import { parse } from 'postcss-values-parser';
153153

154154
const root = parse('calc( 100px + 20% )'); // Note the extra spaces
155155
console.log(root.toString()); // Preserves original spacing
@@ -160,7 +160,7 @@ console.log(root.toString()); // Preserves original spacing
160160
When nodes have source mapping information, the stringify function can utilize this information to maintain accurate positioning:
161161

162162
```js
163-
const { parse } = require('postcss-values-parser');
163+
import { parse } from 'postcss-values-parser';
164164

165165
const root = parse('calc(100px + 20%)');
166166
// Source mapping information is preserved during stringification
@@ -171,7 +171,7 @@ const root = parse('calc(100px + 20%)');
171171
The builder function can be used to create complex string manipulations:
172172

173173
```js
174-
const { parse, stringify } = require('postcss-values-parser');
174+
import { parse, stringify } from 'postcss-values-parser';
175175

176176
const root = parse('10px solid red');
177177
const parts = [];

0 commit comments

Comments
 (0)