Skip to content

Commit 68bd3cd

Browse files
Allow loading Tailwind CSS with a custom npm package name (#366)
* Hoist package name * Add internal option to change the package name to use when loading Tailwind CSS * Update lockfile
1 parent 63c22de commit 68bd3cd

File tree

15 files changed

+1475
-6
lines changed

15 files changed

+1475
-6
lines changed

package-lock.json

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

src/config.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ let prettierConfigCache = expiringMap<string, string | null>(10_000)
2929
export async function getTailwindConfig(
3030
options: ParserOptions,
3131
): Promise<ContextContainer> {
32+
let pkgName = options.tailwindPackageName ?? 'tailwindcss'
33+
3234
let key = [
3335
options.filepath,
3436
options.tailwindStylesheet ?? '',
3537
options.tailwindEntryPoint ?? '',
3638
options.tailwindConfig ?? '',
39+
pkgName,
3740
].join(':')
41+
3842
let baseDir = await getBaseDir(options)
3943

4044
// Map the source file to it's associated Tailwind config file
@@ -58,7 +62,12 @@ export async function getTailwindConfig(
5862
}
5963

6064
// By this point we know we need to load the Tailwind config file
61-
let result = await loadTailwindConfig(baseDir, configPath, entryPoint)
65+
let result = await loadTailwindConfig(
66+
baseDir,
67+
pkgName,
68+
configPath,
69+
entryPoint,
70+
)
6271

6372
pathToContextMap.set(contextKey, result)
6473

@@ -100,6 +109,7 @@ async function getBaseDir(options: ParserOptions): Promise<string> {
100109

101110
async function loadTailwindConfig(
102111
baseDir: string,
112+
pkgName: string,
103113
tailwindConfigPath: string | null,
104114
entryPoint: string | null,
105115
): Promise<ContextContainer> {
@@ -110,11 +120,11 @@ async function loadTailwindConfig(
110120
let tailwindConfig: RequiredConfig = { content: [] }
111121

112122
try {
113-
let pkgFile = resolveJsFrom(baseDir, 'tailwindcss/package.json')
123+
let pkgFile = resolveJsFrom(baseDir, `${pkgName}/package.json`)
114124
let pkgDir = path.dirname(pkgFile)
115125

116126
try {
117-
let v4 = await loadV4(baseDir, pkgDir, entryPoint)
127+
let v4 = await loadV4(baseDir, pkgDir, pkgName, entryPoint)
118128
if (v4) {
119129
return v4
120130
}
@@ -197,10 +207,11 @@ function createLoader<T>({
197207
async function loadV4(
198208
baseDir: string,
199209
pkgDir: string,
210+
pkgName: string,
200211
entryPoint: string | null,
201212
) {
202213
// Import Tailwind — if this is v4 it'll have APIs we can use directly
203-
let pkgPath = resolveJsFrom(baseDir, 'tailwindcss')
214+
let pkgPath = resolveJsFrom(baseDir, pkgName)
204215

205216
let tw = await import(pathToFileURL(pkgPath).toString())
206217

src/internal.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export interface InternalOptions {
22
printer: Printer<any>
3+
4+
/**
5+
* The package name to use when loading Tailwind CSS
6+
*/
7+
tailwindPackageName?: string
38
}
49

510
export interface InternalPlugin {

src/options.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ export const options: Record<string, SupportOption> = {
5555
category: 'Tailwind CSS',
5656
description: 'Preserve duplicate classes inside a class list when sorting',
5757
},
58+
59+
tailwindPackageName: {
60+
type: 'string',
61+
default: 'tailwindcss',
62+
category: 'Tailwind CSS',
63+
description: 'The package name to use when loading Tailwind CSS',
64+
},
5865
}
5966

6067
export function getCustomizations(

tests/fixtures.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ let fixtures = [
7373
dir: 'v4/css-loading-js',
7474
ext: 'html',
7575
},
76+
{
77+
name: 'custom npm package name: v3',
78+
dir: 'custom-pkg-name-v3',
79+
ext: 'html',
80+
},
81+
{
82+
name: 'custom npm package name: v4',
83+
dir: 'custom-pkg-name-v4',
84+
ext: 'html',
85+
},
7686
]
7787

7888
let configs = [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
theme: {
3+
extend: {
4+
colors: {
5+
'tomato': 'tomato',
6+
}
7+
}
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="sm:bg-tomato bg-red-500"></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bg-red-500 sm:bg-tomato"></div>

0 commit comments

Comments
 (0)