diff --git a/README.md b/README.md index 97ce78c..0f9c33a 100644 --- a/README.md +++ b/README.md @@ -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?)` @@ -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' @@ -228,12 +231,13 @@ 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, @@ -241,6 +245,7 @@ import { 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 diff --git a/package.json b/package.json index 92210dd..f021a4c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index 190bc6c..63d595a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/node-types.ts b/src/node-types.ts index 3d34052..54549bb 100644 --- a/src/node-types.ts +++ b/src/node-types.ts @@ -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, diff --git a/src/string-helpers.ts b/src/string-helpers.ts new file mode 100644 index 0000000..8eddc34 --- /dev/null +++ b/src/string-helpers.ts @@ -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' diff --git a/test/build/bundle-sizes.ts b/test/build/bundle-sizes.ts index 14c2fcf..a08e12f 100644 --- a/test/build/bundle-sizes.ts +++ b/test/build/bundle-sizes.ts @@ -17,6 +17,7 @@ const SOURCE_OVERRIDES: Record = { './tokenizer': 'src/tokenize.ts', './walker': 'src/walk.ts', './nodes': 'src/constants.ts', + './string-utils': 'src/string-helpers.ts', } const DESCRIPTIONS: Record = { @@ -31,6 +32,9 @@ const DESCRIPTIONS: Record = { './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') diff --git a/tsdown.config.ts b/tsdown.config.ts index 8824abe..b9d5f66 100644 --- a/tsdown.config.ts +++ b/tsdown.config.ts @@ -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,