tailwindcss-patch 2.2.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +11 -8
- package/dist/cli.mjs +9 -7
- package/dist/index.cjs +30 -587
- package/dist/index.d.cts +30 -20
- package/dist/index.d.mts +30 -20
- package/dist/index.d.ts +30 -20
- package/dist/index.mjs +14 -556
- package/dist/shared/tailwindcss-patch.2fb4223c.cjs +799 -0
- package/dist/shared/tailwindcss-patch.c6a99ac9.mjs +768 -0
- package/package.json +36 -34
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
import { gte } from 'semver';
|
|
5
|
+
import * as t from '@babel/types';
|
|
6
|
+
import _babelGenerate from '@babel/generator';
|
|
7
|
+
import _babelTraverse from '@babel/traverse';
|
|
8
|
+
import { parse } from '@babel/parser';
|
|
9
|
+
import fs$1 from 'node:fs/promises';
|
|
10
|
+
import pkg from 'resolve';
|
|
11
|
+
import postcss from 'postcss';
|
|
12
|
+
import { lilconfig } from 'lilconfig';
|
|
13
|
+
import createJiti from 'jiti';
|
|
14
|
+
import '@tailwindcss-mangle/config';
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
// -- Unbuild CommonJS Shims --
|
|
19
|
+
import __cjs_url__ from 'url';
|
|
20
|
+
import __cjs_path__ from 'path';
|
|
21
|
+
import __cjs_mod__ from 'module';
|
|
22
|
+
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
|
23
|
+
const __dirname = __cjs_path__.dirname(__filename);
|
|
24
|
+
const require = __cjs_mod__.createRequire(import.meta.url);
|
|
25
|
+
const pkgName = "tailwindcss-patch";
|
|
26
|
+
|
|
27
|
+
function log(message, ...optionalParams) {
|
|
28
|
+
return console.log(`[${pkgName}]:${message}`, ...optionalParams);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var __defProp$1 = Object.defineProperty;
|
|
32
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
33
|
+
var __publicField$1 = (obj, key, value) => {
|
|
34
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
function getCacheOptions(options) {
|
|
38
|
+
let cache;
|
|
39
|
+
switch (typeof options) {
|
|
40
|
+
case "undefined": {
|
|
41
|
+
cache = {
|
|
42
|
+
enable: false
|
|
43
|
+
};
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case "boolean": {
|
|
47
|
+
cache = {
|
|
48
|
+
enable: options
|
|
49
|
+
};
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case "object": {
|
|
53
|
+
cache = { ...options, enable: true };
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return cache;
|
|
58
|
+
}
|
|
59
|
+
class CacheManager {
|
|
60
|
+
constructor(options = {}) {
|
|
61
|
+
__publicField$1(this, "options");
|
|
62
|
+
this.options = this.getOptions(options);
|
|
63
|
+
}
|
|
64
|
+
mkdir(cacheDirectory) {
|
|
65
|
+
const exists = fs.existsSync(cacheDirectory);
|
|
66
|
+
if (!exists) {
|
|
67
|
+
fs.mkdirSync(cacheDirectory, {
|
|
68
|
+
recursive: true
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return cacheDirectory;
|
|
72
|
+
}
|
|
73
|
+
getOptions(options = {}) {
|
|
74
|
+
const cwd = options.cwd ?? process.cwd();
|
|
75
|
+
const dir = options.dir ?? path.resolve(cwd, "node_modules/.cache", pkgName);
|
|
76
|
+
const file = options.file ?? "index.json";
|
|
77
|
+
const filename = path.resolve(dir, file);
|
|
78
|
+
return {
|
|
79
|
+
cwd,
|
|
80
|
+
dir,
|
|
81
|
+
file,
|
|
82
|
+
filename,
|
|
83
|
+
strategy: "merge"
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
write(data) {
|
|
87
|
+
try {
|
|
88
|
+
const { dir, filename } = this.options;
|
|
89
|
+
this.mkdir(dir);
|
|
90
|
+
fs.writeFileSync(filename, JSON.stringify([...data], void 0, 2), "utf8");
|
|
91
|
+
return filename;
|
|
92
|
+
} catch {
|
|
93
|
+
log("write cache file fail!");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
read() {
|
|
97
|
+
const { filename } = this.options;
|
|
98
|
+
try {
|
|
99
|
+
if (fs.existsSync(filename)) {
|
|
100
|
+
const data = fs.readFileSync(filename, "utf8");
|
|
101
|
+
return new Set(JSON.parse(data));
|
|
102
|
+
}
|
|
103
|
+
} catch {
|
|
104
|
+
log(`parse cache content fail! path:${filename}`);
|
|
105
|
+
try {
|
|
106
|
+
fs.unlinkSync(filename);
|
|
107
|
+
} catch {
|
|
108
|
+
log(`delete cache file fail! path:${filename}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function _interopDefaultCompat(e) {
|
|
115
|
+
return e && typeof e === "object" && "default" in e ? e.default : e;
|
|
116
|
+
}
|
|
117
|
+
const generate = _interopDefaultCompat(_babelGenerate);
|
|
118
|
+
const traverse = _interopDefaultCompat(_babelTraverse);
|
|
119
|
+
|
|
120
|
+
function inspectProcessTailwindFeaturesReturnContext$1(content) {
|
|
121
|
+
const ast = parse(content);
|
|
122
|
+
let hasPatched = false;
|
|
123
|
+
traverse(ast, {
|
|
124
|
+
FunctionDeclaration(p) {
|
|
125
|
+
const n = p.node;
|
|
126
|
+
if (n.id?.name === "processTailwindFeatures" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
|
|
127
|
+
const rts = n.body.body[0];
|
|
128
|
+
if (t.isFunctionExpression(rts.argument)) {
|
|
129
|
+
const body = rts.argument.body.body;
|
|
130
|
+
const lastStatement = body[body.length - 1];
|
|
131
|
+
hasPatched = t.isReturnStatement(lastStatement) && t.isIdentifier(lastStatement.argument) && lastStatement.argument.name === "context";
|
|
132
|
+
if (!hasPatched) {
|
|
133
|
+
const rts2 = t.returnStatement(t.identifier("context"));
|
|
134
|
+
body.push(rts2);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
code: hasPatched ? content : generate(ast).code,
|
|
142
|
+
hasPatched
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function inspectPostcssPlugin$1(content) {
|
|
146
|
+
const ast = parse(content);
|
|
147
|
+
const exportKey = "contextRef";
|
|
148
|
+
const variableName = "contextRef";
|
|
149
|
+
const valueKey = "value";
|
|
150
|
+
let hasPatched = false;
|
|
151
|
+
traverse(ast, {
|
|
152
|
+
Program(p) {
|
|
153
|
+
const n = p.node;
|
|
154
|
+
const idx = n.body.findIndex((x) => {
|
|
155
|
+
return t.isExpressionStatement(x) && t.isAssignmentExpression(x.expression) && t.isMemberExpression(x.expression.left) && t.isFunctionExpression(x.expression.right) && x.expression.right.id?.name === "tailwindcss";
|
|
156
|
+
});
|
|
157
|
+
if (idx > -1) {
|
|
158
|
+
const prevStatement = n.body[idx - 1];
|
|
159
|
+
const lastStatement = n.body[n.body.length - 1];
|
|
160
|
+
const hasPatchedCondition0 = prevStatement && t.isVariableDeclaration(prevStatement) && prevStatement.declarations.length === 1 && t.isIdentifier(prevStatement.declarations[0].id) && prevStatement.declarations[0].id.name === variableName;
|
|
161
|
+
const hasPatchedCondition1 = t.isExpressionStatement(lastStatement) && t.isAssignmentExpression(lastStatement.expression) && t.isIdentifier(lastStatement.expression.right) && lastStatement.expression.right.name === variableName;
|
|
162
|
+
hasPatched = hasPatchedCondition0 || hasPatchedCondition1;
|
|
163
|
+
if (!hasPatched) {
|
|
164
|
+
const statement = t.variableDeclaration("const", [
|
|
165
|
+
t.variableDeclarator(t.identifier(variableName), t.objectExpression([t.objectProperty(t.identifier(valueKey), t.arrayExpression())]))
|
|
166
|
+
]);
|
|
167
|
+
n.body.splice(idx, 0, statement);
|
|
168
|
+
n.body.push(
|
|
169
|
+
t.expressionStatement(
|
|
170
|
+
t.assignmentExpression(
|
|
171
|
+
"=",
|
|
172
|
+
t.memberExpression(t.memberExpression(t.identifier("module"), t.identifier("exports")), t.identifier(exportKey)),
|
|
173
|
+
t.identifier(variableName)
|
|
174
|
+
)
|
|
175
|
+
)
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
FunctionExpression(p) {
|
|
181
|
+
if (hasPatched) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const n = p.node;
|
|
185
|
+
if (n.id?.name === "tailwindcss" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
|
|
186
|
+
const returnStatement = n.body.body[0];
|
|
187
|
+
if (t.isObjectExpression(returnStatement.argument) && returnStatement.argument.properties.length === 2) {
|
|
188
|
+
const properties = returnStatement.argument.properties;
|
|
189
|
+
if (t.isObjectProperty(properties[0]) && t.isObjectProperty(properties[1])) {
|
|
190
|
+
const keyMatched = t.isIdentifier(properties[0].key) && properties[0].key.name === "postcssPlugin";
|
|
191
|
+
const pluginsMatched = t.isIdentifier(properties[1].key) && properties[1].key.name === "plugins";
|
|
192
|
+
if (pluginsMatched && keyMatched && t.isCallExpression(properties[1].value) && t.isMemberExpression(properties[1].value.callee) && t.isArrayExpression(properties[1].value.callee.object)) {
|
|
193
|
+
const pluginsCode = properties[1].value.callee.object.elements;
|
|
194
|
+
if (pluginsCode[1] && t.isFunctionExpression(pluginsCode[1])) {
|
|
195
|
+
const targetBlockStatement = pluginsCode[1].body;
|
|
196
|
+
const lastStatement = targetBlockStatement.body[targetBlockStatement.body.length - 1];
|
|
197
|
+
if (t.isExpressionStatement(lastStatement)) {
|
|
198
|
+
const newExpressionStatement = t.expressionStatement(
|
|
199
|
+
t.callExpression(
|
|
200
|
+
t.memberExpression(
|
|
201
|
+
t.memberExpression(t.identifier(variableName), t.identifier("value")),
|
|
202
|
+
t.identifier("push")
|
|
203
|
+
),
|
|
204
|
+
[lastStatement.expression]
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
targetBlockStatement.body[targetBlockStatement.body.length - 1] = newExpressionStatement;
|
|
208
|
+
}
|
|
209
|
+
const ifIdx = targetBlockStatement.body.findIndex((x) => t.isIfStatement(x));
|
|
210
|
+
if (ifIdx > -1) {
|
|
211
|
+
const ifRoot = targetBlockStatement.body[ifIdx];
|
|
212
|
+
if (t.isBlockStatement(ifRoot.consequent) && ifRoot.consequent.body[1] && t.isForOfStatement(ifRoot.consequent.body[1])) {
|
|
213
|
+
const forOf = ifRoot.consequent.body[1];
|
|
214
|
+
if (t.isBlockStatement(forOf.body) && forOf.body.body.length === 1 && t.isIfStatement(forOf.body.body[0])) {
|
|
215
|
+
const if2 = forOf.body.body[0];
|
|
216
|
+
if (t.isBlockStatement(if2.consequent) && if2.consequent.body.length === 1 && t.isExpressionStatement(if2.consequent.body[0])) {
|
|
217
|
+
const target = if2.consequent.body[0];
|
|
218
|
+
const newExpressionStatement = t.expressionStatement(
|
|
219
|
+
t.callExpression(t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier("value")), t.identifier("push")), [target.expression])
|
|
220
|
+
);
|
|
221
|
+
if2.consequent.body[0] = newExpressionStatement;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
targetBlockStatement.body.unshift(
|
|
227
|
+
// contentRef.value = []
|
|
228
|
+
// t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression()))
|
|
229
|
+
// contentRef.value.length = 0
|
|
230
|
+
t.expressionStatement(
|
|
231
|
+
t.assignmentExpression(
|
|
232
|
+
"=",
|
|
233
|
+
t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.identifier("length")),
|
|
234
|
+
t.numericLiteral(0)
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// BlockStatement(p) {
|
|
245
|
+
// const n = p.node
|
|
246
|
+
// if (start && p.parent.type === 'FunctionExpression' && !p.parent.id) {
|
|
247
|
+
// n.body.unshift(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression())))
|
|
248
|
+
// }
|
|
249
|
+
// }
|
|
250
|
+
});
|
|
251
|
+
return {
|
|
252
|
+
code: hasPatched ? content : generate(ast).code,
|
|
253
|
+
hasPatched
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function inspectProcessTailwindFeaturesReturnContext(content) {
|
|
258
|
+
const ast = parse(content, {
|
|
259
|
+
sourceType: "unambiguous"
|
|
260
|
+
});
|
|
261
|
+
let hasPatched = false;
|
|
262
|
+
traverse(ast, {
|
|
263
|
+
FunctionDeclaration(p) {
|
|
264
|
+
const n = p.node;
|
|
265
|
+
if (n.id?.name === "processTailwindFeatures" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
|
|
266
|
+
const rts = n.body.body[0];
|
|
267
|
+
if (t.isFunctionExpression(rts.argument)) {
|
|
268
|
+
const body = rts.argument.body.body;
|
|
269
|
+
const lastStatement = body[body.length - 1];
|
|
270
|
+
hasPatched = t.isReturnStatement(lastStatement) && t.isIdentifier(lastStatement.argument) && lastStatement.argument.name === "context";
|
|
271
|
+
if (!hasPatched) {
|
|
272
|
+
const rts2 = t.returnStatement(t.identifier("context"));
|
|
273
|
+
body.push(rts2);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
return {
|
|
280
|
+
code: hasPatched ? content : generate(ast).code,
|
|
281
|
+
hasPatched
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function inspectPostcssPlugin(content) {
|
|
285
|
+
const ast = parse(content);
|
|
286
|
+
const exportKey = "contextRef";
|
|
287
|
+
const variableName = "contextRef";
|
|
288
|
+
const valueKey = "value";
|
|
289
|
+
let hasPatched = false;
|
|
290
|
+
traverse(ast, {
|
|
291
|
+
Program(p) {
|
|
292
|
+
const n = p.node;
|
|
293
|
+
const idx = n.body.findIndex((x) => {
|
|
294
|
+
return t.isFunctionDeclaration(x) && x.id?.name === "_default";
|
|
295
|
+
});
|
|
296
|
+
if (idx > -1) {
|
|
297
|
+
const prevStatement = n.body[idx - 1];
|
|
298
|
+
const lastStatement = n.body[idx - 2];
|
|
299
|
+
const hasPatchedCondition0 = prevStatement && t.isVariableDeclaration(prevStatement) && prevStatement.declarations.length === 1 && t.isIdentifier(prevStatement.declarations[0].id) && prevStatement.declarations[0].id.name === variableName;
|
|
300
|
+
const hasPatchedCondition1 = t.isExpressionStatement(lastStatement) && t.isAssignmentExpression(lastStatement.expression) && t.isIdentifier(lastStatement.expression.right) && lastStatement.expression.right.name === variableName;
|
|
301
|
+
hasPatched = hasPatchedCondition0 || hasPatchedCondition1;
|
|
302
|
+
if (!hasPatched) {
|
|
303
|
+
const statement = t.variableDeclaration("var", [
|
|
304
|
+
t.variableDeclarator(t.identifier(variableName), t.objectExpression([t.objectProperty(t.identifier(valueKey), t.arrayExpression())]))
|
|
305
|
+
]);
|
|
306
|
+
n.body.splice(
|
|
307
|
+
idx,
|
|
308
|
+
0,
|
|
309
|
+
statement,
|
|
310
|
+
// exports.contextRef = contextRef;
|
|
311
|
+
t.expressionStatement(
|
|
312
|
+
t.assignmentExpression(
|
|
313
|
+
"=",
|
|
314
|
+
t.memberExpression(t.identifier("exports"), t.identifier(exportKey)),
|
|
315
|
+
t.identifier(variableName)
|
|
316
|
+
)
|
|
317
|
+
)
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
FunctionDeclaration(p) {
|
|
323
|
+
if (hasPatched) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const n = p.node;
|
|
327
|
+
if (n.id?.name === "_default" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
|
|
328
|
+
const returnStatement = n.body.body[0];
|
|
329
|
+
if (t.isCallExpression(returnStatement.argument) && t.isMemberExpression(returnStatement.argument.callee) && t.isArrayExpression(returnStatement.argument.callee.object)) {
|
|
330
|
+
const targetFn = returnStatement.argument.callee.object.elements[1];
|
|
331
|
+
if (t.isFunctionExpression(targetFn)) {
|
|
332
|
+
const targetBlockStatement = targetFn.body;
|
|
333
|
+
if (t.isExpressionStatement(targetBlockStatement.body[0]) && t.isAssignmentExpression(targetBlockStatement.body[0].expression) && t.isNumericLiteral(targetBlockStatement.body[0].expression.right)) {
|
|
334
|
+
hasPatched = true;
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const lastStatement = targetBlockStatement.body[targetBlockStatement.body.length - 1];
|
|
338
|
+
if (t.isExpressionStatement(lastStatement)) {
|
|
339
|
+
const newExpressionStatement = t.expressionStatement(
|
|
340
|
+
t.callExpression(
|
|
341
|
+
t.memberExpression(
|
|
342
|
+
t.memberExpression(t.identifier(variableName), t.identifier("value")),
|
|
343
|
+
t.identifier("push")
|
|
344
|
+
),
|
|
345
|
+
[lastStatement.expression]
|
|
346
|
+
)
|
|
347
|
+
);
|
|
348
|
+
targetBlockStatement.body[targetBlockStatement.body.length - 1] = newExpressionStatement;
|
|
349
|
+
}
|
|
350
|
+
const ifIdx = targetBlockStatement.body.findIndex((x) => t.isIfStatement(x));
|
|
351
|
+
if (ifIdx > -1) {
|
|
352
|
+
const ifRoot = targetBlockStatement.body[ifIdx];
|
|
353
|
+
if (t.isBlockStatement(ifRoot.consequent) && ifRoot.consequent.body[1] && t.isForOfStatement(ifRoot.consequent.body[1])) {
|
|
354
|
+
const forOf = ifRoot.consequent.body[1];
|
|
355
|
+
if (t.isBlockStatement(forOf.body) && forOf.body.body.length === 1 && t.isIfStatement(forOf.body.body[0])) {
|
|
356
|
+
const if2 = forOf.body.body[0];
|
|
357
|
+
if (t.isBlockStatement(if2.consequent) && if2.consequent.body.length === 1 && t.isExpressionStatement(if2.consequent.body[0])) {
|
|
358
|
+
const target = if2.consequent.body[0];
|
|
359
|
+
const newExpressionStatement = t.expressionStatement(
|
|
360
|
+
t.callExpression(t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier("value")), t.identifier("push")), [target.expression])
|
|
361
|
+
);
|
|
362
|
+
if2.consequent.body[0] = newExpressionStatement;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
targetBlockStatement.body.unshift(
|
|
368
|
+
// contentRef.value = []
|
|
369
|
+
// t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression()))
|
|
370
|
+
// contentRef.value.length = 0
|
|
371
|
+
t.expressionStatement(
|
|
372
|
+
t.assignmentExpression(
|
|
373
|
+
"=",
|
|
374
|
+
t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.identifier("length")),
|
|
375
|
+
t.numericLiteral(0)
|
|
376
|
+
)
|
|
377
|
+
)
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
return {
|
|
385
|
+
code: hasPatched ? content : generate(ast).code,
|
|
386
|
+
hasPatched
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const { sync } = pkg;
|
|
391
|
+
function ensureFileContent(filepaths) {
|
|
392
|
+
if (typeof filepaths === "string") {
|
|
393
|
+
filepaths = [filepaths];
|
|
394
|
+
}
|
|
395
|
+
let content;
|
|
396
|
+
for (const filepath of filepaths) {
|
|
397
|
+
if (fs.existsSync(filepath)) {
|
|
398
|
+
content = fs.readFileSync(filepath, {
|
|
399
|
+
encoding: "utf8"
|
|
400
|
+
});
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return content;
|
|
405
|
+
}
|
|
406
|
+
function requireResolve(id, opts) {
|
|
407
|
+
return sync(id, opts);
|
|
408
|
+
}
|
|
409
|
+
async function ensureDir(p) {
|
|
410
|
+
try {
|
|
411
|
+
await fs$1.access(p);
|
|
412
|
+
} catch {
|
|
413
|
+
await fs$1.mkdir(p, {
|
|
414
|
+
recursive: true
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function searchPackageJSON(dir) {
|
|
419
|
+
let packageJsonPath;
|
|
420
|
+
while (true) {
|
|
421
|
+
if (!dir) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
const newDir = path.dirname(dir);
|
|
425
|
+
if (newDir === dir) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
dir = newDir;
|
|
429
|
+
packageJsonPath = path.join(dir, "package.json");
|
|
430
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return packageJsonPath;
|
|
435
|
+
}
|
|
436
|
+
function getTailwindcssEntry(name = "tailwindcss", opts) {
|
|
437
|
+
return requireResolve(name, opts);
|
|
438
|
+
}
|
|
439
|
+
function getPackageJsonPath(name, options = {}) {
|
|
440
|
+
const entry = getTailwindcssEntry(name, options);
|
|
441
|
+
if (!entry) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
return searchPackageJSON(entry);
|
|
445
|
+
}
|
|
446
|
+
function getPackageInfoSync(name, options = {}) {
|
|
447
|
+
const packageJsonPath = getPackageJsonPath(name, options);
|
|
448
|
+
if (!packageJsonPath) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
452
|
+
return {
|
|
453
|
+
name,
|
|
454
|
+
version: packageJson.version,
|
|
455
|
+
rootPath: path.dirname(packageJsonPath),
|
|
456
|
+
packageJsonPath,
|
|
457
|
+
packageJson
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
function isObject(val) {
|
|
461
|
+
return val != null && typeof val === "object" && Array.isArray(val) === false;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function monkeyPatchForExposingContextV3(twDir, opt) {
|
|
465
|
+
const processTailwindFeaturesFilePath = path.resolve(twDir, "lib/processTailwindFeatures.js");
|
|
466
|
+
const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath);
|
|
467
|
+
const result = {};
|
|
468
|
+
if (processTailwindFeaturesContent) {
|
|
469
|
+
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext$1(processTailwindFeaturesContent);
|
|
470
|
+
if (!hasPatched && opt.overwrite) {
|
|
471
|
+
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
|
|
472
|
+
encoding: "utf8"
|
|
473
|
+
});
|
|
474
|
+
console.log("patch tailwindcss processTailwindFeatures for return content successfully!");
|
|
475
|
+
}
|
|
476
|
+
result.processTailwindFeatures = code;
|
|
477
|
+
}
|
|
478
|
+
const pluginFilePath = path.resolve(twDir, "lib/plugin.js");
|
|
479
|
+
const indexFilePath = path.resolve(twDir, "lib/index.js");
|
|
480
|
+
const pluginContent = ensureFileContent([pluginFilePath, indexFilePath]);
|
|
481
|
+
if (pluginContent) {
|
|
482
|
+
const { code, hasPatched } = inspectPostcssPlugin$1(pluginContent);
|
|
483
|
+
if (!hasPatched && opt.overwrite) {
|
|
484
|
+
fs.writeFileSync(pluginFilePath, code, {
|
|
485
|
+
encoding: "utf8"
|
|
486
|
+
});
|
|
487
|
+
console.log("patch tailwindcss for expose runtime content successfully!");
|
|
488
|
+
}
|
|
489
|
+
result.plugin = code;
|
|
490
|
+
}
|
|
491
|
+
opt.custom && typeof opt.custom === "function" && opt.custom(twDir, result);
|
|
492
|
+
return result;
|
|
493
|
+
}
|
|
494
|
+
function monkeyPatchForExposingContextV2(twDir, opt) {
|
|
495
|
+
const processTailwindFeaturesFilePath = path.resolve(twDir, "lib/jit/processTailwindFeatures.js");
|
|
496
|
+
const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath);
|
|
497
|
+
const result = {};
|
|
498
|
+
if (processTailwindFeaturesContent) {
|
|
499
|
+
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext(processTailwindFeaturesContent);
|
|
500
|
+
if (!hasPatched && opt.overwrite) {
|
|
501
|
+
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
|
|
502
|
+
encoding: "utf8"
|
|
503
|
+
});
|
|
504
|
+
console.log("patch tailwindcss processTailwindFeatures for return content successfully!");
|
|
505
|
+
}
|
|
506
|
+
result.processTailwindFeatures = code;
|
|
507
|
+
}
|
|
508
|
+
const indexFilePath = path.resolve(twDir, "lib/jit/index.js");
|
|
509
|
+
const pluginContent = ensureFileContent([indexFilePath]);
|
|
510
|
+
if (pluginContent) {
|
|
511
|
+
const { code, hasPatched } = inspectPostcssPlugin(pluginContent);
|
|
512
|
+
if (!hasPatched && opt.overwrite) {
|
|
513
|
+
fs.writeFileSync(indexFilePath, code, {
|
|
514
|
+
encoding: "utf8"
|
|
515
|
+
});
|
|
516
|
+
console.log("patch tailwindcss for expose runtime content successfully!");
|
|
517
|
+
}
|
|
518
|
+
result.plugin = code;
|
|
519
|
+
}
|
|
520
|
+
opt.custom && typeof opt.custom === "function" && opt.custom(twDir, result);
|
|
521
|
+
return result;
|
|
522
|
+
}
|
|
523
|
+
function internalPatch(pkgJsonPath, options) {
|
|
524
|
+
if (pkgJsonPath) {
|
|
525
|
+
const pkgJson = require(pkgJsonPath);
|
|
526
|
+
const twDir = path.dirname(pkgJsonPath);
|
|
527
|
+
if (gte(pkgJson.version, "3.0.0")) {
|
|
528
|
+
options.version = pkgJson.version;
|
|
529
|
+
const result = monkeyPatchForExposingContextV3(twDir, options);
|
|
530
|
+
return result;
|
|
531
|
+
} else if (gte(pkgJson.version, "2.0.0")) {
|
|
532
|
+
options.version = pkgJson.version;
|
|
533
|
+
const result = monkeyPatchForExposingContextV2(twDir, options);
|
|
534
|
+
return result;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const jiti = createJiti(__filename);
|
|
540
|
+
async function processTailwindcss(options) {
|
|
541
|
+
options.cwd = options.cwd ?? process.cwd();
|
|
542
|
+
let config = options.config;
|
|
543
|
+
if (!(typeof options.config === "string" && path.isAbsolute(options.config))) {
|
|
544
|
+
const moduleName = "tailwind";
|
|
545
|
+
const result = await lilconfig("tailwindcss", {
|
|
546
|
+
searchPlaces: [
|
|
547
|
+
`${moduleName}.config.js`,
|
|
548
|
+
`${moduleName}.config.cjs`,
|
|
549
|
+
`${moduleName}.config.mjs`,
|
|
550
|
+
`${moduleName}.config.ts`,
|
|
551
|
+
`${moduleName}.config.cts`,
|
|
552
|
+
`${moduleName}.config.mts`
|
|
553
|
+
],
|
|
554
|
+
loaders: {
|
|
555
|
+
// 默认支持 js 和 cjs 2种格式
|
|
556
|
+
".js": jiti,
|
|
557
|
+
".cjs": jiti,
|
|
558
|
+
".mjs": jiti,
|
|
559
|
+
".ts": jiti,
|
|
560
|
+
".cts": jiti,
|
|
561
|
+
".mts": jiti
|
|
562
|
+
}
|
|
563
|
+
}).search(options.cwd);
|
|
564
|
+
if (!result) {
|
|
565
|
+
throw new Error(`No TailwindCSS Config found in: ${options.cwd}`);
|
|
566
|
+
}
|
|
567
|
+
config = result.filepath;
|
|
568
|
+
}
|
|
569
|
+
return await postcss([
|
|
570
|
+
// eslint-disable-next-line ts/no-var-requires, ts/no-require-imports
|
|
571
|
+
require("tailwindcss")({
|
|
572
|
+
config
|
|
573
|
+
})
|
|
574
|
+
]).process("@tailwind base;@tailwind components;@tailwind utilities;", {
|
|
575
|
+
from: void 0
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function isPlainObject(value) {
|
|
580
|
+
if (value === null || typeof value !== "object") {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
const prototype = Object.getPrototypeOf(value);
|
|
584
|
+
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
if (Symbol.iterator in value) {
|
|
588
|
+
return false;
|
|
589
|
+
}
|
|
590
|
+
if (Symbol.toStringTag in value) {
|
|
591
|
+
return Object.prototype.toString.call(value) === "[object Module]";
|
|
592
|
+
}
|
|
593
|
+
return true;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function _defu(baseObject, defaults, namespace = ".", merger) {
|
|
597
|
+
if (!isPlainObject(defaults)) {
|
|
598
|
+
return _defu(baseObject, {}, namespace, merger);
|
|
599
|
+
}
|
|
600
|
+
const object = Object.assign({}, defaults);
|
|
601
|
+
for (const key in baseObject) {
|
|
602
|
+
if (key === "__proto__" || key === "constructor") {
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
const value = baseObject[key];
|
|
606
|
+
if (value === null || value === void 0) {
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (merger && merger(object, key, value, namespace)) {
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
if (Array.isArray(value) && Array.isArray(object[key])) {
|
|
613
|
+
object[key] = [...value, ...object[key]];
|
|
614
|
+
} else if (isPlainObject(value) && isPlainObject(object[key])) {
|
|
615
|
+
object[key] = _defu(
|
|
616
|
+
value,
|
|
617
|
+
object[key],
|
|
618
|
+
(namespace ? `${namespace}.` : "") + key.toString(),
|
|
619
|
+
merger
|
|
620
|
+
);
|
|
621
|
+
} else {
|
|
622
|
+
object[key] = value;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return object;
|
|
626
|
+
}
|
|
627
|
+
function createDefu(merger) {
|
|
628
|
+
return (...arguments_) => (
|
|
629
|
+
// eslint-disable-next-line unicorn/no-array-reduce
|
|
630
|
+
arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
const defu = createDefu();
|
|
634
|
+
|
|
635
|
+
function getDefaultPatchOptions() {
|
|
636
|
+
return {
|
|
637
|
+
overwrite: true
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
function getPatchOptions(options = {}) {
|
|
641
|
+
return defu(
|
|
642
|
+
options,
|
|
643
|
+
{
|
|
644
|
+
basedir: process.cwd()
|
|
645
|
+
},
|
|
646
|
+
getDefaultPatchOptions()
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
var __defProp = Object.defineProperty;
|
|
651
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
652
|
+
var __publicField = (obj, key, value) => {
|
|
653
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
654
|
+
return value;
|
|
655
|
+
};
|
|
656
|
+
class TailwindcssPatcher {
|
|
657
|
+
constructor(options = {}) {
|
|
658
|
+
__publicField(this, "rawOptions");
|
|
659
|
+
__publicField(this, "cacheOptions");
|
|
660
|
+
__publicField(this, "patchOptions");
|
|
661
|
+
__publicField(this, "patch");
|
|
662
|
+
__publicField(this, "cacheManager");
|
|
663
|
+
__publicField(this, "packageInfo");
|
|
664
|
+
__publicField(this, "majorVersion");
|
|
665
|
+
this.rawOptions = options;
|
|
666
|
+
this.cacheOptions = getCacheOptions(options.cache);
|
|
667
|
+
this.patchOptions = getPatchOptions(options.patch);
|
|
668
|
+
this.cacheManager = new CacheManager(this.cacheOptions);
|
|
669
|
+
this.packageInfo = getPackageInfoSync("tailwindcss", { basedir: this.patchOptions.basedir });
|
|
670
|
+
if (this.packageInfo && this.packageInfo.version) {
|
|
671
|
+
this.majorVersion = Number.parseInt(this.packageInfo.version[0]);
|
|
672
|
+
}
|
|
673
|
+
this.patch = () => {
|
|
674
|
+
try {
|
|
675
|
+
return internalPatch(this.packageInfo?.packageJsonPath, this.patchOptions);
|
|
676
|
+
} catch (error) {
|
|
677
|
+
console.warn(`patch tailwindcss failed: ${error.message}`);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
setCache(set) {
|
|
682
|
+
if (this.cacheOptions.enable) {
|
|
683
|
+
return this.cacheManager.write(set);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
getCache() {
|
|
687
|
+
return this.cacheManager.read();
|
|
688
|
+
}
|
|
689
|
+
getContexts() {
|
|
690
|
+
if (this.packageInfo) {
|
|
691
|
+
const distPath = path.join(this.packageInfo.rootPath, "lib");
|
|
692
|
+
let injectFilePath;
|
|
693
|
+
if (this.majorVersion === 2) {
|
|
694
|
+
injectFilePath = path.join(distPath, "jit/index.js");
|
|
695
|
+
} else {
|
|
696
|
+
injectFilePath = path.join(distPath, "plugin.js");
|
|
697
|
+
if (!fs.existsSync(injectFilePath)) {
|
|
698
|
+
injectFilePath = path.join(distPath, "index.js");
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
if (injectFilePath) {
|
|
702
|
+
const mo = require(injectFilePath);
|
|
703
|
+
if (mo.contextRef) {
|
|
704
|
+
return mo.contextRef.value;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return [];
|
|
709
|
+
}
|
|
710
|
+
getClassCaches() {
|
|
711
|
+
const contexts = this.getContexts();
|
|
712
|
+
return contexts.filter((x) => isObject(x)).map((x) => x.classCache);
|
|
713
|
+
}
|
|
714
|
+
getClassCacheSet(options) {
|
|
715
|
+
const classCaches = this.getClassCaches();
|
|
716
|
+
const classSet = /* @__PURE__ */ new Set();
|
|
717
|
+
for (const classCacheMap of classCaches) {
|
|
718
|
+
const keys = classCacheMap.keys();
|
|
719
|
+
for (const key of keys) {
|
|
720
|
+
const v = key.toString();
|
|
721
|
+
if (options?.removeUniversalSelector && v === "*") {
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
classSet.add(v);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
return classSet;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
|
|
731
|
+
*/
|
|
732
|
+
getClassSet(options) {
|
|
733
|
+
const { cacheStrategy = this.cacheOptions.strategy ?? "merge", removeUniversalSelector = true } = options ?? {};
|
|
734
|
+
const set = this.getClassCacheSet({
|
|
735
|
+
removeUniversalSelector
|
|
736
|
+
});
|
|
737
|
+
if (cacheStrategy === "overwrite") {
|
|
738
|
+
set.size > 0 && this.setCache(set);
|
|
739
|
+
} else if (cacheStrategy === "merge") {
|
|
740
|
+
const cacheSet = this.getCache();
|
|
741
|
+
if (cacheSet) {
|
|
742
|
+
for (const x of cacheSet) {
|
|
743
|
+
set.add(x);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
this.setCache(set);
|
|
747
|
+
}
|
|
748
|
+
return set;
|
|
749
|
+
}
|
|
750
|
+
async extract(options) {
|
|
751
|
+
const { output, tailwindcss } = options ?? {};
|
|
752
|
+
if (output && tailwindcss) {
|
|
753
|
+
const { removeUniversalSelector, filename, loose } = output;
|
|
754
|
+
await processTailwindcss(tailwindcss);
|
|
755
|
+
const set = this.getClassSet({
|
|
756
|
+
removeUniversalSelector
|
|
757
|
+
});
|
|
758
|
+
if (filename) {
|
|
759
|
+
await ensureDir(path.dirname(filename));
|
|
760
|
+
const classList = [...set];
|
|
761
|
+
fs.writeFileSync(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
|
|
762
|
+
return filename;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
export { CacheManager as C, TailwindcssPatcher as T, inspectPostcssPlugin$1 as a, monkeyPatchForExposingContextV2 as b, internalPatch as c, getCacheOptions as d, ensureFileContent as e, ensureDir as f, getPatchOptions as g, getPackageInfoSync as h, inspectProcessTailwindFeaturesReturnContext$1 as i, isObject as j, monkeyPatchForExposingContextV3 as m, requireResolve as r };
|