# postcss-styled-syntax

> PostCSS syntax for template literals CSS-in-JS (e. g. styled-components).

Latest version **0.7.4** (published 2026-10-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install postcss-styled-syntax
pnpm add postcss-styled-syntax
yarn add postcss-styled-syntax
bun add postcss-styled-syntax
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.7.4 |
| Published | 2026-10-15 |
| First published | 2023-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=14.17 |
| Dependencies | 1 |
| Unpacked size | 38.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 78 |
| Author | Aleks Hudochenkov |
| Maintainers | hudochenkov |
| Keywords | postcss, postcss-syntax, parser, css-in-js, styled-components |

## Links

- npm: https://www.npmjs.com/package/postcss-styled-syntax
- Repository: https://github.com/hudochenkov/postcss-styled-syntax
- Homepage: https://github.com/hudochenkov/postcss-styled-syntax#readme
- Issues: https://github.com/hudochenkov/postcss-styled-syntax/issues
- npm.io page: https://npm.io/package/postcss-styled-syntax

## Dependencies (1)

- [typescript](https://npm.io/package/typescript.md) ^6.0.3

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 0.7.4 (latest) — 2026-10-15
- 0.7.3 — 2026-09-27
- 0.7.2 — 2026-06-23
- 0.7.1 — 2025-01-16
- 0.7.0 — 2024-11-04
- 0.6.4 — 2024-01-04
- 0.6.3 — 2024-01-02
- 0.6.2 — 2023-12-30
- 0.6.1 — 2023-12-29
- 0.6.0 — 2023-12-27
- 0.5.0 — 2023-09-12
- 0.4.0 — 2023-03-07
- 0.3.3 — 2023-02-20
- 0.3.2 — 2023-02-15
- 0.3.1 — 2023-01-09
- … 3 more at https://npm.io/package/postcss-styled-syntax/versions

## README

# postcss-styled-syntax

PostCSS syntax for template literals CSS-in-JS (e. g. styled-components, Emotion). It was built to be used as [Stylelint] custom syntax or with [PostCSS] plugins.

Syntax supports:

- Full spectrum of styled-components syntax
- Deeply nested interpolations
- Interpolations in selectors, property names, and values
- JavaScript and TypeScript (including files with JSX)
- All functions:
	- <code>styled.foo``</code>
	- <code>styled(Component)``</code>
	- <code>styled.foo.attrs({})``</code>
	- <code>styled(Component).attrs({})``</code>
	- <code>styled.foo(props => ``)</code>
	- <code>styled(Component)(props => ``)</code>
	- <code>css``</code>
	- <code>createGlobalStyle``</code>

```js
let Component = styled.p`
	color: #bada55;
`;
```

## Install

```
npm install --save-dev postcss-styled-syntax
```

## Usage

### Stylelint

Install syntax and add to a Stylelint config:

```json
{
	"customSyntax": "postcss-styled-syntax"
}
```

Stylelint [custom syntax documentation](https://stylelint.io/user-guide/usage/options#customsyntax).

### PostCSS

Install syntax and add to a PostCSS config:

```js
export default {
	syntax: 'postcss-styled-syntax',
	plugins: [ /* ... */ ],
};
```

An example assumes using [PostCSS CLI](https://github.com/postcss/postcss-cli) or another PostCSS runner with config support.

## How it works

### Parsing

Syntax parser JavaScript/TypeScript code and find all supported components and functions (e.g., <code>css\`\`</code>). Then, it goes over them and builds a PostCSS AST, where all found components become `Root` nodes inside the `Document` node.

All interpolations within the found component CSS end up in the AST. E. g. for a declaration `color: ${brand}` `Decl` node will look like this:

```js
Decl {
	prop: 'color',
	value: '${brand}',
}
```

When interpolation is not part of any node, it goes to the next node's `raws.before`. For example, for the following code:

```js
let Component = styled.p`
	${textStyles}

	color: red;
`;
```

AST will look like:

```js
Decl {
	prop: 'color',
	value: 'red',
	raws: {
		before: '\n\t${textStyles}\n\n\t',
		// ...
	}
}
```

If there is no next node after interpolation, it will go to parents `raws.after`. For example, for the following code:

```js
let Component = styled.p`
	color: red;

	${textStyles}
`;
```

AST will look like:

```js
Root {
	nodes: [
		Decl {
			prop: 'color',
			value: 'red',
		},
	],
	raws: {
		after: '\n\n\t${textStyles}\n'
		// ...
	},
}
```

### Stringifying

Mostly, it works just as the default PostCSS stringifyer. The main difference is the `css` helper in interpolations within a styled component code. E. g. situations like this:

```js
let Component = styled.p`
	${(props) =>
		props.isPrimary
			? css`
					background: green;
			  `
			: css`
					border: 1px solid blue;
			  `}

	color: red;
`;
```

`css` helper inside an interpolation within `Component` code.

During parsing, the whole interpolation (`${(props) ... }`) is added as `raws.before` to `color: red` node. And it should not be modified. Each `css` helper remembers their original content (as a string).

When stringifyer reaches a node's `raws.before`, it checks if it has interpolations with `css` helpers. If yes, it searches for the original content of `css` helper and replaces it with a stringified `css` helper. This way, changes to the AST of the `css` helper will be stringified.

## Known issues

- Double-slash comments (`//`) will result in a parsing error. Use standard CSS comments instead (`/* */`). It is definitely possible to add support for double-slash comments, but let's use standard CSS as much as possible

- Source maps won't work or cannot be trusted. I did not disable them on purpose. But did not test them at all. Because of the way we need to handle `css` helpers within a styled component, `source.end` positions on a node might change if `css` AST changes. See the “How it works” section on stringifying for more info.

## Acknowledgements

[PostCSS] for tokenizer, parser, stringifier, and tests for them.

[Prettier](https://prettier.io/) for styled-components detection function in an ESTree AST.

[Stylelint]: https://stylelint.io/
[PostCSS]: https://postcss.org/

---
_Source: https://npm.io/package/postcss-styled-syntax · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
