-
Notifications
You must be signed in to change notification settings - Fork 2
/
compiler.mjs
89 lines (69 loc) · 2.43 KB
/
compiler.mjs
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
import Less from 'less';
import { UndiciFileManager } from './undici-file-manager.mjs';
import console from 'node:console';
const FAKE_FILENAME = '__input__.less';
const lessCompiler = async (input, baseURL, compress) => {
const lessOptions = {
compress,
filename: FAKE_FILENAME,
plugins: [
new UndiciFileManager()
]
};
if (baseURL) {
lessOptions.filename = baseURL.toString();
}
try {
const output = await Less.render(input, lessOptions);
return output.css;
} catch (error) {
console.error('Error compiling LESS', lessOptions.filename, error);
throw prepareError(error, baseURL);
}
}
function prepareError(err, baseURL) {
console.error('Compilation Error', err.message, err);
const newError = new Error();
let originalMessage = err.message,
errorLines = [],
extractLines = [];
if (err.stack && !err.type) {
newError.message = 'message:' + err.stack;
newError.stack = err.stack;
return newError;
}
if (!err.hasOwnProperty('index') || !err.extract) {
err.message = 'message:' + (err.stack || originalMessage);
return err;
}
errorLines.push('column:' + (err.column + 1));
errorLines.push('line:' + err.line);
if (err.filename) {
if (baseURL) {
const baseURLString = baseURL.toString();
if (err.filename.indexOf(baseURLString) === 0) {
err.filename = err.filename.slice(baseURLString.length);
}
}
}
errorLines.push('filename:' + (err.filename || ''));
errorLines.push('type:' + err.type + 'Error');
if (typeof err.extract[0] == 'string') {
extractLines.push('' + (err.line - 1) + ' ' + err.extract[0]);
}
if (typeof err.extract[1] == 'string') {
extractLines.push('' + err.line + ' ' + err.extract[1]);
let whitespacePrefix = '',
_groups = err.extract[1].match(/^(\s*)/i);
if (_groups) {
whitespacePrefix = _groups[0];
}
const pointerPosition = ('' + err.line).length + 1 + err.column + 1 - whitespacePrefix.length;
extractLines.push(whitespacePrefix + new Array(pointerPosition).join(' ') + '^');
}
errorLines.push('message:' + originalMessage);
errorLines = errorLines.concat(extractLines);
err.message = errorLines.join('\n');
return err;
}
export { lessCompiler };