weapp-tailwindcss 2.6.2 → 2.7.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.
@@ -1,141 +1,21 @@
1
1
  import { isMatch } from 'micromatch';
2
- import * as t from '@babel/types';
2
+ import MagicString from 'magic-string';
3
+ import { replaceJs as replaceWxml } from './replace.mjs';
4
+ import { escapeStringRegexp, makeCustomAttributes, tagWithEitherClassAndHoverClassRegexp, templateClassExactRegexp, wxsTagRegexp, variableRegExp } from '@weapp-core/regex';
3
5
  import generate from '@babel/generator';
4
6
  import { parse, parseExpression } from '@babel/parser';
5
7
  import traverse from '@babel/traverse';
6
- import { replaceJs as replaceWxml } from './replace.mjs';
7
- import { i as isRegexp, n as noop, d as defaultOptions, a as isMap } from './defaults-c397b5f9.mjs';
8
+ import { n as noop, d as defaultOptions, i as isMap } from './defaults-0743f523.mjs';
9
+ import * as t from '@babel/types';
8
10
  import postcss from 'postcss';
9
- import { p as postcssWeappTailwindcss } from './postcss-99efb521.mjs';
11
+ import { p as postcssWeappTailwindcss } from './postcss-a551ddc0.mjs';
10
12
  import postcssIsPseudoClass from '@csstools/postcss-is-pseudo-class';
11
- import { S as SimpleMappingChars2String, M as MappingChars2String } from './dic-3790a3a4.mjs';
13
+ import { SimpleMappingChars2String, MappingChars2String } from '@weapp-core/escape';
12
14
  import path from 'node:path';
13
15
  import fs from 'node:fs';
14
16
  import { gte } from 'semver';
15
17
  import { monkeyPatchForExposingContext, requireResolve, TailwindcssPatcher } from 'tailwindcss-patch';
16
- import { ClassGenerator, defaultMangleClassFilter } from 'tailwindcss-mangle-shared';
17
-
18
- function isObject(value) {
19
- return value !== null && typeof value === "object";
20
- }
21
- function _defu(baseObject, defaults, namespace = ".", merger) {
22
- if (!isObject(defaults)) {
23
- return _defu(baseObject, {}, namespace, merger);
24
- }
25
- const object = Object.assign({}, defaults);
26
- for (const key in baseObject) {
27
- if (key === "__proto__" || key === "constructor") {
28
- continue;
29
- }
30
- const value = baseObject[key];
31
- if (value === null || value === void 0) {
32
- continue;
33
- }
34
- if (merger && merger(object, key, value, namespace)) {
35
- continue;
36
- }
37
- if (Array.isArray(value) && Array.isArray(object[key])) {
38
- object[key] = [...value, ...object[key]];
39
- } else if (isObject(value) && isObject(object[key])) {
40
- object[key] = _defu(
41
- value,
42
- object[key],
43
- (namespace ? `${namespace}.` : "") + key.toString(),
44
- merger
45
- );
46
- } else {
47
- object[key] = value;
48
- }
49
- }
50
- return object;
51
- }
52
- function createDefu(merger) {
53
- return (...arguments_) => (
54
- // eslint-disable-next-line unicorn/no-array-reduce
55
- arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
56
- );
57
- }
58
- const defu = createDefu();
59
-
60
- function escapeStringRegexp(str) {
61
- if (typeof str !== 'string') {
62
- throw new TypeError('Expected a string');
63
- }
64
- return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&').replaceAll('-', '\\x2d');
65
- }
66
- const templateClassExactRegexp = /(?<=^|\s)(?:hover-)?class=(?:["']\W+\s*\w+\()?["']([^"]+)["']/gs;
67
- const tagWithEitherClassAndHoverClassRegexp = /<[a-z][a-z-]*[a-z]*\s+[^>]*?(?:hover-)?class="[^"]*"[^>]*?\/?>/g;
68
- function handleRegexp(reg) {
69
- return `(?:${reg.source})`;
70
- }
71
- function getSourceString(input) {
72
- let result;
73
- if (typeof input === 'string') {
74
- result = input;
75
- }
76
- else if (isRegexp(input)) {
77
- result = input.source;
78
- }
79
- else {
80
- result = input.toString();
81
- }
82
- return result;
83
- }
84
- function makePattern(arr) {
85
- let pattern = '';
86
- if (Array.isArray(arr)) {
87
- pattern = arr
88
- .reduce((acc, cur) => {
89
- if (typeof cur === 'string') {
90
- acc.push(cur);
91
- }
92
- else if (isRegexp(cur)) {
93
- acc.push(handleRegexp(cur));
94
- }
95
- return acc;
96
- }, [])
97
- .join('|');
98
- }
99
- else if (typeof arr === 'string') {
100
- pattern = arr;
101
- }
102
- else if (isRegexp(arr)) {
103
- pattern = handleRegexp(arr);
104
- }
105
- return pattern;
106
- }
107
- function createTemplateHandlerMatchRegexp(tag, attrs, options = {}) {
108
- const { exact = true } = options;
109
- const prefix = exact ? '(?<=^|\\s)' : '';
110
- const pattern = makePattern(attrs);
111
- let tagPattern = getSourceString(tag);
112
- if (tagPattern === '*') {
113
- tagPattern = '[a-z][-a-z]*[a-z]*';
114
- }
115
- const source = `<(${tagPattern})\\s+[^>]*?(?:${prefix}(${pattern})="(?:[^"]*)")[^>]*?\\/?>`;
116
- return new RegExp(source, 'g');
117
- }
118
- function createTemplateClassRegexp(attrs, options = {}) {
119
- const { exact = true } = options;
120
- const prefix = exact ? '(?<=^|\\s)' : '';
121
- const pattern = makePattern(attrs);
122
- const source = `(?:${prefix}${pattern})=(?:["']\\W+\\s*(?:\\w+)\\()?["']([^"]+)['"]`;
123
- return new RegExp(source, 'gs');
124
- }
125
- function makeCustomAttributes(entries) {
126
- if (Array.isArray(entries)) {
127
- return entries.map(([k, v]) => {
128
- return {
129
- tagRegexp: createTemplateHandlerMatchRegexp(k, v),
130
- attrRegexp: createTemplateClassRegexp(v),
131
- tag: getSourceString(k),
132
- attrs: v
133
- };
134
- });
135
- }
136
- }
137
- const variableRegExp = /{{(.*?)}}/gs;
138
- const wxsTagRegexp = /<wxs\s*(?:[a-z][a-z-]*[a-z]*(?:\s*=\s*".*?")?)*\s*>(.*?)<\/wxs>/gs;
18
+ import { ClassGenerator, defaultMangleClassFilter } from '@tailwindcss-mangle/shared';
139
19
 
140
20
  const validateFilterRE = /[\w%-?\u00A0-\uFFFF-]/;
141
21
  function isValidSelector(selector = '') {
@@ -146,9 +26,7 @@ const splitCode = (code, allowDoubleQuotes = false) => {
146
26
  return code.split(splitter).filter((element) => isValidSelector(element));
147
27
  };
148
28
 
149
- const isProd = () => process.env.NODE_ENV === 'production';
150
-
151
- function handleValue(str, node, options) {
29
+ function regenerateHandleValue(str, node, options) {
152
30
  var _a;
153
31
  const set = options.classNameSet;
154
32
  const escapeMap = options.escapeMap;
@@ -175,44 +53,126 @@ function handleValue(str, node, options) {
175
53
  }
176
54
  return rawStr;
177
55
  }
56
+ function replaceHandleValue(str, node, options, ms, offset = 1) {
57
+ var _a;
58
+ const set = options.classNameSet;
59
+ const escapeMap = options.escapeMap;
60
+ const allowDoubleQuotes = (_a = options.arbitraryValues) === null || _a === void 0 ? void 0 : _a.allowDoubleQuotes;
61
+ const ctx = options.mangleContext;
62
+ const jsPreserveClass = options.jsPreserveClass;
63
+ const arr = splitCode(str, allowDoubleQuotes);
64
+ let rawStr = str;
65
+ for (const v of arr) {
66
+ if (set.has(v) && !(jsPreserveClass === null || jsPreserveClass === void 0 ? void 0 : jsPreserveClass(v))) {
67
+ let ignoreFlag = false;
68
+ if (Array.isArray(node.leadingComments)) {
69
+ ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes('weapp-tw') && x.value.includes('ignore')) > -1;
70
+ }
71
+ if (!ignoreFlag) {
72
+ if (ctx) {
73
+ rawStr = ctx.jsHandler(rawStr);
74
+ }
75
+ rawStr = rawStr.replaceAll(new RegExp(escapeStringRegexp(v), 'g'), replaceWxml(v, {
76
+ escapeMap
77
+ }));
78
+ }
79
+ }
80
+ }
81
+ if (typeof node.start === 'number' && typeof node.end === 'number') {
82
+ const start = node.start + offset;
83
+ const end = node.end - offset;
84
+ if (start < end) {
85
+ ms.update(node.start + offset, node.end - offset, rawStr);
86
+ }
87
+ }
88
+ return rawStr;
89
+ }
90
+
91
+ const isProd = () => process.env.NODE_ENV === 'production';
92
+
178
93
  function jsHandler(rawSource, options) {
179
94
  var _a;
180
95
  const ast = parse(rawSource, {
181
96
  sourceType: 'unambiguous'
182
97
  });
183
- const topt = {
184
- StringLiteral: {
185
- enter(p) {
186
- const n = p.node;
187
- n.value = handleValue(n.value, n, options);
188
- }
189
- },
190
- TemplateElement: {
191
- enter(p) {
192
- const n = p.node;
193
- n.value.raw = handleValue(n.value.raw, n, options);
98
+ if (options.strategy === 'replace') {
99
+ const ms = new MagicString(rawSource);
100
+ const ropt = {
101
+ StringLiteral: {
102
+ enter(p) {
103
+ const n = p.node;
104
+ replaceHandleValue(n.value, n, options, ms);
105
+ }
106
+ },
107
+ TemplateElement: {
108
+ enter(p) {
109
+ const n = p.node;
110
+ replaceHandleValue(n.value.raw, n, options, ms, 0);
111
+ }
112
+ },
113
+ CallExpression: {
114
+ enter(p) {
115
+ const calleePath = p.get('callee');
116
+ if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
117
+ p.traverse({
118
+ StringLiteral: {
119
+ enter(s) {
120
+ const res = jsHandler(s.node.value, options);
121
+ if (res.code) {
122
+ s.node.value = res.code;
123
+ }
124
+ }
125
+ }
126
+ });
127
+ }
128
+ }
194
129
  }
195
- },
196
- CallExpression: {
197
- enter(p) {
198
- const n = p.node;
199
- if (t.isIdentifier(n.callee) && n.callee.name === 'eval' && t.isStringLiteral(n.arguments[0])) {
200
- const res = jsHandler(n.arguments[0].value, options);
201
- if (res.code) {
202
- n.arguments[0].value = res.code;
130
+ };
131
+ traverse(ast, ropt);
132
+ return {
133
+ code: ms.toString()
134
+ };
135
+ }
136
+ else {
137
+ const gopt = {
138
+ StringLiteral: {
139
+ enter(p) {
140
+ const n = p.node;
141
+ n.value = regenerateHandleValue(n.value, n, options);
142
+ }
143
+ },
144
+ TemplateElement: {
145
+ enter(p) {
146
+ const n = p.node;
147
+ n.value.raw = regenerateHandleValue(n.value.raw, n, options);
148
+ }
149
+ },
150
+ CallExpression: {
151
+ enter(p) {
152
+ const calleePath = p.get('callee');
153
+ if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
154
+ p.traverse({
155
+ StringLiteral: {
156
+ enter(s) {
157
+ const res = jsHandler(s.node.value, options);
158
+ if (res.code) {
159
+ s.node.value = res.code;
160
+ }
161
+ }
162
+ }
163
+ });
203
164
  }
204
165
  }
205
166
  }
206
- },
207
- noScope: true
208
- };
209
- traverse(ast, topt);
210
- return generate(ast, {
211
- minified: (_a = options.minifiedJs) !== null && _a !== void 0 ? _a : isProd()
212
- });
167
+ };
168
+ traverse(ast, gopt);
169
+ return generate(ast, {
170
+ minified: (_a = options.minifiedJs) !== null && _a !== void 0 ? _a : isProd()
171
+ });
172
+ }
213
173
  }
214
174
  function createjsHandler(options) {
215
- const { mangleContext, arbitraryValues, minifiedJs, escapeMap, jsPreserveClass } = options;
175
+ const { mangleContext, arbitraryValues, minifiedJs, escapeMap, jsPreserveClass, strategy } = options;
216
176
  return (rawSource, set) => {
217
177
  return jsHandler(rawSource, {
218
178
  classNameSet: set,
@@ -220,11 +180,54 @@ function createjsHandler(options) {
220
180
  escapeMap,
221
181
  arbitraryValues,
222
182
  mangleContext,
223
- jsPreserveClass
183
+ jsPreserveClass,
184
+ strategy
224
185
  });
225
186
  };
226
187
  }
227
188
 
189
+ function isObject(value) {
190
+ return value !== null && typeof value === "object";
191
+ }
192
+ function _defu(baseObject, defaults, namespace = ".", merger) {
193
+ if (!isObject(defaults)) {
194
+ return _defu(baseObject, {}, namespace, merger);
195
+ }
196
+ const object = Object.assign({}, defaults);
197
+ for (const key in baseObject) {
198
+ if (key === "__proto__" || key === "constructor") {
199
+ continue;
200
+ }
201
+ const value = baseObject[key];
202
+ if (value === null || value === void 0) {
203
+ continue;
204
+ }
205
+ if (merger && merger(object, key, value, namespace)) {
206
+ continue;
207
+ }
208
+ if (Array.isArray(value) && Array.isArray(object[key])) {
209
+ object[key] = [...value, ...object[key]];
210
+ } else if (isObject(value) && isObject(object[key])) {
211
+ object[key] = _defu(
212
+ value,
213
+ object[key],
214
+ (namespace ? `${namespace}.` : "") + key.toString(),
215
+ merger
216
+ );
217
+ } else {
218
+ object[key] = value;
219
+ }
220
+ }
221
+ return object;
222
+ }
223
+ function createDefu(merger) {
224
+ return (...arguments_) => (
225
+ // eslint-disable-next-line unicorn/no-array-reduce
226
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
227
+ );
228
+ }
229
+ const defu = createDefu();
230
+
228
231
  function generateCode(match, options = {}) {
229
232
  const ast = parseExpression(match);
230
233
  traverse(ast, {
@@ -587,7 +590,7 @@ function getOptions(options = {}) {
587
590
  const result = defu(options, defaultOptions, {
588
591
  minifiedJs: isProd()
589
592
  });
590
- const { cssPreflight, customRuleCallback, cssPreflightRange, replaceUniversalSelectorWith, customAttributes, customReplaceDictionary, supportCustomLengthUnitsPatch, arbitraryValues, cssChildCombinatorReplaceValue, inlineWxs, injectAdditionalCssVarScope, jsPreserveClass, disabledDefaultTemplateHandler } = result;
593
+ const { cssPreflight, customRuleCallback, cssPreflightRange, replaceUniversalSelectorWith, customAttributes, customReplaceDictionary, supportCustomLengthUnitsPatch, arbitraryValues, cssChildCombinatorReplaceValue, inlineWxs, injectAdditionalCssVarScope, jsPreserveClass, disabledDefaultTemplateHandler, jsEscapeStrategy } = result;
591
594
  result.escapeMap = customReplaceDictionary;
592
595
  const cssInjectPreflight = createInjectPreflight(cssPreflight);
593
596
  const customAttributesEntities = isMap(options.customAttributes)
@@ -612,7 +615,8 @@ function getOptions(options = {}) {
612
615
  escapeMap,
613
616
  mangleContext,
614
617
  arbitraryValues,
615
- jsPreserveClass
618
+ jsPreserveClass,
619
+ strategy: jsEscapeStrategy
616
620
  });
617
621
  result.jsHandler = jsHandler;
618
622
  const templateHandler = createTemplateHandler({