weapp-tailwindcss 2.7.1 → 2.8.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.
package/README.md CHANGED
@@ -24,7 +24,7 @@
24
24
  > `小程序` + `tailwindcss` 全方面解决方案
25
25
  > 想试试在小程序里使用 `CSS-in-JS` 思想? 👉🏻👉🏻试试 [`weapp-pandacss`](https://github.com/sonofmagic/weapp-pandacss)
26
26
 
27
- \[[国内部署的文档地址](https://weapp-tw.icebreaker.top)\] \| \[[备用Github Page](https://sonofmagic.github.io/weapp-tailwindcss/)\] \| \[[1.x文档]('./v1.md')\]
27
+ \[[国内部署的文档地址](https://weapp-tw.icebreaker.top)\] \| \[[备用Github Page](https://sonofmagic.github.io/weapp-tailwindcss/)\] \| \[[1.x文档](./v1.md)\]
28
28
 
29
29
  - [Tips](#tips)
30
30
  - [特性](#特性)
package/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var options = require('./options-b7411c31.js');
3
+ var options = require('./options-4284d635.js');
4
4
  require('micromatch');
5
5
  require('magic-string');
6
6
  require('./replace.js');
@@ -10,8 +10,9 @@ require('@weapp-core/regex');
10
10
  require('@babel/generator');
11
11
  require('@babel/parser');
12
12
  require('@babel/traverse');
13
- require('./defaults-344ba6d9.js');
13
+ require('./defaults-c6437917.js');
14
14
  require('@babel/types');
15
+ require('htmlparser2');
15
16
  require('postcss');
16
17
  require('./postcss-4e99a8e8.js');
17
18
  require('postcss-selector-parser');
package/dist/cli.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { g as getOptions, c as createPatch } from './options-8805cf56.mjs';
1
+ import { g as getOptions, c as createPatch } from './options-ad0be036.mjs';
2
2
  import 'micromatch';
3
3
  import 'magic-string';
4
4
  import './replace.mjs';
@@ -8,8 +8,9 @@ import '@weapp-core/regex';
8
8
  import '@babel/generator';
9
9
  import '@babel/parser';
10
10
  import '@babel/traverse';
11
- import './defaults-0743f523.mjs';
11
+ import './defaults-72ea9566.mjs';
12
12
  import '@babel/types';
13
+ import 'htmlparser2';
13
14
  import 'postcss';
14
15
  import './postcss-a551ddc0.mjs';
15
16
  import 'postcss-selector-parser';
@@ -126,7 +126,7 @@ const defaultOptions = {
126
126
  return false;
127
127
  },
128
128
  disabledDefaultTemplateHandler: false,
129
- jsEscapeStrategy: 'regenerate'
129
+ jsEscapeStrategy: 'replace'
130
130
  };
131
131
 
132
132
  export { defaultOptions as d, getGroupedEntries as g, isMap as i, noop as n };
@@ -128,7 +128,7 @@ const defaultOptions = {
128
128
  return false;
129
129
  },
130
130
  disabledDefaultTemplateHandler: false,
131
- jsEscapeStrategy: 'regenerate'
131
+ jsEscapeStrategy: 'replace'
132
132
  };
133
133
 
134
134
  exports.defaultOptions = defaultOptions;
package/dist/defaults.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var defaults = require('./defaults-344ba6d9.js');
5
+ var defaults = require('./defaults-c6437917.js');
6
6
  require('@weapp-core/escape');
7
7
 
8
8
 
package/dist/defaults.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { d as defaultOptions } from './defaults-0743f523.mjs';
1
+ export { d as defaultOptions } from './defaults-72ea9566.mjs';
2
2
  import '@weapp-core/escape';
package/dist/gulp.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var stream = require('node:stream');
6
- var options = require('./options-b7411c31.js');
6
+ var options = require('./options-4284d635.js');
7
7
  require('micromatch');
8
8
  require('magic-string');
9
9
  require('./replace.js');
@@ -13,8 +13,9 @@ require('@weapp-core/regex');
13
13
  require('@babel/generator');
14
14
  require('@babel/parser');
15
15
  require('@babel/traverse');
16
- require('./defaults-344ba6d9.js');
16
+ require('./defaults-c6437917.js');
17
17
  require('@babel/types');
18
+ require('htmlparser2');
18
19
  require('postcss');
19
20
  require('./postcss-4e99a8e8.js');
20
21
  require('postcss-selector-parser');
package/dist/gulp.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import stream from 'node:stream';
2
- import { g as getOptions, a as createTailwindcssPatcher } from './options-8805cf56.mjs';
2
+ import { g as getOptions, a as createTailwindcssPatcher } from './options-ad0be036.mjs';
3
3
  import 'micromatch';
4
4
  import 'magic-string';
5
5
  import './replace.mjs';
@@ -9,8 +9,9 @@ import '@weapp-core/regex';
9
9
  import '@babel/generator';
10
10
  import '@babel/parser';
11
11
  import '@babel/traverse';
12
- import './defaults-0743f523.mjs';
12
+ import './defaults-72ea9566.mjs';
13
13
  import '@babel/types';
14
+ import 'htmlparser2';
14
15
  import 'postcss';
15
16
  import './postcss-a551ddc0.mjs';
16
17
  import 'postcss-selector-parser';
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ var vite = require('./vite.js');
7
7
  var gulp = require('./gulp.js');
8
8
  require('node:path');
9
9
  require('node:fs');
10
- require('./options-b7411c31.js');
10
+ require('./options-4284d635.js');
11
11
  require('micromatch');
12
12
  require('magic-string');
13
13
  require('./replace.js');
@@ -17,8 +17,9 @@ require('@weapp-core/regex');
17
17
  require('@babel/generator');
18
18
  require('@babel/parser');
19
19
  require('@babel/traverse');
20
- require('./defaults-344ba6d9.js');
20
+ require('./defaults-c6437917.js');
21
21
  require('@babel/types');
22
+ require('htmlparser2');
22
23
  require('postcss');
23
24
  require('./postcss-4e99a8e8.js');
24
25
  require('postcss-selector-parser');
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ export { UnifiedViteWeappTailwindcssPlugin } from './vite.mjs';
3
3
  export { createPlugins } from './gulp.mjs';
4
4
  import 'node:path';
5
5
  import 'node:fs';
6
- import './options-8805cf56.mjs';
6
+ import './options-ad0be036.mjs';
7
7
  import 'micromatch';
8
8
  import 'magic-string';
9
9
  import './replace.mjs';
@@ -13,8 +13,9 @@ import '@weapp-core/regex';
13
13
  import '@babel/generator';
14
14
  import '@babel/parser';
15
15
  import '@babel/traverse';
16
- import './defaults-0743f523.mjs';
16
+ import './defaults-72ea9566.mjs';
17
17
  import '@babel/types';
18
+ import 'htmlparser2';
18
19
  import 'postcss';
19
20
  import './postcss-a551ddc0.mjs';
20
21
  import 'postcss-selector-parser';
@@ -1,6 +1,5 @@
1
1
  import type { StringLiteral, TemplateElement } from '@babel/types';
2
2
  import MagicString from 'magic-string';
3
3
  import type { IJsHandlerOptions } from "../types";
4
- export declare function jsStringEscape(str: unknown): string;
5
4
  export declare function regenerateHandleValue(str: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions): string;
6
5
  export declare function replaceHandleValue(str: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions, ms: MagicString, offset?: number, needEscaped?: boolean): string;
@@ -4,15 +4,16 @@ var micromatch = require('micromatch');
4
4
  var MagicString = require('magic-string');
5
5
  var replace = require('./replace.js');
6
6
  var regex = require('@weapp-core/regex');
7
+ var escape = require('@weapp-core/escape');
7
8
  var generate = require('@babel/generator');
8
9
  var parser = require('@babel/parser');
9
10
  var traverse = require('@babel/traverse');
10
- var defaults = require('./defaults-344ba6d9.js');
11
+ var defaults = require('./defaults-c6437917.js');
11
12
  var t = require('@babel/types');
13
+ var htmlparser2 = require('htmlparser2');
12
14
  var postcss = require('postcss');
13
15
  var postcss$1 = require('./postcss-4e99a8e8.js');
14
16
  var postcssIsPseudoClass = require('@csstools/postcss-is-pseudo-class');
15
- var escape = require('@weapp-core/escape');
16
17
  var path = require('node:path');
17
18
  var fs = require('node:fs');
18
19
  var semver = require('semver');
@@ -57,32 +58,6 @@ const splitCode = (code, allowDoubleQuotes = false) => {
57
58
  return code.split(splitter).filter((element) => isValidSelector(element));
58
59
  };
59
60
 
60
- function jsStringEscape(str) {
61
- return ('' + str).replaceAll(/[\n\r"'\\\u2028\u2029]/g, (character) => {
62
- switch (character) {
63
- case '"':
64
- case "'":
65
- case '\\': {
66
- return '\\' + character;
67
- }
68
- case '\n': {
69
- return '\\n';
70
- }
71
- case '\r': {
72
- return '\\r';
73
- }
74
- case '\u2028': {
75
- return '\\u2028';
76
- }
77
- case '\u2029': {
78
- return '\\u2029';
79
- }
80
- default: {
81
- return character;
82
- }
83
- }
84
- });
85
- }
86
61
  function regenerateHandleValue(str, node, options) {
87
62
  var _a;
88
63
  const set = options.classNameSet;
@@ -138,8 +113,9 @@ function replaceHandleValue(str, node, options, ms, offset = 0, needEscaped = fa
138
113
  if (typeof node.start === 'number' && typeof node.end === 'number') {
139
114
  const start = node.start + offset;
140
115
  const end = node.end - offset;
141
- if (start < end) {
142
- ms.update(node.start + offset, node.end - offset, needEscaped ? jsStringEscape(rawStr) : rawStr);
116
+ if (start < end && str !== rawStr) {
117
+ const content = needEscaped ? escape.jsStringEscape(rawStr) : rawStr;
118
+ ms.update(start, end, content);
143
119
  }
144
120
  }
145
121
  return rawStr;
@@ -147,6 +123,13 @@ function replaceHandleValue(str, node, options, ms, offset = 0, needEscaped = fa
147
123
 
148
124
  const isProd = () => process.env.NODE_ENV === 'production';
149
125
 
126
+ function isEvalPath(p) {
127
+ if (p.isCallExpression()) {
128
+ const calleePath = p.get('callee');
129
+ return calleePath.isIdentifier() && calleePath.node.name === 'eval';
130
+ }
131
+ return false;
132
+ }
150
133
  function jsHandler(rawSource, options) {
151
134
  var _a;
152
135
  const ast = parser.parse(rawSource, {
@@ -157,26 +140,56 @@ function jsHandler(rawSource, options) {
157
140
  const ropt = {
158
141
  StringLiteral: {
159
142
  enter(p) {
143
+ var _a;
144
+ if (isEvalPath(p.parentPath)) {
145
+ return;
146
+ }
160
147
  const n = p.node;
161
- replaceHandleValue(n.value, n, options, ms, 1, true);
148
+ replaceHandleValue(n.value, n, options, ms, 1, (_a = options.needEscaped) !== null && _a !== void 0 ? _a : true);
162
149
  }
163
150
  },
164
151
  TemplateElement: {
165
152
  enter(p) {
153
+ if (p.parentPath.isTemplateLiteral() && isEvalPath(p.parentPath.parentPath)) {
154
+ return;
155
+ }
166
156
  const n = p.node;
167
157
  replaceHandleValue(n.value.raw, n, options, ms, 0, false);
168
158
  }
169
159
  },
170
160
  CallExpression: {
171
161
  enter(p) {
172
- const calleePath = p.get('callee');
173
- if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
162
+ if (isEvalPath(p)) {
174
163
  p.traverse({
175
164
  StringLiteral: {
176
165
  enter(s) {
177
- const res = jsHandler(s.node.value, options);
166
+ const res = jsHandler(s.node.value, Object.assign(Object.assign({}, options), { needEscaped: false }));
178
167
  if (res.code) {
179
- s.node.value = res.code;
168
+ const node = s.node;
169
+ if (typeof node.start === 'number' && typeof node.end === 'number') {
170
+ const start = node.start + 1;
171
+ const end = node.end - 1;
172
+ if (start < end && s.node.value !== res.code) {
173
+ ms.update(start, end, escape.jsStringEscape(res.code));
174
+ node.value = res.code;
175
+ }
176
+ }
177
+ }
178
+ }
179
+ },
180
+ TemplateElement: {
181
+ enter(s) {
182
+ const res = jsHandler(s.node.value.raw, options);
183
+ if (res.code) {
184
+ const node = s.node;
185
+ if (typeof node.start === 'number' && typeof node.end === 'number') {
186
+ const start = node.start;
187
+ const end = node.end;
188
+ if (start < end && s.node.value.raw !== res.code) {
189
+ ms.update(start, end, res.code);
190
+ s.node.value.raw = res.code;
191
+ }
192
+ }
180
193
  }
181
194
  }
182
195
  }
@@ -194,20 +207,25 @@ function jsHandler(rawSource, options) {
194
207
  const gopt = {
195
208
  StringLiteral: {
196
209
  enter(p) {
210
+ if (isEvalPath(p.parentPath)) {
211
+ return;
212
+ }
197
213
  const n = p.node;
198
214
  n.value = regenerateHandleValue(n.value, n, options);
199
215
  }
200
216
  },
201
217
  TemplateElement: {
202
218
  enter(p) {
219
+ if (p.parentPath.isTemplateLiteral() && isEvalPath(p.parentPath.parentPath)) {
220
+ return;
221
+ }
203
222
  const n = p.node;
204
223
  n.value.raw = regenerateHandleValue(n.value.raw, n, options);
205
224
  }
206
225
  },
207
226
  CallExpression: {
208
227
  enter(p) {
209
- const calleePath = p.get('callee');
210
- if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
228
+ if (isEvalPath(p)) {
211
229
  p.traverse({
212
230
  StringLiteral: {
213
231
  enter(s) {
@@ -216,6 +234,14 @@ function jsHandler(rawSource, options) {
216
234
  s.node.value = res.code;
217
235
  }
218
236
  }
237
+ },
238
+ TemplateElement: {
239
+ enter(s) {
240
+ const res = jsHandler(s.node.value.raw, options);
241
+ if (res.code) {
242
+ s.node.value.raw = res.code;
243
+ }
244
+ }
219
245
  }
220
246
  });
221
247
  }
@@ -371,37 +397,77 @@ function templateReplacer(original, options = {}) {
371
397
  });
372
398
  }
373
399
  }
374
- function templateHandler(rawSource, options = {}) {
375
- if (options.disabledDefaultTemplateHandler) {
376
- return rawSource;
377
- }
378
- return rawSource.replace(regex.tagWithEitherClassAndHoverClassRegexp, (m0) => {
379
- return m0.replace(regex.templateClassExactRegexp, (m1, className) => {
380
- return m1.replace(className, templateReplacer(className, options));
381
- });
382
- });
400
+ function regTest(reg, str) {
401
+ reg.lastIndex = 0;
402
+ return reg.test(str);
383
403
  }
384
- function customTemplateHandler(rawSource, options) {
385
- const { customAttributesEntities, inlineWxs, runtimeSet, jsHandler } = options;
386
- let source = templateHandler(rawSource, options);
387
- const regexps = regex.makeCustomAttributes(customAttributesEntities);
388
- if (regexps && Array.isArray(regexps)) {
389
- for (const regexp of regexps) {
390
- source = source.replace(regexp.tagRegexp, (m0) => {
391
- return m0.replace(regexp.attrRegexp, (m1, className) => {
392
- return m1.replace(className, templateReplacer(className, options));
393
- });
394
- });
404
+ function isPropsMatch(props, attr) {
405
+ if (Array.isArray(props)) {
406
+ for (const prop of props) {
407
+ const res = typeof prop === 'string' ? prop.toLowerCase() === attr : regTest(prop, attr);
408
+ if (res) {
409
+ return res;
410
+ }
395
411
  }
412
+ return false;
396
413
  }
397
- if (inlineWxs) {
398
- const wxsTags = extract(source, regex.wxsTagRegexp);
399
- for (const x of wxsTags) {
400
- const code = jsHandler(x.raw, runtimeSet).code;
401
- source = source.replaceAll(x.raw, code);
402
- }
414
+ else if (typeof props === 'string') {
415
+ return props === attr;
416
+ }
417
+ else {
418
+ return regTest(props, attr);
403
419
  }
404
- return source;
420
+ }
421
+ function customTemplateHandler(rawSource, options) {
422
+ const { customAttributesEntities = [], disabledDefaultTemplateHandler, inlineWxs, runtimeSet, jsHandler } = options !== null && options !== void 0 ? options : {};
423
+ const s = new MagicString__default["default"](rawSource);
424
+ let tag = '';
425
+ const parser = new htmlparser2.Parser({
426
+ onopentagname(name) {
427
+ tag = name;
428
+ },
429
+ onattribute(name, value, quote) {
430
+ if (value) {
431
+ if (quote === "'") {
432
+ s.update(parser.startIndex + name.length + 1, parser.startIndex + name.length + 2, '"');
433
+ s.update(parser.startIndex + name.length + value.length + 2, parser.startIndex + name.length + value.length + 3, '"');
434
+ }
435
+ function update() {
436
+ s.update(parser.startIndex + name.length + 2, parser.endIndex, templateReplacer(value, options));
437
+ }
438
+ if (!disabledDefaultTemplateHandler && (name === 'class' || name === 'hover-class')) {
439
+ update();
440
+ }
441
+ for (const [t, props] of customAttributesEntities) {
442
+ if (t === '*') {
443
+ if (isPropsMatch(props, name)) {
444
+ update();
445
+ }
446
+ }
447
+ else if (typeof t === 'string') {
448
+ if (t === tag && isPropsMatch(props, name)) {
449
+ update();
450
+ }
451
+ }
452
+ else if (regTest(t, tag) && isPropsMatch(props, name)) {
453
+ update();
454
+ }
455
+ }
456
+ }
457
+ },
458
+ ontext(data) {
459
+ if (inlineWxs && tag === 'wxs') {
460
+ const code = jsHandler(data, runtimeSet).code;
461
+ s.update(parser.startIndex, parser.endIndex + 1, code);
462
+ }
463
+ },
464
+ onclosetag() {
465
+ tag = '';
466
+ }
467
+ });
468
+ parser.write(s.original);
469
+ parser.end();
470
+ return s.toString();
405
471
  }
406
472
  function createTemplateHandler(options = {}) {
407
473
  return (rawSource, opt = {}) => {
@@ -1,16 +1,17 @@
1
1
  import { isMatch } from 'micromatch';
2
2
  import MagicString from 'magic-string';
3
3
  import { replaceJs as replaceWxml } from './replace.mjs';
4
- import { escapeStringRegexp, makeCustomAttributes, tagWithEitherClassAndHoverClassRegexp, templateClassExactRegexp, wxsTagRegexp, variableRegExp } from '@weapp-core/regex';
4
+ import { escapeStringRegexp, variableRegExp } from '@weapp-core/regex';
5
+ import { jsStringEscape, SimpleMappingChars2String, MappingChars2String } from '@weapp-core/escape';
5
6
  import generate from '@babel/generator';
6
7
  import { parse, parseExpression } from '@babel/parser';
7
8
  import traverse from '@babel/traverse';
8
- import { n as noop, d as defaultOptions, i as isMap } from './defaults-0743f523.mjs';
9
+ import { n as noop, d as defaultOptions, i as isMap } from './defaults-72ea9566.mjs';
9
10
  import * as t from '@babel/types';
11
+ import { Parser } from 'htmlparser2';
10
12
  import postcss from 'postcss';
11
13
  import { p as postcssWeappTailwindcss } from './postcss-a551ddc0.mjs';
12
14
  import postcssIsPseudoClass from '@csstools/postcss-is-pseudo-class';
13
- import { SimpleMappingChars2String, MappingChars2String } from '@weapp-core/escape';
14
15
  import path from 'node:path';
15
16
  import fs from 'node:fs';
16
17
  import { gte } from 'semver';
@@ -26,32 +27,6 @@ const splitCode = (code, allowDoubleQuotes = false) => {
26
27
  return code.split(splitter).filter((element) => isValidSelector(element));
27
28
  };
28
29
 
29
- function jsStringEscape(str) {
30
- return ('' + str).replaceAll(/[\n\r"'\\\u2028\u2029]/g, (character) => {
31
- switch (character) {
32
- case '"':
33
- case "'":
34
- case '\\': {
35
- return '\\' + character;
36
- }
37
- case '\n': {
38
- return '\\n';
39
- }
40
- case '\r': {
41
- return '\\r';
42
- }
43
- case '\u2028': {
44
- return '\\u2028';
45
- }
46
- case '\u2029': {
47
- return '\\u2029';
48
- }
49
- default: {
50
- return character;
51
- }
52
- }
53
- });
54
- }
55
30
  function regenerateHandleValue(str, node, options) {
56
31
  var _a;
57
32
  const set = options.classNameSet;
@@ -107,8 +82,9 @@ function replaceHandleValue(str, node, options, ms, offset = 0, needEscaped = fa
107
82
  if (typeof node.start === 'number' && typeof node.end === 'number') {
108
83
  const start = node.start + offset;
109
84
  const end = node.end - offset;
110
- if (start < end) {
111
- ms.update(node.start + offset, node.end - offset, needEscaped ? jsStringEscape(rawStr) : rawStr);
85
+ if (start < end && str !== rawStr) {
86
+ const content = needEscaped ? jsStringEscape(rawStr) : rawStr;
87
+ ms.update(start, end, content);
112
88
  }
113
89
  }
114
90
  return rawStr;
@@ -116,6 +92,13 @@ function replaceHandleValue(str, node, options, ms, offset = 0, needEscaped = fa
116
92
 
117
93
  const isProd = () => process.env.NODE_ENV === 'production';
118
94
 
95
+ function isEvalPath(p) {
96
+ if (p.isCallExpression()) {
97
+ const calleePath = p.get('callee');
98
+ return calleePath.isIdentifier() && calleePath.node.name === 'eval';
99
+ }
100
+ return false;
101
+ }
119
102
  function jsHandler(rawSource, options) {
120
103
  var _a;
121
104
  const ast = parse(rawSource, {
@@ -126,26 +109,56 @@ function jsHandler(rawSource, options) {
126
109
  const ropt = {
127
110
  StringLiteral: {
128
111
  enter(p) {
112
+ var _a;
113
+ if (isEvalPath(p.parentPath)) {
114
+ return;
115
+ }
129
116
  const n = p.node;
130
- replaceHandleValue(n.value, n, options, ms, 1, true);
117
+ replaceHandleValue(n.value, n, options, ms, 1, (_a = options.needEscaped) !== null && _a !== void 0 ? _a : true);
131
118
  }
132
119
  },
133
120
  TemplateElement: {
134
121
  enter(p) {
122
+ if (p.parentPath.isTemplateLiteral() && isEvalPath(p.parentPath.parentPath)) {
123
+ return;
124
+ }
135
125
  const n = p.node;
136
126
  replaceHandleValue(n.value.raw, n, options, ms, 0, false);
137
127
  }
138
128
  },
139
129
  CallExpression: {
140
130
  enter(p) {
141
- const calleePath = p.get('callee');
142
- if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
131
+ if (isEvalPath(p)) {
143
132
  p.traverse({
144
133
  StringLiteral: {
145
134
  enter(s) {
146
- const res = jsHandler(s.node.value, options);
135
+ const res = jsHandler(s.node.value, Object.assign(Object.assign({}, options), { needEscaped: false }));
147
136
  if (res.code) {
148
- s.node.value = res.code;
137
+ const node = s.node;
138
+ if (typeof node.start === 'number' && typeof node.end === 'number') {
139
+ const start = node.start + 1;
140
+ const end = node.end - 1;
141
+ if (start < end && s.node.value !== res.code) {
142
+ ms.update(start, end, jsStringEscape(res.code));
143
+ node.value = res.code;
144
+ }
145
+ }
146
+ }
147
+ }
148
+ },
149
+ TemplateElement: {
150
+ enter(s) {
151
+ const res = jsHandler(s.node.value.raw, options);
152
+ if (res.code) {
153
+ const node = s.node;
154
+ if (typeof node.start === 'number' && typeof node.end === 'number') {
155
+ const start = node.start;
156
+ const end = node.end;
157
+ if (start < end && s.node.value.raw !== res.code) {
158
+ ms.update(start, end, res.code);
159
+ s.node.value.raw = res.code;
160
+ }
161
+ }
149
162
  }
150
163
  }
151
164
  }
@@ -163,20 +176,25 @@ function jsHandler(rawSource, options) {
163
176
  const gopt = {
164
177
  StringLiteral: {
165
178
  enter(p) {
179
+ if (isEvalPath(p.parentPath)) {
180
+ return;
181
+ }
166
182
  const n = p.node;
167
183
  n.value = regenerateHandleValue(n.value, n, options);
168
184
  }
169
185
  },
170
186
  TemplateElement: {
171
187
  enter(p) {
188
+ if (p.parentPath.isTemplateLiteral() && isEvalPath(p.parentPath.parentPath)) {
189
+ return;
190
+ }
172
191
  const n = p.node;
173
192
  n.value.raw = regenerateHandleValue(n.value.raw, n, options);
174
193
  }
175
194
  },
176
195
  CallExpression: {
177
196
  enter(p) {
178
- const calleePath = p.get('callee');
179
- if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
197
+ if (isEvalPath(p)) {
180
198
  p.traverse({
181
199
  StringLiteral: {
182
200
  enter(s) {
@@ -185,6 +203,14 @@ function jsHandler(rawSource, options) {
185
203
  s.node.value = res.code;
186
204
  }
187
205
  }
206
+ },
207
+ TemplateElement: {
208
+ enter(s) {
209
+ const res = jsHandler(s.node.value.raw, options);
210
+ if (res.code) {
211
+ s.node.value.raw = res.code;
212
+ }
213
+ }
188
214
  }
189
215
  });
190
216
  }
@@ -340,37 +366,77 @@ function templateReplacer(original, options = {}) {
340
366
  });
341
367
  }
342
368
  }
343
- function templateHandler(rawSource, options = {}) {
344
- if (options.disabledDefaultTemplateHandler) {
345
- return rawSource;
346
- }
347
- return rawSource.replace(tagWithEitherClassAndHoverClassRegexp, (m0) => {
348
- return m0.replace(templateClassExactRegexp, (m1, className) => {
349
- return m1.replace(className, templateReplacer(className, options));
350
- });
351
- });
369
+ function regTest(reg, str) {
370
+ reg.lastIndex = 0;
371
+ return reg.test(str);
352
372
  }
353
- function customTemplateHandler(rawSource, options) {
354
- const { customAttributesEntities, inlineWxs, runtimeSet, jsHandler } = options;
355
- let source = templateHandler(rawSource, options);
356
- const regexps = makeCustomAttributes(customAttributesEntities);
357
- if (regexps && Array.isArray(regexps)) {
358
- for (const regexp of regexps) {
359
- source = source.replace(regexp.tagRegexp, (m0) => {
360
- return m0.replace(regexp.attrRegexp, (m1, className) => {
361
- return m1.replace(className, templateReplacer(className, options));
362
- });
363
- });
373
+ function isPropsMatch(props, attr) {
374
+ if (Array.isArray(props)) {
375
+ for (const prop of props) {
376
+ const res = typeof prop === 'string' ? prop.toLowerCase() === attr : regTest(prop, attr);
377
+ if (res) {
378
+ return res;
379
+ }
364
380
  }
381
+ return false;
365
382
  }
366
- if (inlineWxs) {
367
- const wxsTags = extract(source, wxsTagRegexp);
368
- for (const x of wxsTags) {
369
- const code = jsHandler(x.raw, runtimeSet).code;
370
- source = source.replaceAll(x.raw, code);
371
- }
383
+ else if (typeof props === 'string') {
384
+ return props === attr;
385
+ }
386
+ else {
387
+ return regTest(props, attr);
372
388
  }
373
- return source;
389
+ }
390
+ function customTemplateHandler(rawSource, options) {
391
+ const { customAttributesEntities = [], disabledDefaultTemplateHandler, inlineWxs, runtimeSet, jsHandler } = options !== null && options !== void 0 ? options : {};
392
+ const s = new MagicString(rawSource);
393
+ let tag = '';
394
+ const parser = new Parser({
395
+ onopentagname(name) {
396
+ tag = name;
397
+ },
398
+ onattribute(name, value, quote) {
399
+ if (value) {
400
+ if (quote === "'") {
401
+ s.update(parser.startIndex + name.length + 1, parser.startIndex + name.length + 2, '"');
402
+ s.update(parser.startIndex + name.length + value.length + 2, parser.startIndex + name.length + value.length + 3, '"');
403
+ }
404
+ function update() {
405
+ s.update(parser.startIndex + name.length + 2, parser.endIndex, templateReplacer(value, options));
406
+ }
407
+ if (!disabledDefaultTemplateHandler && (name === 'class' || name === 'hover-class')) {
408
+ update();
409
+ }
410
+ for (const [t, props] of customAttributesEntities) {
411
+ if (t === '*') {
412
+ if (isPropsMatch(props, name)) {
413
+ update();
414
+ }
415
+ }
416
+ else if (typeof t === 'string') {
417
+ if (t === tag && isPropsMatch(props, name)) {
418
+ update();
419
+ }
420
+ }
421
+ else if (regTest(t, tag) && isPropsMatch(props, name)) {
422
+ update();
423
+ }
424
+ }
425
+ }
426
+ },
427
+ ontext(data) {
428
+ if (inlineWxs && tag === 'wxs') {
429
+ const code = jsHandler(data, runtimeSet).code;
430
+ s.update(parser.startIndex, parser.endIndex + 1, code);
431
+ }
432
+ },
433
+ onclosetag() {
434
+ tag = '';
435
+ }
436
+ });
437
+ parser.write(s.original);
438
+ parser.end();
439
+ return s.toString();
374
440
  }
375
441
  function createTemplateHandler(options = {}) {
376
442
  return (rawSource, opt = {}) => {
package/dist/types.d.ts CHANGED
@@ -39,6 +39,7 @@ export type IJsHandlerOptions = {
39
39
  mangleContext?: IMangleScopeContext;
40
40
  jsPreserveClass?: (keyword: string) => boolean | undefined;
41
41
  strategy?: UserDefinedOptions['jsEscapeStrategy'];
42
+ needEscaped?: boolean;
42
43
  };
43
44
  export interface RawSource {
44
45
  start: number;
@@ -95,6 +96,7 @@ export interface UserDefinedOptions {
95
96
  jsPreserveClass?: (keyword: string) => boolean | undefined;
96
97
  disabledDefaultTemplateHandler?: boolean;
97
98
  jsEscapeStrategy?: 'regenerate' | 'replace';
99
+ runtimeLoaderPath?: string;
98
100
  }
99
101
  export interface IMangleScopeContext {
100
102
  rawOptions: UserDefinedOptions['mangle'];
package/dist/vite.js CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var options = require('./options-b7411c31.js');
5
+ var options = require('./options-4284d635.js');
6
6
  var postcss = require('./postcss-4e99a8e8.js');
7
- var defaults = require('./defaults-344ba6d9.js');
7
+ var defaults = require('./defaults-c6437917.js');
8
8
  require('micromatch');
9
9
  require('magic-string');
10
10
  require('./replace.js');
@@ -15,6 +15,7 @@ require('@babel/generator');
15
15
  require('@babel/parser');
16
16
  require('@babel/traverse');
17
17
  require('@babel/types');
18
+ require('htmlparser2');
18
19
  require('postcss');
19
20
  require('@csstools/postcss-is-pseudo-class');
20
21
  require('node:path');
@@ -39,10 +40,8 @@ function UnifiedViteWeappTailwindcssPlugin(options$1 = {}) {
39
40
  return {
40
41
  name: postcss.vitePluginName,
41
42
  enforce: 'post',
42
- buildStart() {
43
+ generateBundle(opt, bundle) {
43
44
  onStart();
44
- },
45
- generateBundle(opt, bundle, isWrite) {
46
45
  const entries = Object.entries(bundle);
47
46
  const groupedEntries = defaults.getGroupedEntries(entries, opts);
48
47
  const runtimeSet = twPatcher.getClassSet();
@@ -77,8 +76,6 @@ function UnifiedViteWeappTailwindcssPlugin(options$1 = {}) {
77
76
  onUpdate(file, rawSource, code);
78
77
  }
79
78
  }
80
- },
81
- buildEnd() {
82
79
  onEnd();
83
80
  }
84
81
  };
package/dist/vite.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { g as getOptions, a as createTailwindcssPatcher } from './options-8805cf56.mjs';
1
+ import { g as getOptions, a as createTailwindcssPatcher } from './options-ad0be036.mjs';
2
2
  import { v as vitePluginName } from './postcss-a551ddc0.mjs';
3
- import { g as getGroupedEntries } from './defaults-0743f523.mjs';
3
+ import { g as getGroupedEntries } from './defaults-72ea9566.mjs';
4
4
  import 'micromatch';
5
5
  import 'magic-string';
6
6
  import './replace.mjs';
@@ -11,6 +11,7 @@ import '@babel/generator';
11
11
  import '@babel/parser';
12
12
  import '@babel/traverse';
13
13
  import '@babel/types';
14
+ import 'htmlparser2';
14
15
  import 'postcss';
15
16
  import '@csstools/postcss-is-pseudo-class';
16
17
  import 'node:path';
@@ -35,10 +36,8 @@ function UnifiedViteWeappTailwindcssPlugin(options = {}) {
35
36
  return {
36
37
  name: vitePluginName,
37
38
  enforce: 'post',
38
- buildStart() {
39
+ generateBundle(opt, bundle) {
39
40
  onStart();
40
- },
41
- generateBundle(opt, bundle, isWrite) {
42
41
  const entries = Object.entries(bundle);
43
42
  const groupedEntries = getGroupedEntries(entries, opts);
44
43
  const runtimeSet = twPatcher.getClassSet();
@@ -73,8 +72,6 @@ function UnifiedViteWeappTailwindcssPlugin(options = {}) {
73
72
  onUpdate(file, rawSource, code);
74
73
  }
75
74
  }
76
- },
77
- buildEnd() {
78
75
  onEnd();
79
76
  }
80
77
  };
package/dist/webpack.js CHANGED
@@ -4,9 +4,9 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('node:path');
6
6
  var fs = require('node:fs');
7
- var options = require('./options-b7411c31.js');
7
+ var options = require('./options-4284d635.js');
8
8
  var postcss = require('./postcss-4e99a8e8.js');
9
- var defaults = require('./defaults-344ba6d9.js');
9
+ var defaults = require('./defaults-c6437917.js');
10
10
  require('micromatch');
11
11
  require('magic-string');
12
12
  require('./replace.js');
@@ -17,6 +17,7 @@ require('@babel/generator');
17
17
  require('@babel/parser');
18
18
  require('@babel/traverse');
19
19
  require('@babel/types');
20
+ require('htmlparser2');
20
21
  require('postcss');
21
22
  require('@csstools/postcss-is-pseudo-class');
22
23
  require('semver');
@@ -38,7 +39,7 @@ class UnifiedWebpackPluginV5 {
38
39
  this.appType = this.options.appType;
39
40
  }
40
41
  apply(compiler) {
41
- const { mainCssChunkMatcher, disabled, onLoad, onUpdate, onEnd, onStart, styleHandler, patch, templateHandler, jsHandler, setMangleRuntimeSet } = this.options;
42
+ const { mainCssChunkMatcher, disabled, onLoad, onUpdate, onEnd, onStart, styleHandler, patch, templateHandler, jsHandler, setMangleRuntimeSet, runtimeLoaderPath } = this.options;
42
43
  if (disabled) {
43
44
  return;
44
45
  }
@@ -50,7 +51,7 @@ class UnifiedWebpackPluginV5 {
50
51
  return twPatcher.getClassSet();
51
52
  }
52
53
  onLoad();
53
- const loader = path__default["default"].resolve(__dirname, './weapp-tw-runtime-loader.js');
54
+ const loader = runtimeLoaderPath !== null && runtimeLoaderPath !== void 0 ? runtimeLoaderPath : path__default["default"].resolve(__dirname, './weapp-tw-runtime-loader.js');
54
55
  const isExisted = fs__default["default"].existsSync(loader);
55
56
  const WeappTwRuntimeAopLoader = {
56
57
  loader,
package/dist/webpack.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import path from 'node:path';
2
2
  import fs from 'node:fs';
3
- import { g as getOptions, a as createTailwindcssPatcher } from './options-8805cf56.mjs';
3
+ import { g as getOptions, a as createTailwindcssPatcher } from './options-ad0be036.mjs';
4
4
  import { a as pluginName } from './postcss-a551ddc0.mjs';
5
- import { g as getGroupedEntries } from './defaults-0743f523.mjs';
5
+ import { g as getGroupedEntries } from './defaults-72ea9566.mjs';
6
6
  import 'micromatch';
7
7
  import 'magic-string';
8
8
  import './replace.mjs';
@@ -13,6 +13,7 @@ import '@babel/generator';
13
13
  import '@babel/parser';
14
14
  import '@babel/traverse';
15
15
  import '@babel/types';
16
+ import 'htmlparser2';
16
17
  import 'postcss';
17
18
  import '@csstools/postcss-is-pseudo-class';
18
19
  import 'semver';
@@ -29,7 +30,7 @@ class UnifiedWebpackPluginV5 {
29
30
  this.appType = this.options.appType;
30
31
  }
31
32
  apply(compiler) {
32
- const { mainCssChunkMatcher, disabled, onLoad, onUpdate, onEnd, onStart, styleHandler, patch, templateHandler, jsHandler, setMangleRuntimeSet } = this.options;
33
+ const { mainCssChunkMatcher, disabled, onLoad, onUpdate, onEnd, onStart, styleHandler, patch, templateHandler, jsHandler, setMangleRuntimeSet, runtimeLoaderPath } = this.options;
33
34
  if (disabled) {
34
35
  return;
35
36
  }
@@ -41,7 +42,7 @@ class UnifiedWebpackPluginV5 {
41
42
  return twPatcher.getClassSet();
42
43
  }
43
44
  onLoad();
44
- const loader = path.resolve(__dirname, './weapp-tw-runtime-loader.js');
45
+ const loader = runtimeLoaderPath !== null && runtimeLoaderPath !== void 0 ? runtimeLoaderPath : path.resolve(__dirname, './weapp-tw-runtime-loader.js');
45
46
  const isExisted = fs.existsSync(loader);
46
47
  const WeappTwRuntimeAopLoader = {
47
48
  loader,
@@ -1,2 +1,2 @@
1
1
  export { replaceWxml } from './shared';
2
- export { generateCode, templateHandler, templateReplacer, customTemplateHandler, createTemplateHandler, extractSource } from './utils';
2
+ export { generateCode, templateReplacer, customTemplateHandler, createTemplateHandler, extractSource } from './utils';
@@ -1,7 +1,8 @@
1
+ import { ItemOrItemArray } from "../reg";
1
2
  import type { RawSource, ITemplateHandlerOptions } from "../types";
2
3
  export declare function generateCode(match: string, options?: ITemplateHandlerOptions): string;
3
4
  export declare function extractSource(original: string): RawSource[];
4
5
  export declare function templateReplacer(original: string, options?: ITemplateHandlerOptions): string;
5
- export declare function templateHandler(rawSource: string, options?: ITemplateHandlerOptions): string;
6
+ export declare function isPropsMatch(props: ItemOrItemArray<string | RegExp>, attr: string): boolean;
6
7
  export declare function customTemplateHandler(rawSource: string, options: Required<ITemplateHandlerOptions>): string;
7
8
  export declare function createTemplateHandler(options?: Omit<ITemplateHandlerOptions, 'runtimeSet'>): (rawSource: string, opt?: Pick<ITemplateHandlerOptions, 'runtimeSet'>) => string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "weapp-tailwindcss",
3
- "version": "2.7.1",
4
- "description": "把tailwindcss jit引擎,带给小程序开发者们\nbring tailwindcss jit engine to our miniprogram developers!",
3
+ "version": "2.8.1",
4
+ "description": "把tailwindcss jit引擎,带给小程序开发者们! bring tailwindcss jit engine to miniprogram developers!",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -106,9 +106,9 @@
106
106
  "@icebreakers/cli": "0.1.1",
107
107
  "@icebreakers/readme": "0.1.0",
108
108
  "@rollup/plugin-alias": "^5.0.0",
109
- "@rollup/plugin-commonjs": "^25.0.3",
109
+ "@rollup/plugin-commonjs": "^25.0.4",
110
110
  "@rollup/plugin-json": "^6.0.0",
111
- "@rollup/plugin-node-resolve": "^15.1.0",
111
+ "@rollup/plugin-node-resolve": "^15.2.0",
112
112
  "@rollup/plugin-terser": "^0.4.3",
113
113
  "@rollup/plugin-typescript": "^11.1.2",
114
114
  "@tsconfig/recommended": "^1.0.2",
@@ -123,22 +123,23 @@
123
123
  "@types/loader-utils": "^2.0.3",
124
124
  "@types/lodash": "^4.14.197",
125
125
  "@types/micromatch": "^4.0.2",
126
- "@types/node": "^20.4.9",
126
+ "@types/node": "^20.5.1",
127
127
  "@types/semver": "^7.5.0",
128
128
  "@types/vinyl": "^2.0.7",
129
129
  "@types/webpack": "^5.28.1",
130
130
  "@types/webpack-sources": "^3.2.0",
131
- "@vitest/coverage-v8": "^0.34.1",
132
- "autoprefixer": "^10.4.14",
131
+ "@vitest/coverage-v8": "^0.34.2",
132
+ "autoprefixer": "^10.4.15",
133
133
  "babel-loader": "^9.1.3",
134
- "bumpp": "^9.1.1",
134
+ "bumpp": "^9.2.0",
135
135
  "colorette": "^2.0.20",
136
136
  "cross-env": "^7.0.3",
137
137
  "css-loader": "^6.8.1",
138
138
  "dedent": "^1.5.1",
139
139
  "defu": "6.1.2",
140
140
  "del": "^7.0.0",
141
- "eslint": "8.46.0",
141
+ "domhandler": "^5.0.3",
142
+ "eslint": "8.47.0",
142
143
  "eslint-config-icebreaker": "^1.2.2",
143
144
  "eslint-config-prettier": "^9.0.0",
144
145
  "eslint-plugin-prettier": "^5.0.0",
@@ -148,7 +149,7 @@
148
149
  "gulp": "^4.0.2",
149
150
  "gulp-postcss": "^9.0.1",
150
151
  "html-loader": "^4.2.0",
151
- "jest": "^29.6.2",
152
+ "jest": "^29.6.3",
152
153
  "js-beautify": "^1.14.9",
153
154
  "klaw": "^4.1.0",
154
155
  "lodash": "^4.17.21",
@@ -158,7 +159,7 @@
158
159
  "postcss-load-config": "^4.0.1",
159
160
  "postcss-loader": "^7.3.3",
160
161
  "postcss-rem-to-responsive-pixel": "^5.1.3",
161
- "prettier": "^3.0.1",
162
+ "prettier": "^3.0.2",
162
163
  "promisify-loader-runner": "^1.0.0",
163
164
  "rollup": "^3.28.0",
164
165
  "rollup-plugin-visualizer": "^5.9.2",
@@ -169,12 +170,12 @@
169
170
  "ts-node": "^10.9.1",
170
171
  "ts-patch": "^3.0.2",
171
172
  "tsd": "^0.28.1",
172
- "tslib": "^2.6.1",
173
+ "tslib": "^2.6.2",
173
174
  "typescript": "^5.1.6",
174
175
  "typescript-transform-paths": "^3.4.6",
175
176
  "vinyl": "^3.0.0",
176
177
  "vite": "^4.4.9",
177
- "vitest": "^0.34.1",
178
+ "vitest": "^0.34.2",
178
179
  "weapp-tailwindcss-children": "^0.1.0",
179
180
  "webpack": "^5.88.2",
180
181
  "webpack-build-utils": "^0.0.4"
@@ -185,18 +186,19 @@
185
186
  "@babel/traverse": "^7.22.10",
186
187
  "@babel/types": "^7.22.10",
187
188
  "@csstools/postcss-is-pseudo-class": "^4.0.0",
188
- "@tailwindcss-mangle/shared": "^2.0.4",
189
- "@weapp-core/escape": "^1.0.0",
189
+ "@tailwindcss-mangle/shared": "^2.1.0",
190
+ "@weapp-core/escape": "^1.1.0",
190
191
  "@weapp-core/regex": "^1.0.0",
192
+ "htmlparser2": "^9.0.0",
191
193
  "loader-utils": "^2.0.3",
192
- "magic-string": "^0.30.2",
194
+ "magic-string": "^0.30.3",
193
195
  "micromatch": "^4.0.5",
194
- "postcss": "8.4.27",
196
+ "postcss": "8.4.28",
195
197
  "postcss-selector-parser": "^6.0.13",
196
198
  "semver": "^7.5.4",
197
- "tailwindcss-patch": "^2.0.4"
199
+ "tailwindcss-patch": "^2.1.1"
198
200
  },
199
- "packageManager": "pnpm@8.6.11",
201
+ "packageManager": "pnpm@8.6.12",
200
202
  "engines": {
201
203
  "node": ">=16.6.0"
202
204
  },