Co-locate feature flag utilities

This commit is contained in:
Adam Wathan 2020-08-06 16:02:09 -04:00
parent a7c410656d
commit 61635f4884
3 changed files with 74 additions and 57 deletions

71
src/featureFlags.js Normal file
View file

@ -0,0 +1,71 @@
import _ from 'lodash'
import chalk from 'chalk'
const featureFlags = {
future: [],
experimental: ['uniformColorPalette'],
}
export function flagEnabled(config, flag) {
if (featureFlags.future.includes(flag)) {
return _.get(config, ['future', flag], false)
}
if (featureFlags.experimental.includes(flag)) {
return _.get(config, ['experimental', flag], false)
}
return false
}
export function issueFlagNotices(config) {
const log = {
info(message) {
console.log(chalk.bold.cyan(' info'), '-', message)
},
warn(message) {
console.log(chalk.bold.yellow(' warn'), '-', message)
},
risk(message) {
console.log(chalk.bold.magenta(' risk'), '-', message)
},
}
if (_.some(featureFlags.future, flag => _.get(config, ['future', flag], false))) {
const changes = Object.keys(config.future)
.filter(flag => featureFlags.future.includes(flag) && config.future[flag])
.map(s => chalk.cyan(s))
.join(', ')
console.log()
log.info(`You have opted-in to future-facing breaking changes: ${changes}`)
log.info(
'These changes are stable and will be the default behavior in the next major version of Tailwind.'
)
}
if (_.some(featureFlags.experimental, flag => _.get(config, ['experimental', flag], false))) {
const changes = Object.keys(config.experimental)
.filter(flag => featureFlags.experimental.includes(flag) && config.experimental[flag])
.map(s => chalk.yellow(s))
.join(', ')
console.log()
log.warn(`You have enabled experimental features: ${changes}`)
log.warn(
'Experimental features are not covered by semver, may introduce breaking changes, and can change at any time.'
)
}
if (Object.keys(_.get(config, 'future', {})).length < featureFlags.future.length) {
const changes = featureFlags.future
.filter(flag => !_.has(config, ['future', flag]))
.map(s => chalk.magenta(s))
.join(', ')
console.log()
log.risk(`There are upcoming breaking changes: ${changes}`)
log.risk(
'We highly recommend opting-in to these changes now to simplify upgrading Tailwind in the future.'
)
}
}
export default featureFlags

View file

@ -11,13 +11,14 @@ import formatCSS from './lib/formatCSS'
import resolveConfig from './util/resolveConfig'
import { defaultConfigFile } from './constants'
import defaultConfig from '../stubs/defaultConfig.stub.js'
import { flagEnabled } from './featureFlags'
import uniformColorPalette from './flagged/uniformColorPalette.js'
function getDefaultConfigs(config) {
const configs = [defaultConfig]
if (_.get(config, ['experimental', 'uniformColorPalette'], false)) {
if (flagEnabled(config, 'uniformColorPalette')) {
configs.unshift(uniformColorPalette)
}

View file

@ -13,62 +13,7 @@ import purgeUnusedStyles from './lib/purgeUnusedStyles'
import corePlugins from './corePlugins'
import processPlugins from './util/processPlugins'
import cloneNodes from './util/cloneNodes'
import chalk from 'chalk'
const log = {
info(message) {
console.log(chalk.bold.cyan(' info'), '-', message)
},
warn(message) {
console.log(chalk.bold.yellow(' warn'), '-', message)
},
risk(message) {
console.log(chalk.bold.magenta(' risk'), '-', message)
},
}
function issueFlagNotices(config) {
const featureFlags = {
future: [],
experimental: ['uniformColorPalette'],
}
if (_.some(featureFlags.future, flag => _.get(config, ['future', flag], false))) {
const changes = Object.keys(config.future)
.filter(flag => featureFlags.future.includes(flag) && config.future[flag])
.map(s => chalk.cyan(s))
.join(', ')
console.log()
log.info(`You have opted-in to future-facing breaking changes: ${changes}`)
log.info(
'These changes are stable and will be the default behavior in the next major version of Tailwind.'
)
}
if (_.some(featureFlags.experimental, flag => _.get(config, ['experimental', flag], false))) {
const changes = Object.keys(config.experimental)
.filter(flag => featureFlags.experimental.includes(flag) && config.experimental[flag])
.map(s => chalk.yellow(s))
.join(', ')
console.log()
log.warn(`You have enabled experimental features: ${changes}`)
log.warn(
'Experimental features are not covered by semver, may introduce breaking changes, and can change at any time.'
)
}
if (Object.keys(_.get(config, 'future', {})).length < featureFlags.future.length) {
const changes = featureFlags.future
.filter(flag => !_.has(config, ['future', flag]))
.map(s => chalk.magenta(s))
.join(', ')
console.log()
log.risk(`There are upcoming breaking changes: ${changes}`)
log.risk(
'We highly recommend opting-in to these changes now to simplify upgrading Tailwind in the future.'
)
}
}
import { issueFlagNotices } from './featureFlags.js'
export default function(getConfig) {
return function(css) {