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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 36 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
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

Lines changed: 13 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/v4/basic/package.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/fixtures/v4/configs/index.html

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)