Skip to content

Commit

Permalink
Add .mts and .cts config file detection (#13940)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RobinMalfait authored Jul 3, 2024
1 parent 0de1f0c commit 588a822
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

## [3.4.4] - 2024-06-05

Expand Down
89 changes: 46 additions & 43 deletions integrations/tailwindcss-cli/tests/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,51 +134,54 @@ describe('static build', () => {
)
})

it('can use a tailwind.config.ts configuration file', async () => {
await removeFile('tailwind.config.js')
await writeInputFile('index.html', html`<div class="bg-primary"></div>`)
await writeInputFile(
'index.css',
css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`
)
await writeInputFile(
'../tailwind.config.ts',
javascript`
import type { Config } from 'tailwindcss'
export default {
content: ['./src/index.html'],
theme: {
extend: {
colors: {
primary: 'black',
it.each([['../tailwind.config.ts'], ['../tailwind.config.cts'], ['../tailwind.config.mts']])(
'can use a %s configuration file',
async (path) => {
await removeFile('tailwind.config.js')
await writeInputFile('index.html', html`<div class="bg-primary"></div>`)
await writeInputFile(
'index.css',
css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`
)
await writeInputFile(
path,
javascript`
import type { Config } from 'tailwindcss'
export default {
content: ['./src/index.html'],
theme: {
extend: {
colors: {
primary: 'black',
},
},
},
},
corePlugins: {
preflight: false,
},
} satisfies Config
`
)

await $('node ../../lib/cli.js -i ./src/index.css -o ./dist/main.css', {
env: { NODE_ENV: 'production' },
})

expect(await readOutputFile('main.css')).toIncludeCss(
css`
.bg-primary {
--tw-bg-opacity: 1;
background-color: rgb(0 0 0 / var(--tw-bg-opacity));
}
`
)
})
corePlugins: {
preflight: false,
},
} satisfies Config
`
)

await $('node ../../lib/cli.js -i ./src/index.css -o ./dist/main.css', {
env: { NODE_ENV: 'production' },
})

expect(await readOutputFile('main.css')).toIncludeCss(
css`
.bg-primary {
--tw-bg-opacity: 1;
background-color: rgb(0 0 0 / var(--tw-bg-opacity));
}
`
)
}
)

it('can read from a config file from an @config directive', async () => {
await writeInputFile('index.html', html`<div class="bg-yellow"></div>`)
Expand Down
11 changes: 11 additions & 0 deletions src/lib/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ function lazyJiti() {

export function loadConfig(path: string): Config {
let config = (function () {
// Always use jiti for ESM or TS files
if (
path &&
(path.endsWith('.mjs') ||
path.endsWith('.ts') ||
path.endsWith('.cts') ||
path.endsWith('.mts'))
) {
return lazyJiti()(path)
}

try {
return path ? require(path) : {}
} catch {
Expand Down
2 changes: 2 additions & 0 deletions src/util/resolveConfigPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const defaultConfigFiles = [
'./tailwind.config.cjs',
'./tailwind.config.mjs',
'./tailwind.config.ts',
'./tailwind.config.cts',
'./tailwind.config.mts',
]

function isObject(value) {
Expand Down

0 comments on commit 588a822

Please sign in to comment.