Skip to content

Commit 45a6cfb

Browse files
committed
feat: add singleQuote option to allow custom style for keys with dashes
1 parent 31f0830 commit 45a6cfb

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
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.
@@ -177,6 +181,7 @@ You can set the following options:
177181
- `option.searchDir`: Directory which includes target `*.css` files(default: `'./'`).
178182
- `option.outDir`: Output directory(default: `option.searchDir`).
179183
- `option.camelCase`: Camelize CSS class tokens.
184+
- `option.singleQuote`: Use single quotes on dashed keys.
180185
- `option.namedExports`: Use named exports as opposed to default exports to enable tree shaking. Requires `import * as style from './file.module.css';` (default: `false`)
181186
- `option.EOL`: EOL (end of line) for the generated `d.ts` files. Possible values `'\n'` or `'\r\n'`(default: `os.EOL`).
182187

src/cli.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const yarg = yargs
2828
.alias('c', 'camelCase')
2929
.describe('c', 'Convert CSS class tokens to camelcase')
3030
.boolean('c')
31+
.alias('sq', 'singleQuote')
32+
.describe('sq', 'Use single quotes for writing the keys when they have a dash')
33+
.boolean('sq')
3134
.alias('e', 'namedExports')
3235
.describe('e', 'Use named exports as opposed to default exports to enable tree shaking.')
3336
.boolean('e')
@@ -66,6 +69,7 @@ async function main(): Promise<void> {
6669
outDir: argv.o,
6770
watch: argv.w,
6871
camelCase: argv.c,
72+
singleQuote: argv.sq,
6973
namedExports: argv.e,
7074
dropExtension: argv.d,
7175
silent: argv.s,

src/dts-content.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface DtsContentOptions {
2121
rawTokenList: string[];
2222
namedExports: boolean;
2323
camelCase: CamelCaseOption;
24+
singleQuote?: boolean;
2425
EOL: string;
2526
}
2627

@@ -33,6 +34,7 @@ export class DtsContent {
3334
private rawTokenList: string[];
3435
private namedExports: boolean;
3536
private camelCase: CamelCaseOption;
37+
private singleQuote?: boolean;
3638
private resultList: string[];
3739
private EOL: string;
3840

@@ -45,6 +47,7 @@ export class DtsContent {
4547
this.rawTokenList = options.rawTokenList;
4648
this.namedExports = options.namedExports;
4749
this.camelCase = options.camelCase;
50+
this.singleQuote = options.singleQuote;
4851
this.EOL = options.EOL;
4952

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

74-
return (
77+
const data = (
7578
['declare const styles: {', ...this.resultList.map(line => ' ' + line), '};', 'export = styles;', ''].join(
7679
os.EOL,
7780
) + this.EOL
7881
);
82+
83+
return this.singleQuote ? data.replace(/\"/g, "'") : data;
7984
}
8085

8186
public get tokens(): string[] {

src/dts-creator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface DtsCreatorOptions {
1010
searchDir?: string;
1111
outDir?: string;
1212
camelCase?: CamelCaseOption;
13+
singleQuote?: boolean;
1314
namedExports?: boolean;
1415
dropExtension?: boolean;
1516
EOL?: string;
@@ -24,6 +25,7 @@ export class DtsCreator {
2425
private inputDirectory: string;
2526
private outputDirectory: string;
2627
private camelCase: CamelCaseOption;
28+
private singleQuote?: boolean;
2729
private namedExports: boolean;
2830
private dropExtension: boolean;
2931
private EOL: string;
@@ -36,6 +38,7 @@ export class DtsCreator {
3638
this.loader = new FileSystemLoader(this.rootDir, options.loaderPlugins);
3739
this.inputDirectory = path.join(this.rootDir, this.searchDir);
3840
this.outputDirectory = path.join(this.rootDir, this.outDir);
41+
this.singleQuote = options.singleQuote;
3942
this.camelCase = options.camelCase;
4043
this.namedExports = !!options.namedExports;
4144
this.dropExtension = !!options.dropExtension;
@@ -60,6 +63,7 @@ export class DtsCreator {
6063

6164
const content = new DtsContent({
6265
dropExtension: this.dropExtension,
66+
singleQuote: this.singleQuote,
6367
rootDir: this.rootDir,
6468
searchDir: this.searchDir,
6569
outDir: this.outDir,

src/run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface RunOptions {
1313
outDir?: string;
1414
watch?: boolean;
1515
camelCase?: boolean;
16+
singleQuote?: boolean;
1617
namedExports?: boolean;
1718
dropExtension?: boolean;
1819
silent?: boolean;
@@ -27,6 +28,7 @@ export async function run(searchDir: string, options: RunOptions = {}): Promise<
2728
searchDir,
2829
outDir: options.outDir,
2930
camelCase: options.camelCase,
31+
singleQuote: options.singleQuote,
3032
namedExports: options.namedExports,
3133
dropExtension: options.dropExtension,
3234
});

test/dts-creator.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,40 @@ declare const styles: {
233233
};
234234
export = styles;
235235
236+
`,
237+
);
238+
done();
239+
});
240+
});
241+
});
242+
243+
describe('#singleQuote option', () => {
244+
it('singleQuote == undefined: returns same as before', done => {
245+
new DtsCreator().create('test/kebabed.css').then(content => {
246+
assert.equal(
247+
content.formatted,
248+
`\
249+
declare const styles: {
250+
readonly "my-class": string;
251+
};
252+
export = styles;
253+
254+
`,
255+
);
256+
done();
257+
});
258+
});
259+
260+
it('singleQuote == true: returns kebab keys with single quote', done => {
261+
new DtsCreator({ singleQuote: true, EOL: '\n' }).create('test/kebabedUpperCase.css').then(content => {
262+
assert.equal(
263+
content.formatted,
264+
`\
265+
declare const styles: {
266+
readonly 'My-class': string;
267+
};
268+
export = styles;
269+
236270
`,
237271
);
238272
done();

0 commit comments

Comments
 (0)