-
Notifications
You must be signed in to change notification settings - Fork 20
/
stylus-processor.js
80 lines (68 loc) · 2.31 KB
/
stylus-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
77
78
79
80
import Future from 'fibers/future';
import path from 'path';
import pluginOptions from './options';
import logger from './logger';
export default class StylusProcessor {
constructor(pluginOptions) {
this.fileCache = {};
this.filesByName = null;
this.pluginOptions = pluginOptions;
this.stylus = pluginOptions.enableStylusCompilation ? require('stylus') : 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 stylusCompilationExtensions = this.pluginOptions.enableStylusCompilation;
if (!stylusCompilationExtensions || typeof stylusCompilationExtensions === 'boolean') {
return stylusCompilationExtensions;
}
return stylusCompilationExtensions.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: Stylus 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, sourceMap } = this._transpile(file);
file.contents = css;
file.sourceMap = sourceMap;
file.isPreprocessed = true;
}
_transpile(sourceFile) {
const future = new Future();
const options = {
filename: sourceFile.importPath,
sourcemap: {
comment: false
}
};
this.stylus.render(sourceFile.rawContents, options, (err, css) => {
if (err) {
return future.throw(err);
}
future.return({ css, sourceMap: this.stylus.sourcemap });
});
return future.wait();
}
};