Skip to content

Commit 69fdeab

Browse files
committed
Support specifying plugins or hooks when using rtlcss
1 parent f2e3c96 commit 69fdeab

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ All the options are optional, and a default value will be used if any of them is
382382
| stringMap | `PluginStringMap[]` | Check below | An array of strings maps that will be used to make the replacements of the declarations' URLs and to match the names of the rules if `processRuleNames` is `true` |
383383
| greedy | `boolean` | `false` | When greedy is `true`, the matches of `stringMap` will not take into account word boundaries |
384384
| aliases | `Record<string, string>` | `{}` | A strings map to treat some declarations as others |
385+
| plugins | `Array<{name: string, key?: string}>` | `[]` | A list of RTLCSS plugins |
386+
| hooks | `{pre?: function, post?: function}` | `{}` | RTLCSS hooks |
385387

386388
---
387389

@@ -1447,6 +1449,108 @@ const options = {
14471449

14481450
---
14491451

1452+
#### plugins
1453+
1454+
<details><summary>Expand</summary>
1455+
<p>
1456+
1457+
RTLCSS support to extend itself via [plugins](https://rtlcss.com/learn/extending-rtlcss/writing-a-plugin/)
1458+
1459+
##### input
1460+
1461+
```css
1462+
.test {
1463+
background-position: 0 100%;
1464+
}
1465+
```
1466+
1467+
##### Convert `0` to `100%` (default)
1468+
1469+
##### output
1470+
1471+
```css
1472+
.test {
1473+
background-position: 100% 100%;
1474+
}
1475+
```
1476+
1477+
##### Set a plugin to avoid flipping
1478+
1479+
```javascript
1480+
const options = {
1481+
plugins: [
1482+
{
1483+
name: 'ignore-background',
1484+
priority: 99, // above the core RTLCSS plugin which has a priority value of 100
1485+
directives: {
1486+
control: {},
1487+
value: []
1488+
},
1489+
processors: [{
1490+
expr: /(background|object)(-position(-x)?|-image)?$/i,
1491+
action: (prop, value) => ({prop, value})}
1492+
]
1493+
}
1494+
]
1495+
};
1496+
```
1497+
1498+
##### output
1499+
1500+
```css
1501+
.test {
1502+
background-position: 0 100%;
1503+
}
1504+
```
1505+
1506+
</p>
1507+
1508+
</details>
1509+
1510+
---
1511+
1512+
#### hooks
1513+
1514+
<details><summary>Expand</summary>
1515+
<p>
1516+
1517+
RTLCSS support to set up [hooks](https://rtlcss.com/learn/usage-guide/hooks/) before of after processing the CSS.
1518+
1519+
##### input
1520+
1521+
```css
1522+
body {
1523+
direction: ltr;
1524+
}
1525+
```
1526+
1527+
##### Set a plugin to add comments
1528+
1529+
```javascript
1530+
const options = {
1531+
hooks: {
1532+
post(root, postcss) {
1533+
root.insertBefore(root.nodes[0], postcss.comment({text: 'Generated by RTLCSS'}));
1534+
}
1535+
}
1536+
};
1537+
```
1538+
1539+
##### output
1540+
1541+
```css
1542+
/* Generated by RTLCSS */
1543+
body {
1544+
direction: ltr;
1545+
}
1546+
```
1547+
1548+
</p>
1549+
1550+
</details>
1551+
1552+
---
1553+
14501554
Control Directives
14511555
---
14521556

src/@types/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
AtRule,
44
Declaration
55
} from 'postcss';
6+
import {Plugin, HookOptions} from 'rtlcss';
67

78
export enum Mode {
89
combined = 'combined',
@@ -58,6 +59,8 @@ export interface PluginOptions {
5859
stringMap?: PluginStringMap[];
5960
greedy?: boolean;
6061
aliases?: Record<string, string>;
62+
plugins?: Plugin[];
63+
hooks?: HookOptions;
6164
}
6265

6366
export interface PluginOptionsNormalized extends Omit<Required<PluginOptions>, 'stringMap' | 'prefixSelectorTransformer'> {

src/data/store.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ const defaultOptions = (): PluginOptionsNormalized => ({
143143
useCalc: false,
144144
stringMap: getRTLCSSStringMap(defaultStringMap),
145145
greedy: false,
146-
aliases: {}
146+
aliases: {},
147+
plugins: [],
148+
hooks: {}
147149
});
148150

149151
const store: Store = {

src/parsers/atrules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const addToIgnoreKeyframesInDiffMode = (node: Node): void => {
7777

7878
export const parseKeyFrames = (css: Root): void => {
7979

80-
const { source, processUrls, useCalc, stringMap, processKeyFrames } = store.options;
80+
const { source, processUrls, useCalc, stringMap, processKeyFrames, plugins, hooks } = store.options;
8181

8282
if (!processKeyFrames) {
8383
return;
@@ -115,7 +115,7 @@ export const parseKeyFrames = (css: Root): void => {
115115
}
116116

117117
const atRuleString = atRule.toString();
118-
const atRuleFlippedString = rtlcss.process(atRuleString, { processUrls, useCalc, stringMap });
118+
const atRuleFlippedString = rtlcss.process(atRuleString, { processUrls, useCalc, stringMap }, plugins, hooks);
119119

120120
if (atRuleString === atRuleFlippedString) {
121121
addToIgnoreKeyframesInDiffMode(atRule);

src/parsers/declarations.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ export const parseDeclarations = (
5959
useCalc,
6060
stringMap,
6161
greedy,
62-
aliases
62+
aliases,
63+
plugins,
64+
hooks
6365
} = store.options;
6466

6567
const deleteDeclarations: Declaration[] = [];
@@ -160,7 +162,7 @@ export const parseDeclarations = (
160162
stringMap,
161163
greedy,
162164
aliases
163-
});
165+
}, plugins, hooks);
164166

165167
/* the source could be undefined in certain cases but not during the tests */
166168
/* istanbul ignore next */

0 commit comments

Comments
 (0)