weapp-tailwindcss 3.7.0 → 3.8.0-alpha.1

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.
Files changed (149) hide show
  1. package/README.md +1 -23
  2. package/dist/chunk-2STNBSVB.js +216 -0
  3. package/dist/chunk-35CYTBPM.js +26 -0
  4. package/dist/chunk-6FNC3XMI.js +45 -0
  5. package/dist/chunk-6Z776ZWK.js +43 -0
  6. package/dist/chunk-75EWCRQW.mjs +13 -0
  7. package/dist/chunk-AB467PG7.mjs +43 -0
  8. package/dist/chunk-AXBFBHS2.mjs +45 -0
  9. package/dist/chunk-EODGLB63.mjs +173 -0
  10. package/dist/chunk-FG63RW47.mjs +7106 -0
  11. package/dist/chunk-HBQASQKY.js +13 -0
  12. package/dist/chunk-IETFIAKJ.js +152 -0
  13. package/dist/chunk-KE7J6XFD.mjs +537 -0
  14. package/dist/chunk-LQN2VTMU.js +537 -0
  15. package/dist/chunk-LWRZXI5T.js +1003 -0
  16. package/dist/chunk-MUJ65IQ3.mjs +26 -0
  17. package/dist/chunk-MWTUP5KG.mjs +1003 -0
  18. package/dist/chunk-NLFQ7XRK.mjs +152 -0
  19. package/dist/chunk-QEEWOML6.mjs +216 -0
  20. package/dist/chunk-RFNUATFD.js +24 -0
  21. package/dist/chunk-U7O2XXYR.mjs +24 -0
  22. package/dist/chunk-VKSQ6BRD.mjs +27 -0
  23. package/dist/chunk-XSZXPWWL.js +27 -0
  24. package/dist/chunk-YSEHQ3FF.js +173 -0
  25. package/dist/chunk-YYT4NLHK.js +7109 -0
  26. package/dist/cli.d.mts +2 -0
  27. package/dist/cli.d.ts +2 -1
  28. package/dist/cli.js +23 -3400
  29. package/dist/cli.mjs +28 -3392
  30. package/dist/core.d.mts +27 -0
  31. package/dist/core.d.ts +23 -4
  32. package/dist/core.js +34 -56
  33. package/dist/core.mjs +36 -54
  34. package/dist/css-macro/postcss.d.mts +7 -0
  35. package/dist/css-macro/postcss.d.ts +5 -3
  36. package/dist/css-macro/postcss.js +54 -45
  37. package/dist/css-macro/postcss.mjs +54 -45
  38. package/dist/css-macro.d.mts +18 -0
  39. package/dist/css-macro.d.ts +18 -0
  40. package/dist/css-macro.js +40 -0
  41. package/dist/css-macro.mjs +38 -0
  42. package/dist/defaults.d.mts +16 -0
  43. package/dist/defaults.d.ts +16 -2
  44. package/dist/defaults.js +5 -9
  45. package/dist/defaults.mjs +8 -4
  46. package/dist/escape.d.mts +19 -0
  47. package/dist/escape.d.ts +19 -4
  48. package/dist/escape.js +11 -0
  49. package/dist/escape.mjs +11 -0
  50. package/dist/gulp.d.mts +26 -0
  51. package/dist/gulp.d.ts +26 -1
  52. package/dist/gulp.js +11 -36
  53. package/dist/gulp.mjs +14 -31
  54. package/dist/index-g4epDIUu.d.mts +534 -0
  55. package/dist/index-g4epDIUu.d.ts +534 -0
  56. package/dist/index.d.mts +17 -0
  57. package/dist/index.d.ts +17 -4
  58. package/dist/index.js +22 -44
  59. package/dist/index.mjs +22 -34
  60. package/dist/postcss.d.mts +28 -0
  61. package/dist/postcss.d.ts +28 -1
  62. package/dist/postcss.js +9 -24
  63. package/dist/postcss.mjs +16 -8
  64. package/dist/replace.d.mts +22 -0
  65. package/dist/replace.d.ts +22 -3
  66. package/dist/replace.js +33 -50
  67. package/dist/replace.mjs +32 -25
  68. package/dist/shared-5ArEo9kL.d.mts +5 -0
  69. package/dist/shared-DI2Qz1ou.d.ts +5 -0
  70. package/dist/vite.d.mts +22 -0
  71. package/dist/vite.d.ts +22 -1
  72. package/dist/vite.js +11 -34
  73. package/dist/vite.mjs +14 -29
  74. package/dist/weapp-tw-runtime-loader.d.mts +7 -0
  75. package/dist/{bundlers/webpack/loaders/weapp-tw-runtime-loader.d.ts → weapp-tw-runtime-loader.d.ts} +4 -2
  76. package/dist/weapp-tw-runtime-loader.js +12 -12
  77. package/dist/weapp-tw-runtime-loader.mjs +12 -8
  78. package/dist/webpack.d.mts +26 -0
  79. package/dist/webpack.d.ts +26 -1
  80. package/dist/webpack.js +11 -35
  81. package/dist/webpack.mjs +14 -30
  82. package/dist/webpack4.d.mts +27 -0
  83. package/dist/webpack4.d.ts +27 -1
  84. package/dist/webpack4.js +204 -192
  85. package/dist/webpack4.mjs +210 -189
  86. package/package.json +29 -19
  87. package/dist/babel/index.d.ts +0 -5
  88. package/dist/bundlers/gulp/index.d.ts +0 -7
  89. package/dist/bundlers/vite/index.d.ts +0 -3
  90. package/dist/bundlers/webpack/BaseUnifiedPlugin/v4.d.ts +0 -8
  91. package/dist/bundlers/webpack/BaseUnifiedPlugin/v5.d.ts +0 -8
  92. package/dist/bundlers/webpack/index.d.ts +0 -1
  93. package/dist/cache/index.d.ts +0 -31
  94. package/dist/constants-CFM1J-h1.js +0 -42
  95. package/dist/constants-Dy2Dd3Fz.mjs +0 -36
  96. package/dist/constants.d.ts +0 -7
  97. package/dist/css-macro/constants.d.ts +0 -14
  98. package/dist/css-macro/index.d.ts +0 -15
  99. package/dist/css-macro/index.js +0 -36
  100. package/dist/css-macro/index.mjs +0 -30
  101. package/dist/debug/index.d.ts +0 -2
  102. package/dist/defaults-5A8JI2hW.mjs +0 -139
  103. package/dist/defaults-CdqzSWsR.js +0 -145
  104. package/dist/defu-Cdz2PomB.js +0 -60
  105. package/dist/defu-n46TJT6t.mjs +0 -57
  106. package/dist/env.d.ts +0 -2
  107. package/dist/extractors/split.d.ts +0 -3
  108. package/dist/index-BEIjgKpF.mjs +0 -11
  109. package/dist/index-BF8k8rHr.mjs +0 -517
  110. package/dist/index-BTXkyxen.js +0 -136
  111. package/dist/index-C0QsyqD6.mjs +0 -134
  112. package/dist/index-ClSBbhnm.js +0 -130
  113. package/dist/index-DoMb0-xW.mjs +0 -124
  114. package/dist/index-HC-Qkn0i.js +0 -17
  115. package/dist/index-h2-ZMO3S.js +0 -530
  116. package/dist/init.d.ts +0 -34
  117. package/dist/js/ast-grep.d.ts +0 -1
  118. package/dist/js/babel.d.ts +0 -3
  119. package/dist/js/handlers.d.ts +0 -11
  120. package/dist/js/index.d.ts +0 -7
  121. package/dist/logger.d.ts +0 -1
  122. package/dist/mangle/index.d.ts +0 -8
  123. package/dist/npm.d.ts +0 -22
  124. package/dist/options-Bv-1GQJY.js +0 -891
  125. package/dist/options-yXyCFCtr.mjs +0 -858
  126. package/dist/options.d.ts +0 -2
  127. package/dist/postcss/cssVars.d.ts +0 -5
  128. package/dist/postcss/index.d.ts +0 -4
  129. package/dist/postcss/mp.d.ts +0 -8
  130. package/dist/postcss/plugins/ctx.d.ts +0 -6
  131. package/dist/postcss/plugins/index.d.ts +0 -7
  132. package/dist/postcss/plugins/post.d.ts +0 -5
  133. package/dist/postcss/plugins/pre.d.ts +0 -5
  134. package/dist/postcss/preflight.d.ts +0 -3
  135. package/dist/postcss/selectorParser.d.ts +0 -6
  136. package/dist/postcss/shared.d.ts +0 -3
  137. package/dist/postcss/symbols.d.ts +0 -1
  138. package/dist/reg.d.ts +0 -1
  139. package/dist/shared-COWjpokr.mjs +0 -25
  140. package/dist/shared-r_GpWheO.js +0 -28
  141. package/dist/tailwindcss/patcher.d.ts +0 -3
  142. package/dist/types.d.ts +0 -165
  143. package/dist/utils.d.ts +0 -18
  144. package/dist/v5-DwFXG7m_.js +0 -181
  145. package/dist/v5-N5BYJ5JY.mjs +0 -174
  146. package/dist/wxml/Tokenizer.d.ts +0 -31
  147. package/dist/wxml/index.d.ts +0 -2
  148. package/dist/wxml/shared.d.ts +0 -2
  149. package/dist/wxml/utils.d.ts +0 -10
@@ -0,0 +1,1003 @@
1
+ import {
2
+ getDefaultOptions
3
+ } from "./chunk-FG63RW47.mjs";
4
+ import {
5
+ defuOverrideArray,
6
+ isMap
7
+ } from "./chunk-MUJ65IQ3.mjs";
8
+ import {
9
+ getPlugins
10
+ } from "./chunk-KE7J6XFD.mjs";
11
+ import {
12
+ replaceWxml
13
+ } from "./chunk-U7O2XXYR.mjs";
14
+ import {
15
+ __async,
16
+ __spreadProps,
17
+ __spreadValues
18
+ } from "./chunk-AXBFBHS2.mjs";
19
+
20
+ // src/logger.ts
21
+ import { logger } from "@weapp-tailwindcss/logger";
22
+
23
+ // src/cache/index.ts
24
+ import { LRUCache } from "lru-cache";
25
+ import md5 from "md5";
26
+ function createCache(options) {
27
+ const disabled = options === false;
28
+ const hashMap = /* @__PURE__ */ new Map();
29
+ const instance = new LRUCache({
30
+ // 可能会添加和删除一些页面和组件, 先设定 1024 吧
31
+ max: 1024,
32
+ ttl: 0,
33
+ ttlAutopurge: false
34
+ });
35
+ return {
36
+ hashMap,
37
+ instance,
38
+ hasHashKey(key) {
39
+ return hashMap.has(key);
40
+ },
41
+ getHashValue(key) {
42
+ return hashMap.get(key);
43
+ },
44
+ setHashValue(key, value) {
45
+ return hashMap.set(key, value);
46
+ },
47
+ get(key) {
48
+ return instance.get(key);
49
+ },
50
+ set(key, value) {
51
+ return instance.set(key, value);
52
+ },
53
+ computeHash(message) {
54
+ return md5(message);
55
+ },
56
+ calcHashValueChanged(key, hash) {
57
+ const hit = this.getHashValue(key);
58
+ if (hit) {
59
+ this.setHashValue(key, {
60
+ // new file should be changed
61
+ changed: hash !== hit.hash,
62
+ // new hash
63
+ hash
64
+ });
65
+ } else {
66
+ this.setHashValue(key, {
67
+ // new file should be changed
68
+ changed: true,
69
+ hash
70
+ });
71
+ }
72
+ return this;
73
+ },
74
+ has(key) {
75
+ return instance.has(key);
76
+ },
77
+ process(key, callback, fallback) {
78
+ return __async(this, null, function* () {
79
+ if (disabled) {
80
+ const res = yield fallback();
81
+ if (res) {
82
+ this.set(res.key, res.source);
83
+ }
84
+ } else {
85
+ const hit = this.getHashValue(key);
86
+ if (hit && !hit.changed) {
87
+ const returnFlag = yield callback();
88
+ if (returnFlag !== false) {
89
+ return;
90
+ }
91
+ }
92
+ const res = yield fallback();
93
+ if (res) {
94
+ this.set(res.key, res.source);
95
+ }
96
+ }
97
+ });
98
+ }
99
+ };
100
+ }
101
+
102
+ // src/js/ast-grep.ts
103
+ import MagicString from "magic-string";
104
+
105
+ // src/js/handlers.ts
106
+ import { jsStringEscape } from "@ast-core/escape";
107
+ import { escapeStringRegexp } from "@weapp-core/regex";
108
+
109
+ // src/extractors/split.ts
110
+ var validateFilterRE = /[\w\u00A0-\uFFFF%-?]/;
111
+ function isValidSelector(selector = "") {
112
+ return validateFilterRE.test(selector);
113
+ }
114
+ function splitCode(code, allowDoubleQuotes = false) {
115
+ const splitter = allowDoubleQuotes ? /\s+/ : /\s+|"/;
116
+ return code.split(splitter).filter((element) => isValidSelector(element));
117
+ }
118
+
119
+ // src/utils/decode.ts
120
+ function decodeUnicode2(input) {
121
+ try {
122
+ return JSON.parse(`"${input}"`);
123
+ } catch (_error) {
124
+ return input;
125
+ }
126
+ }
127
+
128
+ // src/js/handlers.ts
129
+ function replaceHandleValue(str, node, options, ms, offset = 0) {
130
+ const {
131
+ classNameSet: set,
132
+ escapeMap,
133
+ mangleContext: ctx,
134
+ needEscaped = false,
135
+ jsPreserveClass,
136
+ arbitraryValues,
137
+ always,
138
+ unescapeUnicode
139
+ } = options;
140
+ const allowDoubleQuotes = arbitraryValues == null ? void 0 : arbitraryValues.allowDoubleQuotes;
141
+ let rawStr = str;
142
+ let needUpdate = false;
143
+ if (unescapeUnicode && rawStr.includes("\\u")) {
144
+ rawStr = decodeUnicode2(rawStr);
145
+ }
146
+ const arr = splitCode(rawStr, allowDoubleQuotes);
147
+ for (const v of arr) {
148
+ if (always || set && set.has(v) && !(jsPreserveClass == null ? void 0 : jsPreserveClass(v))) {
149
+ let ignoreFlag = false;
150
+ if (Array.isArray(node.leadingComments)) {
151
+ ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes("weapp-tw") && x.value.includes("ignore")) > -1;
152
+ }
153
+ if (!ignoreFlag) {
154
+ if (ctx) {
155
+ rawStr = ctx.jsHandler(rawStr);
156
+ }
157
+ rawStr = rawStr.replace(
158
+ new RegExp(escapeStringRegexp(v)),
159
+ replaceWxml(v, {
160
+ escapeMap
161
+ })
162
+ );
163
+ needUpdate = true;
164
+ }
165
+ }
166
+ }
167
+ if (needUpdate && typeof node.start === "number" && typeof node.end === "number") {
168
+ const start = node.start + offset;
169
+ const end = node.end - offset;
170
+ if (start < end && str !== rawStr) {
171
+ const content = needEscaped ? jsStringEscape(rawStr) : rawStr;
172
+ ms.update(start, end, content);
173
+ }
174
+ }
175
+ return rawStr;
176
+ }
177
+
178
+ // src/js/ast-grep.ts
179
+ function getAstGrep() {
180
+ return __async(this, null, function* () {
181
+ try {
182
+ const mod = yield import("@ast-grep/napi");
183
+ return mod;
184
+ } catch (error) {
185
+ logger.warn("\u8BF7\u5148\u5B89\u88C5 `@ast-grep/napi` , \u5B89\u88C5\u5B8C\u6210\u540E\u518D\u5C1D\u8BD5\u8FD0\u884C\uFF01");
186
+ throw error;
187
+ }
188
+ });
189
+ }
190
+ function isRangeWithinArray(ranges, targetRange) {
191
+ const { start: targetStart, end: targetEnd } = targetRange;
192
+ return ranges.some(({ start, end }) => {
193
+ return start.index <= targetStart.index && end.index >= targetEnd.index;
194
+ });
195
+ }
196
+ function astGrepUpdateString(ast, options, ms) {
197
+ return __async(this, null, function* () {
198
+ var _a;
199
+ const { Lang, kind } = yield getAstGrep();
200
+ const callExpressionNodes = ast.findAll(kind(Lang.JavaScript, "call_expression"));
201
+ let chooseNodeRanges = [];
202
+ if (Array.isArray(options.ignoreCallExpressionIdentifiers) && options.ignoreCallExpressionIdentifiers.length) {
203
+ chooseNodeRanges = callExpressionNodes.filter((x) => {
204
+ var _a2;
205
+ const id = x.child(0);
206
+ return (id == null ? void 0 : id.kindToRefine) === "identifier" && ((_a2 = options.ignoreCallExpressionIdentifiers) == null ? void 0 : _a2.includes(id.text()));
207
+ }).map((x) => x == null ? void 0 : x.range());
208
+ }
209
+ const nodes = ast.findAll(kind(Lang.JavaScript, "string"));
210
+ for (const node of nodes) {
211
+ const range = node.range();
212
+ if (isRangeWithinArray(chooseNodeRanges, range)) {
213
+ continue;
214
+ }
215
+ const text = node.text();
216
+ replaceHandleValue(
217
+ text.slice(1, -1),
218
+ {
219
+ end: range.end.index - 1,
220
+ start: range.start.index + 1
221
+ },
222
+ __spreadProps(__spreadValues({}, options), {
223
+ unescapeUnicode: true
224
+ }),
225
+ ms,
226
+ 0
227
+ );
228
+ }
229
+ const templateNodes = ast.findAll(kind(Lang.JavaScript, "template_string"));
230
+ for (const node of templateNodes) {
231
+ const p = node.parent();
232
+ if (p && p.kind() === "call_expression") {
233
+ const c = p.child(0);
234
+ if (c && c.kind() === "identifier" && ((_a = options.ignoreTaggedTemplateExpressionIdentifiers) == null ? void 0 : _a.includes(c.text()))) {
235
+ continue;
236
+ }
237
+ }
238
+ const fragments = node.findAll(kind(Lang.JavaScript, "string_fragment"));
239
+ for (const fragment of fragments) {
240
+ const range = fragment.range();
241
+ if (isRangeWithinArray(chooseNodeRanges, range)) {
242
+ continue;
243
+ }
244
+ const text = fragment.text();
245
+ replaceHandleValue(
246
+ text,
247
+ {
248
+ end: range.end.index,
249
+ start: range.start.index
250
+ },
251
+ __spreadProps(__spreadValues({}, options), {
252
+ unescapeUnicode: true
253
+ }),
254
+ ms,
255
+ 0
256
+ );
257
+ }
258
+ }
259
+ });
260
+ }
261
+ function jsHandlerAsync(rawSource, options) {
262
+ return __async(this, null, function* () {
263
+ const ms = new MagicString(rawSource);
264
+ const { parseAsync, Lang } = yield getAstGrep();
265
+ let ast;
266
+ try {
267
+ const root = yield parseAsync(Lang.JavaScript, rawSource);
268
+ ast = root.root();
269
+ } catch (e) {
270
+ return {
271
+ code: rawSource
272
+ };
273
+ }
274
+ yield astGrepUpdateString(ast, options, ms);
275
+ return {
276
+ code: ms.toString()
277
+ };
278
+ });
279
+ }
280
+
281
+ // src/js/babel.ts
282
+ import { jsStringEscape as jsStringEscape2 } from "@ast-core/escape";
283
+ import MagicString2 from "magic-string";
284
+
285
+ // src/babel/index.ts
286
+ import _babelGenerate from "@babel/generator";
287
+ import _babelTraverse from "@babel/traverse";
288
+ import { parse, parseExpression } from "@babel/parser";
289
+ function _interopDefaultCompat(e) {
290
+ return e && typeof e === "object" && "default" in e ? e.default : e;
291
+ }
292
+ var generate = _interopDefaultCompat(_babelGenerate);
293
+ var traverse = _interopDefaultCompat(_babelTraverse);
294
+
295
+ // src/js/babel.ts
296
+ function isEvalPath(p) {
297
+ if (p.isCallExpression()) {
298
+ const calleePath = p.get("callee");
299
+ return calleePath.isIdentifier() && calleePath.node.name === "eval";
300
+ }
301
+ return false;
302
+ }
303
+ var ignoreFlagMap = /* @__PURE__ */ new WeakMap();
304
+ function jsHandler(rawSource, options) {
305
+ const ms = new MagicString2(rawSource);
306
+ let ast;
307
+ try {
308
+ ast = parse(rawSource, options.babelParserOptions);
309
+ } catch (error) {
310
+ return {
311
+ code: rawSource,
312
+ error
313
+ };
314
+ }
315
+ let ignoreFlag = false;
316
+ const traverseOptions = {
317
+ StringLiteral: {
318
+ enter(p) {
319
+ var _a;
320
+ if (ignoreFlag) {
321
+ return;
322
+ }
323
+ if (isEvalPath(p.parentPath)) {
324
+ return;
325
+ }
326
+ const n = p.node;
327
+ replaceHandleValue(
328
+ n.value,
329
+ n,
330
+ __spreadProps(__spreadValues({}, options), {
331
+ needEscaped: (_a = options.needEscaped) != null ? _a : true
332
+ }),
333
+ ms,
334
+ 1
335
+ );
336
+ }
337
+ },
338
+ TemplateElement: {
339
+ enter(p) {
340
+ var _a;
341
+ if (ignoreFlag) {
342
+ return;
343
+ }
344
+ if (p.parentPath.isTemplateLiteral()) {
345
+ if (p.parentPath.parentPath.isTaggedTemplateExpression() && p.parentPath.parentPath.get("tag").isIdentifier() && ((_a = options.ignoreTaggedTemplateExpressionIdentifiers) == null ? void 0 : _a.includes(p.parentPath.parentPath.get("tag").node.name)) || isEvalPath(p.parentPath.parentPath)) {
346
+ return;
347
+ }
348
+ }
349
+ const n = p.node;
350
+ replaceHandleValue(
351
+ n.value.raw,
352
+ n,
353
+ __spreadProps(__spreadValues({}, options), {
354
+ needEscaped: false
355
+ }),
356
+ ms,
357
+ 0
358
+ );
359
+ }
360
+ },
361
+ CallExpression: {
362
+ enter(p) {
363
+ var _a;
364
+ if (isEvalPath(p)) {
365
+ p.traverse({
366
+ StringLiteral: {
367
+ enter(s) {
368
+ const res = jsHandler(s.node.value, __spreadProps(__spreadValues({}, options), {
369
+ needEscaped: false,
370
+ generateMap: false
371
+ }));
372
+ if (res.code) {
373
+ const node = s.node;
374
+ if (typeof node.start === "number" && typeof node.end === "number") {
375
+ const start = node.start + 1;
376
+ const end = node.end - 1;
377
+ if (start < end && s.node.value !== res.code) {
378
+ ms.update(start, end, jsStringEscape2(res.code));
379
+ node.value = res.code;
380
+ }
381
+ }
382
+ }
383
+ }
384
+ },
385
+ TemplateElement: {
386
+ enter(s) {
387
+ const res = jsHandler(s.node.value.raw, __spreadProps(__spreadValues({}, options), {
388
+ generateMap: false
389
+ }));
390
+ if (res.code) {
391
+ const node = s.node;
392
+ if (typeof node.start === "number" && typeof node.end === "number") {
393
+ const start = node.start;
394
+ const end = node.end;
395
+ if (start < end && s.node.value.raw !== res.code) {
396
+ ms.update(start, end, res.code);
397
+ s.node.value.raw = res.code;
398
+ }
399
+ }
400
+ }
401
+ }
402
+ }
403
+ });
404
+ } else if (p.get("callee").isIdentifier() && ((_a = options.ignoreCallExpressionIdentifiers) == null ? void 0 : _a.includes(p.get("callee").node.name))) {
405
+ ignoreFlag = true;
406
+ ignoreFlagMap.set(p, true);
407
+ }
408
+ },
409
+ exit(p) {
410
+ const flag = ignoreFlagMap.get(p);
411
+ if (flag) {
412
+ ignoreFlag = false;
413
+ ignoreFlagMap.delete(p);
414
+ }
415
+ }
416
+ }
417
+ };
418
+ traverse(ast, traverseOptions);
419
+ return {
420
+ code: ms.toString(),
421
+ get map() {
422
+ return ms.generateMap();
423
+ }
424
+ };
425
+ }
426
+
427
+ // src/js/index.ts
428
+ function createJsHandler(options) {
429
+ const {
430
+ mangleContext,
431
+ arbitraryValues,
432
+ escapeMap,
433
+ jsPreserveClass,
434
+ generateMap,
435
+ jsAstTool,
436
+ babelParserOptions,
437
+ ignoreCallExpressionIdentifiers,
438
+ ignoreTaggedTemplateExpressionIdentifiers
439
+ } = options;
440
+ function _jsHandler(rawSource, set, options2) {
441
+ const opts = defuOverrideArray(options2, {
442
+ classNameSet: set,
443
+ escapeMap,
444
+ arbitraryValues,
445
+ mangleContext,
446
+ jsPreserveClass,
447
+ generateMap,
448
+ jsAstTool,
449
+ babelParserOptions,
450
+ ignoreCallExpressionIdentifiers,
451
+ ignoreTaggedTemplateExpressionIdentifiers
452
+ });
453
+ if (opts.jsAstTool === "ast-grep") {
454
+ return jsHandlerAsync(rawSource, opts);
455
+ }
456
+ return jsHandler(rawSource, opts);
457
+ }
458
+ function sync(rawSource, set, options2) {
459
+ const opts = defuOverrideArray(options2, {
460
+ classNameSet: set,
461
+ escapeMap,
462
+ arbitraryValues,
463
+ mangleContext,
464
+ jsPreserveClass,
465
+ generateMap,
466
+ jsAstTool,
467
+ babelParserOptions,
468
+ ignoreCallExpressionIdentifiers,
469
+ ignoreTaggedTemplateExpressionIdentifiers
470
+ });
471
+ return jsHandler(rawSource, opts);
472
+ }
473
+ _jsHandler.sync = sync;
474
+ return _jsHandler;
475
+ }
476
+
477
+ // src/mangle/index.ts
478
+ import { ClassGenerator, defaultMangleClassFilter } from "@tailwindcss-mangle/shared";
479
+ import { escapeStringRegexp as escapeStringRegexp2 } from "@weapp-core/regex";
480
+ function getSelf(x) {
481
+ return x;
482
+ }
483
+ var defaultMangleContext = {
484
+ rawOptions: false,
485
+ runtimeSet: /* @__PURE__ */ new Set(),
486
+ classGenerator: new ClassGenerator(),
487
+ filter: defaultMangleClassFilter,
488
+ cssHandler: getSelf,
489
+ jsHandler: getSelf,
490
+ wxmlHandler: getSelf
491
+ };
492
+ function useMangleStore() {
493
+ const ctx = Object.assign({}, defaultMangleContext);
494
+ function resetMangle() {
495
+ return Object.assign(ctx, defaultMangleContext);
496
+ }
497
+ function handleValue(rawSource) {
498
+ const arr = splitCode(rawSource);
499
+ for (const x of arr) {
500
+ if (ctx.runtimeSet.has(x)) {
501
+ rawSource = rawSource.replace(new RegExp(escapeStringRegexp2(x)), ctx.classGenerator.generateClassName(x).name);
502
+ }
503
+ }
504
+ return rawSource;
505
+ }
506
+ function initMangle(options) {
507
+ var _a;
508
+ ctx.rawOptions = options;
509
+ if (options) {
510
+ if (options === true) {
511
+ options = {
512
+ classGenerator: {},
513
+ mangleClassFilter: defaultMangleClassFilter
514
+ };
515
+ }
516
+ ctx.classGenerator = new ClassGenerator(options.classGenerator);
517
+ ctx.filter = (_a = options.mangleClassFilter) != null ? _a : defaultMangleClassFilter;
518
+ ctx.jsHandler = (rawSource) => {
519
+ return handleValue(rawSource);
520
+ };
521
+ ctx.cssHandler = (rawSource) => {
522
+ return handleValue(rawSource);
523
+ };
524
+ ctx.wxmlHandler = (rawSource) => {
525
+ return handleValue(rawSource);
526
+ };
527
+ }
528
+ }
529
+ function setMangleRuntimeSet(runtimeSet) {
530
+ const newSet = /* @__PURE__ */ new Set();
531
+ for (const c of runtimeSet) {
532
+ if (ctx.filter(c)) {
533
+ newSet.add(c);
534
+ }
535
+ }
536
+ ctx.runtimeSet = newSet;
537
+ }
538
+ return {
539
+ mangleContext: ctx,
540
+ resetMangle,
541
+ initMangle,
542
+ setMangleRuntimeSet
543
+ };
544
+ }
545
+
546
+ // src/postcss/index.ts
547
+ import postcss from "postcss";
548
+ function styleHandler(rawSource, options) {
549
+ var _a, _b;
550
+ return postcss(getPlugins(options)).process(
551
+ rawSource,
552
+ (_b = (_a = options.postcssOptions) == null ? void 0 : _a.options) != null ? _b : {
553
+ from: void 0
554
+ }
555
+ ).async();
556
+ }
557
+ function createStyleHandler(options) {
558
+ return (rawSource, opt) => {
559
+ return styleHandler(
560
+ rawSource,
561
+ defuOverrideArray(opt, options)
562
+ );
563
+ };
564
+ }
565
+
566
+ // src/postcss/preflight.ts
567
+ function createInjectPreflight(options) {
568
+ const result = [];
569
+ if (options && typeof options === "object") {
570
+ const entries = Object.entries(options);
571
+ for (const [prop, value] of entries) {
572
+ if (value !== false) {
573
+ result.push({
574
+ prop,
575
+ value: value.toString()
576
+ });
577
+ }
578
+ }
579
+ }
580
+ return () => {
581
+ return result;
582
+ };
583
+ }
584
+
585
+ // src/tailwindcss/patcher.ts
586
+ import path from "node:path";
587
+ import process from "node:process";
588
+ import { TailwindcssPatcher } from "tailwindcss-patch";
589
+ function createTailwindcssPatcher(basedir, cacheDir, supportCustomLengthUnitsPatch) {
590
+ const cache = {};
591
+ if (cacheDir) {
592
+ if (path.isAbsolute(cacheDir)) {
593
+ cache.dir = cacheDir;
594
+ } else if (basedir) {
595
+ cache.dir = path.resolve(basedir, cacheDir);
596
+ } else {
597
+ cache.dir = path.resolve(process.cwd(), cacheDir);
598
+ }
599
+ }
600
+ return new TailwindcssPatcher({
601
+ cache,
602
+ patch: {
603
+ basedir,
604
+ applyPatches: {
605
+ exportContext: true,
606
+ extendLengthUnits: supportCustomLengthUnitsPatch
607
+ }
608
+ }
609
+ });
610
+ }
611
+
612
+ // src/wxml/utils.ts
613
+ import * as t from "@babel/types";
614
+ import { Parser } from "htmlparser2";
615
+ import MagicString3 from "magic-string";
616
+
617
+ // src/wxml/Tokenizer.ts
618
+ var Tokenizer = class {
619
+ constructor() {
620
+ this.reset();
621
+ }
622
+ processChar(char, index) {
623
+ switch (this.state) {
624
+ case 0 /* START */:
625
+ if (char === " ") {
626
+ } else if (char === "{") {
627
+ this.state = 2 /* OPEN_BRACE */;
628
+ this.bufferStartIndex = index;
629
+ this.buffer += char;
630
+ this.expressionBuffer = char;
631
+ this.expressionStartIndex = index;
632
+ } else {
633
+ this.state = 1 /* TEXT */;
634
+ this.bufferStartIndex = index;
635
+ this.buffer += char;
636
+ }
637
+ break;
638
+ case 1 /* TEXT */:
639
+ if (char === " ") {
640
+ this.tokens.push({ start: this.bufferStartIndex, end: index, value: this.buffer, expressions: this.expressions });
641
+ this.buffer = "";
642
+ this.expressions = [];
643
+ this.state = 0 /* START */;
644
+ } else if (char === "{") {
645
+ this.buffer += char;
646
+ this.expressionBuffer = char;
647
+ this.expressionStartIndex = index;
648
+ this.state = 2 /* OPEN_BRACE */;
649
+ } else {
650
+ this.buffer += char;
651
+ }
652
+ break;
653
+ case 2 /* OPEN_BRACE */:
654
+ if (char === "}") {
655
+ this.buffer += char;
656
+ this.expressionBuffer += char;
657
+ this.state = 3 /* POTENTIAL_CLOSE */;
658
+ } else {
659
+ this.buffer += char;
660
+ this.expressionBuffer += char;
661
+ }
662
+ break;
663
+ case 3 /* POTENTIAL_CLOSE */:
664
+ if (char === "}") {
665
+ this.buffer += char;
666
+ this.expressionBuffer += char;
667
+ this.expressions.push({
668
+ start: this.expressionStartIndex,
669
+ end: index + 1,
670
+ value: this.expressionBuffer
671
+ });
672
+ this.expressionBuffer = "";
673
+ this.state = 4 /* BRACES_COMPLETE */;
674
+ } else {
675
+ this.buffer += char;
676
+ this.expressionBuffer += char;
677
+ this.state = 2 /* OPEN_BRACE */;
678
+ }
679
+ break;
680
+ case 4 /* BRACES_COMPLETE */:
681
+ if (char === " ") {
682
+ this.tokens.push({
683
+ start: this.bufferStartIndex,
684
+ end: index,
685
+ value: this.buffer,
686
+ expressions: this.expressions
687
+ });
688
+ this.buffer = "";
689
+ this.expressions = [];
690
+ this.state = 0 /* START */;
691
+ } else if (char === "{") {
692
+ this.expressionStartIndex = index;
693
+ this.expressionBuffer = char;
694
+ this.buffer += char;
695
+ this.state = 2 /* OPEN_BRACE */;
696
+ } else {
697
+ this.buffer += char;
698
+ this.state = 1 /* TEXT */;
699
+ }
700
+ break;
701
+ default:
702
+ throw new Error("Unexpected state");
703
+ }
704
+ }
705
+ run(input) {
706
+ for (let i = 0; i < input.length; i++) {
707
+ const char = input[i];
708
+ this.processChar(char, i);
709
+ }
710
+ if (this.buffer.length > 0) {
711
+ this.tokens.push({
712
+ start: this.bufferStartIndex,
713
+ end: input.length,
714
+ value: this.buffer,
715
+ expressions: this.expressions
716
+ });
717
+ }
718
+ return this.tokens;
719
+ }
720
+ reset() {
721
+ this.state = 0 /* START */;
722
+ this.buffer = "";
723
+ this.tokens = [];
724
+ this.bufferStartIndex = 0;
725
+ this.expressionBuffer = "";
726
+ this.expressionStartIndex = 0;
727
+ this.expressions = [];
728
+ }
729
+ };
730
+
731
+ // src/wxml/utils.ts
732
+ function generateCode(match, options = {}) {
733
+ try {
734
+ const { jsHandler: jsHandler2, runtimeSet } = options;
735
+ if (jsHandler2 && jsHandler2.sync && runtimeSet) {
736
+ const { code } = jsHandler2.sync(match, runtimeSet);
737
+ return code;
738
+ } else {
739
+ const ms = new MagicString3(match);
740
+ const ast = parseExpression(match);
741
+ traverse(ast, {
742
+ StringLiteral(path2) {
743
+ var _a, _b;
744
+ if (t.isMemberExpression(path2.parent)) {
745
+ return;
746
+ }
747
+ if (t.isBinaryExpression(path2.parent) && (t.isConditionalExpression((_a = path2.parentPath) == null ? void 0 : _a.parent) || t.isLogicalExpression((_b = path2.parentPath) == null ? void 0 : _b.parent))) {
748
+ return;
749
+ }
750
+ const n = path2.node;
751
+ replaceHandleValue(
752
+ n.value,
753
+ n,
754
+ {
755
+ mangleContext: options.mangleContext,
756
+ escapeMap: options.escapeMap,
757
+ classNameSet: options.runtimeSet,
758
+ needEscaped: true,
759
+ always: true
760
+ },
761
+ ms,
762
+ 1
763
+ );
764
+ },
765
+ noScope: true
766
+ });
767
+ return ms.toString();
768
+ }
769
+ } catch (e) {
770
+ return match;
771
+ }
772
+ }
773
+ function handleEachClassFragment(ms, tokens, options = {}) {
774
+ for (const token of tokens) {
775
+ let p = token.start;
776
+ if (token.expressions.length > 0) {
777
+ for (const exp of token.expressions) {
778
+ if (exp.start > token.start && p < exp.start) {
779
+ ms.update(p, exp.start, replaceWxml(ms.slice(p, exp.start), {
780
+ keepEOL: true,
781
+ escapeMap: options.escapeMap,
782
+ mangleContext: options.mangleContext,
783
+ // 首的str才会被转译
784
+ // example: 2xl:xx 2x{{y}}
785
+ ignoreHead: p > 0
786
+ }));
787
+ }
788
+ const code = `{{${generateCode(exp.value.slice(2, -2), options)}}}`;
789
+ ms.update(exp.start, exp.end, code);
790
+ p = exp.end;
791
+ }
792
+ if (token.end > p) {
793
+ ms.update(p, token.end, replaceWxml(ms.slice(p, token.end), {
794
+ keepEOL: false,
795
+ escapeMap: options.escapeMap,
796
+ mangleContext: options.mangleContext,
797
+ ignoreHead: true
798
+ }));
799
+ }
800
+ } else {
801
+ ms.update(token.start, token.end, replaceWxml(token.value, {
802
+ keepEOL: false,
803
+ escapeMap: options.escapeMap,
804
+ mangleContext: options.mangleContext,
805
+ ignoreHead: false
806
+ }));
807
+ }
808
+ }
809
+ }
810
+ function templateReplacer(original, options = {}) {
811
+ const ms = new MagicString3(original);
812
+ const tokenizer = new Tokenizer();
813
+ const tokens = tokenizer.run(ms.original);
814
+ handleEachClassFragment(ms, tokens, options);
815
+ return ms.toString();
816
+ }
817
+ function regTest(reg, str) {
818
+ reg.lastIndex = 0;
819
+ return reg.test(str);
820
+ }
821
+ function isPropsMatch(props, attr) {
822
+ if (Array.isArray(props)) {
823
+ for (const prop of props) {
824
+ const res = typeof prop === "string" ? prop.toLowerCase() === attr.toLowerCase() : regTest(prop, attr);
825
+ if (res) {
826
+ return res;
827
+ }
828
+ }
829
+ return false;
830
+ } else if (typeof props === "string") {
831
+ return props === attr;
832
+ } else {
833
+ return regTest(props, attr);
834
+ }
835
+ }
836
+ function customTemplateHandler(rawSource, options) {
837
+ return __async(this, null, function* () {
838
+ const {
839
+ customAttributesEntities = [],
840
+ disabledDefaultTemplateHandler,
841
+ inlineWxs,
842
+ runtimeSet,
843
+ jsHandler: jsHandler2
844
+ } = options != null ? options : {};
845
+ const s = new MagicString3(rawSource);
846
+ let tag = "";
847
+ const wxsArray = [];
848
+ const parser = new Parser(
849
+ {
850
+ onopentagname(name) {
851
+ tag = name;
852
+ },
853
+ onattribute(name, value, quote) {
854
+ if (value) {
855
+ let update2 = function() {
856
+ s.update(
857
+ parser.startIndex + name.length + 2,
858
+ // !important
859
+ // htmlparser2 9.0.0: parser.endIndex
860
+ // htmlparser2 9.1.0: parser.endIndex - 1
861
+ // https://github.com/sonofmagic/weapp-tailwindcss/issues/269
862
+ parser.endIndex - 1,
863
+ templateReplacer(value, __spreadProps(__spreadValues({}, options), {
864
+ quote
865
+ }))
866
+ );
867
+ };
868
+ var update = update2;
869
+ if (!disabledDefaultTemplateHandler && (name === "class" || name === "hover-class" || name === "virtualHostClass" || name === "virtualhostclass")) {
870
+ update2();
871
+ }
872
+ for (const [t2, props] of customAttributesEntities) {
873
+ if (t2 === "*") {
874
+ if (isPropsMatch(props, name)) {
875
+ update2();
876
+ }
877
+ } else if (typeof t2 === "string") {
878
+ if (t2 === tag && isPropsMatch(props, name)) {
879
+ update2();
880
+ }
881
+ } else if (regTest(t2, tag) && isPropsMatch(props, name)) {
882
+ update2();
883
+ }
884
+ }
885
+ }
886
+ },
887
+ ontext(data) {
888
+ if (inlineWxs && tag === "wxs") {
889
+ wxsArray.push({
890
+ data,
891
+ endIndex: parser.endIndex + 1,
892
+ startIndex: parser.startIndex
893
+ });
894
+ }
895
+ },
896
+ onclosetag() {
897
+ tag = "";
898
+ }
899
+ },
900
+ {
901
+ xmlMode: true
902
+ }
903
+ );
904
+ parser.write(s.original);
905
+ parser.end();
906
+ for (const { data, endIndex, startIndex } of wxsArray) {
907
+ const { code } = yield jsHandler2(data, runtimeSet);
908
+ s.update(startIndex, endIndex, code);
909
+ }
910
+ return s.toString();
911
+ });
912
+ }
913
+ function createTemplateHandler(options = {}) {
914
+ return (rawSource, opt = {}) => {
915
+ return customTemplateHandler(rawSource, defuOverrideArray(opt, options));
916
+ };
917
+ }
918
+
919
+ // src/options.ts
920
+ function getOptions(opts) {
921
+ const result = defuOverrideArray(
922
+ opts,
923
+ getDefaultOptions(),
924
+ {}
925
+ );
926
+ result.escapeMap = result.customReplaceDictionary;
927
+ const {
928
+ cssPreflight,
929
+ customRuleCallback,
930
+ cssPreflightRange,
931
+ customAttributes,
932
+ supportCustomLengthUnitsPatch,
933
+ arbitraryValues,
934
+ cssChildCombinatorReplaceValue,
935
+ inlineWxs,
936
+ injectAdditionalCssVarScope,
937
+ jsPreserveClass,
938
+ disabledDefaultTemplateHandler,
939
+ cssSelectorReplacement,
940
+ rem2rpx,
941
+ cache,
942
+ jsAstTool,
943
+ babelParserOptions,
944
+ postcssOptions,
945
+ cssRemoveHoverPseudoClass,
946
+ escapeMap,
947
+ mangle,
948
+ tailwindcssBasedir,
949
+ appType,
950
+ ignoreCallExpressionIdentifiers,
951
+ ignoreTaggedTemplateExpressionIdentifiers
952
+ } = result;
953
+ const cssInjectPreflight = createInjectPreflight(cssPreflight);
954
+ const customAttributesEntities = isMap(customAttributes) ? [...customAttributes.entries()] : Object.entries(customAttributes);
955
+ const { initMangle, mangleContext, setMangleRuntimeSet } = useMangleStore();
956
+ initMangle(mangle);
957
+ const styleHandler2 = createStyleHandler({
958
+ cssInjectPreflight,
959
+ customRuleCallback,
960
+ cssPreflightRange,
961
+ escapeMap,
962
+ mangleContext,
963
+ cssChildCombinatorReplaceValue,
964
+ injectAdditionalCssVarScope,
965
+ cssSelectorReplacement,
966
+ rem2rpx,
967
+ postcssOptions,
968
+ cssRemoveHoverPseudoClass
969
+ });
970
+ const jsHandler2 = createJsHandler({
971
+ escapeMap,
972
+ mangleContext,
973
+ arbitraryValues,
974
+ jsPreserveClass,
975
+ generateMap: true,
976
+ jsAstTool,
977
+ babelParserOptions,
978
+ ignoreCallExpressionIdentifiers,
979
+ ignoreTaggedTemplateExpressionIdentifiers
980
+ });
981
+ const templateHandler = createTemplateHandler({
982
+ customAttributesEntities,
983
+ escapeMap,
984
+ mangleContext,
985
+ inlineWxs,
986
+ jsHandler: jsHandler2,
987
+ disabledDefaultTemplateHandler
988
+ });
989
+ result.styleHandler = styleHandler2;
990
+ result.jsHandler = jsHandler2;
991
+ result.templateHandler = templateHandler;
992
+ const twPatcher = createTailwindcssPatcher(tailwindcssBasedir, appType === "mpx" ? "node_modules/tailwindcss-patch/.cache" : void 0, supportCustomLengthUnitsPatch != null ? supportCustomLengthUnitsPatch : true);
993
+ result.patch = twPatcher.patch;
994
+ result.setMangleRuntimeSet = setMangleRuntimeSet;
995
+ result.cache = cache === void 0 || typeof cache === "boolean" ? createCache(cache) : cache;
996
+ result.twPatcher = twPatcher;
997
+ return result;
998
+ }
999
+
1000
+ export {
1001
+ logger,
1002
+ getOptions
1003
+ };