This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
92 lines (85 loc) · 2.43 KB
/
index.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
import stylelint from 'stylelint';
import loaderUtils from 'loader-utils';
import { join } from 'path';
/**
* Class representing a StylelintError.
* @extends Error
*/
class StylelintError extends Error {
/**
* Create a StylelintError.
* @param {string} messages - Formatted stylelint errors.
*/
constructor(messages) {
super();
this.name = 'StylelintError';
this.message = messages;
this.stack = '';
}
/**
* Returns a stringified representation of our error. This is called
* when an error is consumed by console methods
* ex: console.error(new StylelintError(formattedMessage))
* @return {string} error - A stringified representation of the error.
*/
inspect() {
return this.message;
}
}
// prevents process-ending errors if m is non-existent,
// returns null if non-existent
function safeResolve(m) {
try {
return require.resolve(m);
} catch (error) {
return null;
}
}
// first tries to resolve from root directory...
// if non-existent, tries to resolve from node_modules...
const projectRoot = process.cwd();
function resolveConfigPath(configPath) {
return safeResolve(join(projectRoot, configPath)) || safeResolve(configPath);
}
// eslint-disable-next-line func-names
module.exports = function(content) {
const options = loaderUtils.getOptions(this);
const lintArgument = {
code: content,
codeFilename: this.resourcePath,
formatter: 'string',
};
// shortcut for emitWarning option
const emitWarning = options && options.emitWarning;
if (options && options.configPath) {
// resolve the path
const resolved = resolveConfigPath(options.configPath);
if (resolved) {
// if it exists, use this to set the `configFile` prop
lintArgument.configFile = resolved;
} else {
// otherwise, do it the old way
let processedPath = loaderUtils.stringifyRequest(
this,
options.configPath
);
processedPath = processedPath.substring(1, processedPath.length - 1);
lintArgument.configFile = processedPath;
}
}
const callback = this.async();
stylelint
.lint(lintArgument)
.then(resultObject => {
const { output } = resultObject;
if (resultObject.errored && !emitWarning) {
this.emitError(new StylelintError(output));
} else if (output) {
this.emitWarning(new StylelintError(output));
}
callback(null, content);
})
.catch(e => {
callback(e);
});
};