Skip to content

Add ability to pass options to postcss plugins #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Missing a configuration? [Raise an issue](https://github.com/gajus/babel-plugin-
```js
type FiletypeOptionsType = {|
+syntax: string,
+plugins?: $ReadOnlyArray<string>
+plugins?: $ReadOnlyArray<string | $ReadOnlyArray<[string, mixed]>>
|};

type FiletypesConfigurationType = {
Expand Down Expand Up @@ -251,11 +251,25 @@ To add support for different CSS syntaxes (e.g. SCSS), perform the following two
"filetypes": {
".scss": {
"syntax": "postcss-scss",
"plugins": ["postcss-nested"]
"plugins": [
"postcss-nested"
]
}
}
```


Postcss plugins can have options specified by wrapping the name and an options object in an array inside your config

```json
"plugins": [
["postcss-import-sync2", {
"path": ["src/styles", "shared/styles"]
}],
"postcss-nested"
]
```


### Custom Attribute Mapping

You can set your own attribute mapping rules using the `attributeNames` option.
Expand Down
9 changes: 8 additions & 1 deletion src/requireCssModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {

type FiletypeOptionsType = {|
+syntax: string,
+plugins?: $ReadOnlyArray<string>
+plugins?: $ReadOnlyArray<string | $ReadOnlyArray<[string, mixed]>>
|};

type FiletypesConfigurationType = {
Expand Down Expand Up @@ -52,6 +52,13 @@ const getExtraPlugins = (filetypeOptions: ?FiletypeOptionsType): $ReadOnlyArray<
}

return filetypeOptions.plugins.map((plugin) => {
if (Array.isArray(plugin)) {
const [pluginName, pluginOptions] = plugin;

// eslint-disable-next-line import/no-dynamic-require, global-require
return require(pluginName)(pluginOptions);
}

// eslint-disable-next-line import/no-dynamic-require, global-require
return require(plugin);
});
Expand Down
9 changes: 8 additions & 1 deletion src/schemas/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
"properties": {
"plugins": {
"items": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "array"
}
]
},
"type": "array"
},
Expand Down