add `target` plugin helper

master
Brad Cornes 2020-05-03 19:37:37 +01:00
parent 3836cbf2a5
commit 243c9b4011
4 changed files with 37 additions and 4 deletions

View File

@ -18,7 +18,12 @@ const proxyHandler = (base = []) => ({
}, },
}) })
export async function getUtilityConfigMap({ cwd, resolvedConfig, postcss }) { export async function getUtilityConfigMap({
cwd,
resolvedConfig,
postcss,
browserslist,
}) {
const builtInPlugins = await getBuiltInPlugins({ cwd, resolvedConfig }) const builtInPlugins = await getBuiltInPlugins({ cwd, resolvedConfig })
const userPlugins = Array.isArray(resolvedConfig.plugins) const userPlugins = Array.isArray(resolvedConfig.plugins)
? resolvedConfig.plugins ? resolvedConfig.plugins
@ -31,6 +36,7 @@ export async function getUtilityConfigMap({ cwd, resolvedConfig, postcss }) {
;[...builtInPlugins, ...userPlugins].forEach((plugin) => { ;[...builtInPlugins, ...userPlugins].forEach((plugin) => {
runPlugin(plugin, { runPlugin(plugin, {
postcss, postcss,
browserslist,
config: proxiedConfig, config: proxiedConfig,
addUtilities: (utilities) => { addUtilities: (utilities) => {
Object.keys(utilities).forEach((util) => { Object.keys(utilities).forEach((util) => {

View File

@ -1,7 +1,12 @@
import semver from 'semver' import semver from 'semver'
import { runPlugin } from './runPlugin' import { runPlugin } from './runPlugin'
export default function getVariants({ config, version, postcss }) { export default function getVariants({
config,
version,
postcss,
browserslist,
}) {
let variants = ['responsive', 'hover'] let variants = ['responsive', 'hover']
semver.gte(version, '0.3.0') && variants.push('focus', 'group-hover') semver.gte(version, '0.3.0') && variants.push('focus', 'group-hover')
semver.gte(version, '0.5.0') && variants.push('active') semver.gte(version, '0.5.0') && variants.push('active')
@ -16,6 +21,7 @@ export default function getVariants({ config, version, postcss }) {
plugins.forEach((plugin) => { plugins.forEach((plugin) => {
runPlugin(plugin, { runPlugin(plugin, {
postcss, postcss,
browserslist,
config, config,
addVariant: (name) => { addVariant: (name) => {
variants.push(name) variants.push(name)

View File

@ -43,6 +43,7 @@ export default async function getClassNames(
let configPath let configPath
let postcss let postcss
let tailwindcss let tailwindcss
let browserslist
let version let version
configPath = await globSingle(CONFIG_GLOB, { configPath = await globSingle(CONFIG_GLOB, {
@ -58,6 +59,11 @@ export default async function getClassNames(
tailwindcss = importFrom(configDir, 'tailwindcss') tailwindcss = importFrom(configDir, 'tailwindcss')
version = importFrom(configDir, 'tailwindcss/package.json').version version = importFrom(configDir, 'tailwindcss/package.json').version
try {
// this is not required
browserslist = importFrom(configDir, 'browserslist')
} catch (_) {}
const sepLocation = semver.gte(version, '0.99.0') const sepLocation = semver.gte(version, '0.99.0')
? ['separator'] ? ['separator']
: ['options', 'separator'] : ['options', 'separator']
@ -103,11 +109,12 @@ export default async function getClassNames(
classNames: await extractClassNames(ast), classNames: await extractClassNames(ast),
dependencies: hook.deps, dependencies: hook.deps,
plugins: getPlugins(config), plugins: getPlugins(config),
variants: getVariants({ config, version, postcss }), variants: getVariants({ config, version, postcss, browserslist }),
utilityConfigMap: await getUtilityConfigMap({ utilityConfigMap: await getUtilityConfigMap({
cwd: configDir, cwd: configDir,
resolvedConfig, resolvedConfig,
postcss, postcss,
browserslist,
}), }),
} }
} }

View File

@ -1,7 +1,11 @@
import dlv from 'dlv' import dlv from 'dlv'
export function runPlugin(plugin, params = {}) { export function runPlugin(plugin, params = {}) {
const { config, ...rest } = params const { config, browserslist, ...rest } = params
const browserslistTarget =
browserslist && browserslist().includes('ie 11') ? 'ie11' : 'relaxed'
try { try {
;(plugin.handler || plugin)({ ;(plugin.handler || plugin)({
addUtilities: () => {}, addUtilities: () => {},
@ -13,6 +17,16 @@ export function runPlugin(plugin, params = {}) {
theme: (path, defaultValue) => dlv(config, `theme.${path}`, defaultValue), theme: (path, defaultValue) => dlv(config, `theme.${path}`, defaultValue),
variants: () => [], variants: () => [],
config: (path, defaultValue) => dlv(config, path, defaultValue), config: (path, defaultValue) => dlv(config, path, defaultValue),
target: (path) => {
if (typeof config.target === 'string') {
return config.target === 'browserslist'
? browserslistTarget
: config.target
}
const [defaultTarget, targetOverrides] = dlv(config, 'target')
const target = dlv(targetOverrides, path, defaultTarget)
return target === 'browserslist' ? browserslistTarget : target
},
...rest, ...rest,
}) })
} catch (_) {} } catch (_) {}