-
Notifications
You must be signed in to change notification settings - Fork 20
/
less-processor.js
76 lines (63 loc) · 2.17 KB
/
less-processor.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
import Future from 'fibers/future';
import path from 'path';
import logger from './logger';
export default class LessProcessor {
constructor(pluginOptions) {
this.fileCache = {};
this.filesByName = null;
this.pluginOptions = pluginOptions;
this.less = pluginOptions.enableLessCompilation ? require('less') : null;
}
isRoot(inputFile) {
const fileOptions = inputFile.getFileOptions();
if (fileOptions.hasOwnProperty('isImport')) {
return !fileOptions.isImport;
}
return !hasUnderscore(inputFile.getPathInPackage());
function hasUnderscore(file) {
return path.basename(file)[0] === '_';
}
}
shouldProcess(file) {
const lessCompilationExtensions = this.pluginOptions.enableLessCompilation;
if (!lessCompilationExtensions || typeof lessCompilationExtensions === 'boolean') {
return lessCompilationExtensions;
}
return lessCompilationExtensions.some((extension) => file.getPathInPackage().endsWith(extension));
}
process(file, filesByName) {
this.filesByName = filesByName;
try {
this._process(file);
} catch (err) {
const numberOfAdditionalLines = this.pluginOptions.globalVariablesTextLineCount
? this.pluginOptions.globalVariablesTextLineCount + 1
: 0;
const adjustedLineNumber = err.line - numberOfAdditionalLines;
logger.error(`\n/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`);
logger.error(`Processing Step: Less compilation`);
logger.error(`Unable to compile ${file.importPath}\nLine: ${adjustedLineNumber}, Column: ${err.column}\n${err}`);
logger.error(`\n/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`);
throw err;
}
}
_process(file) {
if (file.isPreprocessed) return;
const {css, map} = this._transpile(file);
file.contents = css;
file.sourceMap = map;
file.isPreprocessed = true;
}
_transpile(sourceFile) {
const future = new Future();
const options = {
filename: sourceFile.importPath,
sourceMap: {
comment: false
}
};
this.less.render(sourceFile.rawContents, options)
.then(output => future.return(output), error => future.throw(error));
return future.wait();
}
};