|
1 | 1 | import { CSSExports } from "icss-utils"; |
2 | 2 | import { camelCase } from "lodash"; |
3 | | -import reserved from "reserved-words"; |
4 | 3 | import IOptions from "./IOptions"; |
5 | 4 |
|
6 | 5 | const dashesCamelCase = (className: string) => { |
7 | 6 | return className.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase()); |
8 | 7 | }; |
9 | 8 |
|
10 | 9 | export default class ExportGenerator { |
11 | | - private static readonly NOT_CAMELCASE_REGEXP = /[\-_]/; |
12 | | - |
13 | 10 | private readonly options: IOptions; |
14 | 11 |
|
15 | 12 | public constructor(options: IOptions) { |
16 | 13 | this.options = options; |
17 | 14 | } |
18 | 15 |
|
19 | 16 | public generate(classes: CSSExports) { |
20 | | - const isCamelCase = (className: string) => !ExportGenerator.NOT_CAMELCASE_REGEXP.test(className); |
21 | | - const isReservedWord = (className: string) => !reserved.check(className); |
22 | | - |
23 | 17 | const classNames = Object.keys(classes) |
24 | 18 | .map(className => this.transformClassName(className)) |
25 | 19 | // Flatter array because transformClassName() may return multiple classes |
26 | 20 | .reduce((previousValue: Array<string> = [], currentValue: Array<string>) => { |
27 | 21 | return previousValue.concat(currentValue); |
28 | 22 | }, []); |
29 | 23 |
|
30 | | - const camelCasedKeys = classNames |
31 | | - .filter(isCamelCase) |
32 | | - .filter(isReservedWord) |
33 | | - .map(className => `export const ${className}: string;`); |
34 | | - |
35 | 24 | const defaultExport = `\ |
36 | 25 | declare const styles: { |
| 26 | + [index: string]: string; |
37 | 27 | ${classNames.map(className => `"${className}": string;`).join("\n ")} |
38 | 28 | }; |
39 | 29 | export default styles; |
40 | 30 | `; |
41 | 31 |
|
42 | | - if (camelCasedKeys.length) { |
43 | | - return `${defaultExport}${camelCasedKeys.join("\n")}\n`; |
44 | | - } |
45 | | - |
46 | 32 | return defaultExport; |
47 | 33 | } |
48 | 34 |
|
|
0 commit comments