Skip to content

Commit

Permalink
Wrap Tailwind plugins in new plugin to only unwrap config once
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Jun 26, 2018
1 parent 64cda2f commit b21d258
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 62 deletions.
2 changes: 1 addition & 1 deletion __tests__/applyAtRule.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import postcss from 'postcss'
import plugin from '../src/lib/substituteClassApplyAtRules'

function run(input, opts = () => {}) {
function run(input, opts = {}) {
return postcss([plugin(opts)]).process(input, { from: undefined })
}

Expand Down
2 changes: 1 addition & 1 deletion __tests__/configFunction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import postcss from 'postcss'
import plugin from '../src/lib/evaluateTailwindFunctions'

function run(input, opts = {}) {
return postcss([plugin(() => opts)]).process(input, { from: undefined })
return postcss([plugin(opts)]).process(input, { from: undefined })
}

test('it looks up values in the config using dot notation', () => {
Expand Down
30 changes: 15 additions & 15 deletions __tests__/responsiveAtRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import postcss from 'postcss'
import plugin from '../src/lib/substituteResponsiveAtRules'
import config from '../defaultConfig.stub.js'

function run(input, opts = () => config) {
function run(input, opts = config) {
return postcss([plugin(opts)]).process(input, { from: undefined })
}

Expand Down Expand Up @@ -31,7 +31,7 @@ test('it can generate responsive variants', () => {
}
`

return run(input, () => ({
return run(input, {
screens: {
sm: '500px',
md: '750px',
Expand All @@ -40,7 +40,7 @@ test('it can generate responsive variants', () => {
options: {
separator: ':',
},
})).then(result => {
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
Expand Down Expand Up @@ -71,7 +71,7 @@ test('it can generate responsive variants with a custom separator', () => {
}
`

return run(input, () => ({
return run(input, {
screens: {
sm: '500px',
md: '750px',
Expand All @@ -80,7 +80,7 @@ test('it can generate responsive variants with a custom separator', () => {
options: {
separator: '__',
},
})).then(result => {
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
Expand Down Expand Up @@ -117,7 +117,7 @@ test('responsive variants are grouped', () => {
}
`

return run(input, () => ({
return run(input, {
screens: {
sm: '500px',
md: '750px',
Expand All @@ -126,7 +126,7 @@ test('responsive variants are grouped', () => {
options: {
separator: ':',
},
})).then(result => {
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
Expand All @@ -152,7 +152,7 @@ test('screen prefix is only applied to the last class in a selector', () => {
}
`

return run(input, () => ({
return run(input, {
screens: {
sm: '500px',
md: '750px',
Expand All @@ -161,7 +161,7 @@ test('screen prefix is only applied to the last class in a selector', () => {
options: {
separator: ':',
},
})).then(result => {
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
Expand All @@ -187,7 +187,7 @@ test('responsive variants are generated for all selectors in a rule', () => {
}
`

return run(input, () => ({
return run(input, {
screens: {
sm: '500px',
md: '750px',
Expand All @@ -196,7 +196,7 @@ test('responsive variants are generated for all selectors in a rule', () => {
options: {
separator: ':',
},
})).then(result => {
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
Expand All @@ -209,7 +209,7 @@ test('selectors with no classes cannot be made responsive', () => {
}
`
expect.assertions(1)
return run(input, () => ({
return run(input, {
screens: {
sm: '500px',
md: '750px',
Expand All @@ -218,7 +218,7 @@ test('selectors with no classes cannot be made responsive', () => {
options: {
separator: ':',
},
})).catch(e => {
}).catch(e => {
expect(e).toMatchObject({ name: 'CssSyntaxError' })
})
})
Expand All @@ -230,7 +230,7 @@ test('all selectors in a rule must contain classes', () => {
}
`
expect.assertions(1)
return run(input, () => ({
return run(input, {
screens: {
sm: '500px',
md: '750px',
Expand All @@ -239,7 +239,7 @@ test('all selectors in a rule must contain classes', () => {
options: {
separator: ':',
},
})).catch(e => {
}).catch(e => {
expect(e).toMatchObject({ name: 'CssSyntaxError' })
})
})
6 changes: 3 additions & 3 deletions __tests__/variantsAtRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import postcss from 'postcss'
import plugin from '../src/lib/substituteVariantsAtRules'
import config from '../defaultConfig.stub.js'

function run(input, opts = () => config) {
function run(input, opts = config) {
return postcss([plugin(opts)]).process(input, { from: undefined })
}

Expand Down Expand Up @@ -182,7 +182,7 @@ test('plugin variants work', () => {
.first-child\\:chocolate:first-child { color: brown; }
`

return run(input, () => ({
return run(input, {
...config,
plugins: [
...config.plugins,
Expand All @@ -192,7 +192,7 @@ test('plugin variants work', () => {
})
},
],
})).then(result => {
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
Expand Down
40 changes: 14 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ import postcss from 'postcss'
import perfectionist from 'perfectionist'

import registerConfigAsDependency from './lib/registerConfigAsDependency'
import substituteTailwindAtRules from './lib/substituteTailwindAtRules'
import evaluateTailwindFunctions from './lib/evaluateTailwindFunctions'
import substituteVariantsAtRules from './lib/substituteVariantsAtRules'
import substituteResponsiveAtRules from './lib/substituteResponsiveAtRules'
import substituteScreenAtRules from './lib/substituteScreenAtRules'
import substituteClassApplyAtRules from './lib/substituteClassApplyAtRules'

import processTailwindFeatures from './processTailwindFeatures'
import mergeConfigWithDefaults from './util/mergeConfigWithDefaults'


const plugin = postcss.plugin('tailwind', config => {
const plugins = []

Expand All @@ -36,26 +31,19 @@ const plugin = postcss.plugin('tailwind', config => {
)
}

return postcss(
return postcss([
...plugins,
...[
substituteTailwindAtRules(lazyConfig),
evaluateTailwindFunctions(lazyConfig),
substituteVariantsAtRules(lazyConfig),
substituteResponsiveAtRules(lazyConfig),
substituteScreenAtRules(lazyConfig),
substituteClassApplyAtRules(lazyConfig),
perfectionist({
cascade: true,
colorShorthand: true,
indentSize: 2,
maxSelectorLength: 1,
maxValueLength: false,
trimLeadingZero: true,
trimTrailingZeros: true,
}),
]
)
processTailwindFeatures(lazyConfig),
perfectionist({
cascade: true,
colorShorthand: true,
indentSize: 2,
maxSelectorLength: 1,
maxValueLength: false,
trimLeadingZero: true,
trimTrailingZeros: true,
}),
])
})

plugin.defaultConfig = function() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/evaluateTailwindFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function(config) {
return functions({
functions: {
config: (path, defaultValue) => {
return _.get(config(), _.trim(path, `'"`), defaultValue)
return _.get(config, _.trim(path, `'"`), defaultValue)
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions src/lib/substituteResponsiveAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import buildSelectorVariant from '../util/buildSelectorVariant'

export default function(config) {
return function(css) {
const screens = config().screens
const separator = config().options.separator
const screens = config.screens
const separator = config.options.separator
const responsiveRules = []
let finalRules = []

Expand Down
6 changes: 2 additions & 4 deletions src/lib/substituteScreenAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import buildMediaQuery from '../util/buildMediaQuery'

export default function(config) {
return function(css) {
const options = config()

css.walkAtRules('screen', atRule => {
const screen = atRule.params

if (!_.has(options.screens, screen)) {
if (!_.has(config.screens, screen)) {
throw atRule.error(`No \`${screen}\` screen found.`)
}

atRule.name = 'media'
atRule.params = buildMediaQuery(options.screens[screen])
atRule.params = buildMediaQuery(config.screens[screen])
})
}
}
10 changes: 4 additions & 6 deletions src/lib/substituteTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import processPlugins from '../util/processPlugins'

export default function(config) {
return function(css) {
const unwrappedConfig = config()

const { components: pluginComponents, utilities: pluginUtilities } = processPlugins(
unwrappedConfig
config
)

css.walkAtRules('tailwind', atRule => {
Expand All @@ -37,9 +35,9 @@ export default function(config) {
}

if (atRule.params === 'utilities') {
const utilities = generateModules(utilityModules, unwrappedConfig.modules, unwrappedConfig)
const utilities = generateModules(utilityModules, config.modules, config)

if (unwrappedConfig.options.important) {
if (config.options.important) {
utilities.walkDecls(decl => (decl.important = true))
}

Expand All @@ -51,7 +49,7 @@ export default function(config) {
nodes: pluginUtilities,
})

prefixTree(tailwindUtilityTree, unwrappedConfig.options.prefix)
prefixTree(tailwindUtilityTree, config.options.prefix)

tailwindUtilityTree.walk(node => (node.source = atRule.source))
pluginUtilityTree.walk(node => (node.source = atRule.source))
Expand Down
5 changes: 2 additions & 3 deletions src/lib/substituteVariantsAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ const defaultVariantGenerators = {

export default function(config) {
return function(css) {
const unwrappedConfig = config()
const variantGenerators = {
...defaultVariantGenerators,
...processPlugins(unwrappedConfig).variantGenerators,
...processPlugins(config).variantGenerators,
}

css.walkAtRules('variants', atRule => {
Expand All @@ -49,7 +48,7 @@ export default function(config) {
atRule.before(atRule.clone().nodes)

_.forEach(_.without(variants, 'responsive'), variant => {
variantGenerators[variant](atRule, unwrappedConfig)
variantGenerators[variant](atRule, config)
})

atRule.remove()
Expand Down
25 changes: 25 additions & 0 deletions src/processTailwindFeatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import path from 'path'

import _ from 'lodash'
import postcss from 'postcss'

import registerConfigAsDependency from './lib/registerConfigAsDependency'
import substituteTailwindAtRules from './lib/substituteTailwindAtRules'
import evaluateTailwindFunctions from './lib/evaluateTailwindFunctions'
import substituteVariantsAtRules from './lib/substituteVariantsAtRules'
import substituteResponsiveAtRules from './lib/substituteResponsiveAtRules'
import substituteScreenAtRules from './lib/substituteScreenAtRules'
import substituteClassApplyAtRules from './lib/substituteClassApplyAtRules'

export default function(lazyConfig) {
const config = lazyConfig()

return postcss([
substituteTailwindAtRules(config),
evaluateTailwindFunctions(config),
substituteVariantsAtRules(config),
substituteResponsiveAtRules(config),
substituteScreenAtRules(config),
substituteClassApplyAtRules(config),
])
}

0 comments on commit b21d258

Please sign in to comment.