weapp-tailwindcss 3.0.10 → 3.1.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.
Files changed (39) hide show
  1. package/dist/cache/index.d.ts +2 -1
  2. package/dist/cli.js +2 -2
  3. package/dist/cli.mjs +2 -2
  4. package/dist/core.d.ts +2 -2
  5. package/dist/core.js +13 -11
  6. package/dist/core.mjs +13 -11
  7. package/dist/css-macro/postcss.js +3 -3
  8. package/dist/css-macro/postcss.mjs +3 -3
  9. package/dist/gulp.js +5 -3
  10. package/dist/gulp.mjs +5 -3
  11. package/dist/{index-DfbtBzIV.mjs → index-BatBMY3C.mjs} +10 -10
  12. package/dist/index-C9cFs8_u.js +136 -0
  13. package/dist/{index-CKzzVDhq.js → index-CarwQa1I.js} +6 -2
  14. package/dist/{index-CLxBL7MC.mjs → index-CsZGfl55.mjs} +3 -2
  15. package/dist/index-DEWZ8_xX.mjs +130 -0
  16. package/dist/{index-C7QwtXvo.mjs → index-DKNUquoz.mjs} +6 -2
  17. package/dist/{index-CKXO21Qe.js → index-DyK5Vd4R.js} +3 -2
  18. package/dist/{index-BBGsdK79.js → index-jf-1m663.js} +10 -10
  19. package/dist/index.js +6 -6
  20. package/dist/index.mjs +6 -6
  21. package/dist/js/handlers.d.ts +9 -2
  22. package/dist/js/index.d.ts +2 -1
  23. package/dist/{options-Ce8c4jgF.js → options-AIjWBFPo.js} +154 -60
  24. package/dist/{options-D74YiXdv.mjs → options-D6Mza941.mjs} +154 -60
  25. package/dist/postcss.js +1 -1
  26. package/dist/postcss.mjs +1 -1
  27. package/dist/types.d.ts +7 -3
  28. package/dist/{v5-B5nztYEa.js → v5-BV64BMJS.js} +10 -10
  29. package/dist/{v5-KEbKT7UN.mjs → v5-CH-EIloz.mjs} +10 -10
  30. package/dist/vite.js +4 -4
  31. package/dist/vite.mjs +4 -4
  32. package/dist/webpack.js +4 -4
  33. package/dist/webpack.mjs +4 -4
  34. package/dist/webpack4.js +10 -10
  35. package/dist/webpack4.mjs +10 -10
  36. package/dist/wxml/utils.d.ts +2 -2
  37. package/package.json +41 -36
  38. package/dist/index-DMO3Y3vp.mjs +0 -64
  39. package/dist/index-DXyNWU95.js +0 -70
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
 
3
- var options = require('./options-Ce8c4jgF.js');
4
- var index = require('./index-CKzzVDhq.js');
3
+ var options = require('./options-AIjWBFPo.js');
4
+ var index = require('./index-CarwQa1I.js');
5
5
  var defaults = require('./defaults-BT3A8Rzc.js');
6
- var index$1 = require('./index-CKXO21Qe.js');
6
+ var index$1 = require('./index-DyK5Vd4R.js');
7
7
 
8
- const debug = index$1.createDebug('generateBundle: ');
8
+ const debug = index$1.createDebug();
9
9
  function UnifiedViteWeappTailwindcssPlugin(options$1 = {}) {
10
10
  if (options$1.customReplaceDictionary === undefined) {
11
11
  options$1.customReplaceDictionary = 'simple';
@@ -48,8 +48,8 @@ function UnifiedViteWeappTailwindcssPlugin(options$1 = {}) {
48
48
  else {
49
49
  return false;
50
50
  }
51
- }, () => {
52
- originalSource.source = templateHandler(oldVal, {
51
+ }, () => options.__awaiter(this, void 0, void 0, function* () {
52
+ originalSource.source = yield templateHandler(oldVal, {
53
53
  runtimeSet
54
54
  });
55
55
  onUpdate(file, oldVal, originalSource.source);
@@ -59,7 +59,7 @@ function UnifiedViteWeappTailwindcssPlugin(options$1 = {}) {
59
59
  key: file,
60
60
  source: originalSource.source
61
61
  };
62
- });
62
+ }));
63
63
  }
64
64
  debug('html handle finish, total: %d, no-cached: %d', groupedEntries.html.length, noCachedCount);
65
65
  }
@@ -79,10 +79,10 @@ function UnifiedViteWeappTailwindcssPlugin(options$1 = {}) {
79
79
  else {
80
80
  return false;
81
81
  }
82
- }, () => {
82
+ }, () => options.__awaiter(this, void 0, void 0, function* () {
83
83
  const mapFilename = file + '.map';
84
84
  const hasMap = Boolean(bundle[mapFilename]);
85
- const { code, map } = jsHandler(rawSource, runtimeSet, {
85
+ const { code, map } = yield jsHandler(rawSource, runtimeSet, {
86
86
  generateMap: hasMap
87
87
  });
88
88
  originalSource.code = code;
@@ -96,7 +96,7 @@ function UnifiedViteWeappTailwindcssPlugin(options$1 = {}) {
96
96
  key: file,
97
97
  source: code
98
98
  };
99
- });
99
+ }));
100
100
  }
101
101
  debug('js handle finish, total: %d, no-cached: %d', groupedEntries.js.length, noCachedCount);
102
102
  }
package/dist/index.js CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var v5 = require('./v5-B5nztYEa.js');
6
- var index = require('./index-BBGsdK79.js');
7
- var index$1 = require('./index-DXyNWU95.js');
8
- require('./options-Ce8c4jgF.js');
5
+ var v5 = require('./v5-BV64BMJS.js');
6
+ var index = require('./index-jf-1m663.js');
7
+ var index$1 = require('./index-C9cFs8_u.js');
8
+ require('./options-AIjWBFPo.js');
9
9
  require('magic-string');
10
10
  require('./replace.js');
11
11
  require('@weapp-core/escape');
@@ -20,7 +20,7 @@ require('./defu-Cdz2PomB.js');
20
20
  require('@babel/types');
21
21
  require('htmlparser2');
22
22
  require('postcss');
23
- require('./index-CKzzVDhq.js');
23
+ require('./index-CarwQa1I.js');
24
24
  require('@csstools/postcss-is-pseudo-class');
25
25
  require('postcss-rem-to-responsive-pixel');
26
26
  require('postcss-selector-parser');
@@ -31,7 +31,7 @@ require('tailwindcss-patch');
31
31
  require('@tailwindcss-mangle/shared');
32
32
  require('lru-cache');
33
33
  require('md5');
34
- require('./index-CKXO21Qe.js');
34
+ require('./index-DyK5Vd4R.js');
35
35
  require('debug');
36
36
  require('node:stream');
37
37
 
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- export { U as UnifiedWebpackPluginV5 } from './v5-KEbKT7UN.mjs';
2
- export { U as UnifiedViteWeappTailwindcssPlugin } from './index-DfbtBzIV.mjs';
3
- export { c as createPlugins } from './index-DMO3Y3vp.mjs';
4
- import './options-D74YiXdv.mjs';
1
+ export { U as UnifiedWebpackPluginV5 } from './v5-CH-EIloz.mjs';
2
+ export { U as UnifiedViteWeappTailwindcssPlugin } from './index-BatBMY3C.mjs';
3
+ export { c as createPlugins } from './index-DEWZ8_xX.mjs';
4
+ import './options-D6Mza941.mjs';
5
5
  import 'magic-string';
6
6
  import './replace.mjs';
7
7
  import '@weapp-core/escape';
@@ -16,7 +16,7 @@ import './defu-n46TJT6t.mjs';
16
16
  import '@babel/types';
17
17
  import 'htmlparser2';
18
18
  import 'postcss';
19
- import './index-C7QwtXvo.mjs';
19
+ import './index-DKNUquoz.mjs';
20
20
  import '@csstools/postcss-is-pseudo-class';
21
21
  import 'postcss-rem-to-responsive-pixel';
22
22
  import 'postcss-selector-parser';
@@ -27,6 +27,6 @@ import 'tailwindcss-patch';
27
27
  import '@tailwindcss-mangle/shared';
28
28
  import 'lru-cache';
29
29
  import 'md5';
30
- import './index-CLxBL7MC.mjs';
30
+ import './index-CsZGfl55.mjs';
31
31
  import 'debug';
32
32
  import 'node:stream';
@@ -1,4 +1,11 @@
1
- import type { StringLiteral, TemplateElement } from '@babel/types';
2
1
  import MagicString from 'magic-string';
3
2
  import type { IJsHandlerOptions } from "../types";
4
- export declare function replaceHandleValue(str: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions, ms: MagicString, offset?: number): string;
3
+ interface ReplaceNode {
4
+ leadingComments?: {
5
+ value: string;
6
+ }[] | null | undefined;
7
+ start?: number | null;
8
+ end?: number | null;
9
+ }
10
+ export declare function replaceHandleValue(str: string, node: ReplaceNode, options: IJsHandlerOptions, ms: MagicString, offset?: number): string;
11
+ export {};
@@ -1,3 +1,4 @@
1
1
  import type { CreateJsHandlerOptions, IJsHandlerOptions, JsHandlerResult } from "../types";
2
2
  export declare function jsHandler(rawSource: string, options: IJsHandlerOptions): JsHandlerResult;
3
- export declare function createJsHandler(options: CreateJsHandlerOptions): (rawSource: string, set: Set<string>, options?: CreateJsHandlerOptions) => JsHandlerResult;
3
+ export declare function jsHandlerAsync(rawSource: string, options: IJsHandlerOptions): Promise<JsHandlerResult>;
4
+ export declare function createJsHandler(options: CreateJsHandlerOptions): (rawSource: string, set: Set<string>, options?: CreateJsHandlerOptions) => JsHandlerResult | Promise<JsHandlerResult>;
@@ -12,7 +12,7 @@ var defaults = require('./defaults-BT3A8Rzc.js');
12
12
  var t = require('@babel/types');
13
13
  var htmlparser2 = require('htmlparser2');
14
14
  var postcss = require('postcss');
15
- var index = require('./index-CKzzVDhq.js');
15
+ var index = require('./index-CarwQa1I.js');
16
16
  var path = require('node:path');
17
17
  var fs = require('node:fs');
18
18
  var semver = require('semver');
@@ -91,12 +91,23 @@ const splitCode = (code, allowDoubleQuotes = false) => {
91
91
  return code.split(splitter).filter((element) => isValidSelector(element));
92
92
  };
93
93
 
94
+ function decodeUnicode(s) {
95
+ return unescape(s.replaceAll(/\\(u[\dA-Fa-f]{4})/gm, '%$1'));
96
+ }
94
97
  function replaceHandleValue(str, node, options, ms, offset = 0) {
95
- const { classNameSet: set, escapeMap, mangleContext: ctx, needEscaped = false, jsPreserveClass, arbitraryValues, always } = options;
98
+ const { classNameSet: set, escapeMap, mangleContext: ctx, needEscaped = false, jsPreserveClass, arbitraryValues, always, unescapeUnicode } = options;
96
99
  const allowDoubleQuotes = arbitraryValues === null || arbitraryValues === void 0 ? void 0 : arbitraryValues.allowDoubleQuotes;
97
100
  const arr = splitCode(str, allowDoubleQuotes);
98
101
  let rawStr = str;
99
- for (const v of arr) {
102
+ let needDecodeUnicode = false;
103
+ if (unescapeUnicode && rawStr.includes('\\')) {
104
+ rawStr = decodeUnicode(rawStr);
105
+ needDecodeUnicode = true;
106
+ }
107
+ for (let v of arr) {
108
+ if (needDecodeUnicode && v.includes('\\')) {
109
+ v = decodeUnicode(v);
110
+ }
100
111
  if (always || (set && set.has(v) && !(jsPreserveClass === null || jsPreserveClass === void 0 ? void 0 : jsPreserveClass(v)))) {
101
112
  let ignoreFlag = false;
102
113
  if (Array.isArray(node.leadingComments)) {
@@ -130,7 +141,46 @@ function isEvalPath(p) {
130
141
  }
131
142
  return false;
132
143
  }
144
+ function getAstGrep() {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ try {
147
+ const { js } = yield import('@ast-grep/napi');
148
+ return js;
149
+ }
150
+ catch (error) {
151
+ console.warn('请先安装 `@ast-grep/napi` , 安装完成后再尝试运行!');
152
+ throw error;
153
+ }
154
+ });
155
+ }
156
+ function astGrepUpdateString(ast, options, ms) {
157
+ return __awaiter(this, void 0, void 0, function* () {
158
+ const js = yield getAstGrep();
159
+ const nodes = ast.findAll(js.kind('string'));
160
+ for (const node of nodes) {
161
+ const range = node.range();
162
+ const text = node.text();
163
+ replaceHandleValue(text.slice(1, -1), {
164
+ end: range.end.index - 1,
165
+ start: range.start.index + 1
166
+ }, Object.assign(Object.assign({}, options), { unescapeUnicode: true }), ms, 0);
167
+ }
168
+ const templateNodes = ast.findAll(js.kind('template_string'));
169
+ for (const node of templateNodes) {
170
+ const fragments = node.findAll(js.kind('string_fragment'));
171
+ for (const fragment of fragments) {
172
+ const range = fragment.range();
173
+ const text = fragment.text();
174
+ replaceHandleValue(text, {
175
+ end: range.end.index,
176
+ start: range.start.index
177
+ }, Object.assign(Object.assign({}, options), { unescapeUnicode: true }), ms, 0);
178
+ }
179
+ }
180
+ });
181
+ }
133
182
  function jsHandler(rawSource, options) {
183
+ const ms = new MagicString__default["default"](rawSource);
134
184
  let ast;
135
185
  try {
136
186
  ast = parser.parse(rawSource, {
@@ -142,8 +192,7 @@ function jsHandler(rawSource, options) {
142
192
  code: rawSource
143
193
  };
144
194
  }
145
- const ms = new MagicString__default["default"](rawSource);
146
- const ropt = {
195
+ const traverseOptions = {
147
196
  StringLiteral: {
148
197
  enter(p) {
149
198
  var _a;
@@ -204,14 +253,33 @@ function jsHandler(rawSource, options) {
204
253
  }
205
254
  }
206
255
  };
207
- traverse__default["default"](ast, ropt);
208
- const result = {
256
+ traverse__default["default"](ast, traverseOptions);
257
+ return {
209
258
  code: ms.toString()
210
259
  };
211
- return result;
260
+ }
261
+ function jsHandlerAsync(rawSource, options) {
262
+ return __awaiter(this, void 0, void 0, function* () {
263
+ const ms = new MagicString__default["default"](rawSource);
264
+ const js = yield getAstGrep();
265
+ let ast;
266
+ try {
267
+ const root = yield js.parseAsync(rawSource);
268
+ ast = root.root();
269
+ }
270
+ catch (_a) {
271
+ return {
272
+ code: rawSource
273
+ };
274
+ }
275
+ yield astGrepUpdateString(ast, options, ms);
276
+ return {
277
+ code: ms.toString()
278
+ };
279
+ });
212
280
  }
213
281
  function createJsHandler(options) {
214
- const { mangleContext, arbitraryValues, escapeMap, jsPreserveClass, generateMap } = options;
282
+ const { mangleContext, arbitraryValues, escapeMap, jsPreserveClass, generateMap, jsAstTool } = options;
215
283
  return (rawSource, set, options) => {
216
284
  const opts = defaults.defuOverrideArray(options, {
217
285
  classNameSet: set,
@@ -219,8 +287,12 @@ function createJsHandler(options) {
219
287
  arbitraryValues,
220
288
  mangleContext,
221
289
  jsPreserveClass,
222
- generateMap
290
+ generateMap,
291
+ jsAstTool
223
292
  });
293
+ if (opts.jsAstTool === 'ast-grep') {
294
+ return jsHandlerAsync(rawSource, opts);
295
+ }
224
296
  return jsHandler(rawSource, opts);
225
297
  };
226
298
  }
@@ -323,7 +395,7 @@ function regTest(reg, str) {
323
395
  function isPropsMatch(props, attr) {
324
396
  if (Array.isArray(props)) {
325
397
  for (const prop of props) {
326
- const res = typeof prop === 'string' ? prop.toLowerCase() === attr : regTest(prop, attr);
398
+ const res = typeof prop === 'string' ? prop.toLowerCase() === attr.toLowerCase() : regTest(prop, attr);
327
399
  if (res) {
328
400
  return res;
329
401
  }
@@ -338,51 +410,63 @@ function isPropsMatch(props, attr) {
338
410
  }
339
411
  }
340
412
  function customTemplateHandler(rawSource, options) {
341
- const { customAttributesEntities = [], disabledDefaultTemplateHandler, inlineWxs, runtimeSet, jsHandler } = options !== null && options !== void 0 ? options : {};
342
- const s = new MagicString__default["default"](rawSource);
343
- let tag = '';
344
- const parser = new htmlparser2.Parser({
345
- onopentagname(name) {
346
- tag = name;
347
- },
348
- onattribute(name, value, quote) {
349
- if (value) {
350
- function update() {
351
- s.update(parser.startIndex + name.length + 2, parser.endIndex - 1, templateReplacer(value, Object.assign(Object.assign({}, options), { quote })));
352
- }
353
- if (!disabledDefaultTemplateHandler && (name === 'class' || name === 'hover-class' || name === 'virtualhostclass')) {
354
- update();
355
- }
356
- for (const [t, props] of customAttributesEntities) {
357
- if (t === '*') {
358
- if (isPropsMatch(props, name)) {
359
- update();
360
- }
413
+ return __awaiter(this, void 0, void 0, function* () {
414
+ const { customAttributesEntities = [], disabledDefaultTemplateHandler, inlineWxs, runtimeSet, jsHandler } = options !== null && options !== void 0 ? options : {};
415
+ const s = new MagicString__default["default"](rawSource);
416
+ let tag = '';
417
+ const wxsArray = [];
418
+ const parser = new htmlparser2.Parser({
419
+ onopentagname(name) {
420
+ tag = name;
421
+ },
422
+ onattribute(name, value, quote) {
423
+ if (value) {
424
+ function update() {
425
+ s.update(parser.startIndex + name.length + 2, parser.endIndex - 1, templateReplacer(value, Object.assign(Object.assign({}, options), { quote })));
426
+ }
427
+ if (!disabledDefaultTemplateHandler && (name === 'class' || name === 'hover-class' || name === 'virtualHostClass' || name === 'virtualhostclass')) {
428
+ update();
361
429
  }
362
- else if (typeof t === 'string') {
363
- if (t === tag && isPropsMatch(props, name)) {
430
+ for (const [t, props] of customAttributesEntities) {
431
+ if (t === '*') {
432
+ if (isPropsMatch(props, name)) {
433
+ update();
434
+ }
435
+ }
436
+ else if (typeof t === 'string') {
437
+ if (t === tag && isPropsMatch(props, name)) {
438
+ update();
439
+ }
440
+ }
441
+ else if (regTest(t, tag) && isPropsMatch(props, name)) {
364
442
  update();
365
443
  }
366
444
  }
367
- else if (regTest(t, tag) && isPropsMatch(props, name)) {
368
- update();
369
- }
370
445
  }
446
+ },
447
+ ontext(data) {
448
+ if (inlineWxs && tag === 'wxs') {
449
+ wxsArray.push({
450
+ data,
451
+ endIndex: parser.endIndex + 1,
452
+ startIndex: parser.startIndex
453
+ });
454
+ }
455
+ },
456
+ onclosetag() {
457
+ tag = '';
371
458
  }
372
- },
373
- ontext(data) {
374
- if (inlineWxs && tag === 'wxs') {
375
- const code = jsHandler(data, runtimeSet).code;
376
- s.update(parser.startIndex, parser.endIndex + 1, code);
377
- }
378
- },
379
- onclosetag() {
380
- tag = '';
459
+ }, {
460
+ xmlMode: true
461
+ });
462
+ parser.write(s.original);
463
+ parser.end();
464
+ for (const { data, endIndex, startIndex } of wxsArray) {
465
+ const { code } = yield jsHandler(data, runtimeSet);
466
+ s.update(startIndex, endIndex, code);
381
467
  }
468
+ return s.toString();
382
469
  });
383
- parser.write(s.original);
384
- parser.end();
385
- return s.toString();
386
470
  }
387
471
  function createTemplateHandler(options = {}) {
388
472
  return (rawSource, opt = {}) => {
@@ -602,7 +686,8 @@ function useMangleStore() {
602
686
  };
603
687
  }
604
688
 
605
- function createCache() {
689
+ function createCache(options) {
690
+ const disabled = options === false;
606
691
  const hashMap = new Map();
607
692
  const instance = new lruCache.LRUCache({
608
693
  max: 1024,
@@ -651,16 +736,24 @@ function createCache() {
651
736
  },
652
737
  process(key, callback, fallback) {
653
738
  return __awaiter(this, void 0, void 0, function* () {
654
- const hit = this.getHashValue(key);
655
- if (hit && !hit.changed) {
656
- const returnFlag = yield callback();
657
- if (returnFlag !== false) {
658
- return;
739
+ if (disabled) {
740
+ const res = yield fallback();
741
+ if (res) {
742
+ this.set(res.key, res.source);
659
743
  }
660
744
  }
661
- const res = yield fallback();
662
- if (res) {
663
- this.set(res.key, res.source);
745
+ else {
746
+ const hit = this.getHashValue(key);
747
+ if (hit && !hit.changed) {
748
+ const returnFlag = yield callback();
749
+ if (returnFlag !== false) {
750
+ return;
751
+ }
752
+ }
753
+ const res = yield fallback();
754
+ if (res) {
755
+ this.set(res.key, res.source);
756
+ }
664
757
  }
665
758
  });
666
759
  }
@@ -672,7 +765,7 @@ function getOptions(options = {}) {
672
765
  options.customReplaceDictionary = escape$1.SimpleMappingChars2String;
673
766
  }
674
767
  const result = defaults.defuOverrideArray(options, defaults.defaultOptions, {});
675
- const { cssPreflight, customRuleCallback, cssPreflightRange, customAttributes, customReplaceDictionary, supportCustomLengthUnitsPatch, arbitraryValues, cssChildCombinatorReplaceValue, inlineWxs, injectAdditionalCssVarScope, jsPreserveClass, disabledDefaultTemplateHandler, cssSelectorReplacement, rem2rpx } = result;
768
+ const { cssPreflight, customRuleCallback, cssPreflightRange, customAttributes, customReplaceDictionary, supportCustomLengthUnitsPatch, arbitraryValues, cssChildCombinatorReplaceValue, inlineWxs, injectAdditionalCssVarScope, jsPreserveClass, disabledDefaultTemplateHandler, cssSelectorReplacement, rem2rpx, cache, jsAstTool } = result;
676
769
  result.escapeMap = customReplaceDictionary;
677
770
  const cssInjectPreflight = createInjectPreflight(cssPreflight);
678
771
  const customAttributesEntities = defaults.isMap(options.customAttributes)
@@ -698,7 +791,8 @@ function getOptions(options = {}) {
698
791
  mangleContext,
699
792
  arbitraryValues,
700
793
  jsPreserveClass,
701
- generateMap: true
794
+ generateMap: true,
795
+ jsAstTool
702
796
  });
703
797
  result.jsHandler = jsHandler;
704
798
  const templateHandler = createTemplateHandler({
@@ -712,7 +806,7 @@ function getOptions(options = {}) {
712
806
  result.templateHandler = templateHandler;
713
807
  result.patch = createPatch(supportCustomLengthUnitsPatch);
714
808
  result.setMangleRuntimeSet = setMangleRuntimeSet;
715
- result.cache = createCache();
809
+ result.cache = cache === undefined || typeof cache === 'boolean' ? createCache(cache) : cache;
716
810
  return result;
717
811
  }
718
812