weapp-tailwindcss 5.3.2 → 5.3.4

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 (52) hide show
  1. package/dist/bundlers/shared/source-candidates/types-and-cache.d.ts +3 -0
  2. package/dist/bundlers/shared/style-requests.d.ts +1 -0
  3. package/dist/bundlers/vite/shared/create-framework-plugins.d.ts +1 -0
  4. package/dist/bundlers/vite/shared/framework-module-candidates.d.ts +15 -0
  5. package/dist/{context-Kvr3lLEC.js → context-4ittVpgt.js} +1 -1
  6. package/dist/{context-CB4Ef9U0.cjs → context-Ulhcd5lF.cjs} +3 -3
  7. package/dist/core.cjs +1 -1
  8. package/dist/core.js +1 -1
  9. package/dist/{gulp-QJ_oz-06.cjs → gulp-CDIXMhIO.cjs} +6 -6
  10. package/dist/{gulp-vz36-uQP.js → gulp-DwtrU4V1.js} +3 -3
  11. package/dist/gulp.cjs +1 -1
  12. package/dist/gulp.js +1 -1
  13. package/dist/{hmr-timing-W3kro20F.cjs → hmr-timing-DcFB_kwB.cjs} +2 -2
  14. package/dist/{hmr-timing-HGlM4hX3.js → hmr-timing-utmOgn6p.js} +57 -14
  15. package/dist/index.cjs +3 -3
  16. package/dist/index.js +3 -3
  17. package/dist/local-style-matcher-DRpFEu1s.cjs +34 -0
  18. package/dist/local-style-matcher-DsiI2wIc.js +17 -0
  19. package/dist/{options-CKZW99r4.cjs → options-BJrtIQbH.cjs} +3 -1
  20. package/dist/{pipeline-helpers-t95KPu4O.cjs → pipeline-helpers-kB7Mn2Jy.cjs} +2 -2
  21. package/dist/presets.cjs +2 -2
  22. package/dist/presets.js +1 -1
  23. package/dist/rspack.cjs +2 -2
  24. package/dist/rspack.js +2 -2
  25. package/dist/{source-candidate-scan-signature-BSad7bMQ.cjs → source-candidate-scan-signature-C1r5mHSi.cjs} +2 -2
  26. package/dist/{source-candidate-scan-signature-BHCPQVoD.js → source-candidate-scan-signature-CDrLxVKu.js} +2 -2
  27. package/dist/{style-options-PCgz2ywM.cjs → style-options-DW30H5PV.cjs} +1 -1
  28. package/dist/{tailwindcss-jKzY37Sk.cjs → tailwindcss-CexDJBhx.cjs} +1 -1
  29. package/dist/{tailwindcss-BSEdWieh.js → tailwindcss-ChnpCv25.js} +3 -1
  30. package/dist/{component-local-style-CNhEhd2B.cjs → transform-BQjg2T2T.cjs} +201 -27
  31. package/dist/{component-local-style-C2wC8o9F.js → transform-Cp4KybBy.js} +207 -16
  32. package/dist/types/user-defined-options/important.d.ts +2 -0
  33. package/dist/uni-app-x/component-local-style.d.ts +6 -4
  34. package/dist/uni-app-x/local-style-matcher.d.ts +4 -0
  35. package/dist/uni-app-x/options.d.ts +2 -0
  36. package/dist/uni-app-x/style-asset/harmony-global.d.ts +2 -0
  37. package/dist/uni-app-x/transform.d.ts +2 -0
  38. package/dist/uni-app-x/vite/plugin-options.d.ts +1 -0
  39. package/dist/uni-app-x/vite/style-request.d.ts +0 -1
  40. package/dist/{v4-generation-core-BfeXEc6R.cjs → v4-generation-core-OVshPBGo.cjs} +62 -13
  41. package/dist/{vite-CVr0vIgh.js → vite-D1a6OfVn.js} +130 -91
  42. package/dist/{vite-Dy6THt54.cjs → vite-cXJPnKXp.cjs} +77 -38
  43. package/dist/vite.cjs +1 -1
  44. package/dist/vite.js +1 -1
  45. package/dist/weapp-tw-css-import-rewrite-loader.cjs +4 -4
  46. package/dist/{webpack-Cf5RnhXt.cjs → webpack-DOhCcN_2.cjs} +8 -8
  47. package/dist/{webpack-DwtdWqwr.js → webpack-DQAbh1Z7.js} +4 -4
  48. package/dist/webpack.cjs +1 -1
  49. package/dist/webpack.js +1 -1
  50. package/package.json +2 -2
  51. package/dist/transform-CgohToUV.cjs +0 -200
  52. package/dist/transform-DSRPiBWz.js +0 -201
@@ -1,6 +1,8 @@
1
1
  const require_object = require("./object-DbHtAXRk.cjs");
2
2
  const require_wxml = require("./wxml-CQmBCjOR.cjs");
3
+ const require_local_style_matcher = require("./local-style-matcher-DRpFEu1s.cjs");
3
4
  let _tailwindcss_mangle_engine = require("@tailwindcss-mangle/engine");
5
+ let _vue_compiler_dom = require("@vue/compiler-dom");
4
6
  //#region src/uni-app-x/component-local-style-binding.ts
5
7
  const OPTIONS_CLASS_SECTIONS = /* @__PURE__ */ new Set(["computed", "methods"]);
6
8
  function readStaticName(path) {
@@ -96,8 +98,6 @@ function isClassBindingLiteralPath(path, roots) {
96
98
  //#region src/uni-app-x/component-local-style.ts
97
99
  const EXPRESSION_WRAPPER_PREFIX = "(\n";
98
100
  const EXPRESSION_WRAPPER_SUFFIX = "\n)";
99
- const COMPONENT_RE = /(?:^|[/\\])components(?:[/\\].+)?\.(?:uvue|nvue)$/;
100
- const PAGE_RE = /(?:^|[/\\])pages(?:[/\\].+)?\.(?:uvue|nvue)$/;
101
101
  function createStableHash(input) {
102
102
  let hash = 2166136261;
103
103
  for (let i = 0; i < input.length; i++) {
@@ -170,12 +170,6 @@ function hasTopLevelVariant(candidate) {
170
170
  }
171
171
  return false;
172
172
  }
173
- function shouldEnableComponentLocalStyle(id) {
174
- return COMPONENT_RE.test(id);
175
- }
176
- function shouldEnablePageLocalStyle(id) {
177
- return PAGE_RE.test(id);
178
- }
179
173
  var UniAppXComponentLocalStyleCollector = class {
180
174
  constructor(fileId, runtimeSet) {
181
175
  this.fileId = fileId;
@@ -286,15 +280,16 @@ var UniAppXComponentLocalStyleCollector = class {
286
280
  hasStyles() {
287
281
  return this.aliasByUtility.size > 0;
288
282
  }
289
- toStyleBlock() {
283
+ toStyleBlock(options = {}) {
290
284
  if (!this.hasStyles()) return "";
291
- return `<style scoped>\n${this.toStyleRules()}</style>\n`;
285
+ return `<style scoped>\n${this.toStyleRules(options)}</style>\n`;
292
286
  }
293
- toStyleRules() {
287
+ toStyleRules(options = {}) {
294
288
  if (!this.hasStyles()) return "";
295
289
  const lines = [];
296
290
  for (const [utility, alias] of this.aliasByUtility) {
297
- const selector = this.deepUtilities.has(utility) ? `.${alias}, :deep(.${alias})` : `.${alias}`;
291
+ const localSelector = options.web ? `:global(.${alias})` : `.${alias}`;
292
+ const selector = this.deepUtilities.has(utility) ? `${localSelector}, :deep(.${alias})` : localSelector;
298
293
  lines.push(`${selector} {`);
299
294
  lines.push(` @apply ${serializeApplyUtility(utility)};`);
300
295
  lines.push("}");
@@ -303,21 +298,200 @@ var UniAppXComponentLocalStyleCollector = class {
303
298
  }
304
299
  };
305
300
  //#endregion
306
- Object.defineProperty(exports, "UniAppXComponentLocalStyleCollector", {
307
- enumerable: true,
308
- get: function() {
309
- return UniAppXComponentLocalStyleCollector;
301
+ //#region src/uni-app-x/transform.ts
302
+ function traverse(node, visitor) {
303
+ visitor(node);
304
+ if (Array.isArray(node.children)) {
305
+ for (const child of node.children) if (child && typeof child === "object" && "type" in child) traverse(child, visitor);
310
306
  }
311
- });
312
- Object.defineProperty(exports, "shouldEnableComponentLocalStyle", {
313
- enumerable: true,
314
- get: function() {
315
- return shouldEnableComponentLocalStyle;
307
+ }
308
+ function updateStaticAttribute(ms, prop, offset, content = prop.value?.content) {
309
+ if (!prop.value) return;
310
+ const start = offset + prop.value.loc.start.offset + 1;
311
+ const end = offset + prop.value.loc.end.offset - 1;
312
+ if (start < end) ms.update(start, end, require_wxml.replaceWxml(content ?? ""));
313
+ }
314
+ function updateStaticAttributeWithLocalStyle(ms, prop, offset, collector, deep, content = prop.value?.content) {
315
+ if (!prop.value) return;
316
+ const start = offset + prop.value.loc.start.offset + 1;
317
+ const end = offset + prop.value.loc.end.offset - 1;
318
+ if (start < end) ms.update(start, end, collector.collectAndRewriteStaticClass(content ?? "", { deep }));
319
+ }
320
+ function updateDirectiveExpression(ms, prop, offset, jsHandler, runtimeSet) {
321
+ if (prop.exp?.type !== _vue_compiler_dom.NodeTypes.SIMPLE_EXPRESSION) return;
322
+ const expression = prop.exp.content;
323
+ const start = offset + prop.exp.loc.start.offset;
324
+ const end = offset + prop.exp.loc.end.offset;
325
+ if (start >= end) return;
326
+ const generated = require_wxml.generateCode(expression, {
327
+ jsHandler,
328
+ runtimeSet,
329
+ wrapExpression: true
330
+ });
331
+ ms.update(start, end, generated);
332
+ }
333
+ function updateDirectiveExpressionWithLocalStyle(ms, prop, offset, jsHandler, collector, deep, runtimeSet) {
334
+ if (prop.exp?.type !== _vue_compiler_dom.NodeTypes.SIMPLE_EXPRESSION) return;
335
+ const expression = prop.exp.content;
336
+ const start = offset + prop.exp.loc.start.offset;
337
+ const end = offset + prop.exp.loc.end.offset;
338
+ if (start >= end) return;
339
+ collector.collectRuntimeClasses(expression, {
340
+ deep,
341
+ wrapExpression: true
342
+ });
343
+ const generated = require_wxml.generateCode(expression, {
344
+ jsHandler,
345
+ runtimeSet,
346
+ wrapExpression: true
347
+ });
348
+ ms.update(start, end, collector.rewriteTransformedCode(generated, { wrapExpression: true }));
349
+ }
350
+ function shouldEnableLocalStyle(id, options) {
351
+ if (options.enableComponentLocalStyle && require_local_style_matcher.shouldEnableComponentLocalStyle(id, options.componentMatcher)) return true;
352
+ if (options.enablePageLocalStyle && require_local_style_matcher.shouldEnablePageLocalStyle(id, options.pageMatcher)) return true;
353
+ return false;
354
+ }
355
+ function shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute) {
356
+ const lowerName = attrName.toLowerCase();
357
+ const shouldHandleDefault = !disabledDefaultTemplateHandler && lowerName === "class";
358
+ const shouldHandleCustom = matchCustomAttribute?.(tag, attrName) ?? false;
359
+ return {
360
+ shouldHandleDefault,
361
+ shouldHandleCustom,
362
+ shouldHandle: shouldHandleDefault || shouldHandleCustom
363
+ };
364
+ }
365
+ const defaultCreateJsHandlerOptions = { babelParserOptions: { plugins: ["typescript"] } };
366
+ const UVUE_NVUE_RE = /\.(?:uvue|nvue)(?:\?.*)?$/;
367
+ const SFC_BLOCK_RE = /<(template|script|style)\b([^>]*)>/gi;
368
+ const SCRIPT_SETUP_RE = /(?:^|\s)setup(?:\s|=|$)/;
369
+ const STYLE_SCOPED_RE = /(?:^|\s)scoped(?:\s|=|$)/i;
370
+ function findSfcBlockEnd(code, type, contentStart) {
371
+ const closeTag = new RegExp(`<\\/${type}\\s*>`, "gi");
372
+ if (type !== "template") {
373
+ const match = closeTag.exec(code.slice(contentStart));
374
+ return match ? {
375
+ blockEnd: contentStart + match.index + match[0].length,
376
+ contentEnd: contentStart + match.index
377
+ } : void 0;
378
+ }
379
+ const tag = new RegExp(`<\\/?${type}\\b[^>]*>`, "gi");
380
+ tag.lastIndex = contentStart;
381
+ let depth = 1;
382
+ let match = tag.exec(code);
383
+ while (match) {
384
+ if (match[0].startsWith("</")) depth -= 1;
385
+ else if (!match[0].trimEnd().endsWith("/>")) depth += 1;
386
+ if (depth === 0) return {
387
+ blockEnd: tag.lastIndex,
388
+ contentEnd: match.index
389
+ };
390
+ match = tag.exec(code);
391
+ }
392
+ }
393
+ function parseSfc(code) {
394
+ const descriptor = {
395
+ errors: [],
396
+ styles: []
397
+ };
398
+ for (const match of code.matchAll(SFC_BLOCK_RE)) {
399
+ const type = match[1];
400
+ const attrs = match[2] ?? "";
401
+ const contentStart = (match.index ?? 0) + match[0].length;
402
+ const blockBoundary = findSfcBlockEnd(code, type, contentStart);
403
+ if (!blockBoundary) {
404
+ descriptor.errors.push(/* @__PURE__ */ new Error(`未闭合的 ${type} SFC 块`));
405
+ continue;
406
+ }
407
+ const { contentEnd } = blockBoundary;
408
+ const content = code.slice(contentStart, contentEnd);
409
+ const block = {
410
+ content,
411
+ start: contentStart,
412
+ end: contentEnd,
413
+ attrs
414
+ };
415
+ if (type === "template" && !descriptor.template) {
416
+ try {
417
+ descriptor.template = {
418
+ ...block,
419
+ ast: (0, _vue_compiler_dom.parse)(content)
420
+ };
421
+ } catch (error) {
422
+ descriptor.errors.push(error);
423
+ descriptor.template = block;
424
+ }
425
+ continue;
426
+ }
427
+ if (type === "script") {
428
+ if (SCRIPT_SETUP_RE.test(attrs)) descriptor.scriptSetup ?? (descriptor.scriptSetup = block);
429
+ else descriptor.script ?? (descriptor.script = block);
430
+ continue;
431
+ }
432
+ if (type === "style") descriptor.styles.push(block);
316
433
  }
317
- });
318
- Object.defineProperty(exports, "shouldEnablePageLocalStyle", {
319
- enumerable: true,
320
- get: function() {
321
- return shouldEnablePageLocalStyle;
434
+ return descriptor;
435
+ }
436
+ function transformUVue(code, id, jsHandler, runtimeSet, options = {}) {
437
+ if (!UVUE_NVUE_RE.test(id)) return;
438
+ const { customAttributesEntities, disabledDefaultTemplateHandler = false } = options;
439
+ const matchCustomAttribute = require_wxml.createAttributeMatcher(customAttributesEntities);
440
+ const ms = new require_wxml.MagicString(code);
441
+ const descriptor = parseSfc(code);
442
+ const localStyleCollector = shouldEnableLocalStyle(id, options) ? new UniAppXComponentLocalStyleCollector(id, runtimeSet) : void 0;
443
+ if (descriptor.errors.length === 0) {
444
+ if (descriptor.template?.ast) {
445
+ const templateOffset = descriptor.template.start;
446
+ traverse(descriptor.template.ast, (node) => {
447
+ if (node.type !== _vue_compiler_dom.NodeTypes.ELEMENT) return;
448
+ const tag = node.tag;
449
+ for (const prop of node.props) if (prop.type === _vue_compiler_dom.NodeTypes.ATTRIBUTE) {
450
+ const { shouldHandle, shouldHandleCustom, shouldHandleDefault } = shouldHandleAttribute(tag, prop.name, disabledDefaultTemplateHandler, matchCustomAttribute);
451
+ if (!shouldHandle) continue;
452
+ if (localStyleCollector) updateStaticAttributeWithLocalStyle(ms, prop, templateOffset, localStyleCollector, options.webCustomAttributeDeep === true && shouldHandleCustom);
453
+ else updateStaticAttribute(ms, prop, templateOffset);
454
+ if (shouldHandleDefault) continue;
455
+ } else if (prop.type === _vue_compiler_dom.NodeTypes.DIRECTIVE && prop.name === "bind" && prop.arg?.type === _vue_compiler_dom.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic) {
456
+ const attrName = prop.arg.content;
457
+ const { shouldHandle, shouldHandleCustom } = shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute);
458
+ if (!shouldHandle) continue;
459
+ if (localStyleCollector) updateDirectiveExpressionWithLocalStyle(ms, prop, templateOffset, jsHandler, localStyleCollector, options.webCustomAttributeDeep === true && shouldHandleCustom, runtimeSet);
460
+ else updateDirectiveExpression(ms, prop, templateOffset, jsHandler, runtimeSet);
461
+ }
462
+ });
463
+ }
464
+ if (descriptor.script) {
465
+ localStyleCollector?.collectRuntimeClasses(descriptor.script.content);
466
+ const { code } = jsHandler(descriptor.script.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
467
+ ms.update(descriptor.script.start, descriptor.script.end, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
468
+ }
469
+ if (descriptor.scriptSetup) {
470
+ localStyleCollector?.collectRuntimeClasses(descriptor.scriptSetup.content);
471
+ const { code } = jsHandler(descriptor.scriptSetup.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
472
+ ms.update(descriptor.scriptSetup.start, descriptor.scriptSetup.end, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
473
+ }
474
+ if (localStyleCollector?.hasStyles()) {
475
+ const scopedStyle = descriptor.styles.findLast((style) => STYLE_SCOPED_RE.test(style.attrs));
476
+ if (scopedStyle && options.onWebLocalStyleRules) options.onWebLocalStyleRules(localStyleCollector.toStyleRules({ web: true }));
477
+ else if (scopedStyle) {
478
+ const separator = scopedStyle.content.endsWith("\n") ? "" : "\n";
479
+ ms.appendLeft(scopedStyle.end, `${separator}${localStyleCollector.toStyleRules()}`);
480
+ } else ms.append(`\n${localStyleCollector.toStyleBlock({ web: Boolean(options.onWebLocalStyleRules) })}`);
481
+ }
322
482
  }
323
- });
483
+ const result = {
484
+ code: ms.toString(),
485
+ map: null
486
+ };
487
+ Object.defineProperty(result, "map", {
488
+ configurable: true,
489
+ enumerable: true,
490
+ get() {
491
+ return ms.generateMap();
492
+ }
493
+ });
494
+ return result;
495
+ }
496
+ //#endregion
497
+ exports.transformUVue = transformUVue;
@@ -1,7 +1,9 @@
1
1
  import { n as _defineProperty } from "./object-B-8IHefx.js";
2
- import { g as traverse, h as babelParse, i as analyzeSource, o as JsTokenUpdater, p as replaceWxml, u as isClassContextLiteralPath } from "./wxml-B7OAVpXJ.js";
2
+ import { g as traverse$1, h as babelParse, i as analyzeSource, n as generateCode, o as JsTokenUpdater, p as replaceWxml, r as createAttributeMatcher, u as isClassContextLiteralPath } from "./wxml-B7OAVpXJ.js";
3
+ import { n as shouldEnableComponentLocalStyle, r as shouldEnablePageLocalStyle } from "./local-style-matcher-DsiI2wIc.js";
3
4
  import { splitCandidateTokens } from "@tailwindcss-mangle/engine";
4
5
  import MagicString from "magic-string";
6
+ import { NodeTypes, parse } from "@vue/compiler-dom";
5
7
  //#region src/uni-app-x/component-local-style-binding.ts
6
8
  const OPTIONS_CLASS_SECTIONS = /* @__PURE__ */ new Set(["computed", "methods"]);
7
9
  function readStaticName(path) {
@@ -58,14 +60,14 @@ function isClassAssignmentTarget(path, names) {
58
60
  return object.isThisExpression() && isClassBindingName(property, names);
59
61
  }
60
62
  function collectClassBindingNames(ast, names) {
61
- traverse(ast, { ReferencedIdentifier(path) {
63
+ traverse$1(ast, { ReferencedIdentifier(path) {
62
64
  names.add(path.node.name);
63
65
  } });
64
66
  }
65
67
  function collectClassBindingRoots(ast, names) {
66
68
  const roots = /* @__PURE__ */ new WeakSet();
67
69
  if (names.size === 0) return roots;
68
- traverse(ast, {
70
+ traverse$1(ast, {
69
71
  Program(path) {
70
72
  for (const name of names) {
71
73
  const binding = path.scope.getBinding(name);
@@ -97,8 +99,6 @@ function isClassBindingLiteralPath(path, roots) {
97
99
  //#region src/uni-app-x/component-local-style.ts
98
100
  const EXPRESSION_WRAPPER_PREFIX = "(\n";
99
101
  const EXPRESSION_WRAPPER_SUFFIX = "\n)";
100
- const COMPONENT_RE = /(?:^|[/\\])components(?:[/\\].+)?\.(?:uvue|nvue)$/;
101
- const PAGE_RE = /(?:^|[/\\])pages(?:[/\\].+)?\.(?:uvue|nvue)$/;
102
102
  function createStableHash(input) {
103
103
  let hash = 2166136261;
104
104
  for (let i = 0; i < input.length; i++) {
@@ -171,12 +171,6 @@ function hasTopLevelVariant(candidate) {
171
171
  }
172
172
  return false;
173
173
  }
174
- function shouldEnableComponentLocalStyle(id) {
175
- return COMPONENT_RE.test(id);
176
- }
177
- function shouldEnablePageLocalStyle(id) {
178
- return PAGE_RE.test(id);
179
- }
180
174
  var UniAppXComponentLocalStyleCollector = class {
181
175
  constructor(fileId, runtimeSet) {
182
176
  this.fileId = fileId;
@@ -287,15 +281,16 @@ var UniAppXComponentLocalStyleCollector = class {
287
281
  hasStyles() {
288
282
  return this.aliasByUtility.size > 0;
289
283
  }
290
- toStyleBlock() {
284
+ toStyleBlock(options = {}) {
291
285
  if (!this.hasStyles()) return "";
292
- return `<style scoped>\n${this.toStyleRules()}</style>\n`;
286
+ return `<style scoped>\n${this.toStyleRules(options)}</style>\n`;
293
287
  }
294
- toStyleRules() {
288
+ toStyleRules(options = {}) {
295
289
  if (!this.hasStyles()) return "";
296
290
  const lines = [];
297
291
  for (const [utility, alias] of this.aliasByUtility) {
298
- const selector = this.deepUtilities.has(utility) ? `.${alias}, :deep(.${alias})` : `.${alias}`;
292
+ const localSelector = options.web ? `:global(.${alias})` : `.${alias}`;
293
+ const selector = this.deepUtilities.has(utility) ? `${localSelector}, :deep(.${alias})` : localSelector;
299
294
  lines.push(`${selector} {`);
300
295
  lines.push(` @apply ${serializeApplyUtility(utility)};`);
301
296
  lines.push("}");
@@ -304,4 +299,200 @@ var UniAppXComponentLocalStyleCollector = class {
304
299
  }
305
300
  };
306
301
  //#endregion
307
- export { shouldEnableComponentLocalStyle as n, shouldEnablePageLocalStyle as r, UniAppXComponentLocalStyleCollector as t };
302
+ //#region src/uni-app-x/transform.ts
303
+ function traverse(node, visitor) {
304
+ visitor(node);
305
+ if (Array.isArray(node.children)) {
306
+ for (const child of node.children) if (child && typeof child === "object" && "type" in child) traverse(child, visitor);
307
+ }
308
+ }
309
+ function updateStaticAttribute(ms, prop, offset, content = prop.value?.content) {
310
+ if (!prop.value) return;
311
+ const start = offset + prop.value.loc.start.offset + 1;
312
+ const end = offset + prop.value.loc.end.offset - 1;
313
+ if (start < end) ms.update(start, end, replaceWxml(content ?? ""));
314
+ }
315
+ function updateStaticAttributeWithLocalStyle(ms, prop, offset, collector, deep, content = prop.value?.content) {
316
+ if (!prop.value) return;
317
+ const start = offset + prop.value.loc.start.offset + 1;
318
+ const end = offset + prop.value.loc.end.offset - 1;
319
+ if (start < end) ms.update(start, end, collector.collectAndRewriteStaticClass(content ?? "", { deep }));
320
+ }
321
+ function updateDirectiveExpression(ms, prop, offset, jsHandler, runtimeSet) {
322
+ if (prop.exp?.type !== NodeTypes.SIMPLE_EXPRESSION) return;
323
+ const expression = prop.exp.content;
324
+ const start = offset + prop.exp.loc.start.offset;
325
+ const end = offset + prop.exp.loc.end.offset;
326
+ if (start >= end) return;
327
+ const generated = generateCode(expression, {
328
+ jsHandler,
329
+ runtimeSet,
330
+ wrapExpression: true
331
+ });
332
+ ms.update(start, end, generated);
333
+ }
334
+ function updateDirectiveExpressionWithLocalStyle(ms, prop, offset, jsHandler, collector, deep, runtimeSet) {
335
+ if (prop.exp?.type !== NodeTypes.SIMPLE_EXPRESSION) return;
336
+ const expression = prop.exp.content;
337
+ const start = offset + prop.exp.loc.start.offset;
338
+ const end = offset + prop.exp.loc.end.offset;
339
+ if (start >= end) return;
340
+ collector.collectRuntimeClasses(expression, {
341
+ deep,
342
+ wrapExpression: true
343
+ });
344
+ const generated = generateCode(expression, {
345
+ jsHandler,
346
+ runtimeSet,
347
+ wrapExpression: true
348
+ });
349
+ ms.update(start, end, collector.rewriteTransformedCode(generated, { wrapExpression: true }));
350
+ }
351
+ function shouldEnableLocalStyle(id, options) {
352
+ if (options.enableComponentLocalStyle && shouldEnableComponentLocalStyle(id, options.componentMatcher)) return true;
353
+ if (options.enablePageLocalStyle && shouldEnablePageLocalStyle(id, options.pageMatcher)) return true;
354
+ return false;
355
+ }
356
+ function shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute) {
357
+ const lowerName = attrName.toLowerCase();
358
+ const shouldHandleDefault = !disabledDefaultTemplateHandler && lowerName === "class";
359
+ const shouldHandleCustom = matchCustomAttribute?.(tag, attrName) ?? false;
360
+ return {
361
+ shouldHandleDefault,
362
+ shouldHandleCustom,
363
+ shouldHandle: shouldHandleDefault || shouldHandleCustom
364
+ };
365
+ }
366
+ const defaultCreateJsHandlerOptions = { babelParserOptions: { plugins: ["typescript"] } };
367
+ const UVUE_NVUE_RE = /\.(?:uvue|nvue)(?:\?.*)?$/;
368
+ const SFC_BLOCK_RE = /<(template|script|style)\b([^>]*)>/gi;
369
+ const SCRIPT_SETUP_RE = /(?:^|\s)setup(?:\s|=|$)/;
370
+ const STYLE_SCOPED_RE = /(?:^|\s)scoped(?:\s|=|$)/i;
371
+ function findSfcBlockEnd(code, type, contentStart) {
372
+ const closeTag = new RegExp(`<\\/${type}\\s*>`, "gi");
373
+ if (type !== "template") {
374
+ const match = closeTag.exec(code.slice(contentStart));
375
+ return match ? {
376
+ blockEnd: contentStart + match.index + match[0].length,
377
+ contentEnd: contentStart + match.index
378
+ } : void 0;
379
+ }
380
+ const tag = new RegExp(`<\\/?${type}\\b[^>]*>`, "gi");
381
+ tag.lastIndex = contentStart;
382
+ let depth = 1;
383
+ let match = tag.exec(code);
384
+ while (match) {
385
+ if (match[0].startsWith("</")) depth -= 1;
386
+ else if (!match[0].trimEnd().endsWith("/>")) depth += 1;
387
+ if (depth === 0) return {
388
+ blockEnd: tag.lastIndex,
389
+ contentEnd: match.index
390
+ };
391
+ match = tag.exec(code);
392
+ }
393
+ }
394
+ function parseSfc(code) {
395
+ const descriptor = {
396
+ errors: [],
397
+ styles: []
398
+ };
399
+ for (const match of code.matchAll(SFC_BLOCK_RE)) {
400
+ const type = match[1];
401
+ const attrs = match[2] ?? "";
402
+ const contentStart = (match.index ?? 0) + match[0].length;
403
+ const blockBoundary = findSfcBlockEnd(code, type, contentStart);
404
+ if (!blockBoundary) {
405
+ descriptor.errors.push(/* @__PURE__ */ new Error(`未闭合的 ${type} SFC 块`));
406
+ continue;
407
+ }
408
+ const { contentEnd } = blockBoundary;
409
+ const content = code.slice(contentStart, contentEnd);
410
+ const block = {
411
+ content,
412
+ start: contentStart,
413
+ end: contentEnd,
414
+ attrs
415
+ };
416
+ if (type === "template" && !descriptor.template) {
417
+ try {
418
+ descriptor.template = {
419
+ ...block,
420
+ ast: parse(content)
421
+ };
422
+ } catch (error) {
423
+ descriptor.errors.push(error);
424
+ descriptor.template = block;
425
+ }
426
+ continue;
427
+ }
428
+ if (type === "script") {
429
+ if (SCRIPT_SETUP_RE.test(attrs)) descriptor.scriptSetup ?? (descriptor.scriptSetup = block);
430
+ else descriptor.script ?? (descriptor.script = block);
431
+ continue;
432
+ }
433
+ if (type === "style") descriptor.styles.push(block);
434
+ }
435
+ return descriptor;
436
+ }
437
+ function transformUVue(code, id, jsHandler, runtimeSet, options = {}) {
438
+ if (!UVUE_NVUE_RE.test(id)) return;
439
+ const { customAttributesEntities, disabledDefaultTemplateHandler = false } = options;
440
+ const matchCustomAttribute = createAttributeMatcher(customAttributesEntities);
441
+ const ms = new MagicString(code);
442
+ const descriptor = parseSfc(code);
443
+ const localStyleCollector = shouldEnableLocalStyle(id, options) ? new UniAppXComponentLocalStyleCollector(id, runtimeSet) : void 0;
444
+ if (descriptor.errors.length === 0) {
445
+ if (descriptor.template?.ast) {
446
+ const templateOffset = descriptor.template.start;
447
+ traverse(descriptor.template.ast, (node) => {
448
+ if (node.type !== NodeTypes.ELEMENT) return;
449
+ const tag = node.tag;
450
+ for (const prop of node.props) if (prop.type === NodeTypes.ATTRIBUTE) {
451
+ const { shouldHandle, shouldHandleCustom, shouldHandleDefault } = shouldHandleAttribute(tag, prop.name, disabledDefaultTemplateHandler, matchCustomAttribute);
452
+ if (!shouldHandle) continue;
453
+ if (localStyleCollector) updateStaticAttributeWithLocalStyle(ms, prop, templateOffset, localStyleCollector, options.webCustomAttributeDeep === true && shouldHandleCustom);
454
+ else updateStaticAttribute(ms, prop, templateOffset);
455
+ if (shouldHandleDefault) continue;
456
+ } else if (prop.type === NodeTypes.DIRECTIVE && prop.name === "bind" && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic) {
457
+ const attrName = prop.arg.content;
458
+ const { shouldHandle, shouldHandleCustom } = shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute);
459
+ if (!shouldHandle) continue;
460
+ if (localStyleCollector) updateDirectiveExpressionWithLocalStyle(ms, prop, templateOffset, jsHandler, localStyleCollector, options.webCustomAttributeDeep === true && shouldHandleCustom, runtimeSet);
461
+ else updateDirectiveExpression(ms, prop, templateOffset, jsHandler, runtimeSet);
462
+ }
463
+ });
464
+ }
465
+ if (descriptor.script) {
466
+ localStyleCollector?.collectRuntimeClasses(descriptor.script.content);
467
+ const { code } = jsHandler(descriptor.script.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
468
+ ms.update(descriptor.script.start, descriptor.script.end, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
469
+ }
470
+ if (descriptor.scriptSetup) {
471
+ localStyleCollector?.collectRuntimeClasses(descriptor.scriptSetup.content);
472
+ const { code } = jsHandler(descriptor.scriptSetup.content, runtimeSet ?? /* @__PURE__ */ new Set(), defaultCreateJsHandlerOptions);
473
+ ms.update(descriptor.scriptSetup.start, descriptor.scriptSetup.end, localStyleCollector ? localStyleCollector.rewriteTransformedCode(code) : code);
474
+ }
475
+ if (localStyleCollector?.hasStyles()) {
476
+ const scopedStyle = descriptor.styles.findLast((style) => STYLE_SCOPED_RE.test(style.attrs));
477
+ if (scopedStyle && options.onWebLocalStyleRules) options.onWebLocalStyleRules(localStyleCollector.toStyleRules({ web: true }));
478
+ else if (scopedStyle) {
479
+ const separator = scopedStyle.content.endsWith("\n") ? "" : "\n";
480
+ ms.appendLeft(scopedStyle.end, `${separator}${localStyleCollector.toStyleRules()}`);
481
+ } else ms.append(`\n${localStyleCollector.toStyleBlock({ web: Boolean(options.onWebLocalStyleRules) })}`);
482
+ }
483
+ }
484
+ const result = {
485
+ code: ms.toString(),
486
+ map: null
487
+ };
488
+ Object.defineProperty(result, "map", {
489
+ configurable: true,
490
+ enumerable: true,
491
+ get() {
492
+ return ms.generateMap();
493
+ }
494
+ });
495
+ return result;
496
+ }
497
+ //#endregion
498
+ export { transformUVue };
@@ -2,8 +2,10 @@ import type { CssCalcOptions, CssPreflightOptions, IStyleHandlerOptions, PresetE
2
2
  import type { ICustomAttributes } from '../shared.js';
3
3
  import type { TailwindCssRuntimeOptions } from '../../tailwindcss/runtime-types.js';
4
4
  export interface UniAppXComponentLocalStylesOptions {
5
+ componentMatcher?: ((id: string) => boolean) | undefined;
5
6
  enabled?: boolean | undefined;
6
7
  onlyWhenStyleIsolationVersion2?: boolean | undefined;
8
+ pageMatcher?: ((id: string) => boolean) | undefined;
7
9
  }
8
10
  export interface UniAppXOptions {
9
11
  enabled?: boolean | undefined;
@@ -3,8 +3,6 @@ interface RewriteCodeOptions {
3
3
  wrapExpression?: boolean;
4
4
  }
5
5
  export declare function hasTopLevelVariant(candidate: string): boolean;
6
- export declare function shouldEnableComponentLocalStyle(id: string): boolean;
7
- export declare function shouldEnablePageLocalStyle(id: string): boolean;
8
6
  export declare class UniAppXComponentLocalStyleCollector {
9
7
  private readonly fileId;
10
8
  private readonly runtimeSet?;
@@ -19,7 +17,11 @@ export declare class UniAppXComponentLocalStyleCollector {
19
17
  collectRuntimeClasses(rawSource: string, options?: RewriteCodeOptions): void;
20
18
  rewriteTransformedCode(rawSource: string, options?: RewriteCodeOptions): string;
21
19
  hasStyles(): boolean;
22
- toStyleBlock(): string;
23
- toStyleRules(): string;
20
+ toStyleBlock(options?: {
21
+ web?: boolean;
22
+ }): string;
23
+ toStyleRules(options?: {
24
+ web?: boolean;
25
+ }): string;
24
26
  }
25
27
  export {};
@@ -0,0 +1,4 @@
1
+ export type UniAppXLocalStyleMatcher = (id: string) => boolean;
2
+ export declare function normalizeUniAppXLocalStyleId(id: string): string;
3
+ export declare function shouldEnableComponentLocalStyle(id: string, matcher?: UniAppXLocalStyleMatcher): boolean;
4
+ export declare function shouldEnablePageLocalStyle(id: string, matcher?: UniAppXLocalStyleMatcher): boolean;
@@ -1,7 +1,9 @@
1
1
  import type { UserDefinedOptions } from '../types/index.js';
2
2
  export interface ResolvedUniAppXComponentLocalStylesOptions {
3
+ componentMatcher?: ((id: string) => boolean) | undefined;
3
4
  enabled: boolean;
4
5
  onlyWhenStyleIsolationVersion2: boolean;
6
+ pageMatcher?: ((id: string) => boolean) | undefined;
5
7
  }
6
8
  export interface ResolvedUniAppXOptions {
7
9
  enabled: boolean;
@@ -1,4 +1,6 @@
1
+ import type { UniAppXLocalStyleMatcher } from '../local-style-matcher.js';
1
2
  export interface HarmonyStyleInjectOptions {
3
+ componentMatcher?: UniAppXLocalStyleMatcher | undefined;
2
4
  cssSources?: Iterable<string | undefined> | undefined;
3
5
  styleAssetFiles?: Iterable<string | undefined> | ((file: string) => Iterable<string | undefined>) | undefined;
4
6
  excludeComponents?: boolean | undefined;
@@ -1,10 +1,12 @@
1
1
  import type { TransformResult } from 'vite';
2
2
  import type { ICustomAttributesEntities, JsHandler } from '../types/index.js';
3
3
  interface TransformUVueOptions {
4
+ componentMatcher?: (id: string) => boolean;
4
5
  customAttributesEntities?: ICustomAttributesEntities;
5
6
  disabledDefaultTemplateHandler?: boolean;
6
7
  enableComponentLocalStyle?: boolean;
7
8
  enablePageLocalStyle?: boolean;
9
+ pageMatcher?: (id: string) => boolean;
8
10
  webCustomAttributeDeep?: boolean;
9
11
  onWebLocalStyleRules?: (rules: string) => void;
10
12
  }
@@ -5,6 +5,7 @@ export interface CreateUniAppXPluginsOptions {
5
5
  customAttributesEntities: ICustomAttributesEntities;
6
6
  disabledDefaultTemplateHandler: boolean | undefined;
7
7
  mainCssChunkMatcher: NonNullable<InternalUserDefinedOptions['mainCssChunkMatcher']>;
8
+ registerModuleGraphCandidates?: ((id: string, source: string) => Promise<Set<string>>) | undefined;
8
9
  runtimeState: {
9
10
  readyPromise: Promise<unknown>;
10
11
  };
@@ -5,7 +5,6 @@ export declare function normalizeRelativeTailwindReferences(code: string, id: st
5
5
  interface ResolvePreprocessorTransformOptions {
6
6
  isIosPlatform: boolean;
7
7
  isNativeAppStyleTarget: boolean;
8
- isWebGeneratorTarget: boolean;
9
8
  }
10
9
  export declare function resolvePreprocessorTransform(code: string, id: string, lang: string | undefined, options: ResolvePreprocessorTransformOptions): {
11
10
  result: {