/
loadLinariaOptions.ts
83 lines (70 loc) · 2.06 KB
/
loadLinariaOptions.ts
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
import { cosmiconfigSync } from 'cosmiconfig';
import type { StrictOptions } from '@linaria/utils';
import type { Stage } from '../../types';
export type PluginOptions = StrictOptions & {
configFile?: string;
stage?: Stage;
};
const searchPlaces = [
`.linariarc`,
`.linariarc.json`,
`.linariarc.yaml`,
`.linariarc.yml`,
`.linariarc.js`,
`.linariarc.cjs`,
`.config/linariarc`,
`.config/linariarc.json`,
`.config/linariarc.yaml`,
`.config/linariarc.yml`,
`.config/linariarc.js`,
`.config/linariarc.cjs`,
`linaria.config.js`,
`linaria.config.cjs`,
];
const explorerSync = cosmiconfigSync('linaria', { searchPlaces });
const cache = new WeakMap<Partial<PluginOptions>, StrictOptions>();
const defaultOverrides = {};
const nodeModulesRegExp = /[\\/]node_modules[\\/]/;
export default function loadLinariaOptions(
overrides: Partial<PluginOptions> = defaultOverrides
): StrictOptions {
if (cache.has(overrides)) {
return cache.get(overrides)!;
}
const { configFile, ignore, rules, babelOptions = {}, ...rest } = overrides;
const result =
configFile !== undefined
? explorerSync.load(configFile)
: explorerSync.search();
const options = {
displayName: false,
evaluate: true,
extensions: ['.cjs', '.cts', '.js', '.jsx', '.mjs', '.mts', '.ts', '.tsx'],
rules: rules ?? [
{
action: require.resolve('@linaria/shaker'),
},
{
// The old `ignore` option is used as a default value for `ignore` rule.
test: ignore ?? nodeModulesRegExp,
action: 'ignore',
},
{
// Do not ignore ES-modules
test: (filename, code) => {
if (!nodeModulesRegExp.test(filename)) {
return false;
}
// If a file contains `export` or `import` keywords, we assume it's an ES-module
return /\b(?:export|import)\b/.test(code);
},
action: require.resolve('@linaria/shaker'),
},
],
babelOptions,
...(result ? result.config : null),
...rest,
};
cache.set(overrides, options);
return options;
}