Skip to content

Commit ae2326e

Browse files
authored
Merge pull request #69 from sonofmagic/alpha
Alpha
2 parents 84e32fb + 2226436 commit ae2326e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+8049
-490
lines changed

.changeset/young-shrimps-boil.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

package.json

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,55 @@
3232
"@tailwindcss-mangle/core": "workspace:*",
3333
"@tailwindcss-mangle/shared": "workspace:*",
3434
"@tsconfig/recommended": "^1.0.7",
35+
"@types/babel__generator": "^7.6.8",
36+
"@types/babel__helper-plugin-utils": "^7.10.3",
37+
"@types/babel__traverse": "^7.20.6",
3538
"@types/fs-extra": "^11.0.4",
3639
"@types/html-minifier-terser": "^7.0.2",
3740
"@types/lodash-es": "^4.17.12",
3841
"@types/node": "^20.14.11",
42+
"@types/resolve": "^1.20.6",
43+
"@types/semver": "^7.5.8",
3944
"@vitest/coverage-v8": "^2.0.3",
45+
"astro": "^4.12.2",
4046
"bumpp": "^9.4.1",
4147
"cross-env": "^7.0.3",
48+
"css-loader": "^7.1.2",
4249
"dedent": "^1.5.3",
4350
"defu": "^6.1.4",
4451
"del": "^7.1.0",
4552
"eslint": "^9.7.0",
4653
"fs-extra": "^11.2.0",
4754
"html-minifier-terser": "^7.2.0",
55+
"html-webpack-plugin": "^5.6.0",
4856
"local-pkg": "^0.5.0",
4957
"lodash-es": "^4.17.21",
58+
"mini-css-extract-plugin": "^2.9.0",
59+
"normalize-newline": "^4.1.0",
5060
"only-allow": "^1.2.1",
5161
"pathe": "^1.1.2",
62+
"pkg-types": "^1.1.3",
63+
"postcss": "^8.4.39",
64+
"postcss-loader": "^8.1.1",
65+
"postcss7": "npm:postcss@7",
5266
"prettier": "^3.3.3",
5367
"rollup": "^4.18.1",
68+
"tailwindcss": "^3.4.6",
5469
"tailwindcss-patch": "workspace:*",
70+
"tailwindcss2": "npm:@tailwindcss/postcss7-compat@^2.2.17",
5571
"ts-node": "^10.9.2",
5672
"tslib": "^2.6.3",
57-
"tsup": "^8.1.2",
58-
"turbo": "^2.0.7",
73+
"tsup": "^8.2.0",
74+
"tsx": "^4.16.2",
75+
"turbo": "^2.0.8",
5976
"typescript": "^5.5.3",
6077
"unbuild": "^2.0.0",
6178
"unplugin": "^1.11.0",
6279
"unplugin-tailwindcss-mangle": "workspace:*",
80+
"vite": "^5.3.4",
6381
"vite-tsconfig-paths": "^4.3.2",
64-
"vitest": "^2.0.3"
82+
"vitest": "^2.0.3",
83+
"webpack": "^5.93.0",
84+
"webpack-build-utils": "^0.0.7"
6585
}
6686
}

packages/core/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,5 @@
6262
"magic-string": "^0.30.10",
6363
"postcss": "^8.4.39",
6464
"postcss-selector-parser": "^6.1.1"
65-
},
66-
"devDependencies": {
67-
"@types/babel__traverse": "^7.20.6"
6865
}
6966
}

packages/tailwindcss-patch/README.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ get tailwindcss context at runtime ! extract all classes into file!
1212
- [Nodejs API](#nodejs-api)
1313
- [Config](#config)
1414
- [Init Config File](#init-config-file)
15-
- [Migration form v1 to v2](#migration-form-v1-to-v2)
16-
- [0. cli command change](#0-cli-command-change)
17-
- [1. default remove `*` in json array result](#1-default-remove--in-json-array-result)
1815
- [What's next?](#whats-next)
1916

2017
> Nodejs version should >= `16.6.0`
@@ -112,27 +109,6 @@ export default defineConfig({
112109
})
113110
```
114111

115-
## Migration form v1 to v2
116-
117-
### 0. cli command change
118-
119-
```diff
120-
{
121-
- "tw-patch"
122-
+ "tw-patch install"
123-
}
124-
```
125-
126-
### 1. default remove `*` in json array result
127-
128-
```diff
129-
[
130-
- "*",
131-
"text-[99px]",
132-
"text-[100px]"
133-
]
134-
```
135-
136112
## What's next?
137113

138114
At the moment I just extracted all the tool classes to actually get the context of `tailwindcss` to analyze. You can add more functionality to this project by giving me `issue` or `pr`.

packages/tailwindcss-patch/package.json

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tailwindcss-patch",
3-
"version": "3.0.2",
3+
"version": "4.0.0",
44
"description": "patch tailwindcss for exposing context and extract classes",
55
"author": "SonOfMagic <[email protected]>",
66
"license": "MIT",
@@ -62,18 +62,5 @@
6262
"postcss": "^8.4.39",
6363
"resolve": "^1.22.8",
6464
"semver": "^7.6.3"
65-
},
66-
"devDependencies": {
67-
"@types/babel__generator": "^7.6.8",
68-
"@types/babel__traverse": "^7.20.6",
69-
"@types/resolve": "^1.20.6",
70-
"@types/semver": "^7.5.8",
71-
"pkg-types": "^1.1.3",
72-
"postcss7": "npm:postcss@7",
73-
"tailwindcss": "^3.4.6",
74-
"tailwindcss2": "npm:@tailwindcss/postcss7-compat@^2.2.17"
75-
},
76-
"directories": {
77-
"test": "test"
7865
}
7966
}

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'
Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
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'
6-
import { ensureFileContent } from '@/utils'
76
import logger from '@/logger'
87

98
export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatchOptions) {
10-
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/processTailwindFeatures.js')
9+
const k0 = 'lib/processTailwindFeatures.js'
10+
const processTailwindFeaturesFilePath = path.resolve(twDir, k0)
11+
12+
const processTailwindFeaturesContent = fs.readFileSync(processTailwindFeaturesFilePath, 'utf8')
13+
const result: Record<string, any> = {}
1114

12-
const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath)
13-
const result: { processTailwindFeatures?: string, plugin?: string } & Record<string, any> = {}
1415
if (processTailwindFeaturesContent) {
1516
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext(processTailwindFeaturesContent)
1617
if (!hasPatched && opt.overwrite) {
@@ -19,32 +20,46 @@ export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatc
1920
})
2021
logger.success('patch tailwindcss processTailwindFeatures for return content successfully!')
2122
}
22-
result.processTailwindFeatures = code
23+
result[k0] = code
24+
}
25+
let injectFilepath
26+
let k1
27+
const try0 = 'lib/plugin.js'
28+
const try1 = 'lib/index.js'
29+
const pluginFilePath = path.resolve(twDir, try0)
30+
const indexFilePath = path.resolve(twDir, try1)
31+
if (fs.existsSync(pluginFilePath)) {
32+
k1 = try0
33+
injectFilepath = pluginFilePath
34+
}
35+
else if (fs.existsSync(indexFilePath)) {
36+
k1 = try1
37+
injectFilepath = indexFilePath
2338
}
2439

25-
const pluginFilePath = path.resolve(twDir, 'lib/plugin.js')
26-
const indexFilePath = path.resolve(twDir, 'lib/index.js')
27-
const pluginContent = ensureFileContent([pluginFilePath, indexFilePath])
28-
if (pluginContent) {
29-
const { code, hasPatched } = inspectPostcssPlugin(pluginContent)
30-
if (!hasPatched && opt.overwrite) {
31-
fs.writeFileSync(pluginFilePath, code, {
32-
encoding: 'utf8',
33-
})
34-
logger.success('patch tailwindcss for expose runtime context successfully!')
40+
if (injectFilepath && k1) {
41+
const pluginContent = fs.readFileSync(injectFilepath, 'utf8')
42+
if (pluginContent) {
43+
const { code, hasPatched } = inspectPostcssPlugin(pluginContent)
44+
if (!hasPatched && opt.overwrite) {
45+
fs.writeFileSync(injectFilepath, code, {
46+
encoding: 'utf8',
47+
})
48+
logger.success('patch tailwindcss for expose runtime context successfully!')
49+
}
50+
result[k1] = code
3551
}
36-
result.plugin = code
37-
}
3852

39-
opt.custom && typeof opt.custom === 'function' && opt.custom(twDir, result)
40-
return result
53+
return result
54+
}
4155
}
4256

4357
export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatchOptions) {
44-
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/jit/processTailwindFeatures.js')
58+
const k0 = 'lib/jit/processTailwindFeatures.js'
59+
const processTailwindFeaturesFilePath = path.resolve(twDir, k0)
4560

46-
const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath)
47-
const result: { processTailwindFeatures?: string, plugin?: string } & Record<string, any> = {}
61+
const processTailwindFeaturesContent = fs.readFileSync(processTailwindFeaturesFilePath, 'utf8')
62+
const result: Record<string, any> = {}
4863
if (processTailwindFeaturesContent) {
4964
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContextCompat(processTailwindFeaturesContent)
5065
if (!hasPatched && opt.overwrite) {
@@ -53,11 +68,11 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
5368
})
5469
logger.success('patch tailwindcss processTailwindFeatures for return content successfully!')
5570
}
56-
result.processTailwindFeatures = code
71+
result[k0] = code
5772
}
58-
59-
const indexFilePath = path.resolve(twDir, 'lib/jit/index.js')
60-
const pluginContent = ensureFileContent([indexFilePath])
73+
const k1 = 'lib/jit/index.js'
74+
const indexFilePath = path.resolve(twDir, k1)
75+
const pluginContent = fs.readFileSync(indexFilePath, 'utf8')
6176
if (pluginContent) {
6277
const { code, hasPatched } = inspectPostcssPluginCompat(pluginContent)
6378
if (!hasPatched && opt.overwrite) {
@@ -66,9 +81,8 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
6681
})
6782
logger.success('patch tailwindcss for expose runtime content successfully!')
6883
}
69-
result.plugin = code
84+
result[k1] = code
7085
}
7186

72-
opt.custom && typeof opt.custom === 'function' && opt.custom(twDir, result)
7387
return result
7488
}

0 commit comments

Comments
 (0)