weapp-tailwindcss 2.4.1 → 2.4.3

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.
@@ -6,14 +6,15 @@ var generate = require('@babel/generator');
6
6
  var parser = require('@babel/parser');
7
7
  var traverse = require('@babel/traverse');
8
8
  var replace = require('./replace.js');
9
- var shared = require('./shared-823d1fc1.js');
9
+ var shared = require('./shared-cdb5ee55.js');
10
10
  var postcss = require('postcss');
11
- var postcss$1 = require('./postcss-06f57f3f.js');
11
+ var postcss$1 = require('./postcss-b8952a11.js');
12
12
  var postcssIsPseudoClass = require('@csstools/postcss-is-pseudo-class');
13
13
  var path = require('node:path');
14
14
  var fs = require('node:fs');
15
15
  var semver = require('semver');
16
16
  var tailwindcssPatch = require('tailwindcss-patch');
17
+ var tailwindcssMangleShared = require('tailwindcss-mangle-shared');
17
18
 
18
19
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
20
 
@@ -85,12 +86,152 @@ function createDefu(merger) {
85
86
  }
86
87
  const defu = createDefu();
87
88
 
89
+ function isRegexp(value) {
90
+ return Object.prototype.toString.call(value) === '[object RegExp]';
91
+ }
92
+ function isMap(value) {
93
+ return Object.prototype.toString.call(value) === '[object Map]';
94
+ }
95
+ const noop = () => { };
96
+ function groupBy(arr, cb) {
97
+ if (!Array.isArray(arr)) {
98
+ throw new TypeError('expected an array for first argument');
99
+ }
100
+ if (typeof cb !== 'function') {
101
+ throw new TypeError('expected a function for second argument');
102
+ }
103
+ const result = {};
104
+ for (const item of arr) {
105
+ const bucketCategory = cb(item);
106
+ const bucket = result[bucketCategory];
107
+ if (Array.isArray(bucket)) {
108
+ result[bucketCategory].push(item);
109
+ }
110
+ else {
111
+ result[bucketCategory] = [item];
112
+ }
113
+ }
114
+ return result;
115
+ }
116
+ function getGroupedEntries(entries, options) {
117
+ const { cssMatcher, htmlMatcher, jsMatcher } = options;
118
+ const groupedEntries = groupBy(entries, ([file]) => {
119
+ if (cssMatcher(file)) {
120
+ return 'css';
121
+ }
122
+ else if (htmlMatcher(file)) {
123
+ return 'html';
124
+ }
125
+ else if (jsMatcher(file)) {
126
+ return 'js';
127
+ }
128
+ else {
129
+ return 'other';
130
+ }
131
+ });
132
+ return groupedEntries;
133
+ }
134
+
135
+ function escapeStringRegexp(str) {
136
+ if (typeof str !== 'string') {
137
+ throw new TypeError('Expected a string');
138
+ }
139
+ return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&').replaceAll('-', '\\x2d');
140
+ }
141
+ const templateClassExactRegexp = /(?<=^|\s)(?:hover-)?class=(?:["']\W+\s*\w+\()?["']([^"]+)["']/gs;
142
+ const tagWithEitherClassAndHoverClassRegexp = /<[a-z][a-z-]*[a-z]*\s+[^>]*?(?:hover-)?clas{2}="[^"]*"[^>]*?\/?>/g;
143
+ function handleRegexp(reg) {
144
+ return `(?:${reg.source})`;
145
+ }
146
+ function getSourceString(input) {
147
+ let result;
148
+ if (typeof input === 'string') {
149
+ result = input;
150
+ }
151
+ else if (isRegexp(input)) {
152
+ result = input.source;
153
+ }
154
+ else {
155
+ result = input.toString();
156
+ }
157
+ return result;
158
+ }
159
+ function makePattern(arr) {
160
+ let pattern = '';
161
+ if (Array.isArray(arr)) {
162
+ pattern = arr
163
+ .reduce((acc, cur) => {
164
+ if (typeof cur === 'string') {
165
+ acc.push(cur);
166
+ }
167
+ else if (isRegexp(cur)) {
168
+ acc.push(handleRegexp(cur));
169
+ }
170
+ return acc;
171
+ }, [])
172
+ .join('|');
173
+ }
174
+ else if (typeof arr === 'string') {
175
+ pattern = arr;
176
+ }
177
+ else if (isRegexp(arr)) {
178
+ pattern = handleRegexp(arr);
179
+ }
180
+ return pattern;
181
+ }
182
+ function createTempleteHandlerMatchRegexp(tag, attrs, options = {}) {
183
+ const { exact = true } = options;
184
+ const prefix = exact ? '(?<=^|\\s)' : '';
185
+ const pattern = makePattern(attrs);
186
+ let tagPattern = getSourceString(tag);
187
+ if (tagPattern === '*') {
188
+ tagPattern = '[a-z][-a-z]*[a-z]*';
189
+ }
190
+ const source = `<(${tagPattern})\\s+[^>]*?(?:${prefix}(${pattern})="(?:[^"]*)")[^>]*?\\/?>`;
191
+ return new RegExp(source, 'g');
192
+ }
193
+ function createTemplateClassRegexp(attrs, options = {}) {
194
+ const { exact = true } = options;
195
+ const prefix = exact ? '(?<=^|\\s)' : '';
196
+ const pattern = makePattern(attrs);
197
+ const source = `(?:${prefix}${pattern})=(?:["']\\W+\\s*(?:\\w+)\\()?["']([^"]+)['"]`;
198
+ return new RegExp(source, 'gs');
199
+ }
200
+ function makeCustomAttributes(entries) {
201
+ if (Array.isArray(entries)) {
202
+ return entries.map(([k, v]) => {
203
+ return {
204
+ tagRegexp: createTempleteHandlerMatchRegexp(k, v),
205
+ attrRegexp: createTemplateClassRegexp(v),
206
+ tag: getSourceString(k),
207
+ attrs: v
208
+ };
209
+ });
210
+ }
211
+ }
212
+ const variableRegExp = /{{(.*?)}}/gs;
213
+ function variableMatch(original) {
214
+ return variableRegExp.exec(original);
215
+ }
216
+
217
+ const validateFilterRE = /[\w%-?\u00A0-\uFFFF-]/;
218
+ function isValidSelector(selector = '') {
219
+ return validateFilterRE.test(selector);
220
+ }
221
+ const splitCode = (code, allowDoubleQuotes = false) => {
222
+ const splitter = allowDoubleQuotes ? /\s+/ : /\s+|"/;
223
+ return code.split(splitter).filter((element) => isValidSelector(element));
224
+ };
225
+
88
226
  const isProd = () => process.env.NODE_ENV === 'production';
89
227
 
90
228
  function handleValue(str, node, options) {
229
+ var _a;
91
230
  const set = options.classNameSet;
92
231
  const escapeMap = options.escapeMap;
93
- const arr = shared.splitCode(str);
232
+ const allowDoubleQuotes = (_a = options.arbitraryValues) === null || _a === void 0 ? void 0 : _a.allowDoubleQuotes;
233
+ const ctx = options.mangleContext;
234
+ const arr = splitCode(str, allowDoubleQuotes);
94
235
  let rawStr = str;
95
236
  for (const v of arr) {
96
237
  if (set.has(v)) {
@@ -99,9 +240,10 @@ function handleValue(str, node, options) {
99
240
  ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes('weapp-tw') && x.value.includes('ignore')) > -1;
100
241
  }
101
242
  if (!ignoreFlag) {
102
- const { jsHandler } = shared.useStore();
103
- rawStr = jsHandler(rawStr);
104
- rawStr = rawStr.replaceAll(new RegExp(shared.escapeStringRegexp(v), 'g'), replace.replaceJs(v, {
243
+ if (ctx) {
244
+ rawStr = ctx.jsHandler(rawStr);
245
+ }
246
+ rawStr = rawStr.replaceAll(new RegExp(escapeStringRegexp(v), 'g'), replace.replaceJs(v, {
105
247
  escapeMap
106
248
  }));
107
249
  }
@@ -146,11 +288,14 @@ function jsHandler(rawSource, options) {
146
288
  });
147
289
  }
148
290
  function createjsHandler(options) {
291
+ const { mangleContext, arbitraryValues, minifiedJs, escapeMap } = options;
149
292
  return (rawSource, set) => {
150
293
  return jsHandler(rawSource, {
151
294
  classNameSet: set,
152
- minifiedJs: options.minifiedJs,
153
- escapeMap: options.escapeMap
295
+ minifiedJs,
296
+ escapeMap,
297
+ arbitraryValues,
298
+ mangleContext
154
299
  });
155
300
  };
156
301
  }
@@ -204,11 +349,11 @@ const defaultOptions = {
204
349
  cssPreflightRange: 'view',
205
350
  replaceUniversalSelectorWith: 'view',
206
351
  disabled: false,
207
- customRuleCallback: shared.noop,
208
- onLoad: shared.noop,
209
- onStart: shared.noop,
210
- onEnd: shared.noop,
211
- onUpdate: shared.noop,
352
+ customRuleCallback: noop,
353
+ onLoad: noop,
354
+ onStart: noop,
355
+ onEnd: noop,
356
+ onUpdate: noop,
212
357
  customAttributes: {},
213
358
  customReplaceDictionary: shared.SimpleMappingChars2String,
214
359
  supportCustomLengthUnitsPatch: {
@@ -221,7 +366,10 @@ const defaultOptions = {
221
366
  overwrite: true
222
367
  }
223
368
  },
224
- appType: undefined
369
+ appType: undefined,
370
+ arbitraryValues: {
371
+ allowDoubleQuotes: false
372
+ }
225
373
  };
226
374
 
227
375
  function generateCode(match, options = {}) {
@@ -250,11 +398,11 @@ function generateCode(match, options = {}) {
250
398
  return code;
251
399
  }
252
400
  function extractSource(original) {
253
- let match = shared.variableMatch(original);
401
+ let match = variableMatch(original);
254
402
  const sources = [];
255
403
  while (match !== null) {
256
404
  const start = match.index;
257
- const end = shared.variableRegExp.lastIndex;
405
+ const end = variableRegExp.lastIndex;
258
406
  sources.push({
259
407
  start,
260
408
  end,
@@ -262,7 +410,7 @@ function extractSource(original) {
262
410
  prevConcatenated: !/\s/.test(original[start - 1]),
263
411
  nextConcatenated: !/\s/.test(original[end])
264
412
  });
265
- match = shared.variableMatch(original);
413
+ match = variableMatch(original);
266
414
  }
267
415
  return sources;
268
416
  }
@@ -276,7 +424,8 @@ function templeteReplacer(original, options = {}) {
276
424
  const before = original.slice(p, m.start);
277
425
  resultArray.push(replace.replaceJs(before, {
278
426
  keepEOL: true,
279
- escapeMap: options.escapeMap
427
+ escapeMap: options.escapeMap,
428
+ mangleContext: options.mangleContext
280
429
  }));
281
430
  p = m.start;
282
431
  if (m.raw.trim().length > 0) {
@@ -293,32 +442,31 @@ function templeteReplacer(original, options = {}) {
293
442
  const after = original.slice(m.end);
294
443
  resultArray.push(replace.replaceJs(after, {
295
444
  keepEOL: true,
296
- escapeMap: options.escapeMap
445
+ escapeMap: options.escapeMap,
446
+ mangleContext: options.mangleContext
297
447
  }));
298
448
  }
299
449
  }
300
- return resultArray
301
- .filter(Boolean)
302
- .join('')
303
- .trim();
450
+ return resultArray.filter(Boolean).join('').trim();
304
451
  }
305
452
  else {
306
453
  return replace.replaceJs(original, {
307
454
  keepEOL: false,
308
- escapeMap: options.escapeMap
455
+ escapeMap: options.escapeMap,
456
+ mangleContext: options.mangleContext
309
457
  });
310
458
  }
311
459
  }
312
460
  function templeteHandler(rawSource, options = {}) {
313
- return rawSource.replace(shared.tagWithEitherClassAndHoverClassRegexp, (m0) => {
314
- return m0.replace(shared.templateClassExactRegexp, (m1, className) => {
461
+ return rawSource.replace(tagWithEitherClassAndHoverClassRegexp, (m0) => {
462
+ return m0.replace(templateClassExactRegexp, (m1, className) => {
315
463
  return m1.replace(className, templeteReplacer(className, options));
316
464
  });
317
465
  });
318
466
  }
319
467
  function customTempleteHandler(rawSource, options = {}) {
320
468
  let source = templeteHandler(rawSource, options);
321
- const regexps = shared.makeCustomAttributes(options.customAttributesEntities);
469
+ const regexps = makeCustomAttributes(options.customAttributesEntities);
322
470
  if (regexps) {
323
471
  if (Array.isArray(regexps)) {
324
472
  for (const regexp of regexps) {
@@ -423,7 +571,7 @@ function getInstalledPkgJsonPath(options) {
423
571
  }
424
572
  function createPatch(options) {
425
573
  if (options === false) {
426
- return shared.noop;
574
+ return noop;
427
575
  }
428
576
  return () => {
429
577
  try {
@@ -483,6 +631,72 @@ function createTailwindcssPatcher() {
483
631
  });
484
632
  }
485
633
 
634
+ function getSelf(x) {
635
+ return x;
636
+ }
637
+ const defaultMangleContext = {
638
+ rawOptions: false,
639
+ runtimeSet: new Set(),
640
+ classGenerator: new tailwindcssMangleShared.ClassGenerator(),
641
+ filter: tailwindcssMangleShared.defaultMangleClassFilter,
642
+ cssHandler: getSelf,
643
+ jsHandler: getSelf,
644
+ wxmlHandler: getSelf
645
+ };
646
+ function useMangleStore() {
647
+ const ctx = Object.assign({}, defaultMangleContext);
648
+ function resetMangle() {
649
+ return Object.assign(ctx, defaultMangleContext);
650
+ }
651
+ function handleValue(rawSource) {
652
+ const arr = splitCode(rawSource);
653
+ for (const x of arr) {
654
+ if (ctx.runtimeSet.has(x)) {
655
+ rawSource = rawSource.replaceAll(new RegExp(escapeStringRegexp(x), 'g'), ctx.classGenerator.generateClassName(x).name);
656
+ }
657
+ }
658
+ return rawSource;
659
+ }
660
+ function initMangle(options) {
661
+ var _a;
662
+ ctx.rawOptions = options;
663
+ if (options) {
664
+ if (options === true) {
665
+ options = {
666
+ classGenerator: {},
667
+ mangleClassFilter: tailwindcssMangleShared.defaultMangleClassFilter
668
+ };
669
+ }
670
+ ctx.classGenerator = new tailwindcssMangleShared.ClassGenerator(options.classGenerator);
671
+ ctx.filter = (_a = options.mangleClassFilter) !== null && _a !== void 0 ? _a : tailwindcssMangleShared.defaultMangleClassFilter;
672
+ ctx.jsHandler = (rawSource) => {
673
+ return handleValue(rawSource);
674
+ };
675
+ ctx.cssHandler = (rawSource) => {
676
+ return handleValue(rawSource);
677
+ };
678
+ ctx.wxmlHandler = (rawSource) => {
679
+ return handleValue(rawSource);
680
+ };
681
+ }
682
+ }
683
+ function setMangleRuntimeSet(runtimeSet) {
684
+ const newSet = new Set();
685
+ for (const c of runtimeSet) {
686
+ if (ctx.filter(c)) {
687
+ newSet.add(c);
688
+ }
689
+ }
690
+ ctx.runtimeSet = newSet;
691
+ }
692
+ return {
693
+ mangleContext: ctx,
694
+ resetMangle,
695
+ initMangle,
696
+ setMangleRuntimeSet
697
+ };
698
+ }
699
+
486
700
  function createGlobMatcher(pattern) {
487
701
  return function (file) {
488
702
  return micromatch.isMatch(file, pattern);
@@ -493,18 +707,7 @@ function normalizeMatcher(options, key) {
493
707
  options[key] = createGlobMatcher(options[key]);
494
708
  }
495
709
  }
496
- function getOptions(options = {}, modules = ['style', 'templete', 'patch', 'js']) {
497
- const registerModules = modules.reduce((acc, cur) => {
498
- if (acc[cur] !== undefined) {
499
- acc[cur] = true;
500
- }
501
- return acc;
502
- }, {
503
- templete: false,
504
- style: false,
505
- patch: false,
506
- js: false
507
- });
710
+ function getOptions(options = {}) {
508
711
  if (options.supportCustomLengthUnitsPatch === true) {
509
712
  options.supportCustomLengthUnitsPatch = undefined;
510
713
  }
@@ -521,38 +724,40 @@ function getOptions(options = {}, modules = ['style', 'templete', 'patch', 'js']
521
724
  const result = defu(options, defaultOptions, {
522
725
  minifiedJs: isProd()
523
726
  });
524
- const { cssPreflight, customRuleCallback, cssPreflightRange, replaceUniversalSelectorWith, customAttributes, customReplaceDictionary, supportCustomLengthUnitsPatch } = result;
727
+ const { cssPreflight, customRuleCallback, cssPreflightRange, replaceUniversalSelectorWith, customAttributes, customReplaceDictionary, supportCustomLengthUnitsPatch, arbitraryValues } = result;
525
728
  result.escapeMap = customReplaceDictionary;
526
729
  const cssInjectPreflight = createInjectPreflight(cssPreflight);
527
- const customAttributesEntities = shared.isMap(options.customAttributes) ? [...options.customAttributes.entries()] : Object.entries(customAttributes);
730
+ const customAttributesEntities = isMap(options.customAttributes)
731
+ ? [...options.customAttributes.entries()]
732
+ : Object.entries(customAttributes);
528
733
  const { escapeMap, minifiedJs } = result;
529
- if (registerModules.templete) {
530
- result.templeteHandler = createTempleteHandler({
531
- customAttributesEntities,
532
- escapeMap
533
- });
534
- }
535
- if (registerModules.style) {
536
- result.styleHandler = createStyleHandler({
537
- cssInjectPreflight,
538
- customRuleCallback,
539
- cssPreflightRange,
540
- replaceUniversalSelectorWith,
541
- escapeMap
542
- });
543
- }
544
- if (registerModules.js) {
545
- result.jsHandler = createjsHandler({
546
- minifiedJs,
547
- escapeMap
548
- });
549
- }
550
- if (registerModules.patch) {
551
- result.patch = createPatch(supportCustomLengthUnitsPatch);
552
- }
734
+ const { initMangle, mangleContext, setMangleRuntimeSet } = useMangleStore();
735
+ initMangle(options.mangle);
736
+ result.templeteHandler = createTempleteHandler({
737
+ customAttributesEntities,
738
+ escapeMap,
739
+ mangleContext
740
+ });
741
+ result.styleHandler = createStyleHandler({
742
+ cssInjectPreflight,
743
+ customRuleCallback,
744
+ cssPreflightRange,
745
+ replaceUniversalSelectorWith,
746
+ escapeMap,
747
+ mangleContext
748
+ });
749
+ result.jsHandler = createjsHandler({
750
+ minifiedJs,
751
+ escapeMap,
752
+ mangleContext,
753
+ arbitraryValues
754
+ });
755
+ result.patch = createPatch(supportCustomLengthUnitsPatch);
756
+ result.setMangleRuntimeSet = setMangleRuntimeSet;
553
757
  return result;
554
758
  }
555
759
 
556
760
  exports.createPatch = createPatch;
557
761
  exports.createTailwindcssPatcher = createTailwindcssPatcher;
762
+ exports.getGroupedEntries = getGroupedEntries;
558
763
  exports.getOptions = getOptions;