@plumeria/utils 4.2.0 → 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
@@ -2,3 +2,4 @@ export type { CSSObject, FileStyles } from './types';
2
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,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStyleRecords = exports.scanAll = exports.deepMerge = exports.extractOndemandStyles = exports.tables = exports.t = exports.traverse = exports.collectLocalConsts = exports.objectExpressionToObject = 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
5
  Object.defineProperty(exports, "objectExpressionToObject", { enumerable: true, get: function () { return parser_1.objectExpressionToObject; } });
6
6
  Object.defineProperty(exports, "collectLocalConsts", { enumerable: true, get: function () { return parser_1.collectLocalConsts; } });
@@ -12,3 +12,5 @@ Object.defineProperty(exports, "deepMerge", { enumerable: true, get: function ()
12
12
  Object.defineProperty(exports, "scanAll", { enumerable: true, get: function () { return parser_1.scanAll; } });
13
13
  var create_1 = require("./create");
14
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
@@ -27,8 +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;
31
- export declare function collectLocalConsts(ast: Module, filePath: string): Record<string, any>;
30
+ export declare function objectExpressionToObject(node: ObjectExpression, staticTable: StaticTable, keyframesHashTable: KeyframesHashTable, viewTransitionHashTable: ViewTransitionHashTable, themeTable: ThemeTable, resolveVariable?: (name: string) => any): CSSObject;
31
+ export declare function collectLocalConsts(ast: Module): Record<string, any>;
32
32
  export declare function scanAll(addDependency: (path: string) => void): Tables;
33
33
  export declare function extractOndemandStyles(obj: any, extractedSheets: string[]): void;
34
34
  export declare function deepMerge(target: Record<string, any>, source: Record<string, any>): Record<string, any>;
package/dist/parser.js CHANGED
@@ -84,14 +84,7 @@ exports.tables = {
84
84
  viewTransitionObjectTable: {},
85
85
  createThemeObjectTable: {},
86
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
- }
94
- function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
87
+ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable, resolveVariable) {
95
88
  const obj = {};
96
89
  node.properties.forEach((prop) => {
97
90
  if (!exports.t.isObjectProperty(prop))
@@ -126,13 +119,20 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
126
119
  obj[key] = evaluateUnaryExpression(val);
127
120
  }
128
121
  else if (exports.t.isObjectExpression(val)) {
129
- obj[key] = objectExpressionToObject(val, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
122
+ obj[key] = objectExpressionToObject(val, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable, resolveVariable);
130
123
  }
131
124
  else if (exports.t.isMemberExpression(val)) {
132
125
  const resolved = resolveStaticTableMemberExpression(val, staticTable);
133
126
  obj[key] = resolved !== undefined ? resolved : '[unresolved]';
134
127
  }
135
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
+ }
136
136
  if (staticTable[val.value] !== undefined) {
137
137
  obj[key] = staticTable[val.value];
138
138
  }
@@ -146,31 +146,42 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
146
146
  });
147
147
  return obj;
148
148
  }
149
- function collectLocalConsts(ast, filePath) {
149
+ function collectLocalConsts(ast) {
150
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
- };
151
+ const decls = new Map();
162
152
  traverse(ast, {
163
153
  VariableDeclarator({ node }) {
164
154
  if (exports.t.isIdentifier(node.id) && node.init) {
165
- if (exports.t.isStringLiteral(node.init)) {
166
- localConsts[node.id.value] = node.init.value;
167
- }
168
- else if (exports.t.isObjectExpression(node.init)) {
169
- localConsts[node.id.value] = objectExpressionToObject(node.init, localConsts, createProxy(exports.tables.keyframesHashTable), createProxy(exports.tables.viewTransitionHashTable), createProxy(exports.tables.themeTable));
170
- }
155
+ decls.set(node.id.value, node.init);
171
156
  }
172
157
  },
173
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
+ }
174
185
  return localConsts;
175
186
  }
176
187
  function getPropertyKey(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
@@ -351,7 +362,10 @@ function resolveThemeTableMemberExpressionByNode(node, themeTable) {
351
362
  const fileCache = {};
352
363
  function scanAll(addDependency) {
353
364
  for (const key in exports.tables) {
354
- exports.tables[key] = {};
365
+ const table = exports.tables[key];
366
+ for (const prop in table) {
367
+ delete table[prop];
368
+ }
355
369
  }
356
370
  const files = fs_1.default.globSync(PATTERN_PATH, GLOB_OPTIONS);
357
371
  for (const filePath of files) {
@@ -388,7 +402,6 @@ function scanAll(addDependency) {
388
402
  target: 'es2022',
389
403
  });
390
404
  addDependency(filePath);
391
- const fileId = genFileId(filePath);
392
405
  const localStaticTable = {};
393
406
  const localKeyframesHashTable = {};
394
407
  const localViewTransitionHashTable = {};
@@ -417,7 +430,7 @@ function scanAll(addDependency) {
417
430
  const name = decl.id.value;
418
431
  const init = decl.init;
419
432
  const obj = objectExpressionToObject(init.arguments[0].expression, localStaticTable, localKeyframesHashTable, localViewTransitionHashTable, localThemeTable);
420
- const uniqueKey = `${fileId}-${name}`;
433
+ const uniqueKey = `${filePath}-${name}`;
421
434
  if (method === 'createStatic') {
422
435
  localStaticTable[name] = obj;
423
436
  exports.tables.staticTable[uniqueKey] = obj;
@@ -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.2.0",
3
+ "version": "4.2.1",
4
4
  "description": "Plumeria Utils",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",