Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@css-blocks/core

linkedin80BSD-2-Clause1.5.0TypeScript support: included

Fast, maintainable, optimal, component-oriented CSS.

css

readme

CSS Blocks

@css-blocks/core

@css-blocks/core drives everything that happens between reading a Block file and outputting final CSS! This package nearly deserves to be a mono-repo in its own right. All BlockSyntax features, functionality for constructing BlockTrees, the base class for all Analyzers, the BlockFactory and BlockCompiler implementations, and more, live in this package. As such, this codebase is best described in "packages", as you'll see below.

Options

Option Default Description
rootDir process.cwd() The root directory from which all sources are relative.
outputMode "BEM" Block file output mode. One of OutputMode
preprocessors {} A preprocessor function can be declared by Syntax.
importer NodeJsImporter A custom importer to resolve identifiers passed to @block.
importerData {} Additional data to make available to the importer.
maxConcurrentCompiles 4 Limits block parsing and compilation to this number of threads at any one time.
disablePreprocessChaining false If a preprocessor function is declared for css, all blocks will be ran through it, even those that were pre-processed for another syntax. This can be disabled by setting disablePreprocessChaining to true.

Packages

/src/BlockTree

A BlockTree is an in-memory data model that captures all the complexity represented in a single Block file and exposes APIs for querying and manipulating its attributes.

BlockTrees are strongly typed (read: children must be of a single type), N-Ary (read: every layer may have any number of child nodes) trees composed of four (4) possible node types. These nodes must be arranged in the following hierarchy:

Block => BlockClass => Attribute => AttrValue

The core implementation of BlockTree nodes, which every node type extends, lives in /src/BlockTree/Inheritable.ts. All nodes have common APIs concerning tree hierarchy and inheritance / resolution.

Inheritance Example Here.

These four (4) node types each fit in to one of two (2) classifications: Container Nodes and Style Nodes. These classifications and node types are described in detail below.

Container Nodes

Container Nodes...contain other nodes ;) These nodes contain logical groupings of like-nodes. Each container node type may implement convenience methods that make sense for the type of nodes it contains.

Block

A Block node is always the root of any BlockTree. A Block may be parent to any number of BlockClasses. The :scope selector is considered a special kind of BlockClass selector and is also stored as a child of Block.

Block nodes also store all data related to any @blocks, the block-name, implemented Blocks, the inherited Block, and any other metadata stored in the Block file. Blocks also have a special rootClass property that points directly to the child BlockClass that represents the parsed :scope selector .

Attribute

An Attribute node represents a single unique attribute namespace|name pair and is a parent to any number of AttrValue nodes, which represent all the possible attribute values for this Attribute discovered in the Block file. An Attribute node's parent is always a BlockClass. Attribute selectors where no value is specified are considered a special kind of AttrValue and is also stored as a child of Attribute.

Attribute nodes expose APIs suited for querying information about their children AttrValue.

Style Nodes

Style nodes represent fully resolved selectors that apply a CSS ruleset the the matching elements. For css-blocks these are BlockClass and AttrValue nodes.

Style nodes inherit from the abstract /src/BlockTree/Style.ts class and augment the base Inheritable class to store RulesetContainer objects to track property concerns and property resolutions from all rulesets that target this Style and its pseudo-elements, and possess methods to query own and inherited generated class names for the Style node.

BlockClass

BlockClass nodes represent class selectors discovered in a Block file. BlockClass nodes may contain one to many Attribute container nodes and have methods for querying own and inherited Attribute and their AttrValue nodes.

AttrValue

AttrValue nodes represent an fully qualified attribute selector (meaning namespace, name and value are all defined) discovered in a Block file. AttrValue nodes are leaf nodes and may have no children.

All Together Now

All these BlockTree objects, and the APIs they provide, enable css-blocks core to build an in-memory representation of any Block file, its dependencies, and all its data, in a format that is easily traversable and query-able.

For details on all the APIs available on BlockTrees and their constituent parts, I invite you to explore the API documentation.

/src/BlockCompiler

The BlockCompiler package delivers a single class: the BlockCompiler (go figure 😉).

BlockCompilers are responsible for taking a Block object, a postcss.Root (and an optional Analyzer to help guide final stylesheet output), and returning a transformed postcss.Root with all classes and states replaces with their globally unique output names, and all resolution and inheritance selectors emitted in the stylesheet.

Note: Currently in master we don't accept a whole Analyzer, just a single Analysis. We should.

Much of what currently goes in to Block compilation amounts to a find and replace of all classes and states with their generated, globally unique, class names. The format of these generated selectors are dictated by the OutputMode specified in the CSS Blocks' configuration, and defaults to BEM.

Block compilation becomes a little more complicated once we begin emitting conflict resolution selectors. In cases where explicit resolutions are provided, or when one Block inherits from another and re-defined an inherited CSS property, we need to emit a conflict resolution selector so the browser exhibits the expected behavior. This involves merging two, potentially complicated, selectors so the new selector will only match when both overridden selectors are applied. For example:

Input

/* other.css */
:scope { block-name: "other"; }
:scope[active] .bar { color: blue; }

/* main.css */
@block other from "./other.css";
:scope { block-name: "main"; }
:scope:hover .foo { color: red; color: resolve("other.bar"); }

Output

/* Compiled "other.css" */
.other--active .other__bar { color: blue; }

/* Compiled "main.css" */
.main:hover .main__foo { color: red; }

/* Emitted Resolution Selector */
.other--active.main:hover .main__foo.other__bar { color: blue; }

/src/BlockParser

The BlockParser package contains all constructs that handle converting an Block file into a BlockTree, including preprocessor integrations.

BlockFactory

The primary class delivered by BlockParser is the BlockFactory. The BlockFactory is responsible for creating new Block objects and ensuring that every unique Block file is only parsed once. If the same file is re-requested, the BlockFactory will return the same promise as the previous parse request which will resolve with the same shared Block object. Like most Factory Pattern implementations, most consumers will exclusively interface with the BlockFactory when creating new Blocks and should not have to worry about the parser itself.

Preprocessor Support

It is also the responsibility of the BlockFactory to only start Block compilation after all user-provided preprocessor steps have been finished. The configuration options for preprocessor integration can be found in this package.

BlockParser

Under the hood, the BlockFactory uses a BlockParser to convert the provided postcss.Root into a new Block object. The BlockParser is modeled after something akin to the Builder Pattern and runs the newly minted Block through a series of feature-specific "middleware". Each middleware, found in src/BlockParser/features, is responsible for reading, and augmenting the new Block, with a single language feature concern.

It is important that the supplied postcss.Root is not transformed in any way during this parse phase. The postcss tree should be considered read-only in all BlockParser feature middleware and only be used to construct the resulting BlockTree.

Note: The only place that block-intermediates are used outside of BlockParser is one place in BlockTree. We should work to refactor block-intermediates out of BlockTree and make it a construct completely private to the package.

/src/BlockSyntax

The BlockSyntax package delivers CSS Blocks specific syntax constants, and a few simple parsing functions, used in Block files. [You can look at the API documentation][TODO] for details.

All CSS Blocks specific syntax used in Block files should be defined here. No other package should be re-defining these constants for parsing functions.

/src/Analyzer

Note: I'm writing this section, not as the Analyzer implementation is currently written, but how I'd like to see it implemented in the near future. I have this working as described here in a branch.

The Analyzer package delivers the classes, data models, and types that are required by a [Template Integration][TODO]. It is the Template Integrations' responsibility to, given a number of template entry-points, know how to crawl the template dependency tree and analyze every element of every template discovered.

There are three (3) core classes that drive every Template Analyzer integration:

Analyzer

The Analyzer class is the base class that all Template Integrations must derive from. It represents the project-wide analysis of all templates reachable from the list of entry-points provided. An extender of Analyzer is expected to implement the abstract analyze(...entry-points: string) method, as this is what will be called by Build Integrations to kick off analysis.

The Analyzer has a factory method, getAnalysis() to retrieve a new Analysis object (described below) for each template discovered. The Analyzer will then crawl the contents of the template and log all Block usage data for the template on that Analysis object.

Analyzers have a number of convenience methods for accessing and iterating over Analysis objects created after an analysis pass. Analysis objects may be re-used (ex: in dev rebuilds) by calling their reset() method to clear all caches and saved data from the previous analysis pass. These methods can be explored over in the [Analysis API documentation][TODO].

Analysis

The Analysis object represents a single template's Block usage data. These are created by the Analyzer during an analysis pass for every template discovered when crawling the template dependency tree.

It is the Template Integration's responsibility to assemble each Analysis to accurately represent the Block usage in the template. This includes adding all referenced Blocks to the Analysis object, and creating a new ElementAnalysis (described below) for every element in the template and registering all used Block styles with the ElementAnalysis.

Analysis objects function as factories for ElementAnalysis objects. When a new element is discovered, Template Integrations can call startElement() on the current template's Analysis, to create a new ElementAnalysis. The integration can then add discovered Block styles to the ElementAnalysis. Once all Block styles have been registered with the ElementAnalysis, the integration may then call endElement() to seal the ElementAnalysis.

ElementAnalysis

The ElementAnalysis object represents a single element's Block usage data and are retrieved from a template's Analysis object using the Analysis.startElement() factory function. The last returned ElementAnalysis object remains un-sealed until Analysis.endElement() is called.

Un-sealed ElementAnalysis objects may be used to store Block style usage data saved to them (read: BlockClass and AttrValues). Any given Block style used in a template may be either Static, Dynamic, or Mutually Exclusive.

For example, given the following Block file, we can determine the type of usage in the handlebars snippets below:

.my-class            { /* ... */ }
.other-class         { /* ... */ }
[active]       { /* ... */ }
[color="red"]  { /* ... */ }
[color="blue"] { /* ... */ }

Static styles are guaranteed to never change:

<div block:class="my-class" block:active="true"></div>

Dynamic styles may or may not be applied depending on application state:

<div block:class="{{style-if value 'my-class'}}" block:active={{isActive}}></div>

Mutually Exclusive styles are guaranteed to never be used on the element at the same time:

{{!-- `my-class` and `other-class` are mutually exclusive --}}
{{!-- `[color=red]` and `[color=blue]` are mutually exclusive --}}
<div block:class="{{style-if value 'my-class' 'other-class'}}" block:color={{color}}></div>

Every Template Integration's syntax for consuming Blocks will differ slightly. It is the responsibility of the integration to implement template parsing and Block object discovery to feed in to the ElementAnalysis APIs. You can read more about these style tracking methods on the [ElementAnalysis API documentation][https://css-blocks.com/api/classes/_css_blocks_core.elementanalysis.html].

Once an ElementAnalysis is sealed, a number of automatic validations run on it to ensure no template rules have been violated. These template validators are defined as independent plugins and may be enabled or disabled individually. By default, they are all enabled. These validations live under /src/Analyzer/validations and include:

  • attribute-group-validator: Verify that any given State attribute is only applied once to an element.
  • attribute-parent-validator: Ensure that State attributes are always applied with their owner class.
  • class-paris-validator: If two classes from the same block are applied to the same element, throw.
  • property-conflict-validator: If two styles might be applied at the same time on the same element, and they have an un-resolved conflicting property concern, throw.
  • root-class-validator: Prevent the :scope class and a BlockClass from being applied to the same element.

/src/TemplateRewriter

Because each Template Integration has to leverage whatever plugin / AST transform system is provided by the templating system, Rewriters are a little more free-form than Analyzers. As such, there is no single base class for Rewriters to extend from.

Instead, this package delivers data models that Template Integrations may leverage to query data about how to rewrite elements they encounter during the rewrite phase. It is the responsibility of the Build Integration to shuttle these rewrite data to the actual rewriter integration.

Note: We really need to standardize how data is handed off from the Analyzer to the Rewriter, agnostic of the Build Integration... This works for the limited number of template integrations we have today, but will not scale well. Aka: a Vue integration may need its own Broccoli build integration because Broccoli is currently very Glimmer specific. We have too tight a coupling between Template Integration and Build Integration.

TODO: Write more about TemplateRewriter data models and their APIs.

/src/configuration

The configuration package contains the CSS Blocks build configuration utilities, including Typescript types for the configuration hash, a configuration reader to normalize user-provided configuration hashes with default values.

See the options table at the top of this file for configuration object details.

/src/importing

CSS Blocks needs to know where to get a Block file's contents when provided a file FileIdentifier from an @block. Most of the time, this file path will be a file on disk, in which case the default importer delivered with CSS Blocks will work out of the box. However, in cases where custom resolution of @blocks are required, consumers are able to provide their own implementation of the CSS Blocks Importer interface to deliver this custom behavior.

A custom importer may be passed to CSS Blocks via the importer options of the configuration object. Custom importers will understand how to resolve information about the FileIdentifier passed to @block and are used to abstract application or platform specific path resolution logic.

Any CSS Blocks Importer must implement the interface defined for a CSS Blocks Importer in /src/importing/types.ts. Every importer is required to have a number of introspection methods that return standard metadata for a given FileIdentifier:

  • identifier: Return a globally unique identifier for the FileIdentifier
  • defaultName: Return the default Block name to use if no block-name is set.
  • filesystemPath: If this FileIdentifier is backed by the filesystem, return the absolute file path.
  • debugIdentifier: Returns a string meant for human consumption that identifies the file. Used for debug and error reporting.
  • syntax: Return the syntax type the contents of this file are written in. One of Syntax.

However, the primary method for any importer is its import() method. import() returns a promise that resolves with a metadata object which not only contains all the information outlined above, but also the stringified contents of the file. It is these contents that the BlockFactory will use to create a BlockTree.

For any custom importers that require extra data to be passed by the end-user, the importerData CSS BLocks config option has been specially reserved as a namespaced location for extra importer data to be passed. All importer methods are passed the full CSS Blocks config object as their last argument.

CSS Blocks ships with one (1) pre-defined importers.

  1. NodeJsImporter: This is the default importer used by CSS Blocks if no other is provided. It enables @blocks to resolve relative and absolute file references. Paths are resolved using the following algorithm:
    • If an absolute path, resolve to the specified location.
    • If a relative path, attempt to resolve relative to the containing file.
    • If relative paths do not match a file on disk, test if the relative import path has a first segment that matches any of the aliases provided. The path will be made absolute using that alias's path location.
    • Finally, any relative path is resolved against the rootDir specified in the CSS Block configuration options.

/src/util

Utilities used inside the CSS Blocks repo. These are:

  • PromiseQueue: Enqueue a series of tasks to run in parallel. If a task fails, it will wait for all running jobs to either finish or fail before rejecting.
  • unionInto: Like Object.assign, but for Sets.

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.5.0 (2020-09-16)

Bug Fixes

  • Pick up fix for opticss crash on unknown css declarations. (55f2245)
  • Remove some lingering traces of the async factory. (23308e1)
  • Update the debug identifier for BlockFactorySync. (17588e3)

Features

  • Synchronous Block Factory. (0b31607)

1.4.0 (2020-09-04)

Bug Fixes

  • Only register guids from blocks that are valid. (b234f1d)

Features

  • End-to-end sourcemaps for ember v2 pipeline. (fec42e4)
  • Update concatenation. WIP sourcemaps fix. (da47ce6)

1.3.2 (2020-08-20)

Bug Fixes

  • Make location writeable so ember-cli-htmlbars doesn't choke. (8d513ba)

1.3.0 (2020-08-11)

Bug Fixes

  • Don't use the css-blocks installation location for guid gen. (f21e652)

1.2.0 (2020-08-05)

Bug Fixes

  • Addl. block-interface-index error case. (dc5ba19)
  • Address PR feedback. (6f04b6a)
  • Address PR feedback. (68271ad)
  • Allow quotes surrounding block-name. (9617fc5)
  • Allow the term classname as a single word. (5201155)
  • Analysis serialization had incorrect source locations. (9969614)
  • Apply CR suggestions (b1068f3)
  • Attribute intersections interpreted incorrectly during compilation. (63b06e8)
  • Clarify error message. (b16f3dc)
  • Cleanup the deserialization code for analysis. (2615eb9)
  • Comment should say Style not BlockClass. (006cc0f)
  • Create a 'Source Analysis' for the new rewrite strategy. (deefcdd)
  • Don't hard error on name/guid issues. (18f7364)
  • Emit block style composition declarations in a def file. (d1185db)
  • Failing tests and linting issues for core. (afc157b)
  • Implement inline definitions in compiled blocks. (e8541a0)
  • Lint error. (436d09a)
  • Lint errors. (6eda525)
  • PR feedback regarding errors. (a7384c8)
  • Re-use precomiled css if available. Give compiled css to optimizer. (5027298)
  • Remove superflous property check. (31bcb9e)
  • Remove unnecessary try/catch. (492c1e7)
  • Remove unnecessary type guard named isBooleanAttr. (ebcb555)
  • Removed stray reference to obsolete isBooleanAttr function. (a825a16)
  • Revert last commit. (69494a4)
  • Serialized analysis block paths were missing/wrong. (e680ef6)
  • Several inheritance and composition bugs. (4f23cc3)
  • Throw in BlockFactory if compiled css file. (3d901e6)
  • Trim newlines from end of compiled comments. (62eb34e)
  • Update comment per CR. (7e3fe64)
  • Updates per CR feedback. (717a205)
  • Use debug idents for errors in BlockFactory. (294f0be)
  • Use null in getUniqueBlockName. (8118d49)

Features

  • Add BlockCompiler method for compiling a definition file. (183dc2f)
  • Basic block definition generation. (8a3cade)
  • Basic runtime data generation with optimizer disabled. (cabd495)
  • Compiled CSS importing in NodeJSImporter. (983e7c6)
  • Data schema for Aggregate Rewriting. (ca10a16)
  • Definition ingestion and parsing into block. (0d6e76a)
  • Deserializing block definition files & analysis in the ember-app. (ec338bf)
  • Generate an index for each style in a block. (94d1ded)
  • Infrastructure for single pass analyzer & rewriter. (466b933)
  • Merge rulesets from Compiled CSS into block. (e6c1ca7)
  • Parse and set block-interface-index (7a0150d)
  • Process block-class declarations. (fa35c3d)
  • Show the identifier of the other block if a name collision occurs. (140d3cd)
  • Use incoming GUIDs. Ensure uniqueness. (3912811)
  • Utilities for compiled CSS parsing. (bec10d2)
  • Validate block-syntax-version. (179d3db)
  • Validate each interface-index is unique. (92a5b25)

1.1.2 (2020-07-20)

Bug Fixes

  • Switches in the rewrite didn't work with inheritance. (360a28f)

1.1.1 (2020-06-30)

Bug Fixes

  • Attribute intersections interpreted incorrectly during compilation. (41f9816)

1.0.0 (2020-04-04)

chore

  • Drop support for node 6, 8, and 11. (3806e82)

Features

  • Optional Preprocessors & library/application API contract. (80aba33)

BREAKING CHANGES

  • Node 8 is now out of maintainence so we have dropped support for node 6 and 8. Node 11 is no longer needed because node 12 was released.

1.0.0-alpha.6 (2020-02-19)

Bug Fixes

  • Avoid Promise.all() because of possible race conditions. (61d0e54)
  • More robust importing. (37dcdfb)
  • Only raise MultipleCssBlockErrors if there's more than one. (96fdd29)

1.0.0-alpha.5 (2020-02-14)

Bug Fixes

  • Capture block parsing errors in the promise. (35c3991)
  • Fixing the CLI test failures. (5ff37a1)
  • Getting rid of duplicate assertions. (a3eee56)
  • Rename parseSync to parseRoot. (f4c95c4)

Features

  • Adding a new class of errors - MultipleCssBlockErrors. (14c1d31)
  • Convert methods to start recording multiple errors. (c2a3271)
  • Converting composes block errors. (5455597)
  • Converting export and import blocks to use multple errors. (6b3e3f7)
  • Converting to multiple errors for a few more features. (c9c790e)
  • Getting rid of more thrown errors. (29cc368)

1.0.0-alpha.4 (2019-12-18)

Bug Fixes

  • Conflict Resolutions with Media Queries. (c189613), closes #372

1.0.0-alpha.3 (2019-12-11)

Bug Fixes

  • Don't cache block errors in the factory. (e931e63)

1.0.0-alpha.1 (2019-12-10)

Bug Fixes

1.0.0-alpha.0 (2019-11-22)

Bug Fixes

  • A state cannot be named 'scope'. (12a0f32)
  • Addressing comments from Chris. (afedab9)
  • Cannot export a block as a reserved namespace identifier. (e82f636)
  • Don't allow blocks to be imported with a well-known namespace. (6fc3675)
  • Fix common misspelling of 'cannot'. (457e08c)
  • Fixing a few lint errors after a rebase. (4a05b40)
  • Fixing tests. (7d368cc)
  • For when the block-alias is the same name as a generated className. (bd36033)
  • Global states can be combined with the :scope selector. (92f8093)
  • Making an error message slightly nicer. (e74d019)
  • Removing an addressed TODO. (0e763de)
  • Small tweaks around parameter passing. (5d91c56)

Features

  • Adding a custom importer for the language-server. (d5bd9c3)
  • Introducing the block-alias. (5517d72)
  • Passing all block aliases as reserved classNames for compilation. (aea5fcc)
  • Per block namespaces. (b9c4938)
  • Respect explicit exports for a block interface. (d37e704)

0.24.0 (2019-09-16)

Features

  • Display block import references in error output. (190993f), closes #248
  • Display selector error locations using sourcemaps. (78756f2)
  • Track ranges instead of only the start position for errors. (f7f2dfb)
  • Use sourcemaps for errors involving non-selector nodes. (f7b53fd)
  • cli: Display error in context with the source file's contents. (2317880)

0.23.0 (2019-05-08)

Bug Fixes

  • Don't set default rootDir at time of import. (f1821fd)
  • Silence postcss warning message. (adb7d68)

0.22.0 (2019-05-02)

Bug Fixes

  • Handle legacy type definition for sourcemap's RawSourceMap. (842454a)
  • Over-zealous conflicts from inherited in-stylesheet compositions. (c70ed03)
  • Print an empty string if the source location isn't available. (598477f)
  • Remove code branch that always returned false. (df66b13)

0.21.0 (2019-04-07)

Bug Fixes

  • Properly output conflict resolutions for shorthands. (#238) (2f93f99)

Features

0.20.0 (2019-03-11)

Note: Version bump only for package @css-blocks/core

0.20.0-beta.7 (2019-02-01)

Note: Version bump only for package @css-blocks/core

0.20.0-beta.6 (2019-02-01)

Note: Version bump only for package @css-blocks/core

0.20.0-beta.5 (2019-01-08)

Bug Fixes

  • core: Dont gitignore node_modules importer test fixtures. (fc508eb)
  • core: Remove stray console.log. Add debug logs. (84d5419)
  • Improve Block ref parser. (90bfbff)

Features

  • Extended @block syntax. Issue #192. (9cbb4ea)
  • core: Default and custom 'main' module block resolution. (d8585ee)
  • core: Simple fully-qualified path node_modules Block imports. (7eb9005)

0.20.0-beta.4 (2018-10-19)

Features

  • Manually throw error for Node 6 in Analyzer. (5788fcc)

0.20.0-beta.3 (2018-10-01)

Features

  • Ember CLI addon Preprocessor support. (574483d)

0.20.0-beta.0 (2018-08-20)

Features

0.19.0 (2018-04-25)

Note: Version bump only for package @css-blocks/core

0.18.0 (2018-04-24)

Bug Fixes

  • Update global states to use simplified parser utils. (b953602)

Features

  • Added css-blocks.com website package and custom docs theme. (b5ad979)
  • Block Object asSource methods take optional Block scope. (370dfd1)
  • Enable root-level typedoc generation for the project. (557fd49)
  • Enable root-level typedoc generation for the project. (59c85a3)
  • Require the :scope pseudo for root states. (1e48882)

0.17.0 (2017-12-08)

Note: Version bump only for package css-blocks