PostCSS Inherit Parser
postcss-inherit-parser is a specialized parser for PostCSS, designed to interpret a custom CSS syntax that facilitates "inherit" declarations, specifically in conjunction with the `postcss-inherit` plugin. This parser enables the processing of CSS where rules can leverage inheritance from other selectors, including those with pseudo-classes, as exemplified by syntax like `.a { inherit: .b:before; }`. The package, currently at version 0.2.0, was last published approximately 8 years ago, suggesting it is no longer actively maintained. Its core functionality is to provide the Abstract Syntax Tree (AST) representation for this non-standard inheritance syntax, allowing subsequent PostCSS plugins to apply transformation logic. A key feature is the configurable `propertyRegExp` option, which allows consumers to define custom keywords for inheritance, such as `extend` instead of `inherit`. Given its age and low version, developers should be aware of potential compatibility issues with newer Node.js or PostCSS versions.
Common errors
-
Error: Unknown word (at line X, column Y)
cause The parser encountered an unrecognized token or custom `inherit` syntax that it could not process, likely because the `inheritParser` was not configured correctly or not applied as the PostCSS parser.fixEnsure `postcss-inherit-parser` is correctly passed to the `parser` option of `postcss.process` or `postcss.parse`. Verify that the `propertyRegExp` option is configured to match all custom inheritance keywords used in your CSS (e.g., `inherit`, `extend`). -
TypeError: (0 , _postcssInheritParser.parse) is not a function
cause This error typically occurs when attempting to use ESM `import { parse } from 'postcss-inherit-parser'` for a package that only provides CommonJS exports, which is the case for this old, unmaintained module. The module is imported, but the `parse` symbol is not found as a named export.fixChange your import statement to use CommonJS syntax: `const parse = require('postcss-inherit-parser');`. Ensure your Node.js or bundling environment correctly processes CommonJS modules. -
The 'parse' function must be passed to the PostCSS 'parser' option, not 'plugins'.
cause You've attempted to pass the `postcss-inherit-parser` function directly into the `postcss` plugin array or `.use()` method, rather than specifying it as the parser for the processing step. Parsers and plugins have distinct roles in PostCSS.fixPass the parser function via the `parser` option in `postcss.process(css, { parser: inheritParser.parse })` or when creating a PostCSS processor: `postcss([yourPlugin], { parser: inheritParser.parse })`.
Warnings
- breaking The package is extremely old (v0.2.0, published 8 years ago) and appears to be unmaintained. There will be no further bug fixes, new features, or compatibility updates for newer Node.js versions, PostCSS versions (current PostCSS is v8+), or evolving CSS specifications. This can lead to unexpected behavior or breakage in modern environments.
- gotcha This package is solely a parser; it converts the custom `inherit` syntax into a PostCSS Abstract Syntax Tree (AST). It does not, by itself, apply any inheritance logic to the CSS. To make the inheritance functional, you *must* use it in conjunction with a PostCSS plugin like `postcss-inherit`.
- gotcha Due to its age, `postcss-inherit-parser` is a CommonJS module. Attempting to use `import { parse } from 'postcss-inherit-parser'` in an ESM environment will likely result in a `TypeError` or similar import error, as it does not explicitly provide an ESM export.
- gotcha The parser relies on the `propertyRegExp` option for flexible `inherit` property naming. If you introduce custom keywords (e.g., `extends`) but do not update `propertyRegExp` correctly, the parser will not recognize them, treating them as standard CSS properties or throwing a syntax error.
Install
-
npm install postcss-inherit-parser -
yarn add postcss-inherit-parser -
pnpm add postcss-inherit-parser
Imports
- parse
import { parse } from 'postcss-inherit-parser';const parse = require('postcss-inherit-parser'); - PostCSS with parser
const postcss = require('postcss'); const inheritParser = require('postcss-inherit-parser'); const processor = postcss([/* your plugins here */], { parser: inheritParser }); // or const processor = postcss().use(inheritParser);const postcss = require('postcss'); const inheritParser = require('postcss-inherit-parser'); const processor = postcss().use(/* your plugins here */); const root = await processor.process(cssInput, { parser: inheritParser }).root;
Quickstart
const postcss = require('postcss');
const inheritParser = require('postcss-inherit-parser');
async function processCssWithInherit() {
const cssInput = `
.b:before {
content: "";
color: blue;
}
.a {
inherit: .b:before;
font-size: 16px;
}
.c {
extend: .a;
border: 1px solid black;
}
`;
// Use the parser with custom propertyRegExp
const root = await postcss().process(cssInput, {
from: undefined,
parser: inheritParser.parse,
parserOptions: { propertyRegExp: /^(inherit|extend)s?$/i }
}).root;
console.log('Parsed AST (first rule selector):', root.nodes[0].selector); // .b:before
console.log('Parsed AST (second rule inherit property):', root.nodes[1].nodes[0].prop); // inherit
console.log('Parsed AST (third rule extend property):', root.nodes[2].nodes[0].prop); // extend
// To apply the actual inheritance, you would typically use postcss-inherit as a plugin:
const postcssInherit = require('postcss-inherit');
const result = await postcss([postcssInherit()]).process(cssInput, {
from: undefined,
parser: inheritParser.parse,
parserOptions: { propertyRegExp: /^(inherit|extend)s?$/i }
});
console.log('\nCSS after processing with postcss-inherit:\n', result.css);
}
processCssWithInherit();