Skip to content

Commit 588a822

Browse files
authored
Add .mts and .cts config file detection (#13940)
* add `tailwind.config.cts` and `tailwind.config.mts` as default config files * always use jiti when working with ESM or TS files * update changelog * add integration test for `.cts` and `.mts` configuration files
1 parent 0de1f0c commit 588a822

File tree

4 files changed

+60
-43
lines changed

4 files changed

+60
-43
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Disable automatic `var()` injection for anchor properties ([#13826](https://github.com/tailwindlabs/tailwindcss/pull/13826))
1313
- Use no value instead of `blur(0px)` for `backdrop-blur-none` and `blur-none` utilities ([#13830](https://github.com/tailwindlabs/tailwindcss/pull/13830))
14+
- Add `.mts` and `.cts` config file detection ([#13940](https://github.com/tailwindlabs/tailwindcss/pull/13940))
1415

1516
## [3.4.4] - 2024-06-05
1617

integrations/tailwindcss-cli/tests/integration.test.js

+46-43
Original file line numberDiff line numberDiff line change
@@ -134,51 +134,54 @@ describe('static build', () => {
134134
)
135135
})
136136

137-
it('can use a tailwind.config.ts configuration file', async () => {
138-
await removeFile('tailwind.config.js')
139-
await writeInputFile('index.html', html`<div class="bg-primary"></div>`)
140-
await writeInputFile(
141-
'index.css',
142-
css`
143-
@tailwind base;
144-
@tailwind components;
145-
@tailwind utilities;
146-
`
147-
)
148-
await writeInputFile(
149-
'../tailwind.config.ts',
150-
javascript`
151-
import type { Config } from 'tailwindcss'
152-
153-
export default {
154-
content: ['./src/index.html'],
155-
theme: {
156-
extend: {
157-
colors: {
158-
primary: 'black',
137+
it.each([['../tailwind.config.ts'], ['../tailwind.config.cts'], ['../tailwind.config.mts']])(
138+
'can use a %s configuration file',
139+
async (path) => {
140+
await removeFile('tailwind.config.js')
141+
await writeInputFile('index.html', html`<div class="bg-primary"></div>`)
142+
await writeInputFile(
143+
'index.css',
144+
css`
145+
@tailwind base;
146+
@tailwind components;
147+
@tailwind utilities;
148+
`
149+
)
150+
await writeInputFile(
151+
path,
152+
javascript`
153+
import type { Config } from 'tailwindcss'
154+
155+
export default {
156+
content: ['./src/index.html'],
157+
theme: {
158+
extend: {
159+
colors: {
160+
primary: 'black',
161+
},
159162
},
160163
},
161-
},
162-
corePlugins: {
163-
preflight: false,
164-
},
165-
} satisfies Config
166-
`
167-
)
168-
169-
await $('node ../../lib/cli.js -i ./src/index.css -o ./dist/main.css', {
170-
env: { NODE_ENV: 'production' },
171-
})
172-
173-
expect(await readOutputFile('main.css')).toIncludeCss(
174-
css`
175-
.bg-primary {
176-
--tw-bg-opacity: 1;
177-
background-color: rgb(0 0 0 / var(--tw-bg-opacity));
178-
}
179-
`
180-
)
181-
})
164+
corePlugins: {
165+
preflight: false,
166+
},
167+
} satisfies Config
168+
`
169+
)
170+
171+
await $('node ../../lib/cli.js -i ./src/index.css -o ./dist/main.css', {
172+
env: { NODE_ENV: 'production' },
173+
})
174+
175+
expect(await readOutputFile('main.css')).toIncludeCss(
176+
css`
177+
.bg-primary {
178+
--tw-bg-opacity: 1;
179+
background-color: rgb(0 0 0 / var(--tw-bg-opacity));
180+
}
181+
`
182+
)
183+
}
184+
)
182185

183186
it('can read from a config file from an @config directive', async () => {
184187
await writeInputFile('index.html', html`<div class="bg-yellow"></div>`)

src/lib/load-config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ function lazyJiti() {
3333

3434
export function loadConfig(path: string): Config {
3535
let config = (function () {
36+
// Always use jiti for ESM or TS files
37+
if (
38+
path &&
39+
(path.endsWith('.mjs') ||
40+
path.endsWith('.ts') ||
41+
path.endsWith('.cts') ||
42+
path.endsWith('.mts'))
43+
) {
44+
return lazyJiti()(path)
45+
}
46+
3647
try {
3748
return path ? require(path) : {}
3849
} catch {

src/util/resolveConfigPath.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const defaultConfigFiles = [
66
'./tailwind.config.cjs',
77
'./tailwind.config.mjs',
88
'./tailwind.config.ts',
9+
'./tailwind.config.cts',
10+
'./tailwind.config.mts',
911
]
1012

1113
function isObject(value) {

0 commit comments

Comments
 (0)