@anjianshi/utils 3.0.3 → 3.0.5

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.
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * 针对 Node.js 环境定制 logging
3
- * 注意:使用此模块需要 chalk 依赖
3
+ *
4
+ * 使用前提:
5
+ * - 安装 chalk 依赖
4
6
  */
5
7
  import { type Logger } from '../../logging/index.js';
6
8
  export * from './handlers.js';
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * 针对 Node.js 环境定制 logging
3
- * 注意:使用此模块需要 chalk 依赖
3
+ *
4
+ * 使用前提:
5
+ * - 安装 chalk 依赖
4
6
  */
5
7
  import { logger as defaultLogger } from '../../logging/index.js';
6
8
  import { ConsoleHandler } from './handlers.js';
@@ -5,17 +5,23 @@
5
5
  * 使用前提:
6
6
  * 1. 只支持浏览器渲染
7
7
  * 2. 安装 @emotion/serialize、@emotion/utils
8
- * 2. EmotionCacheProvider 包裹 App 根元素
8
+ * 3. 调用 registerEmotionFunctions() 注册 @emotion/serialize 的 serializeStyles() 和 @emotion/utils 的 insertStyles() 函数
9
+ * 4. 用 EmotionCacheProvider 包裹 App 根元素
9
10
  *
10
11
  * 来自:
11
12
  * https://github.com/emotion-js/emotion/issues/1853#issuecomment-623349622
12
13
  */
13
14
  import { type EmotionCache } from '@emotion/react';
14
- import { type CSSInterpolation } from '@emotion/serialize';
15
+ import type { CSSInterpolation, serializeStyles as serializeStylesT } from '@emotion/serialize';
16
+ import type { insertStyles as insertStylesT } from '@emotion/utils';
15
17
  export declare const useEmotionCache: () => EmotionCache | undefined;
16
18
  export declare const EmotionCacheProvider: import("react").FC<{
17
19
  children: React.ReactNode;
18
20
  } & import("react").RefAttributes<any>> | import("react").ForwardRefExoticComponent<{
19
21
  children: React.ReactNode;
20
22
  } & import("react").RefAttributes<any>>;
23
+ declare let serializeStyles: typeof serializeStylesT | null;
24
+ declare let insertStyles: typeof insertStylesT | null;
25
+ export declare function registerEmotionFunctions(serializeStylesFn: typeof serializeStyles, insertStylesFn: typeof insertStyles): void;
21
26
  export declare function useEmotionClassName(): (...args: CSSInterpolation[]) => string;
27
+ export {};
@@ -5,29 +5,39 @@
5
5
  * 使用前提:
6
6
  * 1. 只支持浏览器渲染
7
7
  * 2. 安装 @emotion/serialize、@emotion/utils
8
- * 2. EmotionCacheProvider 包裹 App 根元素
8
+ * 3. 调用 registerEmotionFunctions() 注册 @emotion/serialize 的 serializeStyles() 和 @emotion/utils 的 insertStyles() 函数
9
+ * 4. 用 EmotionCacheProvider 包裹 App 根元素
9
10
  *
10
11
  * 来自:
11
12
  * https://github.com/emotion-js/emotion/issues/1853#issuecomment-623349622
12
13
  */
13
14
  import { withEmotionCache } from '@emotion/react';
14
- import { serializeStyles } from '@emotion/serialize';
15
- import { insertStyles } from '@emotion/utils';
16
15
  import { createContext, useContext, useCallback } from 'react';
17
16
  const CacheContext = createContext(undefined);
18
17
  export const useEmotionCache = () => useContext(CacheContext);
19
18
  export const EmotionCacheProvider = withEmotionCache(({ children }, cache) => {
20
19
  return <CacheContext.Provider value={cache}>{children}</CacheContext.Provider>;
21
20
  });
21
+ let serializeStyles = null;
22
+ let insertStyles = null;
23
+ export function registerEmotionFunctions(serializeStylesFn, insertStylesFn) {
24
+ serializeStyles = serializeStylesFn;
25
+ insertStyles = insertStylesFn;
26
+ }
22
27
  export function useEmotionClassName() {
23
28
  const cache = useEmotionCache();
24
29
  return useCallback((...args) => {
25
30
  if (!cache) {
26
31
  if (process.env.NODE_ENV === 'production') {
32
+ console.error('useEmotionClassName: emotion-cache-missing');
27
33
  return 'emotion-cache-missing';
28
34
  }
29
35
  throw new Error('No emotion cache found!');
30
36
  }
37
+ if (!serializeStyles || !insertStyles) {
38
+ console.error('useEmotionClassName: need register serializeStyles() and insertStyles() functions first.');
39
+ return 'emotion-functions-missing';
40
+ }
31
41
  const serialized = serializeStyles(args, cache.registered);
32
42
  insertStyles(cache, serialized, false);
33
43
  return cache.key + '-' + serialized.name;
@@ -3,6 +3,9 @@
3
3
  *
4
4
  * 注意:Prisma 的 debugging 日志是直接输出到 console 的,没有提供处理渠道,所以无法记录进日志文件。
5
5
  * 理论上可以重写 console.log/debug... 等方法来实现捕获,但这牵扯面太广,暂不这样做。
6
+ *
7
+ * 使用前提:
8
+ * - 安装 chalk 依赖
6
9
  */
7
10
  import nodeUtil from 'node:util';
8
11
  import chalk from 'chalk';
@@ -1,3 +1,9 @@
1
+ /**
2
+ * 通用日志记录实现
3
+ *
4
+ * 使用前提:
5
+ * - 安装 dayjs 依赖
6
+ */
1
7
  import { type Dayjs } from 'dayjs';
2
8
  export { default as formatters } from './formatters.js';
3
9
  export * from './adapt.js';
package/logging/index.js CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * 通用日志记录实现
3
+ *
4
+ * 使用前提:
5
+ * - 安装 dayjs 依赖
6
+ */
1
7
  import dayjs from 'dayjs';
2
8
  export { default as formatters } from './formatters.js';
3
9
  export * from './adapt.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "3.0.3",
3
+ "version": "3.0.5",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -31,15 +31,17 @@
31
31
  "redis": "^5.5.6",
32
32
  "typescript": "^5.8.3",
33
33
  "vconsole": "^3.15.1",
34
- "@anjianshi/presets-eslint-base": "6.0.0",
35
34
  "@anjianshi/presets-eslint-react": "6.0.0",
35
+ "@anjianshi/presets-eslint-base": "6.0.0",
36
36
  "@anjianshi/presets-eslint-node": "6.0.0",
37
37
  "@anjianshi/presets-eslint-typescript": "6.0.0",
38
- "@anjianshi/presets-typescript": "3.2.5",
39
- "@anjianshi/presets-prettier": "3.0.5"
38
+ "@anjianshi/presets-prettier": "3.0.5",
39
+ "@anjianshi/presets-typescript": "3.2.5"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@emotion/react": "^11.14.0",
43
+ "@emotion/serialize": "^1.3.3",
44
+ "@emotion/utils": "^1.4.2",
43
45
  "@prisma/client": "^6.8.2",
44
46
  "chalk": "^5.4.1",
45
47
  "dayjs": "^1.11.13",
@@ -50,6 +52,12 @@
50
52
  "@emotion/react": {
51
53
  "optional": true
52
54
  },
55
+ "@emotion/serialize": {
56
+ "optional": true
57
+ },
58
+ "@emotion/utils": {
59
+ "optional": true
60
+ },
53
61
  "@prisma/client": {
54
62
  "optional": true
55
63
  },
@@ -1,3 +1,9 @@
1
+ /**
2
+ * 通用验证器实现
3
+ *
4
+ * 使用前提:
5
+ * - 因为涉及时间对象解析,需安装 dayjs 依赖
6
+ */
1
7
  export * from './base.js';
2
8
  export * from './boolean.js';
3
9
  export * from './number.js';
@@ -1,3 +1,9 @@
1
+ /**
2
+ * 通用验证器实现
3
+ *
4
+ * 使用前提:
5
+ * - 因为涉及时间对象解析,需安装 dayjs 依赖
6
+ */
1
7
  export * from './base.js';
2
8
  export * from './boolean.js';
3
9
  export * from './number.js';