WXL
3 天以前 9bce51f651aad297ef9eb6df832bfdaf1de05d84
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
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Gengkun He @ahabhgk
*/
 
"use strict";
 
/** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
// Copy from css-loader
/**
 * Preserve camel case.
 * @param {string} string string
 * @returns {string} result
 */
const preserveCamelCase = (string) => {
    let result = string;
    let isLastCharLower = false;
    let isLastCharUpper = false;
    let isLastLastCharUpper = false;
 
    for (let i = 0; i < result.length; i++) {
        const character = result[i];
 
        if (isLastCharLower && /\p{Lu}/u.test(character)) {
            result = `${result.slice(0, i)}-${result.slice(i)}`;
            isLastCharLower = false;
            isLastLastCharUpper = isLastCharUpper;
            isLastCharUpper = true;
            i += 1;
        } else if (
            isLastCharUpper &&
            isLastLastCharUpper &&
            /\p{Ll}/u.test(character)
        ) {
            result = `${result.slice(0, i - 1)}-${result.slice(i - 1)}`;
            isLastLastCharUpper = isLastCharUpper;
            isLastCharUpper = false;
            isLastCharLower = true;
        } else {
            isLastCharLower =
                character.toLowerCase() === character &&
                character.toUpperCase() !== character;
            isLastLastCharUpper = isLastCharUpper;
            isLastCharUpper =
                character.toUpperCase() === character &&
                character.toLowerCase() !== character;
        }
    }
 
    return result;
};
 
// Copy from css-loader
/**
 * Returns result.
 * @param {string} input input
 * @returns {string} result
 */
module.exports.camelCase = (input) => {
    let result = input.trim();
 
    if (result.length === 0) {
        return "";
    }
 
    if (result.length === 1) {
        return result.toLowerCase();
    }
 
    const hasUpperCase = result !== result.toLowerCase();
 
    if (hasUpperCase) {
        result = preserveCamelCase(result);
    }
 
    return result
        .replace(/^[_.\- ]+/, "")
        .toLowerCase()
        .replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toUpperCase())
        .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, (m) => m.toUpperCase());
};
 
/**
 * Returns results.
 * @param {string} input input
 * @param {CssGeneratorExportsConvention | undefined} convention convention
 * @returns {string[]} results
 */
module.exports.cssExportConvention = (input, convention) => {
    /** @type {Set<string>} */
    const set = new Set();
    if (typeof convention === "function") {
        set.add(convention(input));
    } else {
        switch (convention) {
            case "camel-case": {
                set.add(input);
                set.add(module.exports.camelCase(input));
                break;
            }
            case "camel-case-only": {
                set.add(module.exports.camelCase(input));
                break;
            }
            case "dashes": {
                set.add(input);
                set.add(module.exports.dashesCamelCase(input));
                break;
            }
            case "dashes-only": {
                set.add(module.exports.dashesCamelCase(input));
                break;
            }
            case "as-is": {
                set.add(input);
                break;
            }
        }
    }
    return [...set];
};
 
// Copy from css-loader
/**
 * Returns result.
 * @param {string} input input
 * @returns {string} result
 */
module.exports.dashesCamelCase = (input) =>
    input.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase());