Skip to content

Commit 18001d4

Browse files
committed
feat: add singleQuote option to allow custom style for keys with dashes
1 parent 39188ad commit 18001d4

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export = styles;
117117

118118
See also [webpack css-loader's camelCase option](https://github.com/webpack/css-loader#camelcase).
119119

120+
#### Use single quotes to class names in the .d.ts files
121+
122+
With `-sq` or `--singleQuote`, you can configure what quote to use. Useful when tools like prettier format your .d.ts files.
123+
120124
#### named exports (enable tree shaking)
121125

122126
With `-e` or `--namedExports`, types are exported as named exports as opposed to default exports.
@@ -183,6 +187,7 @@ You can set the following options:
183187
- `option.searchDir`: Directory which includes target `*.css` files(default: `'./'`).
184188
- `option.outDir`: Output directory(default: `option.searchDir`).
185189
- `option.camelCase`: Camelize CSS class tokens.
190+
- `option.singleQuote`: Use single quotes on dashed keys.
186191
- `option.namedExports`: Use named exports as opposed to default exports to enable tree shaking. Requires `import * as style from './file.module.css';` (default: `false`)
187192
- `option.allowArbitraryExtensions`: Output filenames that will be compatible with the "arbitrary file extensions" TypeScript feature
188193
- `option.EOL`: EOL (end of line) for the generated `d.ts` files. Possible values `'\n'` or `'\r\n'`(default: `os.EOL`).

src/cli.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const yarg = yargs(hideBin(process.argv))
3737
type: 'boolean',
3838
alias: 'camelCase',
3939
},
40+
sq: {
41+
desc: 'Use single quotes for writing the keys when they have a dash',
42+
type: 'boolean',
43+
alias: 'singleQuote',
44+
},
4045
e: {
4146
type: 'boolean',
4247
desc: 'Use named exports as opposed to default exports to enable tree shaking.',
@@ -87,6 +92,7 @@ async function main(): Promise<void> {
8792
outDir: argv.o,
8893
watch: argv.w,
8994
camelCase: argv.c,
95+
singleQuote: argv.sq,
9096
namedExports: argv.e,
9197
dropExtension: argv.d,
9298
allowArbitraryExtensions: argv.a,

src/dts-content.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface DtsContentOptions {
1818
namedExports: boolean;
1919
allowArbitraryExtensions: boolean;
2020
camelCase: CamelCaseOption;
21+
singleQuote?: boolean;
2122
EOL: string;
2223
}
2324

@@ -31,6 +32,7 @@ export class DtsContent {
3132
private namedExports: boolean;
3233
private allowArbitraryExtensions: boolean;
3334
private camelCase: CamelCaseOption;
35+
private quote: '"' | "'";
3436
private resultList: string[];
3537
private EOL: string;
3638

@@ -44,6 +46,7 @@ export class DtsContent {
4446
this.namedExports = options.namedExports;
4547
this.allowArbitraryExtensions = options.allowArbitraryExtensions;
4648
this.camelCase = options.camelCase;
49+
this.quote = options.singleQuote ? "'" : '"';
4750
this.EOL = options.EOL;
4851

4952
// when using named exports, camelCase must be enabled by default
@@ -71,11 +74,12 @@ export class DtsContent {
7174
);
7275
}
7376

74-
return (
77+
const data =
7578
['declare const styles: {', ...this.resultList.map(line => ' ' + line), '};', 'export = styles;', ''].join(
7679
this.EOL,
77-
) + this.EOL
78-
);
80+
) + this.EOL;
81+
82+
return data;
7983
}
8084

8185
public get tokens(): string[] {
@@ -152,7 +156,7 @@ export class DtsContent {
152156

153157
const result = this.rawTokenList
154158
.map(k => convertKey(k))
155-
.map(k => (!this.namedExports ? 'readonly "' + k + '": string;' : 'const ' + k + ': string;'))
159+
.map(k => (!this.namedExports ? `readonly ${this.quote}${k}${this.quote}: string;` : 'const ' + k + ': string;'))
156160
.sort();
157161

158162
return result;

src/dts-creator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface DtsCreatorOptions {
1212
searchDir?: string;
1313
outDir?: string;
1414
camelCase?: CamelCaseOption;
15+
singleQuote?: boolean;
1516
namedExports?: boolean;
1617
allowArbitraryExtensions?: boolean;
1718
dropExtension?: boolean;
@@ -26,6 +27,7 @@ export class DtsCreator {
2627
private loader: FileSystemLoader;
2728
private inputDirectory: string;
2829
private camelCase: CamelCaseOption;
30+
private singleQuote?: boolean;
2931
private namedExports: boolean;
3032
private allowArbitraryExtensions: boolean;
3133
private dropExtension: boolean;
@@ -38,6 +40,7 @@ export class DtsCreator {
3840
this.outDir = options.outDir || this.searchDir;
3941
this.loader = new FileSystemLoader(this.rootDir, options.loaderPlugins);
4042
this.inputDirectory = path.join(this.rootDir, this.searchDir);
43+
this.singleQuote = options.singleQuote;
4144
this.camelCase = options.camelCase;
4245
this.namedExports = !!options.namedExports;
4346
this.allowArbitraryExtensions = !!options.allowArbitraryExtensions;
@@ -79,6 +82,7 @@ export class DtsCreator {
7982
namedExports: this.namedExports,
8083
allowArbitraryExtensions: this.allowArbitraryExtensions,
8184
camelCase: this.camelCase,
85+
singleQuote: this.singleQuote,
8286
EOL: this.EOL,
8387
});
8488

src/run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface RunOptions {
99
outDir?: string;
1010
watch?: boolean;
1111
camelCase?: boolean;
12+
singleQuote?: boolean;
1213
namedExports?: boolean;
1314
allowArbitraryExtensions?: boolean;
1415
dropExtension?: boolean;
@@ -24,6 +25,7 @@ export async function run(searchDir: string, options: RunOptions = {}): Promise<
2425
searchDir,
2526
outDir: options.outDir,
2627
camelCase: options.camelCase,
28+
singleQuote: options.singleQuote,
2729
namedExports: options.namedExports,
2830
allowArbitraryExtensions: options.allowArbitraryExtensions,
2931
dropExtension: options.dropExtension,

0 commit comments

Comments
 (0)