Skip to content

Commit 2d4308b

Browse files
feat: add quoting keys
1 parent 1b1071c commit 2d4308b

File tree

13 files changed

+1990
-9
lines changed

13 files changed

+1990
-9
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Make a package.json script and run it for convenience
3535
```json
3636
{
3737
"scripts": {
38-
"export-tailwind-config": "tailwindcss-export-config --config=path/to/tailwind.config.js --destination=destination/of/generated/tailwind-variables --format=scss"
38+
"export-tailwind-config": "tailwindcss-export-config --config=path/to/tailwind.config.js --destination=destination/of/generated/tailwind-variables --format=scss --quoted-keys=true"
3939
}
4040
}
4141
```
@@ -50,7 +50,8 @@ const converter = new TailwindExportConfig({
5050
destination: 'converted/file/destination',
5151
format: 'scss',
5252
prefix: 'tw',
53-
flat: true
53+
flat: true,
54+
quotedKeys: true
5455
})
5556

5657
// writeToFile returns a promise so we can chain off it
@@ -72,7 +73,8 @@ config|String,Object|true| Tailwindcss config path or config object to transform
7273
destination|String|true| Destination to save converted file
7374
format|String|true| The format in which to convert the file
7475
prefix|String|false| An optional prefix for each variable name
75-
flat|Boolean|false| Optionally transforms the variables from nested maps to flat level variables. Less does not support nested maps so we default to flat for them always.
76+
flat|Boolean|false| Optionally transforms the variables from nested maps to flat level variables. Less does not support nested maps so we default to flat for them always. Defaults to `false`.
77+
quoted-keys|Boolean|false| (`quotedKeys` in the Node API) - Whether keys in maps should be quoted or not. We recommend to have this set to `true`. Defaults to `false`.
7678

7779
## Example export
7880
Lets get a portion of the Tailwind config
@@ -209,6 +211,30 @@ $tw-colors-pink-900: #702459;
209211
$tw-colors-cyan: #9cdbff;
210212
```
211213

214+
### Quoted Keys
215+
216+
SASS and other preprocessors recommend defining map keys in quotes. It is possible comply with that, by using the optional `quotedKeys` setting.
217+
218+
```bash
219+
tailwindcss-export-config --config=tailwind.config.js --destination=tailwind-variables --format=scss --quoted-keys=true
220+
```
221+
222+
```scss
223+
$fontFamily: (
224+
"display": (Gilroy,sans-serif),
225+
"body": (Graphik,sans-serif),
226+
);
227+
228+
$colors: (
229+
//... other vars
230+
"pink-700": #b83280,
231+
"pink-800": #97266d,
232+
"pink-900": #702459,
233+
"cyan": #9cdbff,
234+
);
235+
236+
```
237+
212238
## Notice
213239

214240
This branch works with v1.x of Tailwindcss. If you are using the older 0.x version, please use `legacy` version

cli.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const argv = yargs // eslint-disable-line
4242
boolean: true,
4343
nargs: 1
4444
})
45+
.option('quoted-keys', {
46+
describe: 'Should map keys be quoted',
47+
type: 'boolean', /* array | boolean | string */
48+
boolean: true,
49+
nargs: 1
50+
})
4551
.argv
4652

4753
try {
@@ -50,7 +56,8 @@ try {
5056
destination: argv.destination,
5157
format: argv.format,
5258
prefix: argv.prefix,
53-
flat: argv.flat
59+
flat: argv.flat,
60+
quotedKeys: argv['quoted-keys']
5461
})
5562
converter.writeToFile()
5663
.then((options) => {

src/converters/Converter.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ class Converter {
1414
mapOpener = '(\n'
1515
/** @type {string} - the symbol that ends a map */
1616
mapCloser = ')'
17+
/** @type {boolean} - should map keys be quoted */
18+
quotedKeys = false
1719

1820
/**
1921
* @param opts
2022
* @param {Object} opts.config - Tailwind config object
2123
* @param {Boolean} opts.flat - Is flat or not
2224
* @param {String} opts.prefix - If we want a variable prefix
25+
* @param {Boolean} [opts.quotedKeys] - Should map keys be quoted
2326
*/
2427
constructor (opts) {
2528
this.config = opts.config.theme
2629
this.flat = opts.flat
27-
2830
this.prefix = opts.prefix || ''
31+
this.quotedKeys = opts.quotedKeys || false
2932
}
3033

3134
/**
@@ -184,7 +187,7 @@ class Converter {
184187
* @private
185188
*/
186189
_objectEntryKeySanitizer (key) {
187-
return key
190+
return this.quotedKeys ? `"${key}"` : key
188191
}
189192
}
190193

src/converters/Sass.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SassConverter extends Converter {
1212
}
1313

1414
_buildObjectEntry (key, value, indent, index, metricIndex = 0) {
15-
return indentWith(`${key}: ${this._sanitizePropValue(value)},`, indent + ((!index && !metricIndex) ? 0 : 1))
15+
return indentWith(`${this._objectEntryKeySanitizer(key)}: ${this._sanitizePropValue(value)},`, indent + ((!index && !metricIndex) ? 0 : 1))
1616
}
1717
}
1818

src/converters/Stylus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class StylusConverter extends Converter {
1111
}
1212

1313
_objectEntryKeySanitizer (prop) {
14-
if (/\d/.test(prop)) return `"${prop}"`
14+
if (/\d/.test(prop) || this.quotedKeys) return `"${prop}"`
1515
return prop
1616
}
1717
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ConvertTo {
2222
* @param {String} options.destination - Output destination
2323
* @param {Boolean} [options.flat] - Whether the variables should be nested maps or flat level variables
2424
* @param {String} options.format - The desired format
25+
* @param {Boolean} options.quotedSassKeys - Whether SASS keys should be quoted. Both for Sass and SCSS.
2526
*/
2627
constructor (options) {
2728
if (!allowedFormatsMap.hasOwnProperty(options.format)) {
@@ -32,7 +33,7 @@ class ConvertTo {
3233
const Converter = allowedFormatsMap[options.format]
3334
const config = resolveConfig(options.config)
3435

35-
this.converterInstance = new Converter({ config, prefix: options.prefix, flat: options.flat })
36+
this.converterInstance = new Converter({ ...options, config })
3637
}
3738

3839
/**

tests/specs/converters/Sass.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ describe('Sass converter', () => {
1111
expect(converter.convert()).toMatchSnapshot()
1212
})
1313

14+
it('wraps keys in quotes', () => {
15+
const converter = new SassConverter({
16+
config: resolveConfig(testConfig),
17+
quotedKeys: true
18+
})
19+
const result = converter.convert()
20+
expect(result).toContain('$screens: ("sm":')
21+
expect(result).toMatchSnapshot()
22+
})
23+
1424
it('Converts to flat variables', () => {
1525
const converter = new SassConverter({
1626
config: resolveConfig(testConfig),

tests/specs/converters/Scss.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ describe('Scss converter', () => {
1010
expect(converter.convert()).toMatchSnapshot()
1111
})
1212

13+
it('converts a nested map with quoted keys', () => {
14+
const converter = new ScssConverter({
15+
config: resolveConfig(testConfig),
16+
quotedKeys: true
17+
})
18+
const result = converter.convert()
19+
expect(result).toContain('"sm": 640px')
20+
expect(result).toMatchSnapshot()
21+
})
22+
1323
it('Converts to flat variables', () => {
1424
const converter = new ScssConverter({
1525
config: resolveConfig(testConfig),

tests/specs/converters/Stylus.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ describe('Stylus converter', () => {
1111
expect(converter.convert()).toMatchSnapshot()
1212
})
1313

14+
it('Converts to nested map and wraps keys in quotes', () => {
15+
const converter = new StylusConverter({
16+
config: resolveConfig(testConfig),
17+
quotedKeys: true
18+
})
19+
const result = converter.convert()
20+
expect(result).toContain('"sm": 640px')
21+
expect(result).toMatchSnapshot()
22+
})
23+
1424
it('Converts to flat variables', () => {
1525
const converter = new StylusConverter({
1626
config: resolveConfig(testConfig)

0 commit comments

Comments
 (0)