Make the selector and at-rule prelude parsers tree-shakable

Ouverte
#352 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
43/100
Type d'issue
Fonctionnalité
Clarté
Plutôt claire
Activité
Active
Stack technique
css, javascript
Domaine
tooling

Piste de recherche

Start by reading parse.js, ValueNodeParser in the at-rule prelude chunk, and the constructors for ConditionParser and AtRulePreludeParser. Trace how the root entry and /parse assemble the parser, then check the package entry-point and build configuration before adding the lean entry. Done means existing parse() behavior remains intact and a core-only consumer can omit selector and prelude code from its bundle.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Problem

Bundling parse() always includes the selector parser and the at-rule prelude parser, even when parse_selectors and parse_atrule_preludes are never enabled. The options only switch them on at runtime, so a bundler cannot remove them.

Found while working on css-analyzer v10. Its engine only enables the parsers its collectors need, so a bundle with just a z-index collector should not carry selector or prelude code. It does carry it, all of it coming from this package.

Measurements

Minified rolldown bundles, @projectwallace/css-parser 0.19.7 bundled in:

Entry As published With the lazy parsers cut loose (see below)
parse('a{}') only 74.3 KB (15.4 gzip) 42.4 KB (10.2 gzip)
css-analyzer, z_indexes only 84.3 KB (19.1 gzip) 52.5 KB (13.8 gzip)
css-analyzer, colors only 85.3 KB (19.5 gzip) 53.5 KB (14.2 gzip)
css-analyzer, specificity only (needs selectors) 86.2 KB (19.8 gzip) 68.8 KB (17.0 gzip)
css-analyzer, everything 107.2 KB (25.4 gzip) 107.3 KB (25.4 gzip)

The right-hand column came from a patched copy of dist/ that stubs out the constructors described below. It breaks selector and prelude parsing, so read it as an upper bound on what a real change can save: about 32 KB (minified) for anything that only needs declarations and values.

Cause

package.json has sideEffects: false and the build is split into chunks, so this is not a packaging mistake. It is coupling inside the code:

  1. dist/parse.js imports SelectorParser, AtRulePreludeParser and DeclarationParser at the top and constructs them behind parse_selectors_enabled and parse_atrule_preludes_enabled.
  2. Stubbing only those two imports in parse.js changed nothing (84.3 KB before and after). The classes stay reachable another way.
  3. ValueNodeParser, the base of ValueParser, lives in the same chunk as the at-rule prelude parser (parse-atrule-prelude-*.js). It creates two parsers on demand:
    • ConditionParser, in its condition getter (_condition_parser);
    • AtRulePreludeParser, in its media prelude getter (_media_prelude_parser).
  4. ConditionParser and AtRulePreludeParser both construct a SelectorParser.

So every ValueParser, and therefore every parse() that parses values, statically reaches the prelude parser and the selector parser.

Proposal

  1. ValueNodeParser gets the condition and media-prelude parsers from the outside (factories passed to its constructor or set when the full parser is assembled), instead of calling new ConditionParser(...) and new AtRulePreludeParser(...) itself. When a factory is missing, fail with a clear error. These are only reached for syntax like if() / media() inside values.
  2. Move ValueNodeParser into its own module, out of the prelude chunk.
  3. parse() receives the selector and prelude parsers instead of importing them. The root entry and /parse keep behaving exactly as today by wiring everything in. Add a lean entry (name up for discussion, for example @projectwallace/css-parser/parse-core) where the caller passes only what it needs.

Step 3 alone does not help, because of step 1.

Nothing changes for current users of parse() or the root entry. Only consumers of the new entry opt in.

Langage dominant
CSS
Étoiles
4
Forks
0
Merge moyen
1 j 4 h
PR mergées (30 j)
27

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de projectwallace/css-parser

Toutes les issues de projectwallace/css-parser

Issues similaires

Plus d'issues DevTools

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.