Skip to content

Commit 845c89e

Browse files
committed
feat: refactor code
1 parent cbdc22b commit 845c89e

File tree

20 files changed

+70
-69
lines changed

20 files changed

+70
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"tailwindcss2": "npm:@tailwindcss/postcss7-compat@^2.2.17",
7171
"ts-node": "^10.9.2",
7272
"tslib": "^2.6.3",
73-
"tsup": "^8.1.2",
73+
"tsup": "^8.2.0",
7474
"tsx": "^4.16.2",
7575
"turbo": "^2.0.7",
7676
"typescript": "^5.5.3",

packages/tailwindcss-patch/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tailwindcss-patch",
3-
"version": "3.1.0-alpha.1",
3+
"version": "4.0.0-alpha.0",
44
"description": "patch tailwindcss for exposing context and extract classes",
55
"author": "SonOfMagic <qq1324318532@gmail.com>",
66
"license": "MIT",
@@ -62,8 +62,5 @@
6262
"postcss": "^8.4.39",
6363
"resolve": "^1.22.8",
6464
"semver": "^7.6.3"
65-
},
66-
"directories": {
67-
"test": "test"
6865
}
6966
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ export class CacheManager {
3535
this.options = this.getOptions(options)
3636
}
3737

38-
mkdir(cacheDirectory: string) {
39-
const exists = fs.existsSync(cacheDirectory)
40-
if (!exists) {
41-
fs.mkdirSync(cacheDirectory, {
42-
recursive: true,
43-
})
44-
}
45-
return cacheDirectory
46-
}
47-
4838
getOptions(options: CacheOptions = {}): Required<CacheOptions> & { filename: string } {
4939
const cwd = options.cwd ?? process.cwd()
5040
const dir = options.dir ?? path.resolve(cwd, 'node_modules/.cache', pkgName)
@@ -62,8 +52,8 @@ export class CacheManager {
6252
write(data: Set<string>) {
6353
try {
6454
const { dir, filename } = this.options
65-
this.mkdir(dir)
66-
fs.writeFileSync(filename, JSON.stringify([...data], undefined, 2), 'utf8')
55+
fs.ensureDirSync(dir)
56+
fs.outputFileSync(filename, JSON.stringify([...data], undefined, 2), 'utf8')
6757
return filename
6858
}
6959
catch {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './tw-patcher'
1+
export * from './patcher'
22
export * from './patches'
3-
export * from './runtime-patcher'
3+
export * from './runtime'
44
export * from './cache'

packages/tailwindcss-patch/src/core/tw-patcher.ts renamed to packages/tailwindcss-patch/src/core/patcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
22
import fs from 'fs-extra'
33
import { CacheManager, getCacheOptions } from './cache'
4-
import { internalPatch } from './runtime-patcher'
4+
import { internalPatch } from './runtime'
55
import { processTailwindcss } from './postcss'
66
import type { UserConfig } from '@/config'
77
import { getPatchOptions } from '@/defaults'

packages/tailwindcss-patch/src/core/patches/exportContext/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
22
import fs from 'fs-extra'
3-
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './postcss'
4-
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-compat'
3+
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './postcss-v3'
4+
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-v2'
55
import type { InternalPatchOptions } from '@/types'
66
import { ensureFileContent } from '@/utils'
77
import logger from '@/logger'

packages/tailwindcss-patch/src/core/patches/supportCustomUnits/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'node:path'
22
import fs from 'fs-extra'
33
import * as t from '@babel/types'
44
import type { ArrayExpression, StringLiteral } from '@babel/types'
5-
import { defu } from 'defu'
5+
import { defu } from '@/utils'
66
import type { ILengthUnitsPatchOptions } from '@/types'
77
import { generate, parse, traverse } from '@/babel'
88
import logger from '@/logger'
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './core'
2-
export * from './utils'
32
export * from './types'
43
export { defineConfig } from './config'
54
export { default as logger } from './logger'

packages/tailwindcss-patch/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { SyncOpts } from 'resolve'
44
import pkg from 'resolve'
55
import type { PackageJson } from 'pkg-types'
66

7+
export { defu } from 'defu'
78
const { sync } = pkg
89

910
export function ensureFileContent(filepaths: string | string[]) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('cache', () => {
2323

2424
it('mkCacheDirectory', () => {
2525
const dir = path.resolve(__dirname, './fixtures', pkgName)
26-
expect(cm.mkdir(dir)).toBe(dir)
26+
fs.ensureDirSync(dir)
2727
expect(fs.existsSync(dir)).toBe(true)
2828

2929
fs.rmdirSync(dir)
@@ -53,8 +53,8 @@ describe('cache', () => {
5353

5454
const dir = path.resolve(__dirname, './fixtures', `${pkgName}-broken`)
5555
const filepath = path.resolve(dir, 'index.json')
56-
cm.mkdir(dir)
57-
fs.writeFileSync(
56+
57+
fs.outputFileSync(
5858
filepath,
5959
`{
6060
[ '2',"fuck you",{s:'12}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import fs from 'fs-extra'
3-
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss'
3+
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss-v3'
44

55
const tailwindcssCasePath = path.resolve(__dirname, 'fixtures')
66
const twltsLibPath = path.resolve(tailwindcssCasePath, 'versions/3.3.1/lib')

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('postcss', () => {
4545
},
4646
}
4747
const { css } = await postcss([
48+
// eslint-disable-next-line ts/no-var-requires, ts/no-require-imports
4849
require('tailwindcss')({
4950
config,
5051
}),

packages/tailwindcss-patch/test/postcss7-compat.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getTestCase } from './utils'
2-
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss-compat'
2+
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss-v2'
33

44
describe('postcss7-compat', () => {
55
it('processTailwindFeatures patch', async () => {

packages/unplugin-tailwindcss-mangle/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ It is recommended to read the documentation of [tailwindcss-patch](https://githu
2525

2626
```html
2727
<!-- before -->
28-
<div class="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex"></div>
28+
<div
29+
class="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex"
30+
></div>
2931
<!-- after -->
3032
<div class="tw-g tw-h tw-i tw-d tw-e tw-j tw-k tw-l"></div>
3133
```
@@ -62,7 +64,7 @@ npx tw-patch extract
6264

6365
> See more options in [tailwindcss-patch](https://github.com/sonofmagic/tailwindcss-mangle/tree/main/packages/tailwindcss-patch)
6466
65-
Then there will generate a json file: `.tw-patch/tw-class-list.json`
67+
Then there will generate a json file: `.tw-patch/tw-class-list.json`
6668

6769
### 5. Register this plugin
6870

@@ -117,7 +119,7 @@ module.exports = defineConfig({
117119
```ts
118120
import nuxtPlugin from 'unplugin-tailwindcss-mangle/nuxt'
119121
export default defineNuxtConfig({
120-
//...
122+
// ...
121123
// https://github.com/nuxt/nuxt/issues/20428
122124
// you must set this option to false to enable vite extract css
123125
experimental: {
@@ -132,7 +134,6 @@ export default defineNuxtConfig({
132134
]
133135
]
134136
})
135-
136137
```
137138

138139
## Options
@@ -150,7 +151,7 @@ because plugin will **traverse** all `html class attr` and `js StringLiteral` to
150151
it's dangerous to mangle some `js StringLiteral` like:
151152

152153
```js
153-
const innerHTML = "i'm flex and relative and grid"
154+
const innerHTML = 'i\'m flex and relative and grid'
154155
document.body.innerHTML = innerHTML
155156
```
156157

packages/unplugin-tailwindcss-mangle/test/__snapshots__/vite.test.ts.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ exports[`vite build > common build 3`] = `
591591
</head>
592592
<body>
593593
<div id="app"></div>
594-
595594
</body>
596595
</html>
597596
"

packages/unplugin-tailwindcss-mangle/test/vite.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('vite build', () => {
3737
}
3838
expect(output[2].type).toBe('asset')
3939
if (output[2].type === 'asset') {
40-
expect(output[2].source).toMatchSnapshot()
40+
expect(output[2].source.toString().replace(/(\r?\n)+/g, '\n')).toMatchSnapshot()
4141
}
4242
})
4343

0 commit comments

Comments
 (0)