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