-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEngineCSS.js
49 lines (40 loc) · 1.15 KB
/
EngineCSS.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
41
42
43
44
45
46
47
48
49
const path = require('path');
const merge = require('lodash.merge');
const AtImport = require('postcss-import');
const postcss = require('postcss');
function isPluginActive(plugin) {
// eslint-disable-next-line no-param-reassign
if (typeof plugin === 'function') plugin = plugin();
return plugin.postcssPlugin === 'postcss-import';
}
class EngineCSS {
async processCSS(src, { inputPath, outputPath }) {
if (!this.plugins) throw new Error('Could not successfuly read postcss config file');
const config = merge({}, this.options, { from: inputPath, to: outputPath });
return postcss(this.plugins)
.process(src, config)
.then((result) => result.css);
}
setInputDir(inputDir) {
if (inputDir) {
this.inputDir = inputDir;
}
}
setIncludesDir(includesDir) {
if (includesDir) {
this.includesDir = path.join(this.inputDir, includesDir);
}
}
setPlugins(plugins) {
if (!plugins.find(isPluginActive)) {
plugins.unshift(AtImport({
path: [this.includesDir],
}));
}
this.plugins = plugins;
}
setOptions(options) {
this.options = options;
}
}
module.exports = EngineCSS;