Skip to content

Commit 2530335

Browse files
Support loading TypeScript configs in v4 (#342)
* Load TypeScript configs with Jiti * Update tests * Update changelog * Update versions
1 parent 6d3fa07 commit 2530335

23 files changed

+199
-61
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10-
- Nothing yet!
10+
- Support TypeScript configs and plugins when using v4 ([#342](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/342))
1111

1212
## [0.6.10] - 2025-01-15
1313

package-lock.json

+36-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@prettier/plugin-pug": "^3.0",
4040
"@shopify/prettier-plugin-liquid": "^1.4.0",
4141
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
42+
"@types/node": "^22.10.9",
4243
"@zackad/prettier-plugin-twig": "^0.14.1",
4344
"ast-types": "^0.14.2",
4445
"clear-module": "^4.1.2",
@@ -47,6 +48,7 @@
4748
"esbuild": "^0.19.8",
4849
"escalade": "^3.1.1",
4950
"import-sort-style-module": "^6.0.0",
51+
"jiti": "^2.4.2",
5052
"jsesc": "^2.5.2",
5153
"license-checker": "^25.0.1",
5254
"line-column": "^1.0.2",

src/config.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from 'path'
44
import { pathToFileURL } from 'url'
55
import clearModule from 'clear-module'
66
import escalade from 'escalade/sync'
7+
import { createJiti, Jiti } from 'jiti'
78
import postcss from 'postcss'
89
// @ts-ignore
910
import postcssImport from 'postcss-import'
@@ -156,10 +157,12 @@ async function loadTailwindConfig(
156157
*/
157158
function createLoader<T>({
158159
legacy,
160+
jiti,
159161
filepath,
160162
onError,
161163
}: {
162164
legacy: boolean
165+
jiti: Jiti
163166
filepath: string
164167
onError: (id: string, error: unknown, resourceType: string) => T
165168
}) {
@@ -172,7 +175,7 @@ function createLoader<T>({
172175
let url = pathToFileURL(resolved)
173176
url.searchParams.append('t', cacheKey)
174177

175-
return await import(url.href).then((m) => m.default ?? m)
178+
return await jiti.import(url.href, { default: true })
176179
} catch (err) {
177180
return onError(id, err, resourceType)
178181
}
@@ -209,6 +212,12 @@ async function loadV4(
209212
// If the user doesn't define an entrypoint then we use the default theme
210213
entryPoint = entryPoint ?? `${pkgDir}/theme.css`
211214

215+
// Create a Jiti instance that can be used to load plugins and config files
216+
let jiti = createJiti(import.meta.url, {
217+
moduleCache: false,
218+
fsCache: false,
219+
})
220+
212221
let importBasePath = path.dirname(entryPoint)
213222

214223
// Resolve imports in the entrypoint to a flat CSS tree
@@ -242,6 +251,7 @@ async function loadV4(
242251
// v4.0.0-alpha.25+
243252
loadModule: createLoader({
244253
legacy: false,
254+
jiti,
245255
filepath: entryPoint,
246256
onError: (id, err, resourceType) => {
247257
console.error(`Unable to load ${resourceType}: ${id}`, err)
@@ -266,6 +276,7 @@ async function loadV4(
266276
// v4.0.0-alpha.24 and below
267277
loadPlugin: createLoader({
268278
legacy: true,
279+
jiti,
269280
filepath: entryPoint,
270281
onError(id, err) {
271282
console.error(`Unable to load plugin: ${id}`, err)
@@ -276,6 +287,7 @@ async function loadV4(
276287

277288
loadConfig: createLoader({
278289
legacy: true,
290+
jiti,
279291
filepath: entryPoint,
280292
onError(id, err) {
281293
console.error(`Unable to load config: ${id}`, err)

tests/fixtures.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ let fixtures = [
7070
},
7171
{
7272
name: 'v4: configs and plugins',
73-
dir: 'v4/configs',
73+
dir: 'v4/css-loading-js',
7474
ext: 'html',
7575
},
7676
]

tests/fixtures/v4/basic/package-lock.json

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/v4/basic/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"dependencies": {
3-
"tailwindcss": "^4.0.0-alpha.34"
3+
"tailwindcss": "^4.0.0"
44
},
55
"prettier": {
66
"tailwindStylesheet": "./app.css"

tests/fixtures/v4/configs/app.css

-8
This file was deleted.

tests/fixtures/v4/configs/index.html

-3
This file was deleted.

tests/fixtures/v4/configs/my-plugin.mjs

-13
This file was deleted.

tests/fixtures/v4/configs/output.html

-3
This file was deleted.

tests/fixtures/v4/configs/package-lock.json

-17
This file was deleted.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@import 'tailwindcss';
2+
3+
/* Load ESM versions */
4+
@config './esm/my-config.mjs';
5+
@plugin './esm/my-plugin.mjs';
6+
7+
/* Load Common JS versions */
8+
@config './cjs/my-config.cjs';
9+
@plugin './cjs/my-plugin.cjs';
10+
11+
/* Load TypeScript versions */
12+
@config './ts/my-config.ts';
13+
@plugin './ts/my-plugin.ts';
14+
15+
/* Attempt to load files that do not exist */
16+
@config './missing-confg.mjs';
17+
@plugin './missing-plugin.mjs';
18+
19+
@theme {
20+
--color-tomato: tomato;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
theme: {
3+
extend: {
4+
colors: {
5+
'cjs-from-config': 'black',
6+
},
7+
},
8+
},
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const plugin = require('tailwindcss/plugin')
2+
3+
module.exports = plugin(
4+
({ addUtilities }) => {
5+
addUtilities({
6+
'.utility-cjs-from-plugin': {
7+
color: 'black'
8+
},
9+
'.utility-cjs-from-plugin-2': {
10+
width: '100%',
11+
height: '100%',
12+
},
13+
})
14+
},
15+
{
16+
theme: {
17+
extend: {
18+
colors: {
19+
'cjs-from-plugin': 'black',
20+
},
21+
},
22+
},
23+
},
24+
)

tests/fixtures/v4/configs/tailwind.config.mjs tests/fixtures/v4/css-loading-js/esm/my-config.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export default {
22
theme: {
33
extend: {
44
colors: {
5-
"from-config": "#3490dc",
5+
'esm-from-config': 'black',
66
},
77
},
88
},
9-
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import plugin from 'tailwindcss/plugin'
2+
3+
export default plugin(
4+
({ addUtilities }) => {
5+
addUtilities({
6+
'.utility-esm-from-plugin': {
7+
color: 'black'
8+
},
9+
'.utility-esm-from-plugin-2': {
10+
width: '100%',
11+
height: '100%',
12+
},
13+
})
14+
},
15+
{
16+
theme: {
17+
extend: {
18+
colors: {
19+
'esm-from-plugin': 'black',
20+
},
21+
},
22+
},
23+
},
24+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div
2+
class="sm:bg-tomato sm:utility-cjs-from-plugin sm:utility-cjs-from-plugin-2 sm:utility-esm-from-plugin sm:utility-esm-from-plugin-2 sm:utility-ts-from-plugin sm:utility-ts-from-plugin-2 sm:bg-cjs-from-config sm:bg-cjs-from-plugin sm:bg-esm-from-config sm:bg-esm-from-plugin sm:bg-ts-from-config sm:bg-ts-from-plugin bg-red-500 utility-cjs-from-plugin utility-esm-from-plugin utility-ts-from-plugin"
3+
></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div
2+
class="bg-red-500 utility-cjs-from-plugin utility-esm-from-plugin utility-ts-from-plugin sm:utility-cjs-from-plugin-2 sm:utility-esm-from-plugin-2 sm:utility-ts-from-plugin-2 sm:bg-cjs-from-config sm:bg-cjs-from-plugin sm:bg-esm-from-config sm:bg-esm-from-plugin sm:bg-tomato sm:bg-ts-from-config sm:bg-ts-from-plugin sm:utility-cjs-from-plugin sm:utility-esm-from-plugin sm:utility-ts-from-plugin"
3+
></div>

tests/fixtures/v4/css-loading-js/package-lock.json

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)