-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (36 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const postcss = require('postcss');
const postcssKremling = require('./src/postcss-kremling-plugin');
let globalId = 0;
module.exports = function(source) {
const id = globalId;
globalId += 1;
const loaderOptions = this.getOptions() || {};
let kremlingNamespace = 'kremling';
let kremlingNamespaceString = '';
if (loaderOptions.namespace && typeof loaderOptions.namespace === 'string') {
kremlingNamespace = loaderOptions.namespace;
kremlingNamespaceString = `namespace: '${loaderOptions.namespace}'`;
}
const defaultOptions = {
plugins: {},
...loaderOptions.postcss,
};
const { plugins, ...restOfOptions } = defaultOptions;
const pluginsInit = (Object.keys(plugins)).map(key => {
return require(key)(plugins[key]);
});
const callback = this.async();
postcss([ ...pluginsInit, postcssKremling({ id: `p${id}`, namespace: kremlingNamespace }) ])
.process(source, {
...restOfOptions,
to: './',
from: './',
})
.then((result) => {
if (result.css) {
callback(null, `module.exports = { styles: '${result.toString().replace(/(\s)+/g, ' ').replace(/'/g, `\\'`)}', id: 'p${id}', ${kremlingNamespaceString} };`);
} else {
callback(null, `module.exports = { styles: '', id: 'p${id}', ${kremlingNamespaceString} };`);
}
});
};