/
srv-rtlcss.js
108 lines (96 loc) · 3.29 KB
/
srv-rtlcss.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
//#region Imports
var rtlcss = require("rtlcss"),
configLoader = require("rtlcss/lib/config-loader"),
fs = require("fs"),
path = require("path");
//#endregion
//#region Process
var processRtlCSS = function (cssContent, mapContent, sourceFileName, targetFileName, autoprefixer, autoprefixerBrowsers) {
if (mapContent !== true) {
mapContent = { prev: mapContent };
}
var result, css, map;
try {
var config = configLoader.load(null, path.dirname(sourceFileName), { options: { minify: false } });
result = rtlcss.configure(config).process(cssContent, {
map: mapContent,
from: sourceFileName,
to: targetFileName
});
css = result.css;
map = result.map.toJSON();
} catch (e) {
// Return same css and map back so the upstream compilers can continue.
return {
Success: false,
Remarks: "RTLCSS: Exception occured: " + e.message,
css: cssContent,
map: mapContent
};
}
if (autoprefixer !== undefined) {
var autoprefixedOutput = require("./srv-autoprefixer").processAutoprefixer(css, map, autoprefixerBrowsers, sourceFileName, targetFileName);
css = autoprefixedOutput.css;
map = autoprefixedOutput.map;
}
return {
Success: true,
css: css,
map: map
};
};
//#endregion
//#region Handler
var handleRtlCSS = function (writer, params) {
fs.readFile(params.sourceFileName, 'utf8', function (err, data) {
if (err) {
writer.write(JSON.stringify({
Success: false,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
Remarks: "RTLCSS: Error reading input file.",
Details: err,
Errors: [{
Message: "RTLCSS" + err,
FileName: params.sourceFileName
}]
}));
writer.end();
return;
}
var output = processRtlCSS(data,
true,
params.sourceFileName,
params.targetFileName,
params.autoprefixer,
params.autoprefixerBrowsers);
if (!output.Success) {
writer.write(JSON.stringify({
Success: false,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
Remarks: "RTLCSS: " + output.Remarks,
Errors: [{
Message: output.Remarks,
FileName: params.sourceFileName
}]
}));
} else {
writer.write(JSON.stringify({
Success: true,
SourceFileName: params.sourceFileName,
TargetFileName: params.targetFileName,
MapFileName: params.mapFileName,
Remarks: "Successful!",
Content: output.css,
Map: JSON.stringify(output.map)
}));
}
writer.end();
});
};
//#endregion
//#region Exports
module.exports = handleRtlCSS;
module.exports.processRtlCSS = processRtlCSS;
//#endregion