Skip to content

Commit b9b9690

Browse files
committed
feat: release tailwindcss-patch 1.0.1
compat menorepo issue
1 parent cab4183 commit b9b9690

File tree

9 files changed

+54
-28
lines changed

9 files changed

+54
-28
lines changed

apps/vite-vue/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"build": "vue-tsc && vite build",
8+
"build": "vite build",
99
"preview": "vite preview",
1010
"prepare": "tw-patch"
1111
},

apps/webpack5-vue3/vue.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
const { defineConfig } = require('@vue/cli-service')
2+
const utwm = require('unplugin-tailwindcss-mangle')
23
module.exports = defineConfig({
3-
transpileDependencies: true
4+
transpileDependencies: true,
5+
configureWebpack: (config) => {
6+
config.plugins.push(utwm.webpack())
7+
}
48
})

packages/tailwindcss-patch/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tailwindcss-patch",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "patch tailwindcss for exposing context",
55
"main": "dist/index.js",
66
"types": "dist/types/index.d.ts",
@@ -36,6 +36,7 @@
3636
"devDependencies": {
3737
"@types/babel__generator": "^7.6.4",
3838
"@types/babel__traverse": "^7.18.3",
39+
"@types/resolve": "^1.20.2",
3940
"@types/semver": "^7.3.13",
4041
"defu": "^6.1.2",
4142
"pkg-types": "^1.0.2",
@@ -47,6 +48,7 @@
4748
"@babel/parser": "^7.21.4",
4849
"@babel/traverse": "^7.21.4",
4950
"@babel/types": "^7.21.4",
51+
"resolve": "^1.22.2",
5052
"semver": "^7.4.0"
5153
},
5254
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",

packages/tailwindcss-patch/src/exposeContext.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import path from 'path'
22
import fs from 'fs'
33
import type { Rule } from 'postcss'
4+
import { requireResolve } from './utils'
5+
6+
export function getTailwindcssEntry(basedir: string = process.cwd()) {
7+
return requireResolve('tailwindcss', {
8+
basedir
9+
})
10+
}
11+
12+
export function getContexts(basedir?: string) {
13+
const twPath = getTailwindcssEntry(basedir)
14+
15+
const distPath = path.dirname(twPath)
416

5-
export function getContexts() {
6-
// const cwd = process.cwd()
7-
const distPath = path.dirname(require.resolve('tailwindcss/lib')) // path.join(cwd, 'node_modules', 'tailwindcss/lib')
8-
// index.js
9-
// plugin.js
1017
let injectFilePath = path.join(distPath, 'plugin.js')
1118
if (!fs.existsSync(injectFilePath)) {
1219
injectFilePath = path.join(distPath, 'index.js')
@@ -23,10 +30,10 @@ export function getClassCaches(): Map<
2330
string,
2431
(
2532
| {
26-
layer: string
27-
options: Record<string, any>
28-
sort: Record<string, any>
29-
}
33+
layer: string
34+
options: Record<string, any>
35+
sort: Record<string, any>
36+
}
3037
| Rule
3138
)[]
3239
>[] {

packages/tailwindcss-patch/src/patcher.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import type { PatchOptions, InternalPatchOptions } from './type'
66
import type { PackageJson } from 'pkg-types'
77
import { defu } from 'defu'
88
import { defaultOptions } from './defaults'
9-
import { ensureFileContent } from './utils'
9+
import { ensureFileContent, requireResolve } from './utils'
1010

1111
export function getInstalledPkgJsonPath(options: PatchOptions) {
1212
try {
1313
// const cwd = process.cwd()
14-
const tmpJsonPath = require.resolve(`tailwindcss/package.json`, {
15-
paths: options.paths
14+
const tmpJsonPath = requireResolve(`tailwindcss/package.json`, {
15+
paths: options.paths,
16+
basedir: options.basedir ?? process.cwd()
1617
})
1718

1819
const pkgJson = require(tmpJsonPath) as PackageJson
@@ -23,7 +24,7 @@ export function getInstalledPkgJsonPath(options: PatchOptions) {
2324
}
2425
} catch (error) {
2526
if ((<Error & { code: string }>error).code === 'MODULE_NOT_FOUND') {
26-
console.warn('没有找到`tailwindcss`包,请确认是否安装。')
27+
console.warn('Can\'t find npm pkg: `tailwindcss`, Please ensure it has been installed!')
2728
}
2829
}
2930
}
+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
export interface PatchOptions {
22
overwrite?: boolean
33
paths?: string[]
4+
basedir?: string
45
}
56

67
export interface InternalPatchOptions {
78
overwrite: boolean
89
paths?: string[]
10+
basedir?: string
911
}

packages/tailwindcss-patch/src/utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs'
2+
import { sync, type SyncOpts } from 'resolve'
23

34
export function ensureFileContent(filepaths: string | string[]) {
45
if (typeof filepaths === 'string') {
@@ -16,3 +17,7 @@ export function ensureFileContent(filepaths: string | string[]) {
1617
}
1718
return content
1819
}
20+
21+
export function requireResolve(id: string, opts?: SyncOpts) {
22+
return sync(id, opts)
23+
}

packages/unplugin-tailwindcss-mangle/src/index.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import ClassGenerator from './classGenerator'
88
import { htmlHandler } from './html'
99
import { jsHandler } from './js'
1010
import { cssHandler } from './css'
11-
import type {} from 'webpack'
11+
import type { } from 'webpack'
1212
const unplugin = createUnplugin((options: Options | undefined = {}, meta) => {
13-
// const preserveClass = ['filter']
14-
const mangleClass = (className: string) => {
13+
14+
const isMangleClass = (className: string) => {
1515
// ignore className like 'filter','container'
1616
// it may be dangerous to mangle/rename all StringLiteral , so use /-/ test for only those with /-/ like:
1717
// bg-[#123456] w-1 etc...
@@ -21,21 +21,16 @@ const unplugin = createUnplugin((options: Options | undefined = {}, meta) => {
2121
// let cached: boolean
2222
const classGenerator = new ClassGenerator()
2323
function getCachedClassSet() {
24-
// if (cached) {
25-
// return classSet
26-
// }
24+
2725
const set = getClassCacheSet()
2826
set.forEach((c) => {
29-
if (!mangleClass(c)) {
27+
if (!isMangleClass(c)) {
3028
set.delete(c)
31-
// console.log(c)
3229
}
3330
})
34-
// preserveClass.forEach((c) => {
35-
// set.delete(c)
36-
// })
31+
3732
classSet = set
38-
// cached = true
33+
3934
return classSet
4035
}
4136
return {
@@ -45,6 +40,9 @@ const unplugin = createUnplugin((options: Options | undefined = {}, meta) => {
4540
generateBundle: {
4641
handler(options, bundle, isWrite) {
4742
const runtimeSet = getCachedClassSet()
43+
if (!runtimeSet.size) {
44+
return
45+
}
4846
const groupedEntries = getGroupedEntries(Object.entries(bundle))
4947

5048
if (Array.isArray(groupedEntries.html) && groupedEntries.html.length) {
@@ -91,6 +89,9 @@ const unplugin = createUnplugin((options: Options | undefined = {}, meta) => {
9189
// const resolvePath = require.resolve('tailwindcss')
9290
// console.log(resolvePath)
9391
const runtimeSet = getCachedClassSet()
92+
if (!runtimeSet.size) {
93+
return
94+
}
9495
const groupedEntries = getGroupedEntries(Object.entries(assets))
9596
if (Array.isArray(groupedEntries.html) && groupedEntries.html.length) {
9697
for (let i = 0; i < groupedEntries.html.length; i++) {

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)