Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,28 @@ for (const rule of ast) {
- **Location tracking with no penalty** — full line, column, and offset data without slowing the parser down
- **No syntax validation** — skipping spec checks and MDN data means nothing gets in the way of speed

### Bundle sizes

All sizes are minified and bundled with dependencies. Import only what you need — bundlers will tree-shake the rest.

| Import | Description | Minified | Gzip |
| ------------------------------------------------- | ---------------------------- | -------- | ------- |
| `@projectwallace/css-parser` | Full parser (all subparsers) | 68.9 kB | 15.5 kB |
| `@projectwallace/css-parser/parse` | CSS stylesheet parser | 62.2 kB | 13.3 kB |
| `@projectwallace/css-parser/parse-atrule-prelude` | At-rule prelude parser | 35.4 kB | 8.4 kB |
| `@projectwallace/css-parser/parse-selector` | Selector parser | 37.7 kB | 8.9 kB |
| `@projectwallace/css-parser/parse-anplusb` | An+B syntax parser | 26.1 kB | 6.6 kB |
| `@projectwallace/css-parser/parse-declaration` | Declaration parser | 28.3 kB | 7.3 kB |
| `@projectwallace/css-parser/parse-value` | Value parser | 25.2 kB | 6.6 kB |
| `@projectwallace/css-parser/parse-dimension` | Dimension parser | 0.4 kB | 0.3 kB |
| `@projectwallace/css-parser/tokenizer` | Tokenizer | 9.8 kB | 2.4 kB |

## API

Every function is exported from the package root and, where listed in [Bundle sizes](#bundle-sizes), from its own entry point. Node properties and return types ship as TypeScript types, so this section sticks to short examples.
Everything is exported from the package root. Each part is also available from its own entry point, so you can import only what you need and skip evaluating the rest of the package. Node properties and return types ship as TypeScript types, so this section sticks to short examples.

### Entry points

| Import | Exports |
| ------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| `@projectwallace/css-parser` | Everything below, except the `Lexer` and `Parser` classes |
| `@projectwallace/css-parser/parse` | `parse`, `Parser`, `ParserOptions` |
| `@projectwallace/css-parser/parse-selector` | `parse_selector`, `parse_selector_list` |
| `@projectwallace/css-parser/parse-atrule-prelude` | `parse_atrule_prelude` |
| `@projectwallace/css-parser/parse-declaration` | `parse_declaration` |
| `@projectwallace/css-parser/parse-value` | `parse_value` |
| `@projectwallace/css-parser/parse-anplusb` | `parse_anplusb` |
| `@projectwallace/css-parser/parse-dimension` | `parse_dimension` |
| `@projectwallace/css-parser/tokenizer` | `tokenize`, `Lexer` |
| `@projectwallace/css-parser/walker` | `walk`, `traverse`, `SKIP`, `BREAK` |
| `@projectwallace/css-parser/nodes` | Node type constants (`STYLE_RULE`, `DECLARATION`, …), `NODE_TYPES` |
| `@projectwallace/css-parser/node-types` | Type guards (`is_declaration`, `is_rule`, …) and node types (`CSSNode`, `Rule`, `Declaration`, …) |
| `@projectwallace/css-parser/string-utils` | `is_custom`, `is_vendor_prefixed`, `strip_vendor_prefix`, `str_equals`, `str_starts_with`, `str_index_of` |
| `@projectwallace/css-parser/token-types` | Token type constants (`TOKEN_*`) |

### `parse(source, options?)`

Expand Down Expand Up @@ -215,7 +218,7 @@ for (const token of tokenize('a { color: red }')) {

### Node types, type guards and `clone()`

Node type constants (`STYLE_RULE`, `DECLARATION`, `DIMENSION`, …) and `NODE_TYPES` are exported from the root, as are type guards such as `is_declaration()` and `is_dimension()` that narrow a node to its specific type. `node.clone({ deep?, locations? })` returns a mutable plain-object copy of a node.
Node type constants (`STYLE_RULE`, `DECLARATION`, `DIMENSION`, …) and `NODE_TYPES` are exported from the root and from `@projectwallace/css-parser/nodes`. Type guards such as `is_declaration()` and `is_dimension()` that narrow a node to its specific type are exported from the root and from `@projectwallace/css-parser/node-types`. `node.clone({ deep?, locations? })` returns a mutable plain-object copy of a node.

```typescript
import { parse, is_declaration } from '@projectwallace/css-parser'
Expand All @@ -228,19 +231,21 @@ decl.clone({ deep: false }).property // "color"

### String helpers

Allocation-free helpers, handy when working with node names. The lowercase argument is the first one for `str_equals` and the second one for `str_starts_with` and `str_index_of`. This inconsistency will be fixed in a future breaking change.
Allocation-free helpers, handy when working with node names. Also available from `@projectwallace/css-parser/string-utils`; token type constants (`TOKEN_*`) are available from `@projectwallace/css-parser/token-types`. The lowercase argument is the first one for `str_equals` and the second one for `str_starts_with` and `str_index_of`. This inconsistency will be fixed in a future breaking change.

```typescript
import {
is_custom,
is_vendor_prefixed,
strip_vendor_prefix,
str_equals,
str_starts_with,
str_index_of,
} from '@projectwallace/css-parser'

is_custom('--primary') // true
is_vendor_prefixed('-webkit-transform') // true
strip_vendor_prefix('-webkit-transform') // "transform"
str_equals('media', 'MEDIA') // true
str_starts_with('WEBKIT-transform', 'webkit') // true
str_index_of('background-COLOR', 'color') // 11
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
"./nodes": {
"types": "./dist/constants.d.ts",
"import": "./dist/constants.js"
},
"./node-types": {
"types": "./dist/node-types.d.ts",
"import": "./dist/node-types.js"
},
"./string-utils": {
"types": "./dist/string-helpers.d.ts",
"import": "./dist/string-helpers.js"
},
"./token-types": {
"types": "./dist/token-types.d.ts",
"import": "./dist/token-types.js"
}
},
"publishConfig": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export { walk, traverse, SKIP, BREAK } from './walk'
export {
is_custom,
is_vendor_prefixed,
strip_vendor_prefix,
str_equals,
str_starts_with,
str_index_of,
} from './string-utils'
} from './string-helpers'

// Advanced/class-based API
export { type ParserOptions } from './parse'
Expand Down
2 changes: 2 additions & 0 deletions src/node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// Type predicates compile to a single integer comparison each.

import type { CSSNodeType, TypeName, CloneOptions, PlainCSSNode } from './css-node'

export type { CloneOptions, PlainCSSNode }
import {
STYLESHEET,
STYLE_RULE,
Expand Down
10 changes: 10 additions & 0 deletions src/string-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Public string helpers - the subset of string-utils that is part of the public API.
// Kept separate so the rest of string-utils (CHAR_* constants, range helpers) stays internal.
export {
is_custom,
is_vendor_prefixed,
strip_vendor_prefix,
str_equals,
str_starts_with,
str_index_of,
} from './string-utils'
4 changes: 4 additions & 0 deletions test/build/bundle-sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const SOURCE_OVERRIDES: Record<string, string> = {
'./tokenizer': 'src/tokenize.ts',
'./walker': 'src/walk.ts',
'./nodes': 'src/constants.ts',
'./string-utils': 'src/string-helpers.ts',
}

const DESCRIPTIONS: Record<string, string> = {
Expand All @@ -31,6 +32,9 @@ const DESCRIPTIONS: Record<string, string> = {
'./parse-dimension': 'Dimension parser',
'./walker': 'AST walker',
'./nodes': 'Node type constants',
'./node-types': 'Node type guards',
'./string-utils': 'String helpers',
'./token-types': 'Token type constants',
}

const exports_to_measure = Object.keys(pkg.exports).filter((k: string) => k !== './package.json')
Expand Down
3 changes: 3 additions & 0 deletions tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export default defineConfig({
'./src/parse-dimension.ts',
'./src/walk.ts',
'./src/constants.ts',
'./src/node-types.ts',
'./src/string-helpers.ts',
'./src/token-types.ts',
],
platform: 'neutral',
dts: true,
Expand Down
Loading