UNPKG

1.59 kBJavaScriptView Raw
1export function shouldCssModules(options) {
2 const passedInOption = processCssmodulesArgument(options);
3
4 // We should module when my-file.module.css or my-file.css
5 const moduleAllCss = passedInOption === true;
6
7 // We should module when my-file.module.css
8 const allowOnlySuffixModule = passedInOption === null;
9
10 return moduleAllCss || allowOnlySuffixModule;
11}
12
13export function cssModulesConfig(options) {
14 const passedInOption = processCssmodulesArgument(options);
15 const isWatchMode = options.watch;
16 const hasPassedInScopeName = !(
17 typeof passedInOption === 'boolean' || passedInOption === null
18 );
19
20 if (shouldCssModules(options) || hasPassedInScopeName) {
21 let generateScopedName = isWatchMode
22 ? '_[name]__[local]__[hash:base64:5]'
23 : '_[hash:base64:5]';
24
25 if (hasPassedInScopeName) {
26 generateScopedName = passedInOption; // would be the string from --css-modules "_[hash]".
27 }
28
29 return { generateScopedName };
30 }
31
32 return false;
33}
34
35/**
36 * This is done because if you use the cli default property, you get a primiatve "null" or "false",
37 * but when using the cli arguments, you always get back strings. This method aims at correcting those
38 * for both realms. So that both realms _convert_ into primatives.
39 */
40function processCssmodulesArgument(options) {
41 if (options['css-modules'] === 'true' || options['css-modules'] === true)
42 return true;
43 if (options['css-modules'] === 'false' || options['css-modules'] === false)
44 return false;
45 if (options['css-modules'] === 'null' || options['css-modules'] === null)
46 return null;
47
48 return options['css-modules'];
49}