-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
getPostcssResult.js
138 lines (116 loc) · 3.53 KB
/
getPostcssResult.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* @flow */
"use strict";
const autoSyntax = require("postcss-syntax");
const dynamicRequire = require("./dynamicRequire");
const fs = require("fs");
const LazyResult = require("postcss/lib/lazy-result");
const postcss = require("postcss");
const importLazy = require("import-lazy")(require);
const syntaxes /*: {
[syntaxName: string]: postcss$syntax,
}*/ = {
sugarss: importLazy("sugarss"),
css: {
stringify: postcss.stringify
}
};
["less", "sass", "scss", "markdown", "html", "styled", "jsx"].forEach(mod => {
syntaxes[mod] = importLazy("postcss-" + mod);
});
const postcssProcessor = postcss();
module.exports = function(
stylelint /*: stylelint$internalApi*/
) /*: Promise<?Object>*/ {
const options /*: {
code?: string,
codeFilename?: string,
filePath?: string,
codeProcessors?: Array<Function>,
syntax?: stylelint$syntaxes,
customSyntax?: string
}*/ =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const cached /*: ?postcss$result*/ = stylelint._postcssResultCache.get(
options.filePath
);
if (cached) return Promise.resolve(cached);
let getCode;
if (options.code !== undefined) {
getCode = Promise.resolve(options.code);
} else if (options.filePath) {
getCode = readFile(options.filePath);
}
if (!getCode) {
throw new Error("code or filePath required");
}
return getCode
.then(code => {
const customSyntax = stylelint._options.customSyntax;
let syntax = stylelint._options.syntax;
if (customSyntax) {
try {
syntax = dynamicRequire(customSyntax);
} catch (e) {
throw new Error(
`Cannot resolve custom syntax module ${customSyntax}`
);
}
/*
* PostCSS allows for syntaxes that only contain a parser, however,
* it then expects the syntax to be set as the `parser` option rather than `syntax.
*/
if (!syntax.parse) {
syntax = {
parse: syntax,
stringify: postcss.stringify
};
}
} else if (syntax) {
syntax = syntaxes[syntax];
if (!syntax) {
throw new Error(
"You must use a valid syntax option, either: scss, sass, less or sugarss"
);
}
} else if (
!(options.codeProcessors && options.codeProcessors.length) ||
(options.filePath && /\.(scss|sass|less)$/.test(options.filePath))
) {
syntaxes.css.parse = stylelint._options.fix
? importLazy("postcss-safe-parser")
: postcss.parse;
syntax = autoSyntax(syntaxes);
}
const postcssOptions /*: postcss$options*/ = {
from: options.filePath,
syntax
};
const source = options.code ? options.codeFilename : options.filePath;
let preProcessedCode = code;
if (options.codeProcessors) {
options.codeProcessors.forEach(codeProcessor => {
preProcessedCode = codeProcessor(preProcessedCode, source);
});
}
const result = new LazyResult(
postcssProcessor,
preProcessedCode,
postcssOptions
);
return result;
})
.then(postcssResult => {
stylelint._postcssResultCache.set(options.filePath, postcssResult);
return postcssResult;
});
};
function readFile(filePath /*: string*/) /*: Promise<string>*/ {
return new Promise((resolve, reject) => {
fs.readFile(filePath, "utf8", (err, content) => {
if (err) {
return reject(err);
}
resolve(content);
});
});
}