-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (53 loc) · 1.61 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const bless = require('bless');
const path = require('path');
const fs = require('fs-extra');
const assign = require('lodash/assign');
const isArray = require('lodash/isArray');
const isString = require('lodash/isString');
const isFunction = require('lodash/isFunction');
class BlessCompiler {
get brunchPlugin() {
return true;
}
get type() {
return 'stylesheet';
}
constructor(config = {}) {
const plugins = config.plugins || {};
const options = plugins.bless || {};
this.options = assign({
cacheBuster: true,
cleanup: true,
compress: true,
force: false,
imports: true,
outputDirectory: undefined
}, options);
}
onCompile(generatedFiles = [], done) {
if (isArray(generatedFiles) === false) {
generatedFiles = [generatedFiles];
}
const files = generatedFiles.filter((file) => {
return path.extname(file.path) === '.css';
});
files.forEach((file) => {
const content = fs.readFileSync(file.path, 'utf-8').toString();
const parser = new bless.Parser({ output: file.path, options: this.options });
parser.parse(content, (error, files, numSelectors) => {
if (error) {
throw error;
}
files.forEach((file) => {
const outputDirectory = this.options.outputDirectory || path.dirname(file.filename);
fs.ensureDirSync(outputDirectory);
fs.writeFileSync(path.resolve(outputDirectory, path.basename(file.filename)), file.content, 'utf8');
});
if (isFunction(done)) {
done();
}
});
});
}
}
module.exports = BlessCompiler;