weapp-tailwindcss 5.0.4 → 5.0.6

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 (57) hide show
  1. package/dist/{auto-BZ4n4Icc.js → auto-Cvy0zWpS.js} +1 -1
  2. package/dist/{bundle-state-D3RZHkXz.js → bundle-state-CFgzVHFP.js} +8 -2
  3. package/dist/{bundle-state-C-p9eFz1.mjs → bundle-state-CdNXQl-o.mjs} +2 -2
  4. package/dist/bundlers/shared/generator-css/directives.d.ts +1 -0
  5. package/dist/bundlers/vite/generate-bundle/css-handler-options.d.ts +1 -0
  6. package/dist/bundlers/vite/generate-bundle.d.ts +3 -1
  7. package/dist/bundlers/vite/rewrite-css-imports.d.ts +8 -0
  8. package/dist/bundlers/vite/serve-css-generation.d.ts +5 -0
  9. package/dist/bundlers/vite/uni-app-x-css-options.d.ts +2 -0
  10. package/dist/{chunk-D6vf50IK.js → chunk-emK7D4bc.js} +7 -0
  11. package/dist/core.js +1 -1
  12. package/dist/core.mjs +1 -1
  13. package/dist/css-macro.js +3 -3
  14. package/dist/{defaults-C5w9OOrj.js → defaults-BPUuNYcD.js} +1 -1
  15. package/dist/defaults.js +1 -1
  16. package/dist/{generator-DRHWC1t-.mjs → generator-Bgn4m8An.mjs} +1 -1
  17. package/dist/{generator-DaPOkJ7W.js → generator-C56PJYxA.js} +2 -2
  18. package/dist/generator.js +2 -2
  19. package/dist/generator.mjs +2 -2
  20. package/dist/gulp.js +6 -6
  21. package/dist/gulp.mjs +5 -5
  22. package/dist/{incremental-runtime-class-set-Dt-FUQB5.mjs → incremental-runtime-class-set-BEYHQkZc.mjs} +238 -44
  23. package/dist/{incremental-runtime-class-set-BYV0yQdp.js → incremental-runtime-class-set-Bmzdjlk0.js} +239 -45
  24. package/dist/index.js +4 -4
  25. package/dist/index.mjs +4 -4
  26. package/dist/{postcss-Du8hY-MN.js → postcss-B7QNe78h.js} +57 -10
  27. package/dist/{postcss-CJrmfXvi.mjs → postcss-BgINK7Nm.mjs} +55 -8
  28. package/dist/postcss-html-transform.js +1 -1
  29. package/dist/postcss.js +1 -1
  30. package/dist/postcss.mjs +1 -1
  31. package/dist/{precheck-lfenbOTF.mjs → precheck-Copn2SqE.mjs} +14 -3
  32. package/dist/{precheck-DgpVCvYk.js → precheck-CyPbGn38.js} +17 -6
  33. package/dist/presets.js +9 -5
  34. package/dist/presets.mjs +7 -3
  35. package/dist/reset.js +1 -1
  36. package/dist/{source-candidates-D7b-Jzsx.mjs → source-candidates-BODCmzz5.mjs} +1 -1
  37. package/dist/{source-candidates-B4NvId_j.js → source-candidates-Caf2GDvx.js} +2 -2
  38. package/dist/{tailwindcss-D5RogwtV.mjs → tailwindcss-C0Zgo3Es.mjs} +1 -1
  39. package/dist/{tailwindcss-26ZFr5kv.js → tailwindcss-Tw6CSVHL.js} +3 -3
  40. package/dist/transform-95ivJthq.js +14868 -0
  41. package/dist/transform-DgCFGtQR.mjs +14871 -0
  42. package/dist/uni-app-x/vite.d.ts +3 -0
  43. package/dist/{utils-nta4tfFs.js → utils-Dolmt8EO.js} +1 -1
  44. package/dist/{v3-engine-CBahpuIg.js → v3-engine-6FVAcrZl.js} +31 -6
  45. package/dist/{v3-engine-BCUGX3gX.mjs → v3-engine-Ji6AXPh2.mjs} +24 -5
  46. package/dist/{vite-tjYalmVX.js → vite-Bw6LOXvf.js} +733 -79
  47. package/dist/{vite-DS4H5d0f.mjs → vite-ChZ2cbn4.mjs} +745 -91
  48. package/dist/vite.js +1 -1
  49. package/dist/vite.mjs +1 -1
  50. package/dist/weapp-tw-css-import-rewrite-loader.js +247 -45
  51. package/dist/{webpack-CA-40WU9.mjs → webpack-CMJ7iqN7.mjs} +6 -6
  52. package/dist/{webpack-0BwLOE_f.js → webpack-nn3P8_S_.js} +8 -8
  53. package/dist/webpack.js +1 -1
  54. package/dist/webpack.mjs +1 -1
  55. package/package.json +3 -4
  56. package/dist/transform-Crn84vTL.js +0 -257
  57. package/dist/transform-FAxHXVZp.mjs +0 -255
@@ -1,257 +0,0 @@
1
- const require_chunk = require("./chunk-D6vf50IK.js");
2
- const require_precheck = require("./precheck-DgpVCvYk.js");
3
- let tailwindcss_patch = require("tailwindcss-patch");
4
- let magic_string = require("magic-string");
5
- magic_string = require_chunk.__toESM(magic_string);
6
- let _vue_compiler_dom = require("@vue/compiler-dom");
7
- let _vue_compiler_sfc = require("@vue/compiler-sfc");
8
- //#region src/uni-app-x/component-local-style.ts
9
- const EXPRESSION_WRAPPER_PREFIX = "(\n";
10
- const EXPRESSION_WRAPPER_SUFFIX = "\n)";
11
- const COMPONENT_RE = /(?:^|[/\\])components(?:[/\\].+)?\.(?:uvue|nvue)$/;
12
- function createStableHash(input) {
13
- let hash = 2166136261;
14
- for (let i = 0; i < input.length; i++) {
15
- hash ^= input.charCodeAt(i);
16
- hash = Math.imul(hash, 16777619);
17
- }
18
- return (hash >>> 0).toString(36);
19
- }
20
- function extractLiteralValue(path) {
21
- if (path.isStringLiteral()) return {
22
- literal: path.node.value,
23
- offset: 1
24
- };
25
- return {
26
- literal: typeof path.node.value === "string" ? path.node.value : path.node.value.raw,
27
- offset: 0
28
- };
29
- }
30
- function createAlias(fileId, utility, index) {
31
- return `wtu-${createStableHash(`${fileId}:${utility}`)}-${index.toString(36)}`;
32
- }
33
- function isRuntimeCandidate(candidate, runtimeSet) {
34
- if (!runtimeSet || runtimeSet.size === 0) return false;
35
- return runtimeSet.has(candidate) || runtimeSet.has(require_precheck.replaceWxml(candidate));
36
- }
37
- function shouldEnableComponentLocalStyle(id) {
38
- return COMPONENT_RE.test(id);
39
- }
40
- var UniAppXComponentLocalStyleCollector = class {
41
- constructor(fileId, runtimeSet) {
42
- this.fileId = fileId;
43
- this.runtimeSet = runtimeSet;
44
- require_precheck._defineProperty(this, "aliasByUtility", /* @__PURE__ */ new Map());
45
- require_precheck._defineProperty(this, "aliasByLookup", /* @__PURE__ */ new Map());
46
- }
47
- ensureAlias(utility) {
48
- const cached = this.aliasByUtility.get(utility);
49
- if (cached) return cached;
50
- const alias = createAlias(this.fileId, utility, this.aliasByUtility.size);
51
- this.aliasByUtility.set(utility, alias);
52
- this.aliasByLookup.set(utility, alias);
53
- this.aliasByLookup.set(require_precheck.replaceWxml(utility), alias);
54
- return alias;
55
- }
56
- rewriteLiteral(literal, shouldInclude) {
57
- const candidates = (0, tailwindcss_patch.splitCandidateTokens)(literal);
58
- if (candidates.length === 0) return literal;
59
- let rewritten = literal;
60
- for (const candidate of candidates) {
61
- if (!shouldInclude(candidate)) continue;
62
- rewritten = rewritten.replace(candidate, this.ensureAlias(candidate));
63
- }
64
- return rewritten;
65
- }
66
- collectAndRewriteStaticClass(literal) {
67
- return this.rewriteLiteral(literal, (candidate) => isRuntimeCandidate(candidate, this.runtimeSet));
68
- }
69
- collectRuntimeClasses(rawSource, options = {}) {
70
- const wrapped = options.wrapExpression ? `${EXPRESSION_WRAPPER_PREFIX}${rawSource}${EXPRESSION_WRAPPER_SUFFIX}` : rawSource;
71
- try {
72
- const analysis = require_precheck.analyzeSource(require_precheck.babelParse(wrapped, {
73
- plugins: ["typescript"],
74
- sourceType: options.wrapExpression ? "module" : "unambiguous"
75
- }), {}, void 0, false);
76
- for (const path of analysis.targetPaths) {
77
- const { literal } = extractLiteralValue(path);
78
- const candidates = (0, tailwindcss_patch.splitCandidateTokens)(literal);
79
- const classContext = options.wrapExpression || require_precheck.isClassContextLiteralPath(path);
80
- for (const candidate of candidates) {
81
- if (!candidate || !classContext && !isRuntimeCandidate(candidate, this.runtimeSet)) continue;
82
- if (isRuntimeCandidate(candidate, this.runtimeSet)) this.ensureAlias(candidate);
83
- }
84
- }
85
- } catch {}
86
- }
87
- rewriteTransformedCode(rawSource, options = {}) {
88
- if (this.aliasByLookup.size === 0) return rawSource;
89
- const wrapped = options.wrapExpression ? `${EXPRESSION_WRAPPER_PREFIX}${rawSource}${EXPRESSION_WRAPPER_SUFFIX}` : rawSource;
90
- try {
91
- const analysis = require_precheck.analyzeSource(require_precheck.babelParse(wrapped, {
92
- plugins: ["typescript"],
93
- sourceType: options.wrapExpression ? "module" : "unambiguous"
94
- }), {}, void 0, false);
95
- if (analysis.targetPaths.length === 0) return rawSource;
96
- const updater = new require_precheck.JsTokenUpdater();
97
- for (const path of analysis.targetPaths) {
98
- const { literal, offset } = extractLiteralValue(path);
99
- const candidates = (0, tailwindcss_patch.splitCandidateTokens)(literal);
100
- if (candidates.length === 0) continue;
101
- let rewritten = literal;
102
- let mutated = false;
103
- for (const candidate of candidates) {
104
- const alias = this.aliasByLookup.get(candidate);
105
- if (!alias) continue;
106
- const replaced = rewritten.replace(candidate, alias);
107
- if (replaced !== rewritten) {
108
- rewritten = replaced;
109
- mutated = true;
110
- }
111
- }
112
- if (!mutated || typeof path.node.start !== "number" || typeof path.node.end !== "number") continue;
113
- updater.addToken({
114
- start: path.node.start + offset,
115
- end: path.node.end - offset,
116
- value: rewritten,
117
- path
118
- });
119
- }
120
- if (updater.length === 0) return rawSource;
121
- const ms = new magic_string.default(wrapped);
122
- updater.updateMagicString(ms);
123
- if (options.wrapExpression) {
124
- ms.remove(0, 2);
125
- ms.remove(wrapped.length - 2, wrapped.length);
126
- }
127
- return ms.toString();
128
- } catch {
129
- return rawSource;
130
- }
131
- }
132
- hasStyles() {
133
- return this.aliasByUtility.size > 0;
134
- }
135
- toStyleBlock() {
136
- if (!this.hasStyles()) return "";
137
- const lines = ["<style scoped>"];
138
- for (const [utility, alias] of this.aliasByUtility) {
139
- lines.push(`.${alias} {`);
140
- lines.push(` @apply ${utility};`);
141
- lines.push("}");
142
- }
143
- lines.push("</style>");
144
- return `${lines.join("\n")}\n`;
145
- }
146
- };
147
- //#endregion
148
- //#region src/uni-app-x/transform.ts
149
- function traverse(node, visitor) {
150
- visitor(node);
151
- if (Array.isArray(node.children)) {
152
- for (const child of node.children) if (child && typeof child === "object" && "type" in child) traverse(child, visitor);
153
- }
154
- }
155
- function updateStaticAttribute(ms, prop, content = prop.value?.content) {
156
- if (!prop.value) return;
157
- const start = prop.value.loc.start.offset + 1;
158
- const end = prop.value.loc.end.offset - 1;
159
- if (start < end) ms.update(start, end, require_precheck.replaceWxml(content ?? ""));
160
- }
161
- function updateStaticAttributeWithLocalStyle(ms, prop, collector, content = prop.value?.content) {
162
- if (!prop.value) return;
163
- const start = prop.value.loc.start.offset + 1;
164
- const end = prop.value.loc.end.offset - 1;
165
- if (start < end) ms.update(start, end, collector.collectAndRewriteStaticClass(content ?? ""));
166
- }
167
- function updateDirectiveExpression(ms, prop, jsHandler, runtimeSet) {
168
- if (prop.exp?.type !== _vue_compiler_dom.NodeTypes.SIMPLE_EXPRESSION) return;
169
- const expression = prop.exp.content;
170
- const start = prop.exp.loc.start.offset;
171
- const end = prop.exp.loc.end.offset;
172
- if (start >= end) return;
173
- const generated = require_precheck.generateCode(expression, {
174
- jsHandler,
175
- runtimeSet,
176
- wrapExpression: true
177
- });
178
- ms.update(start, end, generated);
179
- }
180
- function updateDirectiveExpressionWithLocalStyle(ms, prop, jsHandler, collector, runtimeSet) {
181
- if (prop.exp?.type !== _vue_compiler_dom.NodeTypes.SIMPLE_EXPRESSION) return;
182
- const expression = prop.exp.content;
183
- const start = prop.exp.loc.start.offset;
184
- const end = prop.exp.loc.end.offset;
185
- if (start >= end) return;
186
- collector.collectRuntimeClasses(expression, { wrapExpression: true });
187
- const generated = require_precheck.generateCode(expression, {
188
- jsHandler,
189
- runtimeSet,
190
- wrapExpression: true
191
- });
192
- ms.update(start, end, collector.rewriteTransformedCode(generated, { wrapExpression: true }));
193
- }
194
- function shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute) {
195
- const lowerName = attrName.toLowerCase();
196
- const shouldHandleDefault = !disabledDefaultTemplateHandler && lowerName === "class";
197
- const shouldHandleCustom = matchCustomAttribute?.(tag, attrName) ?? false;
198
- return {
199
- shouldHandleDefault,
200
- shouldHandleCustom,
201
- shouldHandle: shouldHandleDefault || shouldHandleCustom
202
- };
203
- }
204
- const defaultCreateJsHandlerOptions = { babelParserOptions: { plugins: ["typescript"] } };
205
- const UVUE_NVUE_RE = /\.(?:uvue|nvue)(?:\?.*)?$/;
206
- function transformUVue(code, id, jsHandler, runtimeSet, options = {}) {
207
- if (!UVUE_NVUE_RE.test(id)) return;
208
- const { customAttributesEntities, disabledDefaultTemplateHandler = false } = options;
209
- const matchCustomAttribute = require_precheck.createAttributeMatcher(customAttributesEntities);
210
- const ms = new magic_string.default(code);
211
- const { descriptor, errors } = (0, _vue_compiler_sfc.parse)(code);
212
- const localStyleCollector = options.enableComponentLocalStyle && shouldEnableComponentLocalStyle(id) ? new UniAppXComponentLocalStyleCollector(id, runtimeSet) : void 0;
213
- if (errors.length === 0) {
214
- if (descriptor.template?.ast) traverse(descriptor.template.ast, (node) => {
215
- if (node.type !== _vue_compiler_dom.NodeTypes.ELEMENT) return;
216
- const tag = node.tag;
217
- for (const prop of node.props) if (prop.type === _vue_compiler_dom.NodeTypes.ATTRIBUTE) {
218
- const { shouldHandle, shouldHandleDefault } = shouldHandleAttribute(tag, prop.name, disabledDefaultTemplateHandler, matchCustomAttribute);
219
- if (!shouldHandle) continue;
220
- if (shouldHandleDefault && localStyleCollector) updateStaticAttributeWithLocalStyle(ms, prop, localStyleCollector);
221
- else updateStaticAttribute(ms, prop);
222
- if (shouldHandleDefault) continue;
223
- } else if (prop.type === _vue_compiler_dom.NodeTypes.DIRECTIVE && prop.name === "bind" && prop.arg?.type === _vue_compiler_dom.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic) {
224
- const attrName = prop.arg.content;
225
- const { shouldHandle } = shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute);
226
- if (!shouldHandle) continue;
227
- if (attrName.toLowerCase() === "class" && localStyleCollector) updateDirectiveExpressionWithLocalStyle(ms, prop, jsHandler, localStyleCollector, runtimeSet);
228
- else updateDirectiveExpression(ms, prop, jsHandler, runtimeSet);
229
- }
230
- });
231
- if (descriptor.script) {
232
- localStyleCollector?.collectRuntimeClasses(descriptor.script.content);
233
- const { code } = jsHandler(descriptor.script.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
234
- ms.update(descriptor.script.loc.start.offset, descriptor.script.loc.end.offset, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
235
- }
236
- if (descriptor.scriptSetup) {
237
- localStyleCollector?.collectRuntimeClasses(descriptor.scriptSetup.content);
238
- const { code } = jsHandler(descriptor.scriptSetup.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
239
- ms.update(descriptor.scriptSetup.loc.start.offset, descriptor.scriptSetup.loc.end.offset, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
240
- }
241
- if (localStyleCollector?.hasStyles()) ms.append(`\n${localStyleCollector.toStyleBlock()}`);
242
- }
243
- const result = {
244
- code: ms.toString(),
245
- map: null
246
- };
247
- Object.defineProperty(result, "map", {
248
- configurable: true,
249
- enumerable: true,
250
- get() {
251
- return ms.generateMap();
252
- }
253
- });
254
- return result;
255
- }
256
- //#endregion
257
- exports.transformUVue = transformUVue;
@@ -1,255 +0,0 @@
1
- import { c as createAttributeMatcher, d as _defineProperty, f as isClassContextLiteralPath, l as analyzeSource, m as babelParse, p as replaceWxml, s as generateCode, u as JsTokenUpdater } from "./precheck-lfenbOTF.mjs";
2
- import { splitCandidateTokens } from "tailwindcss-patch";
3
- import MagicString from "magic-string";
4
- import { NodeTypes } from "@vue/compiler-dom";
5
- import { parse } from "@vue/compiler-sfc";
6
- //#region src/uni-app-x/component-local-style.ts
7
- const EXPRESSION_WRAPPER_PREFIX = "(\n";
8
- const EXPRESSION_WRAPPER_SUFFIX = "\n)";
9
- const COMPONENT_RE = /(?:^|[/\\])components(?:[/\\].+)?\.(?:uvue|nvue)$/;
10
- function createStableHash(input) {
11
- let hash = 2166136261;
12
- for (let i = 0; i < input.length; i++) {
13
- hash ^= input.charCodeAt(i);
14
- hash = Math.imul(hash, 16777619);
15
- }
16
- return (hash >>> 0).toString(36);
17
- }
18
- function extractLiteralValue(path) {
19
- if (path.isStringLiteral()) return {
20
- literal: path.node.value,
21
- offset: 1
22
- };
23
- return {
24
- literal: typeof path.node.value === "string" ? path.node.value : path.node.value.raw,
25
- offset: 0
26
- };
27
- }
28
- function createAlias(fileId, utility, index) {
29
- return `wtu-${createStableHash(`${fileId}:${utility}`)}-${index.toString(36)}`;
30
- }
31
- function isRuntimeCandidate(candidate, runtimeSet) {
32
- if (!runtimeSet || runtimeSet.size === 0) return false;
33
- return runtimeSet.has(candidate) || runtimeSet.has(replaceWxml(candidate));
34
- }
35
- function shouldEnableComponentLocalStyle(id) {
36
- return COMPONENT_RE.test(id);
37
- }
38
- var UniAppXComponentLocalStyleCollector = class {
39
- constructor(fileId, runtimeSet) {
40
- this.fileId = fileId;
41
- this.runtimeSet = runtimeSet;
42
- _defineProperty(this, "aliasByUtility", /* @__PURE__ */ new Map());
43
- _defineProperty(this, "aliasByLookup", /* @__PURE__ */ new Map());
44
- }
45
- ensureAlias(utility) {
46
- const cached = this.aliasByUtility.get(utility);
47
- if (cached) return cached;
48
- const alias = createAlias(this.fileId, utility, this.aliasByUtility.size);
49
- this.aliasByUtility.set(utility, alias);
50
- this.aliasByLookup.set(utility, alias);
51
- this.aliasByLookup.set(replaceWxml(utility), alias);
52
- return alias;
53
- }
54
- rewriteLiteral(literal, shouldInclude) {
55
- const candidates = splitCandidateTokens(literal);
56
- if (candidates.length === 0) return literal;
57
- let rewritten = literal;
58
- for (const candidate of candidates) {
59
- if (!shouldInclude(candidate)) continue;
60
- rewritten = rewritten.replace(candidate, this.ensureAlias(candidate));
61
- }
62
- return rewritten;
63
- }
64
- collectAndRewriteStaticClass(literal) {
65
- return this.rewriteLiteral(literal, (candidate) => isRuntimeCandidate(candidate, this.runtimeSet));
66
- }
67
- collectRuntimeClasses(rawSource, options = {}) {
68
- const wrapped = options.wrapExpression ? `${EXPRESSION_WRAPPER_PREFIX}${rawSource}${EXPRESSION_WRAPPER_SUFFIX}` : rawSource;
69
- try {
70
- const analysis = analyzeSource(babelParse(wrapped, {
71
- plugins: ["typescript"],
72
- sourceType: options.wrapExpression ? "module" : "unambiguous"
73
- }), {}, void 0, false);
74
- for (const path of analysis.targetPaths) {
75
- const { literal } = extractLiteralValue(path);
76
- const candidates = splitCandidateTokens(literal);
77
- const classContext = options.wrapExpression || isClassContextLiteralPath(path);
78
- for (const candidate of candidates) {
79
- if (!candidate || !classContext && !isRuntimeCandidate(candidate, this.runtimeSet)) continue;
80
- if (isRuntimeCandidate(candidate, this.runtimeSet)) this.ensureAlias(candidate);
81
- }
82
- }
83
- } catch {}
84
- }
85
- rewriteTransformedCode(rawSource, options = {}) {
86
- if (this.aliasByLookup.size === 0) return rawSource;
87
- const wrapped = options.wrapExpression ? `${EXPRESSION_WRAPPER_PREFIX}${rawSource}${EXPRESSION_WRAPPER_SUFFIX}` : rawSource;
88
- try {
89
- const analysis = analyzeSource(babelParse(wrapped, {
90
- plugins: ["typescript"],
91
- sourceType: options.wrapExpression ? "module" : "unambiguous"
92
- }), {}, void 0, false);
93
- if (analysis.targetPaths.length === 0) return rawSource;
94
- const updater = new JsTokenUpdater();
95
- for (const path of analysis.targetPaths) {
96
- const { literal, offset } = extractLiteralValue(path);
97
- const candidates = splitCandidateTokens(literal);
98
- if (candidates.length === 0) continue;
99
- let rewritten = literal;
100
- let mutated = false;
101
- for (const candidate of candidates) {
102
- const alias = this.aliasByLookup.get(candidate);
103
- if (!alias) continue;
104
- const replaced = rewritten.replace(candidate, alias);
105
- if (replaced !== rewritten) {
106
- rewritten = replaced;
107
- mutated = true;
108
- }
109
- }
110
- if (!mutated || typeof path.node.start !== "number" || typeof path.node.end !== "number") continue;
111
- updater.addToken({
112
- start: path.node.start + offset,
113
- end: path.node.end - offset,
114
- value: rewritten,
115
- path
116
- });
117
- }
118
- if (updater.length === 0) return rawSource;
119
- const ms = new MagicString(wrapped);
120
- updater.updateMagicString(ms);
121
- if (options.wrapExpression) {
122
- ms.remove(0, 2);
123
- ms.remove(wrapped.length - 2, wrapped.length);
124
- }
125
- return ms.toString();
126
- } catch {
127
- return rawSource;
128
- }
129
- }
130
- hasStyles() {
131
- return this.aliasByUtility.size > 0;
132
- }
133
- toStyleBlock() {
134
- if (!this.hasStyles()) return "";
135
- const lines = ["<style scoped>"];
136
- for (const [utility, alias] of this.aliasByUtility) {
137
- lines.push(`.${alias} {`);
138
- lines.push(` @apply ${utility};`);
139
- lines.push("}");
140
- }
141
- lines.push("</style>");
142
- return `${lines.join("\n")}\n`;
143
- }
144
- };
145
- //#endregion
146
- //#region src/uni-app-x/transform.ts
147
- function traverse(node, visitor) {
148
- visitor(node);
149
- if (Array.isArray(node.children)) {
150
- for (const child of node.children) if (child && typeof child === "object" && "type" in child) traverse(child, visitor);
151
- }
152
- }
153
- function updateStaticAttribute(ms, prop, content = prop.value?.content) {
154
- if (!prop.value) return;
155
- const start = prop.value.loc.start.offset + 1;
156
- const end = prop.value.loc.end.offset - 1;
157
- if (start < end) ms.update(start, end, replaceWxml(content ?? ""));
158
- }
159
- function updateStaticAttributeWithLocalStyle(ms, prop, collector, content = prop.value?.content) {
160
- if (!prop.value) return;
161
- const start = prop.value.loc.start.offset + 1;
162
- const end = prop.value.loc.end.offset - 1;
163
- if (start < end) ms.update(start, end, collector.collectAndRewriteStaticClass(content ?? ""));
164
- }
165
- function updateDirectiveExpression(ms, prop, jsHandler, runtimeSet) {
166
- if (prop.exp?.type !== NodeTypes.SIMPLE_EXPRESSION) return;
167
- const expression = prop.exp.content;
168
- const start = prop.exp.loc.start.offset;
169
- const end = prop.exp.loc.end.offset;
170
- if (start >= end) return;
171
- const generated = generateCode(expression, {
172
- jsHandler,
173
- runtimeSet,
174
- wrapExpression: true
175
- });
176
- ms.update(start, end, generated);
177
- }
178
- function updateDirectiveExpressionWithLocalStyle(ms, prop, jsHandler, collector, runtimeSet) {
179
- if (prop.exp?.type !== NodeTypes.SIMPLE_EXPRESSION) return;
180
- const expression = prop.exp.content;
181
- const start = prop.exp.loc.start.offset;
182
- const end = prop.exp.loc.end.offset;
183
- if (start >= end) return;
184
- collector.collectRuntimeClasses(expression, { wrapExpression: true });
185
- const generated = generateCode(expression, {
186
- jsHandler,
187
- runtimeSet,
188
- wrapExpression: true
189
- });
190
- ms.update(start, end, collector.rewriteTransformedCode(generated, { wrapExpression: true }));
191
- }
192
- function shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute) {
193
- const lowerName = attrName.toLowerCase();
194
- const shouldHandleDefault = !disabledDefaultTemplateHandler && lowerName === "class";
195
- const shouldHandleCustom = matchCustomAttribute?.(tag, attrName) ?? false;
196
- return {
197
- shouldHandleDefault,
198
- shouldHandleCustom,
199
- shouldHandle: shouldHandleDefault || shouldHandleCustom
200
- };
201
- }
202
- const defaultCreateJsHandlerOptions = { babelParserOptions: { plugins: ["typescript"] } };
203
- const UVUE_NVUE_RE = /\.(?:uvue|nvue)(?:\?.*)?$/;
204
- function transformUVue(code, id, jsHandler, runtimeSet, options = {}) {
205
- if (!UVUE_NVUE_RE.test(id)) return;
206
- const { customAttributesEntities, disabledDefaultTemplateHandler = false } = options;
207
- const matchCustomAttribute = createAttributeMatcher(customAttributesEntities);
208
- const ms = new MagicString(code);
209
- const { descriptor, errors } = parse(code);
210
- const localStyleCollector = options.enableComponentLocalStyle && shouldEnableComponentLocalStyle(id) ? new UniAppXComponentLocalStyleCollector(id, runtimeSet) : void 0;
211
- if (errors.length === 0) {
212
- if (descriptor.template?.ast) traverse(descriptor.template.ast, (node) => {
213
- if (node.type !== NodeTypes.ELEMENT) return;
214
- const tag = node.tag;
215
- for (const prop of node.props) if (prop.type === NodeTypes.ATTRIBUTE) {
216
- const { shouldHandle, shouldHandleDefault } = shouldHandleAttribute(tag, prop.name, disabledDefaultTemplateHandler, matchCustomAttribute);
217
- if (!shouldHandle) continue;
218
- if (shouldHandleDefault && localStyleCollector) updateStaticAttributeWithLocalStyle(ms, prop, localStyleCollector);
219
- else updateStaticAttribute(ms, prop);
220
- if (shouldHandleDefault) continue;
221
- } else if (prop.type === NodeTypes.DIRECTIVE && prop.name === "bind" && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic) {
222
- const attrName = prop.arg.content;
223
- const { shouldHandle } = shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute);
224
- if (!shouldHandle) continue;
225
- if (attrName.toLowerCase() === "class" && localStyleCollector) updateDirectiveExpressionWithLocalStyle(ms, prop, jsHandler, localStyleCollector, runtimeSet);
226
- else updateDirectiveExpression(ms, prop, jsHandler, runtimeSet);
227
- }
228
- });
229
- if (descriptor.script) {
230
- localStyleCollector?.collectRuntimeClasses(descriptor.script.content);
231
- const { code } = jsHandler(descriptor.script.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
232
- ms.update(descriptor.script.loc.start.offset, descriptor.script.loc.end.offset, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
233
- }
234
- if (descriptor.scriptSetup) {
235
- localStyleCollector?.collectRuntimeClasses(descriptor.scriptSetup.content);
236
- const { code } = jsHandler(descriptor.scriptSetup.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
237
- ms.update(descriptor.scriptSetup.loc.start.offset, descriptor.scriptSetup.loc.end.offset, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
238
- }
239
- if (localStyleCollector?.hasStyles()) ms.append(`\n${localStyleCollector.toStyleBlock()}`);
240
- }
241
- const result = {
242
- code: ms.toString(),
243
- map: null
244
- };
245
- Object.defineProperty(result, "map", {
246
- configurable: true,
247
- enumerable: true,
248
- get() {
249
- return ms.generateMap();
250
- }
251
- });
252
- return result;
253
- }
254
- //#endregion
255
- export { transformUVue };