Skip to content

Support for extra postcss plugins #100

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 9 commits into from
Jun 18, 2017
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
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ NODE_ENV=production ./test
|---|---|---|
|`context`|Must match webpack [`context`](https://webpack.github.io/docs/configuration.html#context) configuration. [`css-loader`](https://github.com/webpack/css-loader) inherits `context` values from webpack. Other CSS module implementations might use different context resolution logic.|`process.cwd()`|
|`exclude`| a RegExp that will exclude otherwise included files e.g., to exclude all styles from node_modules `exclude: 'node_modules'`|
|`filetypes`|Configure [postcss syntax loaders](https://github.com/postcss/postcss#syntaxes) like sugerss, LESS and SCSS. ||
|`filetypes`|Configure [postcss syntax loaders](https://github.com/postcss/postcss#syntaxes) like sugerss, LESS and SCSS and extra plugins for them. ||
|`generateScopedName`|Refer to [Generating scoped names](https://github.com/css-modules/postcss-modules#generating-scoped-names)|`[path]___[name]__[local]___[hash:base64:5]`|
|`removeImport`|Remove the matching style import. This option is used to enable server-side rendering.|`false`|
|`webpackHotModuleReloading`|Enables hot reloading of CSS in webpack|`false`|
Expand All @@ -198,7 +198,20 @@ To add support for different CSS syntaxes (e.g. SCSS), perform the following two

```json
"filetypes": {
".scss": "postcss-scss"
".scss": {
"syntax": "postcss-scss"
}
}
```

And optionaly specify extra plugins

```json
"filetypes": {
".scss": {
"syntax": "postcss-scss",
"plugins": ["postcss-nested"]
}
}
```

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
"babel-preset-es2015": "^6.18.0",
"eslint": "^3.11.1",
"eslint-config-canonical": "^6.0.0",
"flow-bin": "^0.37.4",
"flow-bin": "^0.48.0",
"husky": "^0.12.0",
"mocha": "^3.2.0",
"semantic-release": "^6.3.5",
"postcss-less": "^0.15.0",
"postcss-scss": "^0.4.0"
"postcss-nested": "^1.0.1",
"postcss-scss": "^0.4.0",
"semantic-release": "^6.3.5"
},
"engines": {
"node": ">4.0.0"
Expand Down
49 changes: 42 additions & 7 deletions src/requireCssModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,47 @@ import type {
StyleModuleMapType
} from './types';

const getTokens = (runner, cssSourceFilePath: string, filetypes): StyleModuleMapType => {
type FileTypeOptions = {|
+syntax: string,
// eslint-disable-next-line no-undef
+plugins?: $ReadOnlyArray<string>
|};

const getFiletypeOptions = (cssSourceFilePath: string, filetypes: {[key: string]: FileTypeOptions}): ?FileTypeOptions => {
const extension = cssSourceFilePath.substr(cssSourceFilePath.lastIndexOf('.'));
const syntax = filetypes[extension];
const filetype = filetypes ? filetypes[extension] : null;

return filetype;
};

const getSyntax = (filetypeOptions: FileTypeOptions): ?(Function|Object) => {
if (!filetypeOptions) {
return null;
}

// eslint-disable-next-line import/no-dynamic-require, global-require
return require(filetypeOptions.syntax);
};

// eslint-disable-next-line no-undef
const getExtraPlugins = (filetypeOptions: ?FileTypeOptions): $ReadOnlyArray<any> => {
if (!filetypeOptions || !filetypeOptions.plugins) {
return [];
}

return filetypeOptions.plugins.map((plugin) => {
// eslint-disable-next-line import/no-dynamic-require, global-require
return require(plugin);
});
};

const getTokens = (runner, cssSourceFilePath: string, filetypeOptions: ?FileTypeOptions): StyleModuleMapType => {
const options: Object = {
from: cssSourceFilePath
};

if (syntax) {
// eslint-disable-next-line import/no-dynamic-require, global-require
options.syntax = require(syntax);
if (filetypeOptions) {
options.syntax = getSyntax(filetypeOptions);
}

const lazyResult = runner
Expand Down Expand Up @@ -58,14 +88,19 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap
context: options.context || process.cwd()
});

const filetypeOptions = getFiletypeOptions(cssSourceFilePath, options.filetypes);

const fetch = (to: string, from: string) => {
const fromDirectoryPath = dirname(from);
const toPath = resolve(fromDirectoryPath, to);

return getTokens(runner, toPath, options.filetypes);
return getTokens(runner, toPath, filetypeOptions);
};

const extraPlugins = getExtraPlugins(filetypeOptions);

const plugins = [
...extraPlugins,
Values,
LocalByDefault,
ExtractImports,
Expand All @@ -79,5 +114,5 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap

runner = postcss(plugins);

return getTokens(runner, cssSourceFilePath, options.filetypes);
return getTokens(runner, cssSourceFilePath, filetypeOptions);
};
16 changes: 14 additions & 2 deletions src/schemas/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@
"additionalProperties": false,
"patternProperties": {
"\\..*": {
"type": "string"
"type": "object",
"additionalProperties": false,
"properties": {
"syntax": {
"type": "string"
},
"plugins": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
},
"type": "object"
Expand All @@ -27,4 +39,4 @@
}
},
"type": "object"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './bar.scss';

<div styleName="a_modified"></div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.a {
background-color: #ffffff;

&_modified {
background-color: #000000;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './bar.scss';

<div className="bar__a_modified"></div>;
16 changes: 16 additions & 0 deletions test/fixtures/react-css-modules/applies extra plugins/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"plugins": [
[
"../../../../src",
{
"generateScopedName": "[name]__[local]",
"filetypes": {
".scss": {
"syntax": "postcss-scss",
"plugins": ["postcss-nested"]
}
}
}
]
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
{
"generateScopedName": "[name]__[local]",
"filetypes": {
".less": "postcss-less"
".less": {
"syntax": "postcss-less"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using the old syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to Object { "syntax": "..." }. Sorry, what's wrong?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Sorry. This PR was the first thing I looked at this morning. :-)

}
}
}
]
Expand Down