Skip to content

Commit f9527d1

Browse files
feat: enable reading corePlugins settings, to disable generating unnecessary variables. (#15)
1 parent 2d4308b commit f9527d1

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
lines changed

src/converters/Converter.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ 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'
@@ -25,7 +29,10 @@ class Converter {
2529
* @param {Boolean} [opts.quotedKeys] - Should map keys be quoted
2630
*/
2731
constructor (opts) {
28-
this.config = opts.config.theme
32+
const { theme, ...rest } = opts.config
33+
this.theme = theme
34+
this.configs = rest
35+
2936
this.flat = opts.flat
3037
this.prefix = opts.prefix || ''
3138
this.quotedKeys = opts.quotedKeys || false
@@ -136,15 +143,15 @@ class Converter {
136143
* @returns {string}
137144
*/
138145
convert () {
139-
let metric
146+
let setting
140147
let buffer = ''
141-
for (metric in this.config) {
142-
if (this.config.hasOwnProperty(metric)) {
143-
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]
144151

145152
const body = this.flat
146-
? this._convertObjectToVar(metric, data)
147-
: this._convertObjectToMap(metric, data)
153+
? this._convertObjectToVar(setting, data)
154+
: this._convertObjectToMap(setting, data)
148155

149156
buffer += '\n'
150157
buffer += body
@@ -153,6 +160,18 @@ class Converter {
153160
return buffer
154161
}
155162

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+
156175
/**
157176
* Sanitizes a value, escaping and removing symbols
158177
* @param {*} value

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const ConvertTo = require('../../src/index')
22
const path = require('path')
33

44
const testConfig = require('../tailwind.config')
5+
const testConfigDisabledPlugins = require('../tailwind-disabled-plugins.config')
56
const fse = require('fs-extra')
67

78
jest.mock('fs-extra')
@@ -39,6 +40,37 @@ describe('Tailwind Options Exporter', () => {
3940
expect(converterInstance.converterInstance.format).toBe('scss')
4041
})
4142

43+
it('skips options that are disabled in `corePlugins` using the object pattern', () => {
44+
let converterInstance = new ConvertTo({
45+
config: testConfigDisabledPlugins,
46+
format: 'scss'
47+
})
48+
49+
const scssConfig = converterInstance.convert()
50+
// assert it does not contain a few properties
51+
expect(scssConfig).not.toContain('borderRadius')
52+
expect(scssConfig).not.toContain('backgroundSize')
53+
// assert the whole snapshot
54+
expect(scssConfig).toMatchSnapshot()
55+
})
56+
57+
it('skips options that are disabled in `corePlugins` using the array pattern', () => {
58+
let converterInstance = new ConvertTo({
59+
config: {
60+
...testConfig,
61+
corePlugins: ['cursor']
62+
},
63+
format: 'scss'
64+
})
65+
66+
const scssConfig = converterInstance.convert()
67+
// assert it does not contain a few properties
68+
expect(scssConfig).toContain('cursor')
69+
expect(scssConfig).not.toContain('backgroundSize')
70+
// assert the whole snapshot
71+
expect(scssConfig).toMatchSnapshot()
72+
})
73+
4274
it('it properly includes the provided configuration properties', () => {
4375
let converterInstance = new ConvertTo({
4476
config: testConfig,

tests/tailwind-disabled-plugins.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
flexGrow: false,
3636
flexShrink: false,
3737
fontSize: false,
38+
fontWeight: false,
3839
height: false,
3940
inset: false,
4041
letterSpacing: false,
@@ -43,6 +44,7 @@ module.exports = {
4344
margin: false,
4445
maxHeight: false,
4546
minHeight: false,
47+
maxWidth: false,
4648
objectPosition: false,
4749
opacity: false,
4850
order: false,

0 commit comments

Comments
 (0)