-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
76 lines (57 loc) · 1.87 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const {resolve} = require('path');
const CleanCssPromise = require('clean-css-promise');
const BroccoliPersistentFilter = require('broccoli-persistent-filter');
const jsonStableStringify = require('json-stable-stringify');
const SOURCE_MAP_URL_PREFIX = ' sourceMappingURL=data:application/json;base64,';
const internalInstance = Symbol('internalInstance');
const internalOptions = Symbol('internalOptions');
const optionHash = Symbol('optionHash');
function toBroccoliCleanCssError(error) {
error.message = error.message.replace('clean-css-promise', 'broccoli-clean-css');
Error.captureStackTrace(error, toBroccoliCleanCssError);
return error;
}
class CleanCSSFilter extends BroccoliPersistentFilter {
constructor(...args) {
super(...args);
this.inputTree = args.shift();
try {
new CleanCssPromise(...args);
} catch (err) {
throw toBroccoliCleanCssError(err);
}
this[internalOptions] = args[0] || {};
}
cacheKeyProcessString(string, relativePath) {
this[optionHash] = this[optionHash] || jsonStableStringify(this[internalOptions]);
return `${this[optionHash]}${super.cacheKeyProcessString(string, relativePath)}`;
}
baseDir() { // eslint-disable-line class-methods-use-this
return __dirname;
}
build() {
this[internalInstance] = new CleanCssPromise(this[internalOptions]);
return super.build();
}
async processString(str, fileName) {
try {
const {sourceMap, styles} = await this[internalInstance].minify({
[resolve(this.inputPaths[0], fileName)]: {
styles: str
}
});
if (sourceMap) {
return `${styles}
/*#${SOURCE_MAP_URL_PREFIX}${Buffer.from(JSON.stringify(sourceMap)).toString('base64')}*/
`;
}
return styles;
} catch (err) {
throw toBroccoliCleanCssError(err);
}
}
}
CleanCSSFilter.prototype.extensions = ['css'];
CleanCSSFilter.prototype.targetExtension = 'css';
module.exports = CleanCSSFilter;