Skip to content

Commit a3214e0

Browse files
committed
chore: remove resolve
1 parent e04cdc2 commit a3214e0

File tree

13 files changed

+200
-116
lines changed

13 files changed

+200
-116
lines changed

.changeset/healthy-toes-hunt.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tailwindcss-patch": major
3+
---
4+
5+
chore: prepare for tailwindcss v4
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
import '../dist/cli.js'
2+
require('../dist/cli.js')

packages/tailwindcss-patch/package.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "tailwindcss-patch",
3-
"type": "module",
43
"version": "5.0.2",
54
"description": "patch tailwindcss for exposing context and extract classes",
65
"author": "ice breaker <1324318532@qq.com>",
@@ -55,12 +54,12 @@
5554
"exports": {
5655
".": {
5756
"types": "./dist/index.d.ts",
58-
"import": "./dist/index.js",
59-
"require": "./dist/index.cjs"
57+
"import": "./dist/index.mjs",
58+
"require": "./dist/index.js"
6059
}
6160
},
62-
"main": "./dist/index.cjs",
63-
"module": "./dist/index.js",
61+
"main": "./dist/index.js",
62+
"module": "./dist/index.mjs",
6463
"types": "./dist/index.d.ts"
6564
},
6665
"peerDependencies": {
@@ -84,7 +83,12 @@
8483
"lilconfig": "^3.1.3",
8584
"pathe": "^2.0.2",
8685
"postcss": "^8.5.1",
87-
"resolve": "^1.22.10",
8886
"semver": "^7.6.3"
87+
},
88+
"devDependencies": {
89+
"@tailwindcss/postcss": "^4.0.0",
90+
"@tailwindcss/vite": "^4.0.0",
91+
"tailwindcss": "^4.0.0",
92+
"tailwindcss-3": "npm:tailwindcss@^3"
8993
}
9094
}

packages/tailwindcss-patch/src/core/patcher.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import type { UserConfig } from '../config'
22
import type { CacheStrategy, InternalCacheOptions, InternalPatchOptions, PackageInfo, TailwindcssClassCache, TailwindcssPatcherOptions, TailwindcssRuntimeContext } from '../types'
33
import { createRequire } from 'node:module'
44
import fs from 'fs-extra'
5+
import { getPackageInfoSync } from 'local-pkg'
56
import path from 'pathe'
67
import { getPatchOptions } from '../defaults'
78
import logger from '../logger'
8-
import { getPackageInfoSync, isObject } from '../utils'
9+
import { isObject } from '../utils'
910
import { CacheManager, getCacheOptions } from './cache'
1011
import { processTailwindcss } from './postcss'
1112
import { internalPatch } from './runtime'
@@ -17,7 +18,7 @@ export class TailwindcssPatcher {
1718
public patchOptions: InternalPatchOptions
1819
public patch: () => void
1920
public cacheManager: CacheManager
20-
public packageInfo?: PackageInfo
21+
public packageInfo: PackageInfo
2122
public majorVersion?: number
2223

2324
constructor(options: TailwindcssPatcherOptions = {}) {
@@ -26,10 +27,17 @@ export class TailwindcssPatcher {
2627
this.patchOptions = getPatchOptions(options.patch)
2728

2829
this.cacheManager = new CacheManager(this.cacheOptions)
29-
this.packageInfo = getPackageInfoSync('tailwindcss', { basedir: this.patchOptions.basedir })
30-
if (this.packageInfo && this.packageInfo.version) {
31-
this.majorVersion = Number.parseInt(this.packageInfo.version[0])
30+
31+
const packageInfo = getPackageInfoSync('tailwindcss')
32+
33+
if (!packageInfo) {
34+
throw new Error('tailwindcss not found')
3235
}
36+
37+
if (packageInfo.version) {
38+
this.majorVersion = Number.parseInt(packageInfo.version[0])
39+
}
40+
this.packageInfo = packageInfo
3341
this.patch = () => {
3442
try {
3543
return internalPatch(this.packageInfo?.packageJsonPath, this.patchOptions)
@@ -57,7 +65,7 @@ export class TailwindcssPatcher {
5765
if (this.majorVersion === 2) {
5866
injectFilePath = path.join(distPath, 'jit/index.js')
5967
}
60-
else {
68+
else if (this.majorVersion === 3) {
6169
injectFilePath = path.join(distPath, 'plugin.js')
6270
if (!fs.existsSync(injectFilePath)) {
6371
injectFilePath = path.join(distPath, 'index.js')
@@ -124,7 +132,10 @@ export class TailwindcssPatcher {
124132
if (output && tailwindcss) {
125133
const { removeUniversalSelector, filename, loose } = output
126134

127-
await processTailwindcss(tailwindcss)
135+
await processTailwindcss({
136+
...tailwindcss,
137+
majorVersion: this.majorVersion,
138+
})
128139

129140
const set = this.getClassSet({
130141
removeUniversalSelector,

packages/tailwindcss-patch/src/core/patches/exportContext/postcss-v2.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ export function inspectPostcssPlugin(content: string) {
5454
const lastStatement = n.body[idx - 2]
5555
const hasPatchedCondition0
5656
= prevStatement
57-
&& t.isVariableDeclaration(prevStatement)
58-
&& prevStatement.declarations.length === 1
59-
&& t.isIdentifier(prevStatement.declarations[0].id)
60-
&& prevStatement.declarations[0].id.name === variableName
57+
&& t.isVariableDeclaration(prevStatement)
58+
&& prevStatement.declarations.length === 1
59+
&& t.isIdentifier(prevStatement.declarations[0].id)
60+
&& prevStatement.declarations[0].id.name === variableName
6161
const hasPatchedCondition1
6262
= t.isExpressionStatement(lastStatement)
63-
&& t.isAssignmentExpression(lastStatement.expression)
64-
&& t.isIdentifier(lastStatement.expression.right)
65-
&& lastStatement.expression.right.name === variableName
63+
&& t.isAssignmentExpression(lastStatement.expression)
64+
&& t.isIdentifier(lastStatement.expression.right)
65+
&& lastStatement.expression.right.name === variableName
6666

6767
hasPatched = hasPatchedCondition0 || hasPatchedCondition1
6868
if (!hasPatched) {

packages/tailwindcss-patch/src/core/patches/exportContext/postcss-v3.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ export function inspectPostcssPlugin(content: string) {
5555
const lastStatement = n.body[n.body.length - 1]
5656
const hasPatchedCondition0
5757
= prevStatement
58-
&& t.isVariableDeclaration(prevStatement)
59-
&& prevStatement.declarations.length === 1
60-
&& t.isIdentifier(prevStatement.declarations[0].id)
61-
&& prevStatement.declarations[0].id.name === variableName
58+
&& t.isVariableDeclaration(prevStatement)
59+
&& prevStatement.declarations.length === 1
60+
&& t.isIdentifier(prevStatement.declarations[0].id)
61+
&& prevStatement.declarations[0].id.name === variableName
6262
const hasPatchedCondition1
6363
= t.isExpressionStatement(lastStatement)
64-
&& t.isAssignmentExpression(lastStatement.expression)
65-
&& t.isIdentifier(lastStatement.expression.right)
66-
&& lastStatement.expression.right.name === variableName
64+
&& t.isAssignmentExpression(lastStatement.expression)
65+
&& t.isIdentifier(lastStatement.expression.right)
66+
&& lastStatement.expression.right.name === variableName
6767

6868
hasPatched = hasPatchedCondition0 || hasPatchedCondition1
6969
if (!hasPatched) {

packages/tailwindcss-patch/src/core/postcss.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import { createJiti } from 'jiti'
55
import { lilconfig } from 'lilconfig'
66
import path from 'pathe'
77
import postcss from 'postcss'
8-
import { requireResolve } from '../utils'
98

109
const jiti = createJiti(import.meta.url)
1110
const require = createRequire(import.meta.url)
1211
export interface ProcessTailwindcssOptions {
1312
cwd?: string
1413
config?: string
14+
majorVersion?: number
1515
}
1616

1717
export async function processTailwindcss(options: ProcessTailwindcssOptions) {
18-
const { config: userConfig, cwd } = defu<ProcessTailwindcssOptions, ProcessTailwindcssOptions[]>(options, {
18+
const { config: userConfig, cwd, majorVersion } = defu<ProcessTailwindcssOptions, ProcessTailwindcssOptions[]>(options, {
1919
cwd: process.cwd(),
20+
majorVersion: 3,
2021
})
2122
let config = userConfig
2223
// 没有具体指定的话,就走下面的分支
@@ -47,11 +48,19 @@ export async function processTailwindcss(options: ProcessTailwindcssOptions) {
4748
}
4849
config = result.filepath
4950
}
50-
const id = requireResolve('tailwindcss', {
51-
basedir: cwd,
52-
})
51+
52+
if (majorVersion === 4) {
53+
return await postcss([
54+
require('@tailwindcss/postcss')({
55+
config,
56+
}),
57+
]).process('@import \'tailwindcss\';', {
58+
from: undefined,
59+
})
60+
}
61+
5362
return await postcss([
54-
require(id)({
63+
require('tailwindcss')({
5564
config,
5665
}),
5766
]).process('@tailwind base;@tailwind components;@tailwind utilities;', {
-62
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,3 @@
1-
import type { PackageJson } from 'pkg-types'
2-
import type { SyncOpts } from 'resolve'
3-
import fs from 'fs-extra'
4-
import path from 'pathe'
5-
import pkg from 'resolve'
6-
7-
const { sync } = pkg
8-
9-
export function requireResolve(id: string, opts?: SyncOpts) {
10-
return sync(id, opts)
11-
}
12-
13-
function searchPackageJSON(dir: string) {
14-
let packageJsonPath
15-
while (true) {
16-
if (!dir) {
17-
return
18-
}
19-
const newDir = path.dirname(dir)
20-
if (newDir === dir) {
21-
return
22-
}
23-
dir = newDir
24-
packageJsonPath = path.join(dir, 'package.json')
25-
if (fs.existsSync(packageJsonPath)) {
26-
break
27-
}
28-
}
29-
30-
return packageJsonPath
31-
}
32-
33-
function getTailwindcssEntry(name: string = 'tailwindcss', opts?: SyncOpts) {
34-
return requireResolve(name, opts)
35-
}
36-
37-
function getPackageJsonPath(name: string, options: SyncOpts = {}) {
38-
const entry = getTailwindcssEntry(name, options)
39-
if (!entry) {
40-
return
41-
}
42-
43-
return searchPackageJSON(entry)
44-
}
45-
46-
export function getPackageInfoSync(name: string, options: SyncOpts = {}) {
47-
const packageJsonPath = getPackageJsonPath(name, options)
48-
if (!packageJsonPath) {
49-
return
50-
}
51-
52-
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
53-
54-
return {
55-
name,
56-
version: packageJson.version,
57-
rootPath: path.dirname(packageJsonPath),
58-
packageJsonPath,
59-
packageJson,
60-
}
61-
}
62-
631
export function isObject(val: any) {
642
return val !== null && typeof val === 'object' && Array.isArray(val) === false
653
};

packages/tailwindcss-patch/test/cache.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import path from 'pathe'
21
import { pkgName } from '@/constants'
32
import { CacheManager, TailwindcssPatcher } from '@/core'
43
import { getCacheOptions } from '@/core/cache'
54
import fs from 'fs-extra'
5+
import path from 'pathe'
66
import { getCss } from './utils'
77

88
describe('cache', () => {

packages/tailwindcss-patch/test/postcss8-v3.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('postcss', () => {
4747
},
4848
}
4949
const { css } = await postcss([
50-
require('tailwindcss')({
50+
require('tailwindcss-3')({
5151
config,
5252
}),
5353
]).process('@tailwind base;@tailwind components;@tailwind utilities;', {

packages/tailwindcss-patch/test/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs-extra'
22
import path from 'pathe'
33
import postcss from 'postcss'
4-
import tailwindcss from 'tailwindcss'
4+
import tailwindcss from 'tailwindcss-3'
55

66
export const fixturesRoot = path.resolve(__dirname, './fixtures')
77

packages/tailwindcss-patch/tsup.config.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { defineConfig } from 'tsup'
33
export default defineConfig({
44
entry: ['src/index.ts', 'src/cli.ts'],
55
shims: true,
6-
sourcemap: false,
76
clean: true,
87
format: ['cjs', 'esm'], // , 'esm'
98
dts: true,
10-
// cjsInterop: true,
11-
// splitting: true,
9+
cjsInterop: true,
10+
splitting: true,
1211
})

0 commit comments

Comments
 (0)