/
eslint.js
126 lines (103 loc) · 3.53 KB
/
eslint.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
const { log, logError } = require("../../logger");
const { isFunction, deepMergeWithArray } = require("../../utils");
const { getPlugin, removePlugins, pluginByName } = require("../../webpack-plugins");
const ESLINT_MODES = {
extends: "extends",
file: "file"
};
function disableEslint(webpackConfig) {
const { hasRemovedAny } = removePlugins(webpackConfig, pluginByName("ESLintWebpackPlugin"));
if (hasRemovedAny) {
log("Disabled ESLint.");
} else {
logError("Couldn't disabled ESLint.");
}
}
function extendsEslintConfig(plugin, eslintConfig, context) {
const { configure } = eslintConfig;
if (configure) {
if (isFunction(configure)) {
if (plugin.options) {
plugin.options.baseConfig = configure(plugin.options.baseConfig || {}, context);
} else {
plugin.options = {
baseConfig: configure({}, context)
};
}
if (!plugin.options.baseConfig) {
throw new Error("craco: 'eslint.configure' function didn't return a config object.");
}
} else {
// TODO: ensure is otherwise a plain object, if not, log an error.
if (plugin.options) {
plugin.options.baseConfig = deepMergeWithArray({}, plugin.options.baseConfig || {}, configure);
} else {
plugin.options = {
baseConfig: configure
};
}
}
log("Merged ESLint config with 'eslint.configure'.");
}
}
function useEslintConfigFile(plugin) {
if (plugin.options) {
plugin.options.useEslintrc = true;
delete plugin.options.baseConfig;
} else {
plugin.options = {
useEslintrc: true
};
}
log("Overrided ESLint config to use a config file.");
}
function enableEslintIgnoreFile(plugin) {
if (plugin.options) {
plugin.options.ignore = true;
} else {
plugin.options = {
ignore: true
};
}
log("Overrided ESLint config to enable an ignore file.");
}
function applyPluginOptions(plugin, pluginOptions, context) {
if (isFunction(pluginOptions)) {
plugin.options = pluginOptions(plugin.options || {}, context);
if (!plugin.options) {
throw new Error("craco: 'eslint.pluginOptions' function didn't return a config object.");
}
} else {
// TODO: ensure is otherwise a plain object, if not, log an error.
plugin.options = deepMergeWithArray(plugin.options || {}, pluginOptions);
}
log("Applied ESLint plugin options.");
}
function overrideEsLint(cracoConfig, webpackConfig, context) {
if (cracoConfig.eslint) {
const { isFound, match } = getPlugin(webpackConfig, pluginByName("ESLintWebpackPlugin"));
if (!isFound) {
logError("Cannot find ESLint plugin (ESLintWebpackPlugin).");
return webpackConfig;
}
const { enable, mode, pluginOptions } = cracoConfig.eslint;
if (enable === false) {
disableEslint(webpackConfig);
return webpackConfig;
}
enableEslintIgnoreFile(match);
if (mode === ESLINT_MODES.file) {
useEslintConfigFile(match);
} else {
extendsEslintConfig(match, cracoConfig.eslint, context);
}
if (pluginOptions) {
applyPluginOptions(match, pluginOptions);
}
}
return webpackConfig;
}
module.exports = {
overrideEsLint,
ESLINT_MODES
};