Skip to content

Commit 0b0cb96

Browse files
authored
Merge pull request #115 from tailwindcss/config-default
Support default values in config function
2 parents 6f43d4f + c7061e9 commit 0b0cb96

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

__tests__/configFunction.test.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import postcss from 'postcss'
2+
import plugin from '../src/lib/evaluateTailwindFunctions'
3+
4+
function run(input, opts = {}) {
5+
return postcss([plugin(() => opts)]).process(input)
6+
}
7+
8+
test("it looks up values in the config using dot notation", () => {
9+
const input = `
10+
.banana { color: config('colors.yellow'); }
11+
`
12+
13+
const output = `
14+
.banana { color: #f7cc50; }
15+
`
16+
17+
return run(input, {
18+
colors: {
19+
yellow: '#f7cc50',
20+
}
21+
}).then(result => {
22+
expect(result.css).toEqual(output)
23+
expect(result.warnings().length).toBe(0)
24+
})
25+
})
26+
27+
test("quotes are optional around the lookup path", () => {
28+
const input = `
29+
.banana { color: config(colors.yellow); }
30+
`
31+
32+
const output = `
33+
.banana { color: #f7cc50; }
34+
`
35+
36+
return run(input, {
37+
colors: {
38+
yellow: '#f7cc50',
39+
}
40+
}).then(result => {
41+
expect(result.css).toEqual(output)
42+
expect(result.warnings().length).toBe(0)
43+
})
44+
})
45+
46+
test("a default value can be provided", () => {
47+
const input = `
48+
.cookieMonster { color: config('colors.blue', #0000ff); }
49+
`
50+
51+
const output = `
52+
.cookieMonster { color: #0000ff; }
53+
`
54+
55+
return run(input, {
56+
colors: {
57+
yellow: '#f7cc50',
58+
}
59+
}).then(result => {
60+
expect(result.css).toEqual(output)
61+
expect(result.warnings().length).toBe(0)
62+
})
63+
})
64+
65+
test("quotes are preserved around default values", () => {
66+
const input = `
67+
.heading { font-family: config('fonts.sans', "Helvetica Neue"); }
68+
`
69+
70+
const output = `
71+
.heading { font-family: "Helvetica Neue"; }
72+
`
73+
74+
return run(input, {
75+
fonts: {
76+
serif: "Constantia",
77+
}
78+
}).then(result => {
79+
expect(result.css).toEqual(output)
80+
expect(result.warnings().length).toBe(0)
81+
})
82+
})

src/lib/evaluateTailwindFunctions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export default function(config) {
66

77
return functions({
88
functions: {
9-
config: function (path) {
10-
return _.get(options, _.trim(path, `'"`))
9+
config: function (path, defaultValue) {
10+
return _.get(options, _.trim(path, `'"`), defaultValue)
1111
}
1212
}
1313
})

0 commit comments

Comments
 (0)