# @decisiv/babel-plugin-styled-components-references

> Babel plugin to replace references to styled components inside styled-components template literals with their paths

Latest version **1.3.7** (published 2020-08-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @decisiv/babel-plugin-styled-components-references
pnpm add @decisiv/babel-plugin-styled-components-references
yarn add @decisiv/babel-plugin-styled-components-references
bun add @decisiv/babel-plugin-styled-components-references
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.3.7 |
| Published | 2020-08-04 |
| First published | 2019-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 19.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | decisiv |

## Links

- npm: https://www.npmjs.com/package/@decisiv/babel-plugin-styled-components-references
- npm.io page: https://npm.io/package/@decisiv/babel-plugin-styled-components-references

## Dependencies (5)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [resolve](https://npm.io/package/resolve.md) ^1.10.1
- [fs-extra](https://npm.io/package/fs-extra.md) ^8.0.1
- [@babel/core](https://npm.io/package/@babel/core.md) ^7.4.4
- [find-yarn-workspace-root](https://npm.io/package/find-yarn-workspace-root.md) ^1.2.1

## Recent versions

- 1.3.7 (latest) — 2020-08-04
- 1.3.8-alpha.7 (alpha) — 2026-09-24
- 1.3.8-alpha.3 (next) — 2020-08-10
- 1.3.8-alpha.6 — 2026-09-09
- 1.3.8-alpha.5 — 2026-05-20
- 1.3.8-alpha.4 — 2026-05-20
- 1.3.7-alpha.38 — 2020-05-04
- 1.3.6 — 2019-12-18
- 1.3.5 — 2019-11-22
- 1.3.4 — 2019-10-16
- 1.3.3 — 2019-10-10
- 1.3.2 — 2019-10-09

## README

# babel-plugin-styled-components-references

## Overview

The purpose of this Babel plugin is to replace references to styled components
inside template strings with a string pattern containing the referenced styled
component's name and its path in the filesystem.

Those strings uniquely identify styled components, which is then useful when
extracting the components styles into static stylesheets. Since the strings are
unique per component, they can be easily replaced by any other string one might
want, such as a unique css selector that targets that component.

Extracting styles of a styled component can be done by using the
[SSR features of styled-components](https://www.styled-components.com/docs/advanced#server-side-rendering).
If we do that for the following code...

```jsx
import { Icon } from 'some-place';

const MyComponent = styled.div`
  color: green;

  ${Icon} {
    margin-left: 10px;
  }

  ${Icon}::before {
    content: '';
  }
`;
```

...then we get something like...

```css
.xhh21s2 {
  color: green;
}

.xhh21s2 .kjhkh12 {
  margin-left: 10px;
}

.xhh21s2 .kjhkh12::before {
  content: '';
}
```

...where the css selector for `Icon` is unique to that specific reference in the
whole codebase. By only reading the generated stylesheet, one cannot know to
which component a selector refers to.

By using this babel plugin, the resulting css would be something like:

```css
.xhh21s2 {
  color: green;
}

.xhh21s2 [Icon:path/to/the/component/in/the/filesystem] {
  margin-left: 10px;
}

.xhh21s2 [Icon:path/to/the/component/in/the/filesystem]::before {
  content: '';
}
```

This then allows us to replace all the references to that component by any
selector we want:

```css
.xhh21s2 {
  color: green;
}

.xhh21s2 .icon {
  margin-left: 10px;
}

.xhh21s2 .icon::before {
  content: '';
}
```

And by SSR rendering each component individually, we can also replace the main
selector for that component, which could then result in something like:

```css
.my-component {
  color: green;
}

.my-component .icon {
  margin-left: 10px;
}

.my-component .icon::before {
  content: '';
}
```

---
_Source: https://npm.io/package/@decisiv/babel-plugin-styled-components-references · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
