@plumeria/webpack-plugin 5.0.1 → 6.0.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.
Files changed (2) hide show
  1. package/dist/index.js +431 -210
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -14,15 +14,16 @@ if (process.env.NODE_ENV === 'production') {
14
14
  fs_1.default.writeFileSync(VIRTUAL_FILE_PATH, '/** Placeholder file */', 'utf-8');
15
15
  }
16
16
  function loader(source) {
17
+ const loaderContext = this;
17
18
  const callback = this.async();
19
+ const isProduction = process.env.NODE_ENV === 'production';
18
20
  if (this.resourcePath.includes('node_modules') ||
19
21
  !source.includes('@plumeria/core')) {
20
22
  return callback(null, source);
21
23
  }
22
24
  this.clearDependencies();
23
25
  this.addDependency(this.resourcePath);
24
- (0, utils_1.scanAll)((path) => this.addDependency(path));
25
- const extractedSheets = [];
26
+ const scannedTables = (0, utils_1.scanAll)();
26
27
  const fileStyles = {};
27
28
  const ast = (0, core_1.parseSync)(source, {
28
29
  syntax: 'typescript',
@@ -36,7 +37,8 @@ function loader(source) {
36
37
  ImportDeclaration({ node }) {
37
38
  const sourcePath = node.source.value;
38
39
  const actualPath = (0, utils_1.resolveImportPath)(sourcePath, resourcePath);
39
- if (actualPath && fs_1.default.existsSync(actualPath)) {
40
+ if (actualPath) {
41
+ loaderContext.addDependency(actualPath);
40
42
  node.specifiers.forEach((specifier) => {
41
43
  if (specifier.type === 'ImportSpecifier') {
42
44
  const importedName = specifier.imported
@@ -44,48 +46,94 @@ function loader(source) {
44
46
  : specifier.local.value;
45
47
  const localName = specifier.local.value;
46
48
  const uniqueKey = `${actualPath}-${importedName}`;
47
- if (utils_1.tables.staticTable[uniqueKey]) {
48
- importMap[localName] = utils_1.tables.staticTable[uniqueKey];
49
+ if (scannedTables.staticTable[uniqueKey]) {
50
+ importMap[localName] = scannedTables.staticTable[uniqueKey];
51
+ }
52
+ if (scannedTables.keyframesHashTable[uniqueKey]) {
53
+ importMap[localName] =
54
+ scannedTables.keyframesHashTable[uniqueKey];
55
+ }
56
+ if (scannedTables.viewTransitionHashTable[uniqueKey]) {
57
+ importMap[localName] =
58
+ scannedTables.viewTransitionHashTable[uniqueKey];
49
59
  }
50
- if (utils_1.tables.keyframesHashTable[uniqueKey]) {
51
- importMap[localName] = utils_1.tables.keyframesHashTable[uniqueKey];
60
+ if (scannedTables.themeTable[uniqueKey]) {
61
+ importMap[localName] = scannedTables.themeTable[uniqueKey];
52
62
  }
53
- if (utils_1.tables.viewTransitionHashTable[uniqueKey]) {
54
- importMap[localName] = utils_1.tables.viewTransitionHashTable[uniqueKey];
63
+ if (scannedTables.createHashTable[uniqueKey]) {
64
+ importMap[localName] = scannedTables.createHashTable[uniqueKey];
55
65
  }
56
- if (utils_1.tables.themeTable[uniqueKey]) {
57
- importMap[localName] = utils_1.tables.themeTable[uniqueKey];
66
+ if (scannedTables.variantsHashTable[uniqueKey]) {
67
+ importMap[localName] = scannedTables.variantsHashTable[uniqueKey];
58
68
  }
59
69
  }
60
70
  });
61
71
  }
62
72
  },
63
73
  });
64
- const mergedStaticTable = { ...utils_1.tables.staticTable };
74
+ const mergedStaticTable = {};
75
+ for (const key of Object.keys(scannedTables.staticTable)) {
76
+ mergedStaticTable[key] = scannedTables.staticTable[key];
77
+ }
65
78
  for (const key of Object.keys(localConsts)) {
66
79
  mergedStaticTable[key] = localConsts[key];
67
80
  }
68
81
  for (const key of Object.keys(importMap)) {
69
82
  mergedStaticTable[key] = importMap[key];
70
83
  }
71
- const mergedKeyframesTable = { ...utils_1.tables.keyframesHashTable };
84
+ const mergedKeyframesTable = {};
85
+ for (const key of Object.keys(scannedTables.keyframesHashTable)) {
86
+ mergedKeyframesTable[key] = scannedTables.keyframesHashTable[key];
87
+ }
72
88
  for (const key of Object.keys(importMap)) {
73
89
  mergedKeyframesTable[key] = importMap[key];
74
90
  }
75
- const mergedViewTransitionTable = { ...utils_1.tables.viewTransitionHashTable };
91
+ const mergedViewTransitionTable = {};
92
+ for (const key of Object.keys(scannedTables.viewTransitionHashTable)) {
93
+ mergedViewTransitionTable[key] = scannedTables.viewTransitionHashTable[key];
94
+ }
76
95
  for (const key of Object.keys(importMap)) {
77
96
  mergedViewTransitionTable[key] = importMap[key];
78
97
  }
79
- const mergedThemeTable = { ...utils_1.tables.themeTable };
98
+ const mergedThemeTable = {};
99
+ for (const key of Object.keys(scannedTables.themeTable)) {
100
+ mergedThemeTable[key] = scannedTables.themeTable[key];
101
+ }
80
102
  for (const key of Object.keys(importMap)) {
81
103
  mergedThemeTable[key] = importMap[key];
82
104
  }
83
- const isTSFile = this.resourcePath.endsWith('.ts') && !this.resourcePath.endsWith('.tsx');
105
+ const mergedCreateTable = {};
106
+ for (const key of Object.keys(scannedTables.createHashTable)) {
107
+ mergedCreateTable[key] = scannedTables.createHashTable[key];
108
+ }
109
+ for (const key of Object.keys(importMap)) {
110
+ mergedCreateTable[key] = importMap[key];
111
+ }
112
+ const mergedVariantsTable = {};
113
+ for (const key of Object.keys(scannedTables.variantsHashTable)) {
114
+ mergedVariantsTable[key] = scannedTables.variantsHashTable[key];
115
+ }
116
+ for (const key of Object.keys(importMap)) {
117
+ mergedVariantsTable[key] = importMap[key];
118
+ }
84
119
  const localCreateStyles = {};
85
120
  const replacements = [];
121
+ const extractedSheets = [];
86
122
  const processedDecls = new Set();
87
- const excludedSpans = new Set();
88
123
  const idSpans = new Set();
124
+ const excludedSpans = new Set();
125
+ const checkVariantAssignment = (decl) => {
126
+ if (decl.init &&
127
+ utils_1.t.isCallExpression(decl.init) &&
128
+ utils_1.t.isIdentifier(decl.init.callee)) {
129
+ const varName = decl.init.callee.value;
130
+ if ((localCreateStyles[varName] &&
131
+ localCreateStyles[varName].type === 'variant') ||
132
+ mergedVariantsTable[varName]) {
133
+ throw new Error(`Plumeria: Assigning the return value of "css.variants" to a variable is not supported.\nPlease pass the variant function directly to "css.props". Found assignment to: ${utils_1.t.isIdentifier(decl.id) ? decl.id.value : 'unknown'}`);
134
+ }
135
+ }
136
+ };
89
137
  const registerStyle = (node, declSpan, isExported) => {
90
138
  if (utils_1.t.isIdentifier(node.id) &&
91
139
  node.init &&
@@ -97,15 +145,17 @@ function loader(source) {
97
145
  const propName = node.init.callee.property.value;
98
146
  if (propName === 'create' &&
99
147
  utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
100
- const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
148
+ const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
101
149
  if (obj) {
102
150
  const hashMap = {};
103
151
  Object.entries(obj).forEach(([key, style]) => {
104
152
  const records = (0, utils_1.getStyleRecords)(key, style, 2);
105
- (0, utils_1.extractOndemandStyles)(style, extractedSheets);
106
- records.forEach((r) => {
107
- extractedSheets.push(r.sheet);
108
- });
153
+ if (!isProduction) {
154
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
155
+ records.forEach((r) => {
156
+ extractedSheets.push(r.sheet);
157
+ });
158
+ }
109
159
  const atomMap = {};
110
160
  records.forEach((r) => (atomMap[r.key] = r.hash));
111
161
  hashMap[key] = atomMap;
@@ -118,7 +168,6 @@ function loader(source) {
118
168
  type: 'create',
119
169
  obj,
120
170
  hashMap,
121
- hasDynamicAccess: false,
122
171
  isExported,
123
172
  initSpan: {
124
173
  start: node.init.span.start - ast.span.start,
@@ -139,7 +188,36 @@ function loader(source) {
139
188
  type: 'constant',
140
189
  obj: {},
141
190
  hashMap: {},
142
- hasDynamicAccess: false,
191
+ isExported,
192
+ initSpan: {
193
+ start: node.init.span.start - ast.span.start,
194
+ end: node.init.span.end - ast.span.start,
195
+ },
196
+ declSpan: {
197
+ start: declSpan.start - ast.span.start,
198
+ end: declSpan.end - ast.span.start,
199
+ },
200
+ };
201
+ }
202
+ else if (propName === 'variants' &&
203
+ utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
204
+ const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, (name) => {
205
+ if (localCreateStyles[name]) {
206
+ return localCreateStyles[name].obj;
207
+ }
208
+ if (mergedCreateTable[name]) {
209
+ const hash = mergedCreateTable[name];
210
+ if (scannedTables.createObjectTable[hash]) {
211
+ return scannedTables.createObjectTable[hash];
212
+ }
213
+ }
214
+ return undefined;
215
+ });
216
+ localCreateStyles[node.id.value] = {
217
+ name: node.id.value,
218
+ type: 'variant',
219
+ obj,
220
+ hashMap: {},
143
221
  isExported,
144
222
  initSpan: {
145
223
  start: node.init.span.start - ast.span.start,
@@ -154,10 +232,23 @@ function loader(source) {
154
232
  }
155
233
  };
156
234
  (0, utils_1.traverse)(ast, {
235
+ ImportDeclaration({ node }) {
236
+ if (node.specifiers) {
237
+ node.specifiers.forEach((specifier) => {
238
+ if (specifier.local) {
239
+ excludedSpans.add(specifier.local.span.start);
240
+ }
241
+ if (specifier.imported) {
242
+ excludedSpans.add(specifier.imported.span.start);
243
+ }
244
+ });
245
+ }
246
+ },
157
247
  ExportDeclaration({ node }) {
158
248
  if (utils_1.t.isVariableDeclaration(node.declaration)) {
159
249
  processedDecls.add(node.declaration);
160
250
  node.declaration.declarations.forEach((decl) => {
251
+ checkVariantAssignment(decl);
161
252
  registerStyle(decl, node.span, true);
162
253
  });
163
254
  }
@@ -166,25 +257,10 @@ function loader(source) {
166
257
  if (processedDecls.has(node))
167
258
  return;
168
259
  node.declarations.forEach((decl) => {
260
+ checkVariantAssignment(decl);
169
261
  registerStyle(decl, node.span, false);
170
262
  });
171
263
  },
172
- MemberExpression({ node }) {
173
- if (utils_1.t.isIdentifier(node.object)) {
174
- const styleInfo = localCreateStyles[node.object.value];
175
- if (styleInfo) {
176
- if (utils_1.t.isIdentifier(node.property)) {
177
- const hash = styleInfo.hashMap[node.property.value];
178
- if (!hash && styleInfo.type !== 'constant') {
179
- styleInfo.hasDynamicAccess = true;
180
- }
181
- }
182
- else {
183
- styleInfo.hasDynamicAccess = true;
184
- }
185
- }
186
- }
187
- },
188
264
  CallExpression({ node }) {
189
265
  const callee = node.callee;
190
266
  if (utils_1.t.isMemberExpression(callee) &&
@@ -195,9 +271,9 @@ function loader(source) {
195
271
  if (propName === 'keyframes' &&
196
272
  args.length > 0 &&
197
273
  utils_1.t.isObjectExpression(args[0].expression)) {
198
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
274
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
199
275
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
200
- utils_1.tables.keyframesObjectTable[hash] = obj;
276
+ scannedTables.keyframesObjectTable[hash] = obj;
201
277
  replacements.push({
202
278
  start: node.span.start - ast.span.start,
203
279
  end: node.span.end - ast.span.start,
@@ -207,11 +283,13 @@ function loader(source) {
207
283
  else if (propName === 'viewTransition' &&
208
284
  args.length > 0 &&
209
285
  utils_1.t.isObjectExpression(args[0].expression)) {
210
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
286
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
211
287
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
212
- utils_1.tables.viewTransitionObjectTable[hash] = obj;
213
- (0, utils_1.extractOndemandStyles)(obj, extractedSheets);
214
- (0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets);
288
+ scannedTables.viewTransitionObjectTable[hash] = obj;
289
+ if (!isProduction) {
290
+ (0, utils_1.extractOndemandStyles)(obj, extractedSheets, scannedTables);
291
+ (0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets, scannedTables);
292
+ }
215
293
  replacements.push({
216
294
  start: node.span.start - ast.span.start,
217
295
  end: node.span.end - ast.span.start,
@@ -221,43 +299,69 @@ function loader(source) {
221
299
  else if (propName === 'createTheme' &&
222
300
  args.length > 0 &&
223
301
  utils_1.t.isObjectExpression(args[0].expression)) {
224
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
302
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
303
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
304
+ scannedTables.createThemeObjectTable[hash] = obj;
305
+ }
306
+ else if (propName === 'create' &&
307
+ args.length > 0 &&
308
+ utils_1.t.isObjectExpression(args[0].expression)) {
309
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
225
310
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
226
- utils_1.tables.createThemeObjectTable[hash] = obj;
311
+ scannedTables.createObjectTable[hash] = obj;
312
+ Object.entries(obj).forEach(([key, style]) => {
313
+ if (typeof style === 'object' && style !== null) {
314
+ const records = (0, utils_1.getStyleRecords)(key, style, 2);
315
+ if (!isProduction) {
316
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
317
+ records.forEach((r) => extractedSheets.push(r.sheet));
318
+ }
319
+ }
320
+ });
227
321
  }
228
322
  }
229
323
  },
230
324
  });
231
325
  (0, utils_1.traverse)(ast, {
232
326
  MemberExpression({ node }) {
233
- if (excludedSpans.has(node.span.start))
234
- return;
235
327
  if (utils_1.t.isIdentifier(node.object) && utils_1.t.isIdentifier(node.property)) {
236
- const styleInfo = localCreateStyles[node.object.value];
237
- if (styleInfo && !styleInfo.hasDynamicAccess) {
238
- const atomMap = styleInfo.hashMap[node.property.value];
328
+ const varName = node.object.value;
329
+ const propName = node.property.value;
330
+ const styleInfo = localCreateStyles[varName];
331
+ if (styleInfo) {
332
+ const atomMap = styleInfo.hashMap[propName];
239
333
  if (atomMap) {
240
334
  replacements.push({
241
335
  start: node.span.start - ast.span.start,
242
336
  end: node.span.end - ast.span.start,
243
337
  content: JSON.stringify(atomMap),
244
338
  });
339
+ return;
340
+ }
341
+ }
342
+ const hash = mergedCreateTable[varName];
343
+ if (hash) {
344
+ const obj = scannedTables.createObjectTable[hash];
345
+ if (obj && obj[propName]) {
346
+ const style = obj[propName];
347
+ if (typeof style === 'object' && style !== null) {
348
+ const records = (0, utils_1.getStyleRecords)(propName, style, 2);
349
+ if (!isProduction) {
350
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
351
+ records.forEach((r) => extractedSheets.push(r.sheet));
352
+ }
353
+ const atomMap = {};
354
+ records.forEach((r) => (atomMap[r.key] = r.hash));
355
+ if (Object.keys(atomMap).length > 0) {
356
+ replacements.push({
357
+ start: node.span.start - ast.span.start,
358
+ end: node.span.end - ast.span.start,
359
+ content: JSON.stringify(atomMap),
360
+ });
361
+ }
362
+ }
245
363
  }
246
364
  }
247
- }
248
- },
249
- Identifier({ node }) {
250
- if (excludedSpans.has(node.span.start))
251
- return;
252
- if (idSpans.has(node.span.start))
253
- return;
254
- const styleInfo = localCreateStyles[node.value];
255
- if (styleInfo && !styleInfo.hasDynamicAccess) {
256
- replacements.push({
257
- start: node.span.start - ast.span.start,
258
- end: node.span.end - ast.span.start,
259
- content: JSON.stringify(styleInfo.hashMap),
260
- });
261
365
  }
262
366
  },
263
367
  CallExpression({ node }) {
@@ -266,182 +370,299 @@ function loader(source) {
266
370
  utils_1.t.isIdentifier(callee.object, { name: 'css' }) &&
267
371
  utils_1.t.isIdentifier(callee.property, { name: 'props' })) {
268
372
  const args = node.arguments;
269
- const checkStatic = (expr) => {
270
- if (utils_1.t.isObjectExpression(expr) ||
271
- utils_1.t.isStringLiteral(expr) ||
272
- utils_1.t.isNumericLiteral(expr) ||
273
- utils_1.t.isBooleanLiteral(expr) ||
274
- utils_1.t.isNullLiteral(expr))
275
- return true;
276
- if (utils_1.t.isMemberExpression(expr) &&
373
+ const resolveStyleObject = (expr) => {
374
+ if (utils_1.t.isObjectExpression(expr)) {
375
+ return (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
376
+ }
377
+ else if (utils_1.t.isMemberExpression(expr) &&
277
378
  utils_1.t.isIdentifier(expr.object) &&
278
- utils_1.t.isIdentifier(expr.property)) {
279
- const styleInfo = localCreateStyles[expr.object.value];
280
- return !!(styleInfo &&
281
- !styleInfo.hasDynamicAccess &&
282
- styleInfo.hashMap[expr.property.value]);
379
+ (utils_1.t.isIdentifier(expr.property) || expr.property.type === 'Computed')) {
380
+ if (expr.property.type === 'Computed') {
381
+ return {};
382
+ }
383
+ const varName = expr.object
384
+ .value;
385
+ const propName = expr.property
386
+ .value;
387
+ const styleInfo = localCreateStyles[varName];
388
+ if (styleInfo && styleInfo.obj[propName]) {
389
+ const style = styleInfo.obj[propName];
390
+ if (typeof style === 'object' && style !== null) {
391
+ return style;
392
+ }
393
+ }
394
+ const hash = mergedCreateTable[varName];
395
+ if (hash) {
396
+ const obj = scannedTables.createObjectTable[hash];
397
+ if (obj && obj[propName]) {
398
+ const style = obj[propName];
399
+ if (typeof style === 'object' && style !== null) {
400
+ return style;
401
+ }
402
+ }
403
+ }
283
404
  }
284
- if (utils_1.t.isIdentifier(expr)) {
285
- const styleInfo = localCreateStyles[expr.value];
286
- return !!(styleInfo && !styleInfo.hasDynamicAccess);
405
+ else if (utils_1.t.isIdentifier(expr)) {
406
+ const varName = expr.value;
407
+ const styleInfo = localCreateStyles[varName];
408
+ if (styleInfo && styleInfo.obj) {
409
+ return styleInfo.obj;
410
+ }
411
+ const hash = mergedCreateTable[varName];
412
+ if (hash) {
413
+ const obj = scannedTables.createObjectTable[hash];
414
+ if (obj && typeof obj === 'object') {
415
+ return obj;
416
+ }
417
+ }
418
+ if (localCreateStyles[varName]) {
419
+ return localCreateStyles[varName].obj;
420
+ }
421
+ const vHash = mergedVariantsTable[varName];
422
+ if (vHash) {
423
+ return scannedTables.variantsObjectTable[vHash];
424
+ }
287
425
  }
288
- return false;
426
+ return null;
289
427
  };
290
- const hasDynamicAccess = (expr) => {
291
- if (utils_1.t.isMemberExpression(expr) && utils_1.t.isIdentifier(expr.object)) {
292
- const info = localCreateStyles[expr.object.value];
293
- if (info && info.hasDynamicAccess)
294
- return true;
428
+ const conditionals = [];
429
+ let groupIdCounter = 0;
430
+ let baseStyle = {};
431
+ let isOptimizable = true;
432
+ for (const arg of args) {
433
+ const expr = arg.expression;
434
+ if (utils_1.t.isCallExpression(expr) && utils_1.t.isIdentifier(expr.callee)) {
435
+ const varName = expr.callee.value;
436
+ let variantObj;
437
+ if (localCreateStyles[varName] && localCreateStyles[varName].obj) {
438
+ variantObj = localCreateStyles[varName].obj;
439
+ }
440
+ else if (mergedVariantsTable[varName]) {
441
+ const hash = mergedVariantsTable[varName];
442
+ if (scannedTables.variantsObjectTable[hash]) {
443
+ variantObj = scannedTables.variantsObjectTable[hash];
444
+ }
445
+ }
446
+ if (variantObj) {
447
+ const callArgs = expr.arguments;
448
+ if (callArgs.length === 1 && !callArgs[0].spread) {
449
+ const arg = callArgs[0].expression;
450
+ if (arg.type === 'ObjectExpression') {
451
+ for (const prop of arg.properties) {
452
+ let groupName;
453
+ let valExpr;
454
+ if (prop.type === 'KeyValueProperty' &&
455
+ prop.key.type === 'Identifier') {
456
+ groupName = prop.key.value;
457
+ valExpr = prop.value;
458
+ }
459
+ else if (prop.type === 'Identifier') {
460
+ groupName = prop.value;
461
+ valExpr = prop;
462
+ }
463
+ if (groupName && valExpr) {
464
+ const groupVariants = variantObj[groupName];
465
+ if (!groupVariants)
466
+ continue;
467
+ const currentGroupId = ++groupIdCounter;
468
+ const valStart = valExpr.span.start - ast.span.start;
469
+ const valEnd = valExpr.span.end - ast.span.start;
470
+ const valSource = source.substring(valStart, valEnd);
471
+ if (valExpr.type === 'StringLiteral') {
472
+ if (groupVariants[valExpr.value]) {
473
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, groupVariants[valExpr.value]);
474
+ }
475
+ continue;
476
+ }
477
+ Object.entries(groupVariants).forEach(([optionName, style]) => {
478
+ conditionals.push({
479
+ test: valExpr,
480
+ testString: `${valSource} === '${optionName}'`,
481
+ truthy: style,
482
+ falsy: {},
483
+ groupId: currentGroupId,
484
+ });
485
+ });
486
+ }
487
+ }
488
+ continue;
489
+ }
490
+ const argStart = arg.span.start - ast.span.start;
491
+ const argEnd = arg.span.end - ast.span.start;
492
+ const argSource = source.substring(argStart, argEnd);
493
+ if (utils_1.t.isStringLiteral(arg)) {
494
+ if (variantObj[arg.value]) {
495
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[arg.value]);
496
+ }
497
+ continue;
498
+ }
499
+ const currentGroupId = ++groupIdCounter;
500
+ Object.entries(variantObj).forEach(([key, style]) => {
501
+ conditionals.push({
502
+ test: arg,
503
+ testString: `${argSource} === '${key}'`,
504
+ truthy: style,
505
+ falsy: {},
506
+ groupId: currentGroupId,
507
+ });
508
+ });
509
+ continue;
510
+ }
511
+ isOptimizable = false;
512
+ break;
513
+ }
295
514
  }
296
- if (utils_1.t.isIdentifier(expr)) {
297
- const info = localCreateStyles[expr.value];
298
- if (info && info.hasDynamicAccess)
299
- return true;
515
+ const staticStyle = resolveStyleObject(expr);
516
+ if (staticStyle) {
517
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
518
+ continue;
300
519
  }
301
- if (utils_1.t.isConditionalExpression(expr)) {
302
- return (hasDynamicAccess(expr.consequent) ||
303
- hasDynamicAccess(expr.alternate));
520
+ else if (expr.type === 'ConditionalExpression') {
521
+ const truthyStyle = resolveStyleObject(expr.consequent);
522
+ const falsyStyle = resolveStyleObject(expr.alternate);
523
+ if (truthyStyle !== null && falsyStyle !== null) {
524
+ conditionals.push({
525
+ test: expr.test,
526
+ truthy: truthyStyle,
527
+ falsy: falsyStyle,
528
+ });
529
+ continue;
530
+ }
304
531
  }
305
- if (utils_1.t.isBinaryExpression(expr) &&
306
- (expr.operator === '&&' ||
307
- expr.operator === '||' ||
308
- expr.operator === '??')) {
309
- return hasDynamicAccess(expr.left) || hasDynamicAccess(expr.right);
532
+ else if (expr.type === 'BinaryExpression' &&
533
+ expr.operator === '&&') {
534
+ const truthyStyle = resolveStyleObject(expr.right);
535
+ if (truthyStyle !== null) {
536
+ conditionals.push({
537
+ test: expr.left,
538
+ truthy: truthyStyle,
539
+ falsy: {},
540
+ });
541
+ continue;
542
+ }
310
543
  }
311
- return false;
312
- };
313
- const allStatic = args.every((arg) => checkStatic(arg.expression));
314
- const anyDynamic = args.some((arg) => hasDynamicAccess(arg.expression));
315
- if (allStatic && args.length > 0) {
316
- const merged = args.reduce((acc, arg) => {
317
- const expr = arg.expression;
318
- if (utils_1.t.isObjectExpression(expr)) {
319
- const obj = (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
320
- return obj ? (0, utils_1.deepMerge)(acc, obj) : acc;
544
+ else if (expr.type === 'ParenthesisExpression') {
545
+ const inner = expr.expression;
546
+ const innerStatic = resolveStyleObject(inner);
547
+ if (innerStatic) {
548
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, innerStatic);
549
+ continue;
321
550
  }
322
- else if (utils_1.t.isMemberExpression(expr) &&
323
- utils_1.t.isIdentifier(expr.object) &&
324
- utils_1.t.isIdentifier(expr.property)) {
325
- const styleInfo = localCreateStyles[expr.object.value];
326
- return styleInfo
327
- ? (0, utils_1.deepMerge)(acc, styleInfo.obj[expr.property.value])
328
- : acc;
551
+ }
552
+ isOptimizable = false;
553
+ break;
554
+ }
555
+ if (isOptimizable &&
556
+ (args.length > 0 || Object.keys(baseStyle).length > 0)) {
557
+ if (conditionals.length === 0) {
558
+ if (!isProduction) {
559
+ (0, utils_1.extractOndemandStyles)(baseStyle, extractedSheets, scannedTables);
329
560
  }
330
- else if (utils_1.t.isIdentifier(expr)) {
331
- const styleInfo = localCreateStyles[expr.value];
332
- return styleInfo ? (0, utils_1.deepMerge)(acc, styleInfo.obj) : acc;
561
+ const hash = (0, zss_engine_1.genBase36Hash)(baseStyle, 1, 8);
562
+ const records = (0, utils_1.getStyleRecords)(hash, baseStyle, 2);
563
+ if (!isProduction) {
564
+ records.forEach((r) => extractedSheets.push(r.sheet));
333
565
  }
334
- return acc;
335
- }, {});
336
- if (Object.keys(merged).length > 0) {
337
- (0, utils_1.extractOndemandStyles)(merged, extractedSheets);
338
- const hash = (0, zss_engine_1.genBase36Hash)(merged, 1, 8);
339
- const records = (0, utils_1.getStyleRecords)(hash, merged, 2);
340
- records.forEach((r) => extractedSheets.push(r.sheet));
341
- const resultHash = records
342
- .map((r) => r.hash)
343
- .join(' ');
566
+ const className = records.map((r) => r.hash).join(' ');
344
567
  replacements.push({
345
568
  start: node.span.start - ast.span.start,
346
569
  end: node.span.end - ast.span.start,
347
- content: JSON.stringify(resultHash),
570
+ content: JSON.stringify(className),
348
571
  });
349
572
  }
350
- }
351
- else if (anyDynamic) {
352
- const processExpr = (expr) => {
353
- if (utils_1.t.isIdentifier(expr)) {
354
- const info = localCreateStyles[expr.value];
355
- if (info && info.hasDynamicAccess) {
356
- excludedSpans.add(expr.span.start);
573
+ else {
574
+ const table = {};
575
+ const combinations = 1 << conditionals.length;
576
+ for (let i = 0; i < combinations; i++) {
577
+ const currentClassNames = [];
578
+ const seenGroups = new Set();
579
+ let impossible = false;
580
+ if (Object.keys(baseStyle).length > 0) {
581
+ if (!isProduction) {
582
+ (0, utils_1.extractOndemandStyles)(baseStyle, extractedSheets, scannedTables);
583
+ }
584
+ const hash = (0, zss_engine_1.genBase36Hash)(baseStyle, 1, 8);
585
+ const records = (0, utils_1.getStyleRecords)(hash, baseStyle, 2);
586
+ if (!isProduction) {
587
+ records.forEach((r) => extractedSheets.push(r.sheet));
588
+ }
589
+ currentClassNames.push(...records.map((r) => r.hash));
357
590
  }
358
- }
359
- else if (utils_1.t.isConditionalExpression(expr)) {
360
- processExpr(expr.consequent);
361
- processExpr(expr.alternate);
362
- }
363
- else if (utils_1.t.isBinaryExpression(expr) &&
364
- (expr.operator === '&&' ||
365
- expr.operator === '||' ||
366
- expr.operator === '??')) {
367
- processExpr(expr.left);
368
- processExpr(expr.right);
369
- }
370
- };
371
- args.forEach((arg) => processExpr(arg.expression));
372
- }
373
- else {
374
- const processExpr = (expr) => {
375
- if (utils_1.t.isMemberExpression(expr) &&
376
- utils_1.t.isIdentifier(expr.object) &&
377
- utils_1.t.isIdentifier(expr.property)) {
378
- const info = localCreateStyles[expr.object.value];
379
- if (info) {
380
- const atomMap = info.hashMap[expr.property.value];
381
- if (atomMap) {
382
- excludedSpans.add(expr.span.start);
383
- replacements.push({
384
- start: expr.span.start - ast.span.start,
385
- end: expr.span.end - ast.span.start,
386
- content: JSON.stringify(atomMap),
387
- });
591
+ for (let j = 0; j < conditionals.length; j++) {
592
+ let targetStyle = {};
593
+ if ((i >> j) & 1) {
594
+ if (conditionals[j].groupId !== undefined) {
595
+ if (seenGroups.has(conditionals[j].groupId)) {
596
+ impossible = true;
597
+ break;
598
+ }
599
+ seenGroups.add(conditionals[j].groupId);
600
+ }
601
+ targetStyle = conditionals[j].truthy;
602
+ }
603
+ else {
604
+ targetStyle = conditionals[j].falsy;
605
+ }
606
+ if (Object.keys(targetStyle).length > 0) {
607
+ if (!isProduction) {
608
+ (0, utils_1.extractOndemandStyles)(targetStyle, extractedSheets, scannedTables);
609
+ }
610
+ const hash = (0, zss_engine_1.genBase36Hash)(targetStyle, 1, 8);
611
+ const records = (0, utils_1.getStyleRecords)(hash, targetStyle, 2);
612
+ if (!isProduction) {
613
+ records.forEach((r) => extractedSheets.push(r.sheet));
614
+ }
615
+ currentClassNames.push(...records.map((r) => r.hash));
388
616
  }
389
617
  }
390
- }
391
- else if (utils_1.t.isIdentifier(expr)) {
392
- const info = localCreateStyles[expr.value];
393
- if (info) {
394
- excludedSpans.add(expr.span.start);
395
- replacements.push({
396
- start: expr.span.start - ast.span.start,
397
- end: expr.span.end - ast.span.start,
398
- content: JSON.stringify(info.hashMap),
399
- });
618
+ if (impossible) {
619
+ table[i] = '';
620
+ continue;
400
621
  }
622
+ table[i] = currentClassNames.join(' ');
401
623
  }
402
- else if (utils_1.t.isConditionalExpression(expr)) {
403
- processExpr(expr.consequent);
404
- processExpr(expr.alternate);
624
+ let indexExpr = '';
625
+ if (conditionals.length === 0) {
626
+ indexExpr = '0';
405
627
  }
406
- else if (utils_1.t.isBinaryExpression(expr) &&
407
- (expr.operator === '&&' ||
408
- expr.operator === '||' ||
409
- expr.operator === '??')) {
410
- processExpr(expr.left);
411
- processExpr(expr.right);
628
+ else {
629
+ const parts = conditionals.map((c, idx) => {
630
+ if (c.testString) {
631
+ return `(!!(${c.testString}) << ${idx})`;
632
+ }
633
+ const start = c.test.span.start - ast.span.start;
634
+ const end = c.test.span.end - ast.span.start;
635
+ const testStr = source.substring(start, end);
636
+ return `(!!(${testStr}) << ${idx})`;
637
+ });
638
+ indexExpr = parts.join(' | ');
412
639
  }
413
- };
414
- args.forEach((arg) => processExpr(arg.expression));
640
+ const tableStr = JSON.stringify(table);
641
+ const replacement = `${tableStr}[${indexExpr}]`;
642
+ replacements.push({
643
+ start: node.span.start - ast.span.start,
644
+ end: node.span.end - ast.span.start,
645
+ content: replacement,
646
+ });
647
+ }
415
648
  }
416
649
  }
417
650
  },
418
651
  });
419
652
  Object.values(localCreateStyles).forEach((info) => {
420
653
  if (info.isExported) {
421
- const content = isTSFile || info.hasDynamicAccess
422
- ? JSON.stringify(info.hashMap)
423
- : JSON.stringify('');
424
654
  replacements.push({
425
- start: info.initSpan.start,
426
- end: info.initSpan.end,
427
- content,
655
+ start: info.declSpan.start,
656
+ end: info.declSpan.end,
657
+ content: JSON.stringify(''),
428
658
  });
429
659
  }
430
660
  else {
431
- if (info.hasDynamicAccess) {
432
- replacements.push({
433
- start: info.initSpan.start,
434
- end: info.initSpan.end,
435
- content: JSON.stringify(info.hashMap),
436
- });
437
- }
438
- else {
439
- replacements.push({
440
- start: info.declSpan.start,
441
- end: info.declSpan.end,
442
- content: '',
443
- });
444
- }
661
+ replacements.push({
662
+ start: info.declSpan.start,
663
+ end: info.declSpan.end,
664
+ content: '',
665
+ });
445
666
  }
446
667
  });
447
668
  if (extractedSheets.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/webpack-plugin",
3
- "version": "5.0.1",
3
+ "version": "6.0.0",
4
4
  "description": "Plumeria Webpack plugin",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -22,7 +22,7 @@
22
22
  "zero-virtual.css"
23
23
  ],
24
24
  "dependencies": {
25
- "@plumeria/utils": "^5.0.1"
25
+ "@plumeria/utils": "^6.0.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@swc/core": "1.15.8",