Skip to content

Commit

Permalink
Don't generate color opacity code in color plugins if not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed May 1, 2020
1 parent 52aab17 commit e6fd316
Show file tree
Hide file tree
Showing 8 changed files with 57,132 additions and 22 deletions.
57,055 changes: 57,055 additions & 0 deletions __tests__/fixtures/tailwind-output-no-color-opacity.css

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions __tests__/sanity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ it('generates the right CSS when using @import instead of @tailwind', () => {
})
})

// TODO: Move to per plugin unit tests for this sort of thing
it('generates the right CSS when color opacity plugins are disabled', () => {
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)
const input = fs.readFileSync(inputPath, 'utf8')

return postcss([
tailwind({
...config,
corePlugins: {
textOpacity: false,
backgroundOpacity: false,
borderOpacity: false,
placeholderOpacity: false,
divideOpacity: false,
},
}),
])
.process(input, { from: inputPath })
.then(result => {
const expected = fs.readFileSync(
path.resolve(`${__dirname}/fixtures/tailwind-output-no-color-opacity.css`),
'utf8'
)

expect(result.css).toBe(expected)
})
})

it('does not add any CSS if no Tailwind features are used', () => {
return postcss([tailwind()])
.process('.foo { color: blue; }', { from: undefined })
Expand Down
13 changes: 13 additions & 0 deletions scripts/rebuildFixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ Promise.all([
to: '__tests__/fixtures/tailwind-output-ie11.css',
config: { target: 'ie11' },
}),
build({
from: '__tests__/fixtures/tailwind-input.css',
to: '__tests__/fixtures/tailwind-output-no-color-opacity.css',
config: {
corePlugins: {
textOpacity: false,
backgroundOpacity: false,
borderOpacity: false,
placeholderOpacity: false,
divideOpacity: false,
},
},
}),
]).then(() => {
console.log('\nFinished rebuilding fixtures.')
console.log(
Expand Down
14 changes: 8 additions & 6 deletions src/plugins/backgroundColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import flattenColorPalette from '../util/flattenColorPalette'
import withAlphaVariable from '../util/withAlphaVariable'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
return function({ addUtilities, e, theme, variants, target, corePlugins }) {
if (target('backgroundColor') === 'ie11') {
const utilities = _.fromPairs(
_.map(flattenColorPalette(theme('backgroundColor')), (value, modifier) => {
Expand All @@ -20,11 +20,13 @@ export default function() {
_.map(flattenColorPalette(theme('backgroundColor')), (value, modifier) => {
return [
`.${e(`bg-${modifier}`)}`,
withAlphaVariable({
color: value,
property: 'background-color',
variable: '--bg-opacity',
}),
corePlugins('backgroundOpacity')
? withAlphaVariable({
color: value,
property: 'background-color',
variable: '--bg-opacity',
})
: { 'background-color': value },
]
})
)
Expand Down
14 changes: 8 additions & 6 deletions src/plugins/borderColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import flattenColorPalette from '../util/flattenColorPalette'
import withAlphaVariable from '../util/withAlphaVariable'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
return function({ addUtilities, e, theme, variants, target, corePlugins }) {
if (target('borderColor') === 'ie11') {
const colors = flattenColorPalette(theme('borderColor'))

Expand All @@ -24,11 +24,13 @@ export default function() {
_.map(_.omit(colors, 'default'), (value, modifier) => {
return [
`.${e(`border-${modifier}`)}`,
withAlphaVariable({
color: value,
property: 'border-color',
variable: '--border-opacity',
}),
corePlugins('borderOpacity')
? withAlphaVariable({
color: value,
property: 'border-color',
variable: '--border-opacity',
})
: { 'border-color': value },
]
})
)
Expand Down
14 changes: 8 additions & 6 deletions src/plugins/divideColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import flattenColorPalette from '../util/flattenColorPalette'
import withAlphaVariable from '../util/withAlphaVariable'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
return function({ addUtilities, e, theme, variants, target, corePlugins }) {
const colors = flattenColorPalette(theme('divideColor'))

if (target('divideColor') === 'ie11') {
Expand All @@ -25,11 +25,13 @@ export default function() {
_.map(_.omit(colors, 'default'), (value, modifier) => {
return [
`.${e(`divide-${modifier}`)} > :not(template) ~ :not(template)`,
withAlphaVariable({
color: value,
property: 'border-color',
variable: '--divide-opacity',
}),
corePlugins('divideOpacity')
? withAlphaVariable({
color: value,
property: 'border-color',
variable: '--divide-opacity',
})
: { 'border-color': value },
]
})
)
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/placeholderColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import flattenColorPalette from '../util/flattenColorPalette'
import withAlphaVariable from '../util/withAlphaVariable'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
return function({ addUtilities, e, theme, variants, target, corePlugins }) {
if (target('placeholderColor') === 'ie11') {
const utilities = _.fromPairs(
_.map(flattenColorPalette(theme('placeholderColor')), (value, modifier) => {
Expand All @@ -20,7 +20,13 @@ export default function() {
_.map(flattenColorPalette(theme('placeholderColor')), (value, modifier) => {
return [
`.${e(`placeholder-${modifier}`)}::placeholder`,
withAlphaVariable({ color: value, property: 'color', variable: '--placeholder-opacity' }),
corePlugins('placeholderOpacity')
? withAlphaVariable({
color: value,
property: 'color',
variable: '--placeholder-opacity',
})
: { color: value },
]
})
)
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/textColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import flattenColorPalette from '../util/flattenColorPalette'
import withAlphaVariable from '../util/withAlphaVariable'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
return function({ addUtilities, e, theme, variants, target, corePlugins }) {
if (target('textColor') === 'ie11') {
const utilities = _.fromPairs(
_.map(flattenColorPalette(theme('textColor')), (value, modifier) => {
Expand All @@ -20,7 +20,9 @@ export default function() {
_.map(flattenColorPalette(theme('textColor')), (value, modifier) => {
return [
`.${e(`text-${modifier}`)}`,
withAlphaVariable({ color: value, property: 'color', variable: '--text-opacity' }),
corePlugins('textOpacity')
? withAlphaVariable({ color: value, property: 'color', variable: '--text-opacity' })
: { color: value },
]
})
)
Expand Down

0 comments on commit e6fd316

Please sign in to comment.