Skip to content

Commit 371a79f

Browse files
committed
Support plugin-specific target mode
1 parent e5288ff commit 371a79f

37 files changed

+180
-92
lines changed

__tests__/plugins/divideWidth.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import plugin from '../../src/plugins/divideWidth'
33

44
test('generating divide width utilities', () => {
55
const config = {
6+
target: 'relaxed',
67
theme: {
78
divideWidth: {
89
default: '1px',

__tests__/plugins/space.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import plugin from '../../src/plugins/space'
33

44
test('generating space utilities', () => {
55
const config = {
6+
target: 'relaxed',
67
theme: {
78
space: {
89
'0': '0',

__tests__/sanity.test.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@ it('generates the right CSS when using @import instead of @tailwind', () => {
5252
})
5353
})
5454

55-
it('generates the right CSS in IE11 mode', () => {
56-
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)
57-
const input = fs.readFileSync(inputPath, 'utf8')
58-
59-
return postcss([tailwind({ ...config, target: 'ie11' })])
60-
.process(input, { from: inputPath })
61-
.then(result => {
62-
const expected = fs.readFileSync(
63-
path.resolve(`${__dirname}/fixtures/tailwind-output-ie11.css`),
64-
'utf8'
65-
)
66-
67-
expect(result.css).toBe(expected)
68-
})
69-
})
70-
7155
it('does not add any CSS if no Tailwind features are used', () => {
7256
return postcss([tailwind()])
7357
.process('.foo { color: blue; }', { from: undefined })

__tests__/targetMode.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import _ from 'lodash'
4+
import postcss from 'postcss'
5+
import tailwind from '../src/index'
6+
import config from '../stubs/defaultConfig.stub.js'
7+
import processPlugins from '../src/util/processPlugins'
8+
9+
function css(nodes) {
10+
return postcss.root({ nodes }).toString()
11+
}
12+
13+
it('generates the right CSS in IE11 mode', () => {
14+
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)
15+
const input = fs.readFileSync(inputPath, 'utf8')
16+
17+
return postcss([tailwind({ ...config, target: 'ie11' })])
18+
.process(input, { from: inputPath })
19+
.then(result => {
20+
const expected = fs.readFileSync(
21+
path.resolve(`${__dirname}/fixtures/tailwind-output-ie11.css`),
22+
'utf8'
23+
)
24+
25+
expect(result.css).toBe(expected)
26+
})
27+
})
28+
29+
test('plugins can request the target for a specific plugin key', () => {
30+
const { utilities } = processPlugins(
31+
[
32+
function({ addUtilities, target }) {
33+
addUtilities({
34+
'.testA': {
35+
target: target('testPluginA'),
36+
},
37+
})
38+
},
39+
function({ addUtilities, target }) {
40+
addUtilities({
41+
'.testB': {
42+
target: target('testPluginB'),
43+
},
44+
})
45+
},
46+
function({ addUtilities, target }) {
47+
addUtilities({
48+
'.testC': {
49+
target: target('testPluginC'),
50+
},
51+
})
52+
},
53+
],
54+
{
55+
...config,
56+
target: [
57+
'ie11',
58+
{
59+
testPluginA: 'modern',
60+
testPluginB: 'relaxed',
61+
},
62+
],
63+
}
64+
)
65+
66+
expect(css(utilities)).toMatchCss(`
67+
@variants {
68+
.testA {
69+
target: modern
70+
}
71+
}
72+
@variants {
73+
.testB {
74+
target: relaxed
75+
}
76+
}
77+
@variants {
78+
.testC {
79+
target: ie11
80+
}
81+
}
82+
`)
83+
})

__tests__/util/invokePlugin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export default function(plugin, config) {
1616

1717
return getConfigValue(`variants.${path}`, defaultValue)
1818
},
19+
target: path => {
20+
if (_.isString(config.target)) {
21+
return config.target
22+
}
23+
24+
const [defaultTarget, targetOverrides] = getConfigValue('target')
25+
26+
return _.get(targetOverrides, path, defaultTarget)
27+
},
1928
addUtilities(utilities, variants) {
2029
addedUtilities.push([utilities, variants])
2130
},

src/plugins/backgroundColor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import flattenColorPalette from '../util/flattenColorPalette'
33
import withAlphaVariable from '../util/withAlphaVariable'
44

55
export default function() {
6-
return function({ addUtilities, e, theme, variants, config }) {
7-
if (config('target') === 'ie11') {
6+
return function({ addUtilities, e, theme, variants, target }) {
7+
if (target('backgroundColor') === 'ie11') {
88
const utilities = _.fromPairs(
99
_.map(flattenColorPalette(theme('backgroundColor')), (value, modifier) => {
1010
return [`.${e(`bg-${modifier}`)}`, { 'background-color': value }]

src/plugins/backgroundOpacity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import createUtilityPlugin from '../util/createUtilityPlugin'
22

33
export default function() {
4-
return function({ config, ...args }) {
5-
if (config('target') === 'ie11') {
4+
return function({ target, ...args }) {
5+
if (target('backgroundOpacity') === 'ie11') {
66
return
77
}
88

99
createUtilityPlugin('backgroundOpacity', [['bg-opacity', ['--bg-opacity']]])({
10-
config,
10+
target,
1111
...args,
1212
})
1313
}

src/plugins/borderColor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import flattenColorPalette from '../util/flattenColorPalette'
33
import withAlphaVariable from '../util/withAlphaVariable'
44

55
export default function() {
6-
return function({ addUtilities, e, theme, variants, config }) {
7-
if (config('target') === 'ie11') {
6+
return function({ addUtilities, e, theme, variants, target }) {
7+
if (target('borderColor') === 'ie11') {
88
const colors = flattenColorPalette(theme('borderColor'))
99

1010
const utilities = _.fromPairs(

src/plugins/borderOpacity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import createUtilityPlugin from '../util/createUtilityPlugin'
22

33
export default function() {
4-
return function({ config, ...args }) {
5-
if (config('target') === 'ie11') {
4+
return function({ target, ...args }) {
5+
if (target('borderOpacity') === 'ie11') {
66
return
77
}
88

99
createUtilityPlugin('borderOpacity', [['border-opacity', ['--border-opacity']]])({
10-
config,
10+
target,
1111
...args,
1212
})
1313
}

src/plugins/display.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default function() {
2-
return function({ addUtilities, variants, config }) {
2+
return function({ addUtilities, variants, target }) {
33
addUtilities(
44
{
55
'.block': {
@@ -44,7 +44,7 @@ export default function() {
4444
'.table-row': {
4545
display: 'table-row',
4646
},
47-
...(config('target') === 'ie11'
47+
...(target('display') === 'ie11'
4848
? {}
4949
: {
5050
'.flow-root': {

0 commit comments

Comments
 (0)