Skip to content

Commit 3439685

Browse files
Merge pull request #14 from dobromir-hristov/feat/allow-optional-quoted-keys
* feat: enable reading corePlugins settings, to disable generating unnecessary variables. (#15)
2 parents 1b1071c + f9527d1 commit 3439685

File tree

15 files changed

+2089
-16
lines changed

15 files changed

+2089
-16
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: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,33 @@ class Converter {
99

1010
/** @type {string} - the format and file extension */
1111
format
12+
/** @type {object} - the resolved theme configuration settings */
13+
theme = {}
14+
/** @type {object} - tailwind configurations */
15+
configs = {}
1216

1317
/** @type {string} - the symbol that starts a map */
1418
mapOpener = '(\n'
1519
/** @type {string} - the symbol that ends a map */
1620
mapCloser = ')'
21+
/** @type {boolean} - should map keys be quoted */
22+
quotedKeys = false
1723

1824
/**
1925
* @param opts
2026
* @param {Object} opts.config - Tailwind config object
2127
* @param {Boolean} opts.flat - Is flat or not
2228
* @param {String} opts.prefix - If we want a variable prefix
29+
* @param {Boolean} [opts.quotedKeys] - Should map keys be quoted
2330
*/
2431
constructor (opts) {
25-
this.config = opts.config.theme
26-
this.flat = opts.flat
32+
const { theme, ...rest } = opts.config
33+
this.theme = theme
34+
this.configs = rest
2735

36+
this.flat = opts.flat
2837
this.prefix = opts.prefix || ''
38+
this.quotedKeys = opts.quotedKeys || false
2939
}
3040

3141
/**
@@ -133,15 +143,15 @@ class Converter {
133143
* @returns {string}
134144
*/
135145
convert () {
136-
let metric
146+
let setting
137147
let buffer = ''
138-
for (metric in this.config) {
139-
if (this.config.hasOwnProperty(metric)) {
140-
const data = this.config[metric]
148+
for (setting in this.theme) {
149+
if (this.theme.hasOwnProperty(setting) && this._isSettingEnabled(setting)) {
150+
const data = this.theme[setting]
141151

142152
const body = this.flat
143-
? this._convertObjectToVar(metric, data)
144-
: this._convertObjectToMap(metric, data)
153+
? this._convertObjectToVar(setting, data)
154+
: this._convertObjectToMap(setting, data)
145155

146156
buffer += '\n'
147157
buffer += body
@@ -150,6 +160,18 @@ class Converter {
150160
return buffer
151161
}
152162

163+
/**
164+
* Checks whether a setting is enabled or not.
165+
* @param {string} key
166+
* @return {boolean}
167+
* @private
168+
*/
169+
_isSettingEnabled (key) {
170+
const { corePlugins } = this.configs
171+
if (!corePlugins) return true
172+
return Array.isArray(corePlugins) ? corePlugins.includes(key) : corePlugins[key] !== false
173+
}
174+
153175
/**
154176
* Sanitizes a value, escaping and removing symbols
155177
* @param {*} value
@@ -184,7 +206,7 @@ class Converter {
184206
* @private
185207
*/
186208
_objectEntryKeySanitizer (key) {
187-
return key
209+
return this.quotedKeys ? `"${key}"` : key
188210
}
189211
}
190212

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/__snapshots__/index.spec.js.snap

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,3 +1762,42 @@ $zIndex: (
17621762
);
17631763
"
17641764
`;
1765+
1766+
exports[`Tailwind Options Exporter skips options that are disabled in \`corePlugins\` using the array pattern 1`] = `
1767+
"/* Converted Tailwind Config to scss */
1768+
$cursor: (
1769+
auto: auto,
1770+
default: default,
1771+
pointer: pointer,
1772+
wait: wait,
1773+
text: text,
1774+
move: move,
1775+
not-allowed: not-allowed,
1776+
);
1777+
"
1778+
`;
1779+
1780+
exports[`Tailwind Options Exporter skips options that are disabled in \`corePlugins\` using the object pattern 1`] = `
1781+
"/* Converted Tailwind Config to scss */
1782+
$screens: (
1783+
sm: 640px,
1784+
md: 768px,
1785+
);
1786+
1787+
$fontFamily: (
1788+
display: (Gilroy,sans-serif),
1789+
body: (Graphik,sans-serif),
1790+
);
1791+
1792+
$borderWidth: (
1793+
0: 0,
1794+
default: 1px,
1795+
);
1796+
1797+
$minWidth: (
1798+
0: 0,
1799+
full: 100%,
1800+
half: 50%,
1801+
);
1802+
"
1803+
`;

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)