mirror of
https://github.com/tailwindlabs/tailwindcss.git
synced 2025-12-08 21:36:08 +00:00
Allow plugins to register new config variables
This commit is contained in:
parent
8b1b797b9f
commit
5ea8bbe798
@ -1,8 +1,15 @@
|
||||
import _ from 'lodash'
|
||||
import postcss from 'postcss'
|
||||
import config from '../defaultConfig.stub.js'
|
||||
import plugin from '../src/lib/evaluateTailwindFunctions'
|
||||
import processPlugins from '../src/util/processPlugins'
|
||||
|
||||
function run(input, opts = {}) {
|
||||
return postcss([plugin(opts)]).process(input, { from: undefined })
|
||||
function run(input, opts = config) {
|
||||
return postcss([
|
||||
plugin(opts, processPlugins(_.get(opts, 'plugins', []), opts).configValues),
|
||||
]).process(input, {
|
||||
from: undefined,
|
||||
})
|
||||
}
|
||||
|
||||
test('it looks up values in the config using dot notation', () => {
|
||||
@ -80,3 +87,48 @@ test('quotes are preserved around default values', () => {
|
||||
expect(result.warnings().length).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
test('plugins can register values that should be available to the config function', () => {
|
||||
const input = `
|
||||
.banana { color: config('banana.sandwich'); }
|
||||
`
|
||||
|
||||
const output = `
|
||||
.banana { color: blue; }
|
||||
`
|
||||
|
||||
return run(input, {
|
||||
plugins: [
|
||||
function({ addConfig }) {
|
||||
addConfig('banana', {
|
||||
sandwich: 'blue',
|
||||
})
|
||||
},
|
||||
],
|
||||
}).then(result => {
|
||||
expect(result.css).toMatchCss(output)
|
||||
expect(result.warnings().length).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
test('plugin config values do not override first-class config values', () => {
|
||||
const input = `
|
||||
.banana { color: config('separator'); }
|
||||
`
|
||||
|
||||
const output = `
|
||||
.banana { color: _; }
|
||||
`
|
||||
|
||||
return run(input, {
|
||||
separator: '_',
|
||||
plugins: [
|
||||
function({ addConfig }) {
|
||||
addConfig('separator', '+')
|
||||
},
|
||||
],
|
||||
}).then(result => {
|
||||
expect(result.css).toMatchCss(output)
|
||||
expect(result.warnings().length).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,11 +1,12 @@
|
||||
import _ from 'lodash'
|
||||
import functions from 'postcss-functions'
|
||||
|
||||
export default function(config) {
|
||||
export default function(config, pluginConfigValues) {
|
||||
return functions({
|
||||
functions: {
|
||||
config: (path, defaultValue) => {
|
||||
return _.get(config, _.trim(path, `'"`), defaultValue)
|
||||
const trimmedPath = _.trim(path, `'"`)
|
||||
return _.get(config, trimmedPath, _.get(pluginConfigValues, trimmedPath, defaultValue))
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@ -18,7 +18,7 @@ export default function(getConfig) {
|
||||
|
||||
return postcss([
|
||||
substituteTailwindAtRules(config, processedPlugins),
|
||||
evaluateTailwindFunctions(config),
|
||||
evaluateTailwindFunctions(config, processedPlugins.configValues),
|
||||
substituteVariantsAtRules(config, processedPlugins),
|
||||
substituteResponsiveAtRules(config),
|
||||
substituteScreenAtRules(config),
|
||||
|
||||
@ -19,6 +19,7 @@ export default function(plugins, config) {
|
||||
const pluginComponents = []
|
||||
const pluginUtilities = []
|
||||
const pluginVariantGenerators = {}
|
||||
const pluginConfigValues = {}
|
||||
|
||||
plugins.forEach(plugin => {
|
||||
plugin({
|
||||
@ -64,6 +65,9 @@ export default function(plugins, config) {
|
||||
addVariant: (name, generator) => {
|
||||
pluginVariantGenerators[name] = generateVariantFunction(generator)
|
||||
},
|
||||
addConfig: (namespace, value) => {
|
||||
pluginConfigValues[namespace] = value
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@ -71,5 +75,6 @@ export default function(plugins, config) {
|
||||
components: pluginComponents,
|
||||
utilities: pluginUtilities,
|
||||
variantGenerators: pluginVariantGenerators,
|
||||
configValues: pluginConfigValues,
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user