Skip to content

Commit b2119fd

Browse files
gbiryukovgajus
authored andcommitted
feat: add ability to pass options to postcss plugins (#176)
* add ability to pass options to postcss plugins * add postcss plugin options usage docs into readme
1 parent e5b2d95 commit b2119fd

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Missing a configuration? [Raise an issue](https://github.com/gajus/babel-plugin-
208208
```js
209209
type FiletypeOptionsType = {|
210210
+syntax: string,
211-
+plugins?: $ReadOnlyArray<string>
211+
+plugins?: $ReadOnlyArray<string | $ReadOnlyArray<[string, mixed]>>
212212
|};
213213

214214
type FiletypesConfigurationType = {
@@ -251,11 +251,25 @@ To add support for different CSS syntaxes (e.g. SCSS), perform the following two
251251
"filetypes": {
252252
".scss": {
253253
"syntax": "postcss-scss",
254-
"plugins": ["postcss-nested"]
254+
"plugins": [
255+
"postcss-nested"
256+
]
255257
}
256258
}
257259
```
258-
260+
261+
Postcss plugins can have options specified by wrapping the name and an options object in an array inside your config
262+
263+
```json
264+
"plugins": [
265+
["postcss-import-sync2", {
266+
"path": ["src/styles", "shared/styles"]
267+
}],
268+
"postcss-nested"
269+
]
270+
```
271+
272+
259273
### Custom Attribute Mapping
260274

261275
You can set your own attribute mapping rules using the `attributeNames` option.

src/requireCssModule.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121

2222
type FiletypeOptionsType = {|
2323
+syntax: string,
24-
+plugins?: $ReadOnlyArray<string>
24+
+plugins?: $ReadOnlyArray<string | $ReadOnlyArray<[string, mixed]>>
2525
|};
2626

2727
type FiletypesConfigurationType = {
@@ -52,6 +52,13 @@ const getExtraPlugins = (filetypeOptions: ?FiletypeOptionsType): $ReadOnlyArray<
5252
}
5353

5454
return filetypeOptions.plugins.map((plugin) => {
55+
if (Array.isArray(plugin)) {
56+
const [pluginName, pluginOptions] = plugin;
57+
58+
// eslint-disable-next-line import/no-dynamic-require, global-require
59+
return require(pluginName)(pluginOptions);
60+
}
61+
5562
// eslint-disable-next-line import/no-dynamic-require, global-require
5663
return require(plugin);
5764
});

src/schemas/optionsSchema.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
"properties": {
1616
"plugins": {
1717
"items": {
18-
"type": "string"
18+
"oneOf": [
19+
{
20+
"type": "string"
21+
},
22+
{
23+
"type": "array"
24+
}
25+
]
1926
},
2027
"type": "array"
2128
},

0 commit comments

Comments
 (0)