Skip to content

Commit

Permalink
Support loading TypeScript configs in v4 (#342)
Browse files Browse the repository at this point in the history
* Load TypeScript configs with Jiti

* Update tests

* Update changelog

* Update versions
  • Loading branch information
thecrypticace authored Jan 23, 2025
1 parent 6d3fa07 commit 2530335
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 61 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

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

## [0.6.10] - 2025-01-15

Expand Down
42 changes: 36 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@prettier/plugin-pug": "^3.0",
"@shopify/prettier-plugin-liquid": "^1.4.0",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/node": "^22.10.9",
"@zackad/prettier-plugin-twig": "^0.14.1",
"ast-types": "^0.14.2",
"clear-module": "^4.1.2",
Expand All @@ -47,6 +48,7 @@
"esbuild": "^0.19.8",
"escalade": "^3.1.1",
"import-sort-style-module": "^6.0.0",
"jiti": "^2.4.2",
"jsesc": "^2.5.2",
"license-checker": "^25.0.1",
"line-column": "^1.0.2",
Expand Down
14 changes: 13 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
import { pathToFileURL } from 'url'
import clearModule from 'clear-module'
import escalade from 'escalade/sync'
import { createJiti, Jiti } from 'jiti'
import postcss from 'postcss'
// @ts-ignore
import postcssImport from 'postcss-import'
Expand Down Expand Up @@ -156,10 +157,12 @@ async function loadTailwindConfig(
*/
function createLoader<T>({
legacy,
jiti,
filepath,
onError,
}: {
legacy: boolean
jiti: Jiti
filepath: string
onError: (id: string, error: unknown, resourceType: string) => T
}) {
Expand All @@ -172,7 +175,7 @@ function createLoader<T>({
let url = pathToFileURL(resolved)
url.searchParams.append('t', cacheKey)

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

// Create a Jiti instance that can be used to load plugins and config files
let jiti = createJiti(import.meta.url, {
moduleCache: false,
fsCache: false,
})

let importBasePath = path.dirname(entryPoint)

// Resolve imports in the entrypoint to a flat CSS tree
Expand Down Expand Up @@ -242,6 +251,7 @@ async function loadV4(
// v4.0.0-alpha.25+
loadModule: createLoader({
legacy: false,
jiti,
filepath: entryPoint,
onError: (id, err, resourceType) => {
console.error(`Unable to load ${resourceType}: ${id}`, err)
Expand All @@ -266,6 +276,7 @@ async function loadV4(
// v4.0.0-alpha.24 and below
loadPlugin: createLoader({
legacy: true,
jiti,
filepath: entryPoint,
onError(id, err) {
console.error(`Unable to load plugin: ${id}`, err)
Expand All @@ -276,6 +287,7 @@ async function loadV4(

loadConfig: createLoader({
legacy: true,
jiti,
filepath: entryPoint,
onError(id, err) {
console.error(`Unable to load config: ${id}`, err)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ let fixtures = [
},
{
name: 'v4: configs and plugins',
dir: 'v4/configs',
dir: 'v4/css-loading-js',
ext: 'html',
},
]
Expand Down
9 changes: 5 additions & 4 deletions tests/fixtures/v4/basic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/fixtures/v4/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"tailwindcss": "^4.0.0-alpha.34"
"tailwindcss": "^4.0.0"
},
"prettier": {
"tailwindStylesheet": "./app.css"
Expand Down
8 changes: 0 additions & 8 deletions tests/fixtures/v4/configs/app.css

This file was deleted.

3 changes: 0 additions & 3 deletions tests/fixtures/v4/configs/index.html

This file was deleted.

13 changes: 0 additions & 13 deletions tests/fixtures/v4/configs/my-plugin.mjs

This file was deleted.

3 changes: 0 additions & 3 deletions tests/fixtures/v4/configs/output.html

This file was deleted.

17 changes: 0 additions & 17 deletions tests/fixtures/v4/configs/package-lock.json

This file was deleted.

21 changes: 21 additions & 0 deletions tests/fixtures/v4/css-loading-js/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@import 'tailwindcss';

/* Load ESM versions */
@config './esm/my-config.mjs';
@plugin './esm/my-plugin.mjs';

/* Load Common JS versions */
@config './cjs/my-config.cjs';
@plugin './cjs/my-plugin.cjs';

/* Load TypeScript versions */
@config './ts/my-config.ts';
@plugin './ts/my-plugin.ts';

/* Attempt to load files that do not exist */
@config './missing-confg.mjs';
@plugin './missing-plugin.mjs';

@theme {
--color-tomato: tomato;
}
9 changes: 9 additions & 0 deletions tests/fixtures/v4/css-loading-js/cjs/my-config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
theme: {
extend: {
colors: {
'cjs-from-config': 'black',
},
},
},
}
24 changes: 24 additions & 0 deletions tests/fixtures/v4/css-loading-js/cjs/my-plugin.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const plugin = require('tailwindcss/plugin')

module.exports = plugin(
({ addUtilities }) => {
addUtilities({
'.utility-cjs-from-plugin': {
color: 'black'
},
'.utility-cjs-from-plugin-2': {
width: '100%',
height: '100%',
},
})
},
{
theme: {
extend: {
colors: {
'cjs-from-plugin': 'black',
},
},
},
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export default {
theme: {
extend: {
colors: {
"from-config": "#3490dc",
'esm-from-config': 'black',
},
},
},
};
}
24 changes: 24 additions & 0 deletions tests/fixtures/v4/css-loading-js/esm/my-plugin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import plugin from 'tailwindcss/plugin'

export default plugin(
({ addUtilities }) => {
addUtilities({
'.utility-esm-from-plugin': {
color: 'black'
},
'.utility-esm-from-plugin-2': {
width: '100%',
height: '100%',
},
})
},
{
theme: {
extend: {
colors: {
'esm-from-plugin': 'black',
},
},
},
},
)
3 changes: 3 additions & 0 deletions tests/fixtures/v4/css-loading-js/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div
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"
></div>
3 changes: 3 additions & 0 deletions tests/fixtures/v4/css-loading-js/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div
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"
></div>
18 changes: 18 additions & 0 deletions tests/fixtures/v4/css-loading-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2530335

Please sign in to comment.