Skip to content

Commit

Permalink
ensure options for plugins are not stale between builds (#2695)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Oct 28, 2020
1 parent 86a5a1e commit 584316f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions __tests__/processPlugins.test.js
Expand Up @@ -1766,6 +1766,55 @@ test('plugins with extra options can be created using the `createPlugin.withOpti
})
})

test('plugins should cache correctly', () => {
const plugin = createPlugin.withOptions(
({ className = 'banana' } = {}) => ({ addComponents, variants }) => {
addComponents({ [`.${className}`]: { position: 'absolute' } }, variants('testPlugin'))
},
() => ({ variants: { testPlugin: ['responsive'] } })
)

function run(options = {}) {
return _postcss([
tailwind({
corePlugins: [],
theme: { screens: { sm: '400px' } },
plugins: [plugin(options)],
}),
]).process(`@tailwind base; @tailwind components; @tailwind utilities;`, {
from: undefined,
})
}

return Promise.all([run(), run({ className: 'apple' })]).then(([result1, result2]) => {
const expected1 = `
.banana {
position: absolute;
}
@media (min-width: 400px) {
.sm\\:banana {
position: absolute;
}
}
`

const expected2 = `
.apple {
position: absolute;
}
@media (min-width: 400px) {
.sm\\:apple {
position: absolute;
}
}
`
expect(result1.css).toMatchCss(expected1)
expect(result2.css).toMatchCss(expected2)
})
})

test('plugins created using `createPlugin.withOptions` do not need to be invoked if the user wants to use the default options', () => {
const plugin = createPlugin.withOptions(
function ({ className } = { className: 'banana' }) {
Expand Down
1 change: 1 addition & 0 deletions src/util/createPlugin.js
Expand Up @@ -8,6 +8,7 @@ function createPlugin(plugin, config) {
createPlugin.withOptions = function (pluginFunction, configFunction = () => ({})) {
const optionsFunction = function (options) {
return {
__options: options,
handler: pluginFunction(options),
config: configFunction(options),
}
Expand Down

0 comments on commit 584316f

Please sign in to comment.