-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
122 lines (107 loc) · 3.43 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var postcss = require('postcss');
var precss = require('precss');
var postcssScss = require('postcss-scss');
var calc = require('postcss-calc');
var prettify = require('postcss-prettify');
var functions = require('postcss-functions');
var autoprefixer = require('autoprefixer');
var color = require('color');
function clamp(val) {
return Math.min(100, Math.max(0, val));
}
function addDepends(file, messages) {
if (!file.cache) {
return;
}
var deps = [file.realpath];
messages.forEach(function(message) {
if (message.type === 'dependency' && ~deps.indexOf(message.parent)) {
deps.push(message.file);
file.cache.addDeps(message.file);
}
});
}
module.exports = function(content, file, conf, callback) {
if (!callback) {
throw new Error('Async plugin is not supported in `fis3`, please use `fis3-async`。');
}
// 不处理空文件和代码片段
if (!content || !content.trim() || file.basename[ 0 ] === '_') {
return callback(null, content);
}
var processConf = fis.util.assign({
parser: postcssScss,
from: file.subpath.substring(1)
}, conf.sourceMap ? {
map: {
inline: false
}
} : null);
conf.import = conf.import || {};
var path = conf.import.path || [];
if (typeof path === 'string') {
path = [path];
}
path.push(fis.project.getProjectPath());
path.unshift(file.dirname);
conf.import.path = path;
postcss([
precss(conf),
calc(),
functions({
functions: {
lighten: function (value, percentage) {
var hsl = color(value.trim()).hsl();
hsl.color[2] += parseFloat(percentage);
hsl.color[2] = clamp(hsl.color[2]);
return hsl.rgb().toString();
},
darken: function (value, percentage) {
var hsl = color(value.trim()).hsl();
hsl.color[2] -= parseFloat(percentage);
hsl.color[2] = clamp(hsl.color[2]);
return hsl.rgb().toString();
},
saturate: function (value, percentage) {
var hsl = color(value.trim()).hsl();
hsl.color[1] += parseFloat(percentage);
hsl.color[1] = clamp(hsl.color[1]);
return hsl.rgb().toString();
},
desaturate: function (value, percentage) {
var hsl = color(value.trim()).hsl();
hsl.color[1] -= parseFloat(percentage);
hsl.color[1] = clamp(hsl.color[1]);
return hsl.rgb().toString();
}
}
}),
autoprefixer,
prettify()
])
.process(content, processConf)
.catch(callback)
.then(function (ret) {
content = ret.css;
addDepends(file, ret.messages);
if (ret.map) {
var mapping = fis.file.wrap(file.dirname + '/' + file.filename + file.rExt + '.map');
mapping.setContent(ret.map.toString('utf8'));
var url = mapping.getUrl(fis.compile.settings.hash, fis.compile.settings.domain);
content = ret.css.replace(/\n?\s*\/\*#\ssourceMappingURL=.*?(?:\n|$)/g, '');
content += '\n/*# sourceMappingURL=' + url + '*/\n';
file.extras = file.extras || {};
file.extras.derived = file.extras.derived || [];
file.extras.derived.push(mapping);
}
callback(null, content);
});
}
module.exports.defaultOptions = {
sourceMap: false,
'import': {
extension: '.pcss',
root: fis.project.getProjectPath(),
prefix: '_'
}
};