Skip to content

Commit 02eb6a6

Browse files
authored
Fix issue where dark mode experiment won't work if user has plugins array in config (#2322)
1 parent 8e49e48 commit 02eb6a6

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

__tests__/darkMode.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ test('generating dark mode variants uses the media strategy by default', () => {
4949
})
5050
})
5151

52+
test('dark mode variants can be generated even when the user has their own plugins array', () => {
53+
const input = `
54+
@variants dark {
55+
.text-red {
56+
color: red;
57+
}
58+
}
59+
`
60+
61+
const expected = `
62+
.text-red {
63+
color: red;
64+
}
65+
@media (prefers-color-scheme: dark) {
66+
.dark\\:text-red {
67+
color: red;
68+
}
69+
}
70+
`
71+
72+
expect.assertions(2)
73+
74+
return run(input, { plugins: [] }).then(result => {
75+
expect(result.css).toMatchCss(expected)
76+
expect(result.warnings().length).toBe(0)
77+
})
78+
})
79+
5280
test('dark mode variants can be generated using the class strategy', () => {
5381
const input = `
5482
@variants dark {

src/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import uniformColorPalette from './flagged/uniformColorPalette.js'
1717
import extendedSpacingScale from './flagged/extendedSpacingScale.js'
1818
import defaultLineHeights from './flagged/defaultLineHeights.js'
1919
import extendedFontSizeScale from './flagged/extendedFontSizeScale.js'
20-
import darkModeVariant from './flagged/darkModeVariant'
20+
import darkModeVariant from './flagged/darkModeVariant.js'
2121

22-
function getDefaultConfigs(config) {
22+
function getAllConfigs(config) {
2323
const configs = [defaultConfig]
2424

2525
if (flagEnabled(config, 'uniformColorPalette')) {
@@ -40,9 +40,12 @@ function getDefaultConfigs(config) {
4040

4141
if (flagEnabled(config, 'darkModeVariant')) {
4242
configs.unshift(darkModeVariant)
43+
if (Array.isArray(config.plugins)) {
44+
config.plugins = [...darkModeVariant.plugins, ...config.plugins]
45+
}
4346
}
4447

45-
return configs
48+
return [config, ...configs]
4649
}
4750

4851
function resolveConfigPath(filePath) {
@@ -78,7 +81,7 @@ function resolveConfigPath(filePath) {
7881

7982
const getConfigFunction = config => () => {
8083
if (_.isUndefined(config) && !_.isObject(config)) {
81-
return resolveConfig([...getDefaultConfigs(defaultConfig)])
84+
return resolveConfig([...getAllConfigs(defaultConfig)])
8285
}
8386

8487
// Skip this if Jest is running: https://github.com/facebook/jest/pull/9841#issuecomment-621417584
@@ -92,7 +95,7 @@ const getConfigFunction = config => () => {
9295

9396
const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
9497

95-
return resolveConfig([configObject, ...getDefaultConfigs(configObject)])
98+
return resolveConfig([...getAllConfigs(configObject)])
9699
}
97100

98101
const plugin = postcss.plugin('tailwind', config => {

0 commit comments

Comments
 (0)