@plumeria/utils 4.1.2 → 4.2.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export type { CSSObject, FileStyles } from './types';
2
- export { scanForCreateStatic, scanForCreateTheme, scanForKeyframes, scanForViewTransition, objectExpressionToObject, collectLocalConsts, traverse, t, tables, extractOndemandStyles, deepMerge, } from './parser';
2
+ export { objectExpressionToObject, collectLocalConsts, traverse, t, tables, extractOndemandStyles, deepMerge, scanAll, } from './parser';
3
3
  export { getStyleRecords } from './create';
4
4
  export type { StyleRecord } from './create';
package/dist/index.js CHANGED
@@ -1,11 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStyleRecords = exports.deepMerge = exports.extractOndemandStyles = exports.tables = exports.t = exports.traverse = exports.collectLocalConsts = exports.objectExpressionToObject = exports.scanForViewTransition = exports.scanForKeyframes = exports.scanForCreateTheme = exports.scanForCreateStatic = void 0;
3
+ exports.getStyleRecords = exports.scanAll = exports.deepMerge = exports.extractOndemandStyles = exports.tables = exports.t = exports.traverse = exports.collectLocalConsts = exports.objectExpressionToObject = void 0;
4
4
  var parser_1 = require("./parser");
5
- Object.defineProperty(exports, "scanForCreateStatic", { enumerable: true, get: function () { return parser_1.scanForCreateStatic; } });
6
- Object.defineProperty(exports, "scanForCreateTheme", { enumerable: true, get: function () { return parser_1.scanForCreateTheme; } });
7
- Object.defineProperty(exports, "scanForKeyframes", { enumerable: true, get: function () { return parser_1.scanForKeyframes; } });
8
- Object.defineProperty(exports, "scanForViewTransition", { enumerable: true, get: function () { return parser_1.scanForViewTransition; } });
9
5
  Object.defineProperty(exports, "objectExpressionToObject", { enumerable: true, get: function () { return parser_1.objectExpressionToObject; } });
10
6
  Object.defineProperty(exports, "collectLocalConsts", { enumerable: true, get: function () { return parser_1.collectLocalConsts; } });
11
7
  Object.defineProperty(exports, "traverse", { enumerable: true, get: function () { return parser_1.traverse; } });
@@ -13,5 +9,6 @@ Object.defineProperty(exports, "t", { enumerable: true, get: function () { retur
13
9
  Object.defineProperty(exports, "tables", { enumerable: true, get: function () { return parser_1.tables; } });
14
10
  Object.defineProperty(exports, "extractOndemandStyles", { enumerable: true, get: function () { return parser_1.extractOndemandStyles; } });
15
11
  Object.defineProperty(exports, "deepMerge", { enumerable: true, get: function () { return parser_1.deepMerge; } });
12
+ Object.defineProperty(exports, "scanAll", { enumerable: true, get: function () { return parser_1.scanAll; } });
16
13
  var create_1 = require("./create");
17
14
  Object.defineProperty(exports, "getStyleRecords", { enumerable: true, get: function () { return create_1.getStyleRecords; } });
package/dist/parser.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CSSObject, StaticTable, KeyframesHashTable, KeyframesObjectTable, ThemeTable, ViewTransitionObjectTable, ViewTransitionHashTable, Tables } from './types';
1
+ import type { CSSObject, StaticTable, KeyframesHashTable, ThemeTable, ViewTransitionHashTable, Tables } from './types';
2
2
  import { Module, CallExpression, Identifier, KeyValueProperty, StringLiteral, NumericLiteral, BooleanLiteral, ObjectExpression, MemberExpression, TemplateLiteral, BinaryExpression, UnaryExpression, VariableDeclaration, VariableDeclarator, ExportDeclaration, ConditionalExpression } from '@swc/core';
3
3
  export declare const t: {
4
4
  isObjectExpression: (node: any) => node is ObjectExpression;
@@ -28,19 +28,7 @@ export declare function traverse(node: Module, visitor: {
28
28
  }): void;
29
29
  export declare const tables: Tables;
30
30
  export declare function objectExpressionToObject(node: ObjectExpression, staticTable: StaticTable, keyframesHashTable: KeyframesHashTable, viewTransitionHashTable: ViewTransitionHashTable, themeTable: ThemeTable): CSSObject;
31
- export declare function collectLocalConsts(ast: Module): Record<string, any>;
32
- export declare function scanForKeyframes(addDependency: (path: string) => void): {
33
- keyframesHashTableLocal: KeyframesHashTable;
34
- keyframesObjectTableLocal: KeyframesObjectTable;
35
- };
36
- export declare function scanForViewTransition(addDependency: (path: string) => void): {
37
- viewTransitionHashTableLocal: ViewTransitionHashTable;
38
- viewTransitionObjectTableLocal: ViewTransitionObjectTable;
39
- };
40
- export declare function scanForCreateStatic(addDependency: (path: string) => void): StaticTable;
41
- export declare function scanForCreateTheme(addDependency: (path: string) => void): {
42
- themeTableLocal: Record<string, Record<string, any>>;
43
- createThemeObjectTableLocal: Record<string, Record<string, any>>;
44
- };
31
+ export declare function collectLocalConsts(ast: Module, filePath: string): Record<string, any>;
32
+ export declare function scanAll(addDependency: (path: string) => void): Tables;
45
33
  export declare function extractOndemandStyles(obj: any, extractedSheets: string[]): void;
46
34
  export declare function deepMerge(target: Record<string, any>, source: Record<string, any>): Record<string, any>;
package/dist/parser.js CHANGED
@@ -7,10 +7,7 @@ exports.tables = exports.t = void 0;
7
7
  exports.traverse = traverse;
8
8
  exports.objectExpressionToObject = objectExpressionToObject;
9
9
  exports.collectLocalConsts = collectLocalConsts;
10
- exports.scanForKeyframes = scanForKeyframes;
11
- exports.scanForViewTransition = scanForViewTransition;
12
- exports.scanForCreateStatic = scanForCreateStatic;
13
- exports.scanForCreateTheme = scanForCreateTheme;
10
+ exports.scanAll = scanAll;
14
11
  exports.extractOndemandStyles = extractOndemandStyles;
15
12
  exports.deepMerge = deepMerge;
16
13
  const createTheme_1 = require("./createTheme");
@@ -87,12 +84,19 @@ exports.tables = {
87
84
  viewTransitionObjectTable: {},
88
85
  createThemeObjectTable: {},
89
86
  };
87
+ function genFileId(filePath) {
88
+ const relativePath = path_1.default
89
+ .relative(PROJECT_ROOT, filePath)
90
+ .split(path_1.default.sep)
91
+ .join('/');
92
+ return (0, zss_engine_1.genBase36Hash)(relativePath, 1, 8);
93
+ }
90
94
  function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
91
95
  const obj = {};
92
96
  node.properties.forEach((prop) => {
93
97
  if (!exports.t.isObjectProperty(prop))
94
98
  return;
95
- const key = getPropertyKey(prop.key, staticTable);
99
+ const key = getPropertyKey(prop.key, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
96
100
  if (!key)
97
101
  return;
98
102
  const val = prop.value;
@@ -142,8 +146,19 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
142
146
  });
143
147
  return obj;
144
148
  }
145
- function collectLocalConsts(ast) {
149
+ function collectLocalConsts(ast, filePath) {
146
150
  const localConsts = {};
151
+ const fileId = genFileId(filePath);
152
+ const createProxy = (table) => {
153
+ return new Proxy(table, {
154
+ get: (target, prop) => {
155
+ if (typeof prop === 'string') {
156
+ return target[`${fileId}-${prop}`];
157
+ }
158
+ return Reflect.get(target, prop);
159
+ },
160
+ });
161
+ };
147
162
  traverse(ast, {
148
163
  VariableDeclarator({ node }) {
149
164
  if (exports.t.isIdentifier(node.id) && node.init) {
@@ -151,14 +166,14 @@ function collectLocalConsts(ast) {
151
166
  localConsts[node.id.value] = node.init.value;
152
167
  }
153
168
  else if (exports.t.isObjectExpression(node.init)) {
154
- localConsts[node.id.value] = objectExpressionToObject(node.init, localConsts, exports.tables.keyframesHashTable, exports.tables.viewTransitionHashTable, exports.tables.themeTable);
169
+ localConsts[node.id.value] = objectExpressionToObject(node.init, localConsts, createProxy(exports.tables.keyframesHashTable), createProxy(exports.tables.viewTransitionHashTable), createProxy(exports.tables.themeTable));
155
170
  }
156
171
  }
157
172
  },
158
173
  });
159
174
  return localConsts;
160
175
  }
161
- function getPropertyKey(node, staticTable) {
176
+ function getPropertyKey(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
162
177
  if (exports.t.isIdentifier(node)) {
163
178
  if (staticTable[node.value] &&
164
179
  typeof staticTable[node.value] === 'string') {
@@ -186,42 +201,42 @@ function getPropertyKey(node, staticTable) {
186
201
  return result;
187
202
  }
188
203
  if (exports.t.isTemplateLiteral(expr)) {
189
- return evaluateTemplateLiteral(expr, staticTable);
204
+ return evaluateTemplateLiteral(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
190
205
  }
191
206
  if (exports.t.isBinaryExpression(expr)) {
192
- return evaluateBinaryExpression(expr, staticTable);
207
+ return evaluateBinaryExpression(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
193
208
  }
194
209
  return '';
195
210
  }
196
211
  if (exports.t.isTemplateLiteral(node)) {
197
- return evaluateTemplateLiteral(node, staticTable);
212
+ return evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
198
213
  }
199
214
  if (exports.t.isBinaryExpression(node)) {
200
- return evaluateBinaryExpression(node, staticTable);
215
+ return evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
201
216
  }
202
217
  return '';
203
218
  }
204
- function evaluateTemplateLiteral(node, staticTable) {
219
+ function evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
205
220
  let result = '';
206
221
  for (let i = 0; i < node.quasis.length; i++) {
207
222
  result += node.quasis[i].cooked || node.quasis[i].raw;
208
223
  if (i < node.expressions.length) {
209
224
  const expr = node.expressions[i];
210
- const evaluatedExpr = evaluateExpression(expr, staticTable);
225
+ const evaluatedExpr = evaluateExpression(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
211
226
  result += String(evaluatedExpr);
212
227
  }
213
228
  }
214
229
  return result;
215
230
  }
216
- function evaluateBinaryExpression(node, staticTable) {
217
- const left = evaluateExpression(node.left, staticTable);
218
- const right = evaluateExpression(node.right, staticTable);
231
+ function evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
232
+ const left = evaluateExpression(node.left, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
233
+ const right = evaluateExpression(node.right, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
219
234
  if (node.operator === '+') {
220
235
  return String(left) + String(right);
221
236
  }
222
237
  throw new Error(`Unsupported binary operator: ${node.operator}`);
223
238
  }
224
- function evaluateExpression(node, staticTable) {
239
+ function evaluateExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
225
240
  if (exports.t.isStringLiteral(node)) {
226
241
  return node.value;
227
242
  }
@@ -238,11 +253,11 @@ function evaluateExpression(node, staticTable) {
238
253
  if (staticTable[node.value] !== undefined) {
239
254
  return staticTable[node.value];
240
255
  }
241
- if (exports.tables.keyframesHashTable[node.value] !== undefined) {
242
- return exports.tables.keyframesHashTable[node.value];
256
+ if (keyframesHashTable[node.value] !== undefined) {
257
+ return keyframesHashTable[node.value];
243
258
  }
244
- if (exports.tables.viewTransitionHashTable[node.value] !== undefined) {
245
- return exports.tables.viewTransitionHashTable[node.value];
259
+ if (viewTransitionHashTable[node.value] !== undefined) {
260
+ return viewTransitionHashTable[node.value];
246
261
  }
247
262
  return `[unresolved: ${node.value}]`;
248
263
  }
@@ -251,17 +266,17 @@ function evaluateExpression(node, staticTable) {
251
266
  if (resolved !== undefined) {
252
267
  return resolved;
253
268
  }
254
- const resolvedTheme = resolveThemeTableMemberExpressionByNode(node, exports.tables.themeTable);
269
+ const resolvedTheme = resolveThemeTableMemberExpressionByNode(node, themeTable);
255
270
  if (resolvedTheme !== undefined) {
256
271
  return resolvedTheme;
257
272
  }
258
273
  return `[unresolved member expression]`;
259
274
  }
260
275
  if (exports.t.isBinaryExpression(node)) {
261
- return evaluateBinaryExpression(node, staticTable);
276
+ return evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
262
277
  }
263
278
  if (exports.t.isTemplateLiteral(node)) {
264
- return evaluateTemplateLiteral(node, staticTable);
279
+ return evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
265
280
  }
266
281
  if (exports.t.isUnaryExpression(node)) {
267
282
  return evaluateUnaryExpression(node);
@@ -333,224 +348,124 @@ function resolveThemeTableMemberExpressionByNode(node, themeTable) {
333
348
  }
334
349
  return undefined;
335
350
  }
336
- function scanForKeyframes(addDependency) {
337
- const keyframesHashTableLocal = {};
338
- const keyframesObjectTableLocal = {};
339
- const files = fs_1.default.globSync(PATTERN_PATH, GLOB_OPTIONS);
340
- for (const filePath of files) {
341
- if (!isCSSDefineFile(filePath, 'keyframes'))
342
- continue;
343
- addDependency(filePath);
344
- const source = fs_1.default.readFileSync(filePath, 'utf8');
345
- const ast = (0, core_1.parseSync)(source, {
346
- syntax: 'typescript',
347
- tsx: true,
348
- target: 'es2022',
349
- });
350
- for (const node of ast.body) {
351
- let declarations = [];
352
- if (exports.t.isVariableDeclaration(node)) {
353
- declarations = node.declarations;
354
- }
355
- else if (exports.t.isExportDeclaration(node) &&
356
- exports.t.isVariableDeclaration(node.declaration)) {
357
- declarations = node.declaration.declarations;
358
- }
359
- for (const decl of declarations) {
360
- if (exports.t.isVariableDeclarator(decl) &&
361
- exports.t.isIdentifier(decl.id) &&
362
- decl.init &&
363
- exports.t.isCallExpression(decl.init) &&
364
- exports.t.isMemberExpression(decl.init.callee) &&
365
- exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
366
- exports.t.isIdentifier(decl.init.callee.property, { name: 'keyframes' }) &&
367
- decl.init.arguments.length > 0 &&
368
- exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
369
- const varName = decl.id.value;
370
- const obj = objectExpressionToObject(decl.init.arguments[0].expression, exports.tables.staticTable, keyframesHashTableLocal, exports.tables.viewTransitionHashTable, exports.tables.themeTable);
371
- const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
372
- keyframesHashTableLocal[varName] = hash;
373
- keyframesObjectTableLocal[hash] = obj;
374
- }
375
- }
376
- }
351
+ const fileCache = {};
352
+ function scanAll(addDependency) {
353
+ for (const key in exports.tables) {
354
+ exports.tables[key] = {};
377
355
  }
378
- return {
379
- keyframesHashTableLocal,
380
- keyframesObjectTableLocal,
381
- };
382
- }
383
- function scanForViewTransition(addDependency) {
384
- const viewTransitionHashTableLocal = {};
385
- const viewTransitionObjectTableLocal = {};
386
356
  const files = fs_1.default.globSync(PATTERN_PATH, GLOB_OPTIONS);
387
357
  for (const filePath of files) {
388
- if (!isCSSDefineFile(filePath, 'viewTransition'))
389
- continue;
390
- addDependency(filePath);
391
- const source = fs_1.default.readFileSync(filePath, 'utf8');
392
- const ast = (0, core_1.parseSync)(source, {
393
- syntax: 'typescript',
394
- tsx: true,
395
- target: 'es2022',
396
- });
397
- for (const node of ast.body) {
398
- let declarations = [];
399
- if (exports.t.isVariableDeclaration(node)) {
400
- declarations = node.declarations;
401
- }
402
- else if (exports.t.isExportDeclaration(node) &&
403
- exports.t.isVariableDeclaration(node.declaration)) {
404
- declarations = node.declaration.declarations;
405
- }
406
- for (const decl of declarations) {
407
- if (exports.t.isVariableDeclarator(decl) &&
408
- exports.t.isIdentifier(decl.id) &&
409
- decl.init &&
410
- exports.t.isCallExpression(decl.init) &&
411
- exports.t.isMemberExpression(decl.init.callee) &&
412
- exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
413
- exports.t.isIdentifier(decl.init.callee.property, {
414
- name: 'viewTransition',
415
- }) &&
416
- decl.init.arguments.length > 0 &&
417
- exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
418
- const varName = decl.id.value;
419
- const obj = objectExpressionToObject(decl.init.arguments[0].expression, exports.tables.staticTable, exports.tables.keyframesHashTable, viewTransitionHashTableLocal, exports.tables.themeTable);
420
- const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
421
- viewTransitionHashTableLocal[varName] = hash;
422
- viewTransitionObjectTableLocal[hash] = obj;
358
+ try {
359
+ const stats = fs_1.default.statSync(filePath);
360
+ const cached = fileCache[filePath];
361
+ if (cached && cached.mtimeMs === stats.mtimeMs) {
362
+ if (cached.hasCssUsage) {
363
+ addDependency(filePath);
364
+ Object.assign(exports.tables.staticTable, cached.staticTable);
365
+ Object.assign(exports.tables.keyframesHashTable, cached.keyframesHashTable);
366
+ Object.assign(exports.tables.viewTransitionHashTable, cached.viewTransitionHashTable);
367
+ Object.assign(exports.tables.themeTable, cached.themeTable);
368
+ Object.assign(exports.tables.createThemeObjectTable, cached.createThemeObjectTable);
423
369
  }
370
+ continue;
424
371
  }
425
- }
426
- }
427
- return {
428
- viewTransitionHashTableLocal,
429
- viewTransitionObjectTableLocal,
430
- };
431
- }
432
- function scanForCreateStatic(addDependency) {
433
- const staticTableLocal = {};
434
- const files = fs_1.default.globSync(PATTERN_PATH, GLOB_OPTIONS);
435
- for (const filePath of files) {
436
- if (!isCSSDefineFile(filePath, 'createStatic'))
437
- continue;
438
- addDependency(filePath);
439
- const source = fs_1.default.readFileSync(filePath, 'utf8');
440
- const ast = (0, core_1.parseSync)(source, {
441
- syntax: 'typescript',
442
- tsx: true,
443
- target: 'es2022',
444
- });
445
- for (const node of ast.body) {
446
- let declarations = [];
447
- if (exports.t.isVariableDeclaration(node)) {
448
- declarations = node.declarations;
449
- }
450
- else if (exports.t.isExportDeclaration(node) &&
451
- exports.t.isVariableDeclaration(node.declaration)) {
452
- declarations = node.declaration.declarations;
372
+ const source = fs_1.default.readFileSync(filePath, 'utf8');
373
+ if (!source.includes('css.')) {
374
+ fileCache[filePath] = {
375
+ mtimeMs: stats.mtimeMs,
376
+ staticTable: {},
377
+ keyframesHashTable: {},
378
+ viewTransitionHashTable: {},
379
+ themeTable: {},
380
+ createThemeObjectTable: {},
381
+ hasCssUsage: false,
382
+ };
383
+ continue;
453
384
  }
454
- for (const decl of declarations) {
455
- if (exports.t.isVariableDeclarator(decl) &&
456
- exports.t.isIdentifier(decl.id) &&
457
- decl.init &&
458
- exports.t.isCallExpression(decl.init) &&
459
- exports.t.isMemberExpression(decl.init.callee) &&
460
- exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
461
- exports.t.isIdentifier(decl.init.callee.property, { name: 'createStatic' }) &&
462
- decl.init.arguments.length > 0 &&
463
- exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
464
- const varName = decl.id.value;
465
- const obj = objectExpressionToObject(decl.init.arguments[0].expression, staticTableLocal, exports.tables.keyframesHashTable, exports.tables.viewTransitionHashTable, exports.tables.themeTable);
466
- staticTableLocal[varName] = obj;
385
+ const ast = (0, core_1.parseSync)(source, {
386
+ syntax: 'typescript',
387
+ tsx: true,
388
+ target: 'es2022',
389
+ });
390
+ addDependency(filePath);
391
+ const fileId = genFileId(filePath);
392
+ const localStaticTable = {};
393
+ const localKeyframesHashTable = {};
394
+ const localViewTransitionHashTable = {};
395
+ const localThemeTable = {};
396
+ const localCreateThemeObjectTable = {};
397
+ for (const node of ast.body) {
398
+ let declarations = [];
399
+ if (exports.t.isVariableDeclaration(node)) {
400
+ declarations = node.declarations;
467
401
  }
468
- }
469
- }
470
- }
471
- return staticTableLocal;
472
- }
473
- function scanForCreateTheme(addDependency) {
474
- const themeTableLocal = {};
475
- const createThemeObjectTableLocal = {};
476
- const files = fs_1.default.globSync(PATTERN_PATH, GLOB_OPTIONS);
477
- for (const filePath of files) {
478
- if (!isCSSDefineFile(filePath, 'createTheme'))
479
- continue;
480
- addDependency(filePath);
481
- const source = fs_1.default.readFileSync(filePath, 'utf8');
482
- const ast = (0, core_1.parseSync)(source, {
483
- syntax: 'typescript',
484
- tsx: true,
485
- target: 'es2022',
486
- });
487
- for (const node of ast.body) {
488
- let declarations = [];
489
- if (exports.t.isVariableDeclaration(node)) {
490
- declarations = node.declarations;
491
- }
492
- else if (exports.t.isExportDeclaration(node) &&
493
- exports.t.isVariableDeclaration(node.declaration)) {
494
- declarations = node.declaration.declarations;
495
- }
496
- for (const decl of declarations) {
497
- if (exports.t.isVariableDeclarator(decl) &&
498
- exports.t.isIdentifier(decl.id) &&
499
- decl.init &&
500
- exports.t.isCallExpression(decl.init) &&
501
- exports.t.isMemberExpression(decl.init.callee) &&
502
- exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
503
- exports.t.isIdentifier(decl.init.callee.property, { name: 'createTheme' }) &&
504
- decl.init.arguments.length > 0 &&
505
- exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
506
- const varName = decl.id.value;
507
- const obj = objectExpressionToObject(decl.init.arguments[0].expression, exports.tables.staticTable, exports.tables.keyframesHashTable, exports.tables.viewTransitionHashTable, themeTableLocal);
508
- const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
509
- themeTableLocal[varName] = obj;
510
- createThemeObjectTableLocal[hash] = obj;
402
+ else if (exports.t.isExportDeclaration(node) &&
403
+ exports.t.isVariableDeclaration(node.declaration)) {
404
+ declarations = node.declaration.declarations;
405
+ }
406
+ for (const decl of declarations) {
407
+ if (exports.t.isVariableDeclarator(decl) &&
408
+ exports.t.isIdentifier(decl.id) &&
409
+ decl.init &&
410
+ exports.t.isCallExpression(decl.init) &&
411
+ exports.t.isMemberExpression(decl.init.callee) &&
412
+ exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
413
+ exports.t.isIdentifier(decl.init.callee.property) &&
414
+ decl.init.arguments.length > 0 &&
415
+ exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
416
+ const method = decl.init.callee.property.value;
417
+ const name = decl.id.value;
418
+ const init = decl.init;
419
+ const obj = objectExpressionToObject(init.arguments[0].expression, localStaticTable, localKeyframesHashTable, localViewTransitionHashTable, localThemeTable);
420
+ const uniqueKey = `${fileId}-${name}`;
421
+ if (method === 'createStatic') {
422
+ localStaticTable[name] = obj;
423
+ exports.tables.staticTable[uniqueKey] = obj;
424
+ }
425
+ else if (method === 'keyframes') {
426
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
427
+ localKeyframesHashTable[name] = hash;
428
+ exports.tables.keyframesHashTable[uniqueKey] = hash;
429
+ exports.tables.keyframesObjectTable[hash] = obj;
430
+ }
431
+ else if (method === 'viewTransition') {
432
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
433
+ localViewTransitionHashTable[name] = hash;
434
+ exports.tables.viewTransitionHashTable[uniqueKey] = hash;
435
+ exports.tables.viewTransitionObjectTable[hash] = obj;
436
+ }
437
+ else if (method === 'createTheme') {
438
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
439
+ localThemeTable[name] = obj;
440
+ exports.tables.themeTable[uniqueKey] = obj;
441
+ exports.tables.createThemeObjectTable[hash] = obj;
442
+ localCreateThemeObjectTable[hash] = obj;
443
+ }
444
+ }
511
445
  }
512
446
  }
447
+ fileCache[filePath] = {
448
+ mtimeMs: stats.mtimeMs,
449
+ staticTable: localStaticTable,
450
+ keyframesHashTable: localKeyframesHashTable,
451
+ viewTransitionHashTable: localViewTransitionHashTable,
452
+ themeTable: localThemeTable,
453
+ createThemeObjectTable: localCreateThemeObjectTable,
454
+ hasCssUsage: true,
455
+ };
456
+ }
457
+ catch (e) {
513
458
  }
514
459
  }
515
- return { themeTableLocal, createThemeObjectTableLocal };
516
- }
517
- function isCSSDefineFile(filePath, target) {
518
- if (fs_1.default.statSync(filePath).isDirectory())
519
- return false;
520
- const code = fs_1.default.readFileSync(filePath, 'utf8');
521
- let ast;
522
- try {
523
- ast = (0, core_1.parseSync)(code, {
524
- syntax: 'typescript',
525
- tsx: true,
526
- target: 'es2022',
527
- });
528
- }
529
- catch (err) {
530
- console.log(err);
531
- return false;
532
- }
533
- let found = false;
534
- traverse(ast, {
535
- CallExpression({ node, stop }) {
536
- const callee = node.callee;
537
- if (callee.type === 'MemberExpression' &&
538
- callee.object.type === 'Identifier' &&
539
- callee.object.value === 'css' &&
540
- callee.property.type === 'Identifier' &&
541
- callee.property.value === target) {
542
- found = true;
543
- stop();
544
- }
545
- },
546
- });
547
- return found;
460
+ return exports.tables;
548
461
  }
549
462
  function extractOndemandStyles(obj, extractedSheets) {
550
463
  if (!obj || typeof obj !== 'object')
551
464
  return;
552
465
  const visited = new Set();
553
- const processedThemes = new Set();
466
+ const keyframesHashes = new Set();
467
+ const viewTransitionHashes = new Set();
468
+ let needsTheme = false;
554
469
  function walk(n) {
555
470
  if (!n || typeof n !== 'object' || visited.has(n))
556
471
  return;
@@ -558,41 +473,13 @@ function extractOndemandStyles(obj, extractedSheets) {
558
473
  Object.values(n).forEach((val) => {
559
474
  if (typeof val === 'string') {
560
475
  if (val.startsWith('kf-')) {
561
- const hash = val.slice(3);
562
- const definition = exports.tables.keyframesObjectTable[hash];
563
- if (definition) {
564
- const { styleSheet } = (0, zss_engine_1.transpile)({ [`@keyframes kf-${hash}`]: definition }, undefined, '--global');
565
- if (!extractedSheets.includes(styleSheet)) {
566
- extractedSheets.push(styleSheet);
567
- }
568
- }
476
+ keyframesHashes.add(val.slice(3));
569
477
  }
570
478
  else if (val.startsWith('vt-')) {
571
- const hash = val.slice(3);
572
- const obj = exports.tables.viewTransitionObjectTable[hash];
573
- if (obj) {
574
- const { styleSheet } = (0, zss_engine_1.transpile)((0, viewTransition_1.createViewTransition)(obj, hash), undefined, '--global');
575
- if (!extractedSheets.includes(styleSheet)) {
576
- extractedSheets.push(styleSheet);
577
- }
578
- }
479
+ viewTransitionHashes.add(val.slice(3));
579
480
  }
580
- else if (val.includes('var(--')) {
581
- Object.keys(exports.tables.themeTable).forEach((themeVarName) => {
582
- if (!processedThemes.has(themeVarName)) {
583
- processedThemes.add(themeVarName);
584
- const themeObj = exports.tables.themeTable[themeVarName];
585
- const hash = (0, zss_engine_1.genBase36Hash)(themeObj, 1, 8);
586
- const definition = exports.tables.createThemeObjectTable[hash];
587
- if (definition && typeof definition === 'object') {
588
- const styles = (0, createTheme_1.createTheme)(definition);
589
- const { styleSheet } = (0, zss_engine_1.transpile)(styles, undefined, '--global');
590
- if (!extractedSheets.includes(styleSheet)) {
591
- extractedSheets.push(styleSheet);
592
- }
593
- }
594
- }
595
- });
481
+ else if (!needsTheme && val.includes('var(--')) {
482
+ needsTheme = true;
596
483
  }
597
484
  }
598
485
  else {
@@ -601,6 +488,43 @@ function extractOndemandStyles(obj, extractedSheets) {
601
488
  });
602
489
  }
603
490
  walk(obj);
491
+ const existingSheets = new Set(extractedSheets);
492
+ const addSheet = (sheet) => {
493
+ if (!existingSheets.has(sheet)) {
494
+ existingSheets.add(sheet);
495
+ extractedSheets.push(sheet);
496
+ }
497
+ };
498
+ if (keyframesHashes.size > 0) {
499
+ for (const hash of keyframesHashes) {
500
+ const definition = exports.tables.keyframesObjectTable[hash];
501
+ if (definition) {
502
+ const { styleSheet } = (0, zss_engine_1.transpile)({ [`@keyframes kf-${hash}`]: definition }, undefined, '--global');
503
+ addSheet(styleSheet);
504
+ }
505
+ }
506
+ }
507
+ if (viewTransitionHashes.size > 0) {
508
+ for (const hash of viewTransitionHashes) {
509
+ const obj = exports.tables.viewTransitionObjectTable[hash];
510
+ if (obj) {
511
+ const { styleSheet } = (0, zss_engine_1.transpile)((0, viewTransition_1.createViewTransition)(obj, hash), undefined, '--global');
512
+ addSheet(styleSheet);
513
+ }
514
+ }
515
+ }
516
+ if (needsTheme) {
517
+ for (const themeVarName in exports.tables.themeTable) {
518
+ const themeObj = exports.tables.themeTable[themeVarName];
519
+ const hash = (0, zss_engine_1.genBase36Hash)(themeObj, 1, 8);
520
+ const definition = exports.tables.createThemeObjectTable[hash];
521
+ if (definition && typeof definition === 'object') {
522
+ const styles = (0, createTheme_1.createTheme)(definition);
523
+ const { styleSheet } = (0, zss_engine_1.transpile)(styles, undefined, '--global');
524
+ addSheet(styleSheet);
525
+ }
526
+ }
527
+ }
604
528
  }
605
529
  function deepMerge(target, source) {
606
530
  const result = { ...target };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/utils",
3
- "version": "4.1.2",
3
+ "version": "4.2.0",
4
4
  "description": "Plumeria Utils",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",