Skip to content

Commit 309d0ca

Browse files
feat: implement Css converter (#52)
1 parent 554213e commit 309d0ca

File tree

7 files changed

+2385
-4
lines changed

7 files changed

+2385
-4
lines changed

cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const argv = yargs // eslint-disable-line
2727
alias: 'f',
2828
describe: 'Format to generate - sass,less,stylus',
2929
type: 'string',
30-
choices: ['sass', 'scss', 'less', 'styl', 'json'],
30+
choices: ['sass', 'scss', 'less', 'styl', 'css', 'json'],
3131
nargs: 1,
3232
demand: true
3333
})

src/converters/Converter.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Converter {
2525
flattenMapsAfter = -1
2626
/** @type {array} - config keys to preserve */
2727
preserveKeys = []
28+
prefixContent = ''
29+
suffixContent = ''
2830

2931
/**
3032
* @param opts
@@ -175,7 +177,7 @@ class Converter {
175177
*/
176178
convert () {
177179
let setting
178-
let buffer = ''
180+
let buffer = this.prefixContent
179181
for (setting in this.theme) {
180182
if (this.theme.hasOwnProperty(setting) && this._isSettingEnabled(setting)) {
181183
const data = this.theme[setting]
@@ -188,6 +190,7 @@ class Converter {
188190
buffer += body
189191
}
190192
}
193+
buffer = buffer += this.suffixContent
191194
return buffer
192195
}
193196

src/converters/Css.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Converter from './Converter.js'
2+
3+
class CssConverter extends Converter {
4+
format = 'css'
5+
prefixContent = '\n:root {'
6+
suffixContent = '}'
7+
8+
_buildVar (name, value) {
9+
return `--${name}: ${value};\n`
10+
}
11+
12+
_convertObjectToMap (prop, data) {
13+
return this._convertObjectToVar(prop, data)
14+
}
15+
16+
_sanitizePropValue (value) {
17+
if (Array.isArray(value)) return value.join(', ')
18+
return value
19+
}
20+
21+
_propertyNameSanitizer (property) {
22+
property = super._propertyNameSanitizer(property)
23+
return property.replace(/\./g, '\\.')
24+
}
25+
}
26+
27+
export default CssConverter

src/converters/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import Less from './Less'
22
import Stylus from './Stylus'
33
import Sass from './Sass'
44
import Scss from './Scss'
5+
import Css from './Css'
56
import JSON from './JSON'
67

78
export default {
8-
Less, Sass, Scss, Stylus, JSON
9+
Less, Sass, Scss, Stylus, Css, JSON
910
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const allowedFormatsMap = {
99
sass: converters.Sass,
1010
scss: converters.Scss,
1111
less: converters.Less,
12-
json: converters.JSON
12+
json: converters.JSON,
13+
css: converters.Css,
1314
}
1415

1516
/**

tests/specs/converters/Css.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import CssConverter from '../../../src/converters/Css'
2+
import testConfig from '../../tailwind.config'
3+
import testConfigDefault from '../../tailwind-default.config'
4+
import { resolveConfig } from '../../../src/converters/utils'
5+
6+
describe('Css converter', () => {
7+
describe('full config', () => {
8+
it('Converts to flat variables', () => {
9+
const converter = new CssConverter({
10+
config: resolveConfig(testConfigDefault)
11+
})
12+
expect(converter.convert()).toMatchSnapshot()
13+
})
14+
})
15+
16+
it('converts flat and nested, with the same result', () => {
17+
let converter = new CssConverter({
18+
config: resolveConfig(testConfig),
19+
flat: true
20+
})
21+
const flatResult = converter.convert()
22+
23+
converter = new CssConverter({
24+
config: resolveConfig(testConfig),
25+
flat: false
26+
})
27+
28+
const nestedResult = converter.convert()
29+
30+
expect(flatResult).toBe(nestedResult)
31+
})
32+
33+
it('Converts to flat variables with prefix', () => {
34+
const converter = new CssConverter({
35+
config: resolveConfig(testConfig),
36+
flat: true,
37+
prefix: 'tw'
38+
})
39+
expect(converter.convert()).toMatchSnapshot()
40+
})
41+
42+
it('Converts to nested map with prefix', () => {
43+
const converter = new CssConverter({
44+
config: resolveConfig(testConfig),
45+
prefix: 'tw'
46+
})
47+
expect(converter.convert()).toMatchSnapshot()
48+
})
49+
})

0 commit comments

Comments
 (0)