Skip to content

Commit 1117e28

Browse files
committed
Add support for browserslist as target
1 parent b4acd94 commit 1117e28

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

__tests__/targetMode.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import postcss from 'postcss'
55
import tailwind from '../src/index'
66
import config from '../stubs/defaultConfig.stub.js'
77
import processPlugins from '../src/util/processPlugins'
8+
import runInTempDirectory from '../jest/runInTempDirectory'
89

910
function css(nodes) {
1011
return postcss.root({ nodes }).toString()
@@ -81,3 +82,70 @@ test('plugins can request the target for a specific plugin key', () => {
8182
}
8283
`)
8384
})
85+
86+
test('browserslist target is translated to a target preset', () => {
87+
return runInTempDirectory(() => {
88+
fs.writeFileSync(
89+
path.resolve('./.browserslistrc'),
90+
`
91+
last 2 versions
92+
IE 11
93+
`
94+
)
95+
const { utilities } = processPlugins(
96+
[
97+
function({ addUtilities, target }) {
98+
addUtilities({
99+
'.testA': {
100+
target: target('testPluginA'),
101+
},
102+
})
103+
},
104+
function({ addUtilities, target }) {
105+
addUtilities({
106+
'.testB': {
107+
target: target('testPluginB'),
108+
},
109+
})
110+
},
111+
function({ addUtilities, target }) {
112+
addUtilities({
113+
'.testC': {
114+
target: target('testPluginC'),
115+
},
116+
})
117+
},
118+
],
119+
{
120+
...config,
121+
target: [
122+
'browserslist',
123+
{
124+
testPluginA: 'modern',
125+
testPluginB: 'relaxed',
126+
},
127+
],
128+
}
129+
)
130+
131+
expect(css(utilities)).toMatchCss(`
132+
@variants {
133+
.testA {
134+
target: modern
135+
}
136+
}
137+
@variants {
138+
.testB {
139+
target: relaxed
140+
}
141+
}
142+
@variants {
143+
.testC {
144+
target: ie11
145+
}
146+
}
147+
`)
148+
149+
return Promise.resolve()
150+
})
151+
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"dependencies": {
4444
"@fullhuman/postcss-purgecss": "^2.1.2",
4545
"autoprefixer": "^9.4.5",
46+
"browserslist": "^4.12.0",
4647
"bytes": "^3.0.0",
4748
"chalk": "^4.0.0",
4849
"color": "^3.1.2",

src/util/processPlugins.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from 'lodash'
22
import postcss from 'postcss'
3+
import browserslist from 'browserslist'
34
import Node from 'postcss/lib/node'
45
import isFunction from 'lodash/isFunction'
56
import escapeClassName from '../util/escapeClassName'
@@ -27,6 +28,7 @@ export default function(plugins, config) {
2728
return prefixSelector(config.prefix, selector)
2829
}
2930
const getConfigValue = (path, defaultValue) => _.get(config, path, defaultValue)
31+
const browserslistTarget = browserslist().includes('ie 11') ? 'ie11' : 'relaxed'
3032

3133
plugins.forEach(plugin => {
3234
if (plugin.__isOptionsFunction) {
@@ -53,7 +55,9 @@ export default function(plugins, config) {
5355

5456
const [defaultTarget, targetOverrides] = getConfigValue('target')
5557

56-
return _.get(targetOverrides, path, defaultTarget)
58+
const target = _.get(targetOverrides, path, defaultTarget)
59+
60+
return target === 'browserslist' ? browserslistTarget : target
5761
},
5862
e: escapeClassName,
5963
prefix: applyConfiguredPrefix,

yarn.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,16 @@ browserslist@^4.11.1, browserslist@^4.8.3, browserslist@^4.9.1:
15011501
node-releases "^1.1.53"
15021502
pkg-up "^2.0.0"
15031503

1504+
browserslist@^4.12.0:
1505+
version "4.12.0"
1506+
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.12.0.tgz#06c6d5715a1ede6c51fc39ff67fd647f740b656d"
1507+
integrity sha512-UH2GkcEDSI0k/lRkuDSzFl9ZZ87skSy9w2XAn1MsZnL+4c4rqbBd3e82UWHbYDpztABrPBhZsTEeuxVfHppqDg==
1508+
dependencies:
1509+
caniuse-lite "^1.0.30001043"
1510+
electron-to-chromium "^1.3.413"
1511+
node-releases "^1.1.53"
1512+
pkg-up "^2.0.0"
1513+
15041514
bser@^2.0.0:
15051515
version "2.1.0"
15061516
resolved "https://registry.yarnpkg.com/bser/-/bser-2.1.0.tgz#65fc784bf7f87c009b973c12db6546902fa9c7b5"
@@ -1553,6 +1563,11 @@ caniuse-lite@^1.0.30001038, caniuse-lite@^1.0.30001039:
15531563
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001042.tgz#c91ec21ec2d270bd76dbc2ce261260c292b8c93c"
15541564
integrity sha512-igMQ4dlqnf4tWv0xjaaE02op9AJ2oQzXKjWf4EuAHFN694Uo9/EfPVIPJcmn2WkU9RqozCxx5e2KPcVClHDbDw==
15551565

1566+
caniuse-lite@^1.0.30001043:
1567+
version "1.0.30001048"
1568+
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001048.tgz#4bb4f1bc2eb304e5e1154da80b93dee3f1cf447e"
1569+
integrity sha512-g1iSHKVxornw0K8LG9LLdf+Fxnv7T1Z+mMsf0/YYLclQX4Cd522Ap0Lrw6NFqHgezit78dtyWxzlV2Xfc7vgRg==
1570+
15561571
capture-exit@^2.0.0:
15571572
version "2.0.0"
15581573
resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4"
@@ -1996,6 +2011,11 @@ electron-to-chromium@^1.3.390:
19962011
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.410.tgz#00e0ec61c22933daa8b4de172c03932678783adc"
19972012
integrity sha512-DbCBdwtARI0l3e3m6ZIxVaTNahb6dSsmGjuag/twiVcWuM4MSpL5IfsJsJSyqLqxosE/m0CXlZaBmxegQW/dAg==
19982013

2014+
electron-to-chromium@^1.3.413:
2015+
version "1.3.421"
2016+
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.421.tgz#4abfe7e49070b5b437ec2ce442543add8eb66800"
2017+
integrity sha512-ogxgmvHGfDuLA+GtgfK0jkFWlBb4MCZK2U1MM+l98sf4U3Ixtrfw1iC9w4mQqNvo+lHgM4pR62TqoT4QrvKJCw==
2018+
19992019
emoji-regex@^7.0.1:
20002020
version "7.0.3"
20012021
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-7.0.3.tgz#933a04052860c85e83c122479c4748a8e4c72156"

0 commit comments

Comments
 (0)