Skip to content

Commit 8b42dd5

Browse files
committed
Implemented support for extra postcss plugins
1 parent d221d7c commit 8b42dd5

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
"flow-bin": "^0.37.4",
3232
"husky": "^0.12.0",
3333
"mocha": "^3.2.0",
34-
"semantic-release": "^6.3.5",
3534
"postcss-less": "^0.15.0",
36-
"postcss-scss": "^0.4.0"
35+
"postcss-nested": "^1.0.1",
36+
"postcss-scss": "^0.4.0",
37+
"semantic-release": "^6.3.5"
3738
},
3839
"engines": {
3940
"node": ">4.0.0"

src/requireCssModule.js

+49-9
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,54 @@ import type {
1818
StyleModuleMapType
1919
} from './types';
2020

21-
const getTokens = (runner, cssSourceFilePath: string, filetypes): StyleModuleMapType => {
21+
type FileTypeOptions = {|
22+
syntax: string,
23+
plugins: Array<string>
24+
|};
25+
26+
const getFiletypeOptions = (cssSourceFilePath: string, filetypes: Object): ?(string|FileTypeOptions) => {
2227
const extension = cssSourceFilePath.substr(cssSourceFilePath.lastIndexOf('.'));
23-
const syntax = filetypes[extension];
28+
const filetype = filetypes ? filetypes[extension] : null;
2429

25-
const options: Object = {
26-
from: cssSourceFilePath
27-
};
30+
return filetype;
31+
}
32+
33+
const getSyntax = (filetypeOptions: ?(string|FileTypeOptions)) => {
34+
if (!filetypeOptions) {
35+
return null;
36+
}
37+
38+
if (typeof filetypeOptions === 'string') {
39+
// eslint-disable-next-line import/no-dynamic-require, global-require
40+
return require(filetypeOptions);
41+
}
42+
43+
if (typeof filetypeOptions === 'object') {
44+
// eslint-disable-next-line import/no-dynamic-require, global-require
45+
return require(filetypeOptions.syntax);
46+
}
47+
}
2848

29-
if (syntax) {
49+
const getExtraPlugins = (filetypeOptions: ?(string|FileTypeOptions)): Array<any> => {
50+
console.log(filetypeOptions);
51+
if (!filetypeOptions) {
52+
return [];
53+
}
54+
55+
if (typeof filetypeOptions === 'object') {
3056
// eslint-disable-next-line import/no-dynamic-require, global-require
31-
options.syntax = require(syntax);
57+
return filetypeOptions.plugins.map(plugin => require(plugin));
3258
}
3359

60+
return [];
61+
}
62+
63+
const getTokens = (runner, cssSourceFilePath: string, filetypeOptions: ?(string|FileTypeOptions)): StyleModuleMapType => {
64+
const options: Object = {
65+
from: cssSourceFilePath,
66+
syntax: getSyntax(filetypeOptions)
67+
};
68+
3469
const lazyResult = runner
3570
.process(readFileSync(cssSourceFilePath, 'utf-8'), options);
3671

@@ -58,14 +93,19 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap
5893
context: options.context || process.cwd()
5994
});
6095

96+
const filetypeOptions = getFiletypeOptions(cssSourceFilePath, options.filetypes);
97+
6198
const fetch = (to: string, from: string) => {
6299
const fromDirectoryPath = dirname(from);
63100
const toPath = resolve(fromDirectoryPath, to);
64101

65-
return getTokens(runner, toPath, options.filetypes);
102+
return getTokens(runner, toPath, filetypeOptions);
66103
};
67104

105+
const extraPlugins = getExtraPlugins(filetypeOptions);
106+
68107
const plugins = [
108+
...extraPlugins,
69109
Values,
70110
LocalByDefault,
71111
ExtractImports,
@@ -79,5 +119,5 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap
79119

80120
runner = postcss(plugins);
81121

82-
return getTokens(runner, cssSourceFilePath, options.filetypes);
122+
return getTokens(runner, cssSourceFilePath, filetypeOptions);
83123
};

src/schemas/optionsSchema.json

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@
1111
"additionalProperties": false,
1212
"patternProperties": {
1313
"\\..*": {
14-
"type": "string"
14+
"oneOf": [
15+
{
16+
"type": "string"
17+
},
18+
{
19+
"type": "object",
20+
"additionalProperties": false,
21+
"properties": {
22+
"syntax": {
23+
"type": "string"
24+
},
25+
"plugins": {
26+
"type": "array",
27+
"items": { "type": "string" },
28+
"minItems": 1
29+
}
30+
}
31+
}
32+
]
1533
}
1634
},
1735
"type": "object"
@@ -27,4 +45,4 @@
2745
}
2846
},
2947
"type": "object"
30-
}
48+
}

0 commit comments

Comments
 (0)