@plumeria/utils 4.1.3 → 4.2.1

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,5 @@
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';
5
+ export { resolveImportPath } from './resolver';
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.resolveImportPath = 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,8 @@ 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; } });
15
+ var resolver_1 = require("./resolver");
16
+ Object.defineProperty(exports, "resolveImportPath", { enumerable: true, get: function () { return resolver_1.resolveImportPath; } });
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;
@@ -27,20 +27,8 @@ export declare function traverse(node: Module, visitor: {
27
27
  }) => void;
28
28
  }): void;
29
29
  export declare const tables: Tables;
30
- export declare function objectExpressionToObject(node: ObjectExpression, staticTable: StaticTable, keyframesHashTable: KeyframesHashTable, viewTransitionHashTable: ViewTransitionHashTable, themeTable: ThemeTable): CSSObject;
30
+ export declare function objectExpressionToObject(node: ObjectExpression, staticTable: StaticTable, keyframesHashTable: KeyframesHashTable, viewTransitionHashTable: ViewTransitionHashTable, themeTable: ThemeTable, resolveVariable?: (name: string) => any): CSSObject;
31
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
- };
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,12 @@ exports.tables = {
87
84
  viewTransitionObjectTable: {},
88
85
  createThemeObjectTable: {},
89
86
  };
90
- function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
87
+ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable, resolveVariable) {
91
88
  const obj = {};
92
89
  node.properties.forEach((prop) => {
93
90
  if (!exports.t.isObjectProperty(prop))
94
91
  return;
95
- const key = getPropertyKey(prop.key, staticTable);
92
+ const key = getPropertyKey(prop.key, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
96
93
  if (!key)
97
94
  return;
98
95
  const val = prop.value;
@@ -122,13 +119,20 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
122
119
  obj[key] = evaluateUnaryExpression(val);
123
120
  }
124
121
  else if (exports.t.isObjectExpression(val)) {
125
- obj[key] = objectExpressionToObject(val, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
122
+ obj[key] = objectExpressionToObject(val, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable, resolveVariable);
126
123
  }
127
124
  else if (exports.t.isMemberExpression(val)) {
128
125
  const resolved = resolveStaticTableMemberExpression(val, staticTable);
129
126
  obj[key] = resolved !== undefined ? resolved : '[unresolved]';
130
127
  }
131
128
  else if (exports.t.isIdentifier(val)) {
129
+ if (resolveVariable) {
130
+ const resolved = resolveVariable(val.value);
131
+ if (resolved !== undefined) {
132
+ obj[key] = resolved;
133
+ return;
134
+ }
135
+ }
132
136
  if (staticTable[val.value] !== undefined) {
133
137
  obj[key] = staticTable[val.value];
134
138
  }
@@ -144,21 +148,43 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
144
148
  }
145
149
  function collectLocalConsts(ast) {
146
150
  const localConsts = {};
151
+ const decls = new Map();
147
152
  traverse(ast, {
148
153
  VariableDeclarator({ node }) {
149
154
  if (exports.t.isIdentifier(node.id) && node.init) {
150
- if (exports.t.isStringLiteral(node.init)) {
151
- localConsts[node.id.value] = node.init.value;
152
- }
153
- 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);
155
- }
155
+ decls.set(node.id.value, node.init);
156
156
  }
157
157
  },
158
158
  });
159
+ const visiting = new Set();
160
+ function resolveValue(name) {
161
+ if (localConsts[name] !== undefined)
162
+ return localConsts[name];
163
+ if (!decls.has(name) || visiting.has(name))
164
+ return undefined;
165
+ visiting.add(name);
166
+ const init = decls.get(name);
167
+ let result;
168
+ if (exports.t.isStringLiteral(init) ||
169
+ exports.t.isNumericLiteral(init) ||
170
+ exports.t.isBooleanLiteral(init)) {
171
+ result = init.value;
172
+ }
173
+ else if (exports.t.isObjectExpression(init)) {
174
+ result = objectExpressionToObject(init, localConsts, exports.tables.keyframesHashTable, exports.tables.viewTransitionHashTable, exports.tables.themeTable, resolveValue);
175
+ }
176
+ visiting.delete(name);
177
+ if (result !== undefined) {
178
+ localConsts[name] = result;
179
+ }
180
+ return result;
181
+ }
182
+ for (const name of decls.keys()) {
183
+ resolveValue(name);
184
+ }
159
185
  return localConsts;
160
186
  }
161
- function getPropertyKey(node, staticTable) {
187
+ function getPropertyKey(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
162
188
  if (exports.t.isIdentifier(node)) {
163
189
  if (staticTable[node.value] &&
164
190
  typeof staticTable[node.value] === 'string') {
@@ -186,42 +212,42 @@ function getPropertyKey(node, staticTable) {
186
212
  return result;
187
213
  }
188
214
  if (exports.t.isTemplateLiteral(expr)) {
189
- return evaluateTemplateLiteral(expr, staticTable);
215
+ return evaluateTemplateLiteral(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
190
216
  }
191
217
  if (exports.t.isBinaryExpression(expr)) {
192
- return evaluateBinaryExpression(expr, staticTable);
218
+ return evaluateBinaryExpression(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
193
219
  }
194
220
  return '';
195
221
  }
196
222
  if (exports.t.isTemplateLiteral(node)) {
197
- return evaluateTemplateLiteral(node, staticTable);
223
+ return evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
198
224
  }
199
225
  if (exports.t.isBinaryExpression(node)) {
200
- return evaluateBinaryExpression(node, staticTable);
226
+ return evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
201
227
  }
202
228
  return '';
203
229
  }
204
- function evaluateTemplateLiteral(node, staticTable) {
230
+ function evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
205
231
  let result = '';
206
232
  for (let i = 0; i < node.quasis.length; i++) {
207
233
  result += node.quasis[i].cooked || node.quasis[i].raw;
208
234
  if (i < node.expressions.length) {
209
235
  const expr = node.expressions[i];
210
- const evaluatedExpr = evaluateExpression(expr, staticTable);
236
+ const evaluatedExpr = evaluateExpression(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
211
237
  result += String(evaluatedExpr);
212
238
  }
213
239
  }
214
240
  return result;
215
241
  }
216
- function evaluateBinaryExpression(node, staticTable) {
217
- const left = evaluateExpression(node.left, staticTable);
218
- const right = evaluateExpression(node.right, staticTable);
242
+ function evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
243
+ const left = evaluateExpression(node.left, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
244
+ const right = evaluateExpression(node.right, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
219
245
  if (node.operator === '+') {
220
246
  return String(left) + String(right);
221
247
  }
222
248
  throw new Error(`Unsupported binary operator: ${node.operator}`);
223
249
  }
224
- function evaluateExpression(node, staticTable) {
250
+ function evaluateExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
225
251
  if (exports.t.isStringLiteral(node)) {
226
252
  return node.value;
227
253
  }
@@ -238,11 +264,11 @@ function evaluateExpression(node, staticTable) {
238
264
  if (staticTable[node.value] !== undefined) {
239
265
  return staticTable[node.value];
240
266
  }
241
- if (exports.tables.keyframesHashTable[node.value] !== undefined) {
242
- return exports.tables.keyframesHashTable[node.value];
267
+ if (keyframesHashTable[node.value] !== undefined) {
268
+ return keyframesHashTable[node.value];
243
269
  }
244
- if (exports.tables.viewTransitionHashTable[node.value] !== undefined) {
245
- return exports.tables.viewTransitionHashTable[node.value];
270
+ if (viewTransitionHashTable[node.value] !== undefined) {
271
+ return viewTransitionHashTable[node.value];
246
272
  }
247
273
  return `[unresolved: ${node.value}]`;
248
274
  }
@@ -251,17 +277,17 @@ function evaluateExpression(node, staticTable) {
251
277
  if (resolved !== undefined) {
252
278
  return resolved;
253
279
  }
254
- const resolvedTheme = resolveThemeTableMemberExpressionByNode(node, exports.tables.themeTable);
280
+ const resolvedTheme = resolveThemeTableMemberExpressionByNode(node, themeTable);
255
281
  if (resolvedTheme !== undefined) {
256
282
  return resolvedTheme;
257
283
  }
258
284
  return `[unresolved member expression]`;
259
285
  }
260
286
  if (exports.t.isBinaryExpression(node)) {
261
- return evaluateBinaryExpression(node, staticTable);
287
+ return evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
262
288
  }
263
289
  if (exports.t.isTemplateLiteral(node)) {
264
- return evaluateTemplateLiteral(node, staticTable);
290
+ return evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
265
291
  }
266
292
  if (exports.t.isUnaryExpression(node)) {
267
293
  return evaluateUnaryExpression(node);
@@ -333,224 +359,126 @@ function resolveThemeTableMemberExpressionByNode(node, themeTable) {
333
359
  }
334
360
  return undefined;
335
361
  }
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
- }
362
+ const fileCache = {};
363
+ function scanAll(addDependency) {
364
+ for (const key in exports.tables) {
365
+ const table = exports.tables[key];
366
+ for (const prop in table) {
367
+ delete table[prop];
376
368
  }
377
369
  }
378
- return {
379
- keyframesHashTableLocal,
380
- keyframesObjectTableLocal,
381
- };
382
- }
383
- function scanForViewTransition(addDependency) {
384
- const viewTransitionHashTableLocal = {};
385
- const viewTransitionObjectTableLocal = {};
386
370
  const files = fs_1.default.globSync(PATTERN_PATH, GLOB_OPTIONS);
387
371
  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;
372
+ try {
373
+ const stats = fs_1.default.statSync(filePath);
374
+ const cached = fileCache[filePath];
375
+ if (cached && cached.mtimeMs === stats.mtimeMs) {
376
+ if (cached.hasCssUsage) {
377
+ addDependency(filePath);
378
+ Object.assign(exports.tables.staticTable, cached.staticTable);
379
+ Object.assign(exports.tables.keyframesHashTable, cached.keyframesHashTable);
380
+ Object.assign(exports.tables.viewTransitionHashTable, cached.viewTransitionHashTable);
381
+ Object.assign(exports.tables.themeTable, cached.themeTable);
382
+ Object.assign(exports.tables.createThemeObjectTable, cached.createThemeObjectTable);
423
383
  }
384
+ continue;
424
385
  }
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;
386
+ const source = fs_1.default.readFileSync(filePath, 'utf8');
387
+ if (!source.includes('css.')) {
388
+ fileCache[filePath] = {
389
+ mtimeMs: stats.mtimeMs,
390
+ staticTable: {},
391
+ keyframesHashTable: {},
392
+ viewTransitionHashTable: {},
393
+ themeTable: {},
394
+ createThemeObjectTable: {},
395
+ hasCssUsage: false,
396
+ };
397
+ continue;
453
398
  }
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;
399
+ const ast = (0, core_1.parseSync)(source, {
400
+ syntax: 'typescript',
401
+ tsx: true,
402
+ target: 'es2022',
403
+ });
404
+ addDependency(filePath);
405
+ const localStaticTable = {};
406
+ const localKeyframesHashTable = {};
407
+ const localViewTransitionHashTable = {};
408
+ const localThemeTable = {};
409
+ const localCreateThemeObjectTable = {};
410
+ for (const node of ast.body) {
411
+ let declarations = [];
412
+ if (exports.t.isVariableDeclaration(node)) {
413
+ declarations = node.declarations;
467
414
  }
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;
415
+ else if (exports.t.isExportDeclaration(node) &&
416
+ exports.t.isVariableDeclaration(node.declaration)) {
417
+ declarations = node.declaration.declarations;
418
+ }
419
+ for (const decl of declarations) {
420
+ if (exports.t.isVariableDeclarator(decl) &&
421
+ exports.t.isIdentifier(decl.id) &&
422
+ decl.init &&
423
+ exports.t.isCallExpression(decl.init) &&
424
+ exports.t.isMemberExpression(decl.init.callee) &&
425
+ exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
426
+ exports.t.isIdentifier(decl.init.callee.property) &&
427
+ decl.init.arguments.length > 0 &&
428
+ exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
429
+ const method = decl.init.callee.property.value;
430
+ const name = decl.id.value;
431
+ const init = decl.init;
432
+ const obj = objectExpressionToObject(init.arguments[0].expression, localStaticTable, localKeyframesHashTable, localViewTransitionHashTable, localThemeTable);
433
+ const uniqueKey = `${filePath}-${name}`;
434
+ if (method === 'createStatic') {
435
+ localStaticTable[name] = obj;
436
+ exports.tables.staticTable[uniqueKey] = obj;
437
+ }
438
+ else if (method === 'keyframes') {
439
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
440
+ localKeyframesHashTable[name] = hash;
441
+ exports.tables.keyframesHashTable[uniqueKey] = hash;
442
+ exports.tables.keyframesObjectTable[hash] = obj;
443
+ }
444
+ else if (method === 'viewTransition') {
445
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
446
+ localViewTransitionHashTable[name] = hash;
447
+ exports.tables.viewTransitionHashTable[uniqueKey] = hash;
448
+ exports.tables.viewTransitionObjectTable[hash] = obj;
449
+ }
450
+ else if (method === 'createTheme') {
451
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
452
+ localThemeTable[name] = obj;
453
+ exports.tables.themeTable[uniqueKey] = obj;
454
+ exports.tables.createThemeObjectTable[hash] = obj;
455
+ localCreateThemeObjectTable[hash] = obj;
456
+ }
457
+ }
511
458
  }
512
459
  }
460
+ fileCache[filePath] = {
461
+ mtimeMs: stats.mtimeMs,
462
+ staticTable: localStaticTable,
463
+ keyframesHashTable: localKeyframesHashTable,
464
+ viewTransitionHashTable: localViewTransitionHashTable,
465
+ themeTable: localThemeTable,
466
+ createThemeObjectTable: localCreateThemeObjectTable,
467
+ hasCssUsage: true,
468
+ };
469
+ }
470
+ catch (e) {
513
471
  }
514
472
  }
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;
473
+ return exports.tables;
548
474
  }
549
475
  function extractOndemandStyles(obj, extractedSheets) {
550
476
  if (!obj || typeof obj !== 'object')
551
477
  return;
552
478
  const visited = new Set();
553
- const processedThemes = new Set();
479
+ const keyframesHashes = new Set();
480
+ const viewTransitionHashes = new Set();
481
+ let needsTheme = false;
554
482
  function walk(n) {
555
483
  if (!n || typeof n !== 'object' || visited.has(n))
556
484
  return;
@@ -558,41 +486,13 @@ function extractOndemandStyles(obj, extractedSheets) {
558
486
  Object.values(n).forEach((val) => {
559
487
  if (typeof val === 'string') {
560
488
  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
- }
489
+ keyframesHashes.add(val.slice(3));
569
490
  }
570
491
  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
- }
492
+ viewTransitionHashes.add(val.slice(3));
579
493
  }
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
- });
494
+ else if (!needsTheme && val.includes('var(--')) {
495
+ needsTheme = true;
596
496
  }
597
497
  }
598
498
  else {
@@ -601,6 +501,43 @@ function extractOndemandStyles(obj, extractedSheets) {
601
501
  });
602
502
  }
603
503
  walk(obj);
504
+ const existingSheets = new Set(extractedSheets);
505
+ const addSheet = (sheet) => {
506
+ if (!existingSheets.has(sheet)) {
507
+ existingSheets.add(sheet);
508
+ extractedSheets.push(sheet);
509
+ }
510
+ };
511
+ if (keyframesHashes.size > 0) {
512
+ for (const hash of keyframesHashes) {
513
+ const definition = exports.tables.keyframesObjectTable[hash];
514
+ if (definition) {
515
+ const { styleSheet } = (0, zss_engine_1.transpile)({ [`@keyframes kf-${hash}`]: definition }, undefined, '--global');
516
+ addSheet(styleSheet);
517
+ }
518
+ }
519
+ }
520
+ if (viewTransitionHashes.size > 0) {
521
+ for (const hash of viewTransitionHashes) {
522
+ const obj = exports.tables.viewTransitionObjectTable[hash];
523
+ if (obj) {
524
+ const { styleSheet } = (0, zss_engine_1.transpile)((0, viewTransition_1.createViewTransition)(obj, hash), undefined, '--global');
525
+ addSheet(styleSheet);
526
+ }
527
+ }
528
+ }
529
+ if (needsTheme) {
530
+ for (const themeVarName in exports.tables.themeTable) {
531
+ const themeObj = exports.tables.themeTable[themeVarName];
532
+ const hash = (0, zss_engine_1.genBase36Hash)(themeObj, 1, 8);
533
+ const definition = exports.tables.createThemeObjectTable[hash];
534
+ if (definition && typeof definition === 'object') {
535
+ const styles = (0, createTheme_1.createTheme)(definition);
536
+ const { styleSheet } = (0, zss_engine_1.transpile)(styles, undefined, '--global');
537
+ addSheet(styleSheet);
538
+ }
539
+ }
540
+ }
604
541
  }
605
542
  function deepMerge(target, source) {
606
543
  const result = { ...target };
@@ -0,0 +1 @@
1
+ export declare function resolveImportPath(importPath: string, importerPath: string): string | null;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.resolveImportPath = resolveImportPath;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ let tsConfigCache;
10
+ function loadTsConfig() {
11
+ if (tsConfigCache !== undefined)
12
+ return tsConfigCache;
13
+ let currentDir = process.cwd();
14
+ while (currentDir !== path_1.default.parse(currentDir).root) {
15
+ const tsConfigPath = path_1.default.join(currentDir, 'tsconfig.json');
16
+ if (fs_1.default.existsSync(tsConfigPath)) {
17
+ try {
18
+ const content = fs_1.default.readFileSync(tsConfigPath, 'utf-8');
19
+ const config = JSON.parse(content.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, ''));
20
+ tsConfigCache = config.compilerOptions || null;
21
+ return tsConfigCache;
22
+ }
23
+ catch {
24
+ }
25
+ }
26
+ currentDir = path_1.default.dirname(currentDir);
27
+ }
28
+ tsConfigCache = null;
29
+ return null;
30
+ }
31
+ const extensions = [
32
+ '.ts',
33
+ '.tsx',
34
+ '.js',
35
+ '.jsx',
36
+ '/index.ts',
37
+ '/index.tsx',
38
+ '/index.js',
39
+ '/index.jsx',
40
+ ];
41
+ function resolveWithExtension(basePath) {
42
+ if (fs_1.default.existsSync(basePath) && fs_1.default.statSync(basePath).isFile())
43
+ return basePath;
44
+ for (const ext of extensions) {
45
+ const fullPath = basePath + ext;
46
+ if (fs_1.default.existsSync(fullPath) && fs_1.default.statSync(fullPath).isFile())
47
+ return fullPath;
48
+ }
49
+ return null;
50
+ }
51
+ function resolveImportPath(importPath, importerPath) {
52
+ if (importPath.startsWith('.')) {
53
+ return resolveWithExtension(path_1.default.resolve(path_1.default.dirname(importerPath), importPath));
54
+ }
55
+ const config = loadTsConfig();
56
+ if (config?.paths) {
57
+ const root = process.cwd();
58
+ for (const [alias, targets] of Object.entries(config.paths)) {
59
+ const prefix = alias.replace(/\*$/, '');
60
+ if (importPath.startsWith(prefix)) {
61
+ for (const target of targets) {
62
+ const resolvedTarget = target.replace(/\*$/, '');
63
+ const candidate = path_1.default.resolve(root, resolvedTarget + importPath.slice(prefix.length));
64
+ const result = resolveWithExtension(candidate);
65
+ if (result)
66
+ return result;
67
+ }
68
+ }
69
+ }
70
+ }
71
+ let currentDir = path_1.default.dirname(importerPath);
72
+ while (currentDir !== path_1.default.parse(currentDir).root) {
73
+ if (fs_1.default.existsSync(path_1.default.join(currentDir, 'package.json'))) {
74
+ return resolveWithExtension(path_1.default.resolve(currentDir, importPath));
75
+ }
76
+ currentDir = path_1.default.dirname(currentDir);
77
+ }
78
+ return null;
79
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/utils",
3
- "version": "4.1.3",
3
+ "version": "4.2.1",
4
4
  "description": "Plumeria Utils",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",