@plumeria/unplugin 18.4.1 → 18.4.2

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 (3) hide show
  1. package/dist/core.js +49 -10
  2. package/dist/core.mjs +49 -10
  3. package/package.json +2 -2
package/dist/core.js CHANGED
@@ -65,12 +65,37 @@ const componentFunctionOf = (node) => {
65
65
  }
66
66
  return undefined;
67
67
  };
68
+ const unwrapPatternDefault = (pattern) => pattern?.type === 'AssignmentPattern' ? pattern.left : pattern;
69
+ const destructuredPropAliases = (fn) => {
70
+ const aliases = new Map();
71
+ const first = fn.params[0];
72
+ const pattern = unwrapPatternDefault(first?.pat ?? first);
73
+ if (pattern?.type !== 'ObjectPattern')
74
+ return aliases;
75
+ for (const item of pattern.properties ?? []) {
76
+ if (item.type !== 'KeyValuePatternProperty')
77
+ continue;
78
+ const local = unwrapPatternDefault(item.value);
79
+ const key = item.key;
80
+ if (!utils_1.t.isIdentifier(local))
81
+ continue;
82
+ if (!utils_1.t.isIdentifier(key) && !utils_1.t.isStringLiteral(key))
83
+ continue;
84
+ const name = String(key.value);
85
+ if (name !== local.value)
86
+ aliases.set(local.value, name);
87
+ }
88
+ return aliases;
89
+ };
68
90
  const isStaticArgValue = (node) => node.type === 'StringLiteral' ||
69
91
  node.type === 'NumericLiteral' ||
70
92
  node.type === 'BooleanLiteral' ||
71
93
  (node.type === 'TemplateLiteral' && node.expressions.length === 0) ||
72
94
  utils_1.t.isIdentifier(node) ||
73
95
  utils_1.t.isMemberExpression(node);
96
+ const isNoOpStyle = (node) => utils_1.t.isNullLiteral(node) ||
97
+ (utils_1.t.isBooleanLiteral(node) && node.value === false) ||
98
+ (utils_1.t.isIdentifier(node) && node.value === 'undefined');
74
99
  const foldDynamicVars = (vars) => {
75
100
  const grouped = new Map();
76
101
  [...vars]
@@ -247,18 +272,22 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
247
272
  }
248
273
  }
249
274
  const components = [];
275
+ const propAliases = new Map();
276
+ const addComponent = (name, fn) => {
277
+ components.push({ name, node: fn });
278
+ const aliases = destructuredPropAliases(fn);
279
+ if (aliases.size > 0)
280
+ propAliases.set(name, aliases);
281
+ };
250
282
  for (const node of ast.body) {
251
283
  const statement = unwrapExport(node);
252
284
  if (isFunctionNode(statement)) {
253
- components.push({
254
- name: statement.identifier?.value ?? 'default',
255
- node: statement,
256
- });
285
+ addComponent(statement.identifier?.value ?? 'default', statement);
257
286
  }
258
287
  else if (statement?.type === 'CallExpression') {
259
288
  const fn = componentFunctionOf(statement);
260
289
  if (fn)
261
- components.push({ name: 'default', node: fn });
290
+ addComponent('default', fn);
262
291
  }
263
292
  else if (utils_1.t.isVariableDeclaration(statement)) {
264
293
  for (const decl of statement.declarations) {
@@ -266,7 +295,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
266
295
  continue;
267
296
  const fn = componentFunctionOf(decl.init);
268
297
  if (fn)
269
- components.push({ name: decl.id.value, node: fn });
298
+ addComponent(decl.id.value, fn);
270
299
  }
271
300
  }
272
301
  }
@@ -1121,6 +1150,8 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1121
1150
  return buildDecision(node, prefix, off, { n: 0 }, scoped);
1122
1151
  };
1123
1152
  const collectConditions = (node, currentTestStrings = [], argOrder) => {
1153
+ if (isNoOpStyle(node))
1154
+ return true;
1124
1155
  let branchStyle = resolveStyleObject(node);
1125
1156
  if (!branchStyle) {
1126
1157
  const dynamic = resolveDynamicCall(node);
@@ -1201,17 +1232,20 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1201
1232
  ? expr.value
1202
1233
  : expr.property.value;
1203
1234
  const source = utils_1.t.isIdentifier(expr) ? varName : getSource(expr);
1204
- markStylePropApplied(varName, expr);
1205
1235
  const owner = ownerComponentOf(expr);
1236
+ const propName = (utils_1.t.isIdentifier(expr) && owner
1237
+ ? propAliases.get(owner)?.get(varName)
1238
+ : undefined) ?? varName;
1239
+ markStylePropApplied(propName, expr);
1206
1240
  let possibilities = owner
1207
- ? scannedTables.componentPropsTable?.[`${resourcePath}-${owner}`]?.[varName]
1241
+ ? scannedTables.componentPropsTable?.[`${resourcePath}-${owner}`]?.[propName]
1208
1242
  : undefined;
1209
1243
  if (!possibilities) {
1210
1244
  const candidates = [];
1211
1245
  for (const key of Object.keys(scannedTables.componentPropsTable || {})) {
1212
1246
  if (!key.startsWith(`${resourcePath}-`))
1213
1247
  continue;
1214
- const entries = scannedTables.componentPropsTable?.[key]?.[varName];
1248
+ const entries = scannedTables.componentPropsTable?.[key]?.[propName];
1215
1249
  if (entries)
1216
1250
  candidates.push(...entries);
1217
1251
  }
@@ -1266,6 +1300,8 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1266
1300
  };
1267
1301
  for (const arg of args) {
1268
1302
  const expr = arg.expression;
1303
+ if (isNoOpStyle(expr))
1304
+ continue;
1269
1305
  if (pushPropPossibilities(expr, []))
1270
1306
  continue;
1271
1307
  if (utils_1.t.isCallExpression(expr) && utils_1.t.isIdentifier(expr.callee)) {
@@ -2090,10 +2126,13 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
2090
2126
  });
2091
2127
  }
2092
2128
  else {
2129
+ const keptClass = existingClassExpr
2130
+ ? `className={${existingClassExpr}}`
2131
+ : '';
2093
2132
  replacements.push({
2094
2133
  start: node.span.start - baseByteOffset,
2095
2134
  end: node.span.end - baseByteOffset,
2096
- content: styleAttr || '',
2135
+ content: `${keptClass}${styleAttr}`,
2097
2136
  });
2098
2137
  }
2099
2138
  },
package/dist/core.mjs CHANGED
@@ -29,12 +29,37 @@ const componentFunctionOf = (node) => {
29
29
  }
30
30
  return undefined;
31
31
  };
32
+ const unwrapPatternDefault = (pattern) => pattern?.type === 'AssignmentPattern' ? pattern.left : pattern;
33
+ const destructuredPropAliases = (fn) => {
34
+ const aliases = new Map();
35
+ const first = fn.params[0];
36
+ const pattern = unwrapPatternDefault(first?.pat ?? first);
37
+ if (pattern?.type !== 'ObjectPattern')
38
+ return aliases;
39
+ for (const item of pattern.properties ?? []) {
40
+ if (item.type !== 'KeyValuePatternProperty')
41
+ continue;
42
+ const local = unwrapPatternDefault(item.value);
43
+ const key = item.key;
44
+ if (!t.isIdentifier(local))
45
+ continue;
46
+ if (!t.isIdentifier(key) && !t.isStringLiteral(key))
47
+ continue;
48
+ const name = String(key.value);
49
+ if (name !== local.value)
50
+ aliases.set(local.value, name);
51
+ }
52
+ return aliases;
53
+ };
32
54
  const isStaticArgValue = (node) => node.type === 'StringLiteral' ||
33
55
  node.type === 'NumericLiteral' ||
34
56
  node.type === 'BooleanLiteral' ||
35
57
  (node.type === 'TemplateLiteral' && node.expressions.length === 0) ||
36
58
  t.isIdentifier(node) ||
37
59
  t.isMemberExpression(node);
60
+ const isNoOpStyle = (node) => t.isNullLiteral(node) ||
61
+ (t.isBooleanLiteral(node) && node.value === false) ||
62
+ (t.isIdentifier(node) && node.value === 'undefined');
38
63
  const foldDynamicVars = (vars) => {
39
64
  const grouped = new Map();
40
65
  [...vars]
@@ -211,18 +236,22 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
211
236
  }
212
237
  }
213
238
  const components = [];
239
+ const propAliases = new Map();
240
+ const addComponent = (name, fn) => {
241
+ components.push({ name, node: fn });
242
+ const aliases = destructuredPropAliases(fn);
243
+ if (aliases.size > 0)
244
+ propAliases.set(name, aliases);
245
+ };
214
246
  for (const node of ast.body) {
215
247
  const statement = unwrapExport(node);
216
248
  if (isFunctionNode(statement)) {
217
- components.push({
218
- name: statement.identifier?.value ?? 'default',
219
- node: statement,
220
- });
249
+ addComponent(statement.identifier?.value ?? 'default', statement);
221
250
  }
222
251
  else if (statement?.type === 'CallExpression') {
223
252
  const fn = componentFunctionOf(statement);
224
253
  if (fn)
225
- components.push({ name: 'default', node: fn });
254
+ addComponent('default', fn);
226
255
  }
227
256
  else if (t.isVariableDeclaration(statement)) {
228
257
  for (const decl of statement.declarations) {
@@ -230,7 +259,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
230
259
  continue;
231
260
  const fn = componentFunctionOf(decl.init);
232
261
  if (fn)
233
- components.push({ name: decl.id.value, node: fn });
262
+ addComponent(decl.id.value, fn);
234
263
  }
235
264
  }
236
265
  }
@@ -1085,6 +1114,8 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1085
1114
  return buildDecision(node, prefix, off, { n: 0 }, scoped);
1086
1115
  };
1087
1116
  const collectConditions = (node, currentTestStrings = [], argOrder) => {
1117
+ if (isNoOpStyle(node))
1118
+ return true;
1088
1119
  let branchStyle = resolveStyleObject(node);
1089
1120
  if (!branchStyle) {
1090
1121
  const dynamic = resolveDynamicCall(node);
@@ -1165,17 +1196,20 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1165
1196
  ? expr.value
1166
1197
  : expr.property.value;
1167
1198
  const source = t.isIdentifier(expr) ? varName : getSource(expr);
1168
- markStylePropApplied(varName, expr);
1169
1199
  const owner = ownerComponentOf(expr);
1200
+ const propName = (t.isIdentifier(expr) && owner
1201
+ ? propAliases.get(owner)?.get(varName)
1202
+ : undefined) ?? varName;
1203
+ markStylePropApplied(propName, expr);
1170
1204
  let possibilities = owner
1171
- ? scannedTables.componentPropsTable?.[`${resourcePath}-${owner}`]?.[varName]
1205
+ ? scannedTables.componentPropsTable?.[`${resourcePath}-${owner}`]?.[propName]
1172
1206
  : undefined;
1173
1207
  if (!possibilities) {
1174
1208
  const candidates = [];
1175
1209
  for (const key of Object.keys(scannedTables.componentPropsTable || {})) {
1176
1210
  if (!key.startsWith(`${resourcePath}-`))
1177
1211
  continue;
1178
- const entries = scannedTables.componentPropsTable?.[key]?.[varName];
1212
+ const entries = scannedTables.componentPropsTable?.[key]?.[propName];
1179
1213
  if (entries)
1180
1214
  candidates.push(...entries);
1181
1215
  }
@@ -1230,6 +1264,8 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1230
1264
  };
1231
1265
  for (const arg of args) {
1232
1266
  const expr = arg.expression;
1267
+ if (isNoOpStyle(expr))
1268
+ continue;
1233
1269
  if (pushPropPossibilities(expr, []))
1234
1270
  continue;
1235
1271
  if (t.isCallExpression(expr) && t.isIdentifier(expr.callee)) {
@@ -2054,10 +2090,13 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
2054
2090
  });
2055
2091
  }
2056
2092
  else {
2093
+ const keptClass = existingClassExpr
2094
+ ? `className={${existingClassExpr}}`
2095
+ : '';
2057
2096
  replacements.push({
2058
2097
  start: node.span.start - baseByteOffset,
2059
2098
  end: node.span.end - baseByteOffset,
2060
- content: styleAttr || '',
2099
+ content: `${keptClass}${styleAttr}`,
2061
2100
  });
2062
2101
  }
2063
2102
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/unplugin",
3
- "version": "18.4.1",
3
+ "version": "18.4.2",
4
4
  "description": "Universal Plumeria plugin for various build tools",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -94,7 +94,7 @@
94
94
  "dependencies": {
95
95
  "@rollup/pluginutils": "^5.4.0",
96
96
  "unplugin": "^3.0.0",
97
- "@plumeria/utils": "^18.4.1"
97
+ "@plumeria/utils": "^18.4.2"
98
98
  },
99
99
  "devDependencies": {
100
100
  "@swc/core": "1.15.47",