1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Gengkun He @ahabhgk
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * @param {string} input input
|
---|
12 | * @param {CssGeneratorExportsConvention | undefined} convention convention
|
---|
13 | * @returns {string[]} results
|
---|
14 | */
|
---|
15 | module.exports.cssExportConvention = (input, convention) => {
|
---|
16 | const set = new Set();
|
---|
17 | if (typeof convention === "function") {
|
---|
18 | set.add(convention(input));
|
---|
19 | } else {
|
---|
20 | switch (convention) {
|
---|
21 | case "camel-case": {
|
---|
22 | set.add(input);
|
---|
23 | set.add(module.exports.camelCase(input));
|
---|
24 | break;
|
---|
25 | }
|
---|
26 | case "camel-case-only": {
|
---|
27 | set.add(module.exports.camelCase(input));
|
---|
28 | break;
|
---|
29 | }
|
---|
30 | case "dashes": {
|
---|
31 | set.add(input);
|
---|
32 | set.add(module.exports.dashesCamelCase(input));
|
---|
33 | break;
|
---|
34 | }
|
---|
35 | case "dashes-only": {
|
---|
36 | set.add(module.exports.dashesCamelCase(input));
|
---|
37 | break;
|
---|
38 | }
|
---|
39 | case "as-is": {
|
---|
40 | set.add(input);
|
---|
41 | break;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | return Array.from(set);
|
---|
46 | };
|
---|
47 |
|
---|
48 | // Copy from css-loader
|
---|
49 | /**
|
---|
50 | * @param {string} input input
|
---|
51 | * @returns {string} result
|
---|
52 | */
|
---|
53 | module.exports.dashesCamelCase = input =>
|
---|
54 | input.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase());
|
---|
55 |
|
---|
56 | // Copy from css-loader
|
---|
57 | /**
|
---|
58 | * @param {string} input input
|
---|
59 | * @returns {string} result
|
---|
60 | */
|
---|
61 | module.exports.camelCase = input => {
|
---|
62 | let result = input.trim();
|
---|
63 |
|
---|
64 | if (result.length === 0) {
|
---|
65 | return "";
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (result.length === 1) {
|
---|
69 | return result.toLowerCase();
|
---|
70 | }
|
---|
71 |
|
---|
72 | const hasUpperCase = result !== result.toLowerCase();
|
---|
73 |
|
---|
74 | if (hasUpperCase) {
|
---|
75 | result = preserveCamelCase(result);
|
---|
76 | }
|
---|
77 |
|
---|
78 | return result
|
---|
79 | .replace(/^[_.\- ]+/, "")
|
---|
80 | .toLowerCase()
|
---|
81 | .replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toUpperCase())
|
---|
82 | .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toUpperCase());
|
---|
83 | };
|
---|
84 |
|
---|
85 | // Copy from css-loader
|
---|
86 | /**
|
---|
87 | * @param {string} string string
|
---|
88 | * @returns {string} result
|
---|
89 | */
|
---|
90 | const preserveCamelCase = string => {
|
---|
91 | let result = string;
|
---|
92 | let isLastCharLower = false;
|
---|
93 | let isLastCharUpper = false;
|
---|
94 | let isLastLastCharUpper = false;
|
---|
95 |
|
---|
96 | for (let i = 0; i < result.length; i++) {
|
---|
97 | const character = result[i];
|
---|
98 |
|
---|
99 | if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
|
---|
100 | result = `${result.slice(0, i)}-${result.slice(i)}`;
|
---|
101 | isLastCharLower = false;
|
---|
102 | isLastLastCharUpper = isLastCharUpper;
|
---|
103 | isLastCharUpper = true;
|
---|
104 | i += 1;
|
---|
105 | } else if (
|
---|
106 | isLastCharUpper &&
|
---|
107 | isLastLastCharUpper &&
|
---|
108 | /[\p{Ll}]/u.test(character)
|
---|
109 | ) {
|
---|
110 | result = `${result.slice(0, i - 1)}-${result.slice(i - 1)}`;
|
---|
111 | isLastLastCharUpper = isLastCharUpper;
|
---|
112 | isLastCharUpper = false;
|
---|
113 | isLastCharLower = true;
|
---|
114 | } else {
|
---|
115 | isLastCharLower =
|
---|
116 | character.toLowerCase() === character &&
|
---|
117 | character.toUpperCase() !== character;
|
---|
118 | isLastLastCharUpper = isLastCharUpper;
|
---|
119 | isLastCharUpper =
|
---|
120 | character.toUpperCase() === character &&
|
---|
121 | character.toLowerCase() !== character;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return result;
|
---|
126 | };
|
---|