@wiajs/core 1.2.3 → 2.1.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/core.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia core v1.2.3
2
+ * wia core v2.1.0
3
3
  * (c) 2015-2026 Sibyl Yu and contributors
4
4
  * Released under the MIT License.
5
5
  */
@@ -2863,227 +2863,193 @@
2863
2863
  };
2864
2864
  }
2865
2865
 
2866
- /*!
2867
- * wia util v3.4.25
2868
- * (c) 2018-2024 Yan (Sibyl) Yu and Yuxi (Evan) You and wia/vue contributors
2869
- * Released under the MIT License.
2870
- */
2871
2866
  /**
2872
- * Created by way on 10/6/16.
2873
- * Part of the code comes from @vue/shared
2867
+ * @typedef {Object} LogOptions
2868
+ * @property {string} m - 模块名称
2874
2869
  */
2875
2870
 
2876
-
2877
2871
  /**
2878
- * 前端日志输出,封装 console日志,简化代码,支持模块或直接输出
2879
- * 调用时,描述字符串后置,便于可选缺省,输出时,自带前置,类似 后端pino,保持前后端一致性
2880
- * m 为模块,fn 为函数名称
2872
+ * @typedef {((...args: any[]) => void) & { debug: (...args: any[])=>void, info: (...args: any[])=>void, warn: (...args: any[])=>void, error: (...args: any[])=>void, err: (...args: any[])=>void }} ScopedLogger
2873
+ */
2874
+
2875
+ /**
2876
+ * @typedef {((...args: any[]) => void) & { log: (...args: any[])=>void, debug: (...args: any[])=>void, info: (...args: any[])=>void, warn: (...args: any[])=>void, error: (...args: any[])=>void, err: (...args: any[])=>void, fn: (name: string) => ScopedLogger }} LoggerFn
2877
+ */
2878
+
2879
+ // 1. 安全地获取开发环境状态(兼容打包工具、浏览器和 Node.js)
2880
+ // 如果 Rspack 注入了 __DEV__,就用注入的值;如果是在 Node.js 下,就回退读取 "production"
2881
+ const isDev = typeof __DEV__ !== 'undefined' ? __DEV__ : "production" !== 'production';
2882
+
2883
+ /**
2884
+ * 前端日志输出,封装 console 日志,简化代码
2885
+ * 自动识别并重排提示符与对象,保持前后端一致性
2881
2886
  */
2882
2887
  class Log {
2883
- /** @type {string} 模块 */
2888
+ /** @type {string} 模块名称 */
2884
2889
  m = ''
2885
2890
 
2886
- /** @type {string} 函数 */
2887
- fn = ''
2891
+ /** @type {string} 函数名称 */
2892
+ fnName = ''
2888
2893
 
2889
2894
  /**
2890
- * @param {string} m 模块
2895
+ * @param {string} [m=''] 模块
2896
+ * @param {string} [fnName=''] 函数
2891
2897
  */
2892
- constructor(m) {
2898
+ constructor(m = '', fnName = '') {
2893
2899
  this.m = m;
2900
+ this.fnName = fnName;
2894
2901
  }
2895
2902
 
2896
2903
  /**
2897
- * get log desc
2898
- * 描述字符串后置调用,前置显示
2899
- * @param {*[]} args
2900
- * @returns {string}
2904
+ * 格式化参数:提取提示符,构建前缀,将对象后置
2905
+ * 格式要求:[模块名称:函数名称]提示符: {对象}
2906
+ * @param {any[]} args
2907
+ * @returns {any[]} 返回处理后传递给 console 的参数数组
2901
2908
  */
2902
- getDesc(args) {
2909
+ formatArgs(args) {
2903
2910
  let R = '';
2911
+ const _ = this;
2904
2912
  try {
2905
- const _ = this;
2906
- const { m } = _;
2907
- let fn = '',
2908
- desc = '';
2913
+ const msgs = [];
2914
+ const objs = [];
2909
2915
 
2910
- if (args.length > 1) {
2911
- const last = args.at(-1);
2912
- if (typeof last === 'object') {
2913
- ;({ desc, fn } = last);
2914
- } else if (typeof last === 'string') desc = last;
2915
- if (desc || fn) {
2916
- fn = fn || _.fn;
2917
- _.fn = fn;
2918
- args.pop();
2919
- }
2916
+ // 1. 将字符串(提示符)和非字符串(对象/数值等)分开
2917
+ for (const arg of args) {
2918
+ if (typeof arg === 'string') msgs.push(arg);
2919
+ else objs.push(arg);
2920
2920
  }
2921
- fn = fn || _.fn;
2922
- if (m) desc = `${desc}[${m}${fn ? ':' + fn : ''}]`; // eslint-disable-line
2923
- R = desc;
2921
+
2922
+ let prefix = '';
2923
+
2924
+ // 2. 添加模块与函数前缀
2925
+ if (_.m) prefix = `[${_.m}${_.fnName ? ':' + _.fnName : ''}]`;
2926
+ else if (_.fnName) prefix = `[:${_.fnName}]`;
2927
+
2928
+ // 3. 拼接提示符
2929
+ const msg = msgs.join(' ');
2930
+ if (msg) prefix += msg;
2931
+
2932
+ R = [];
2933
+ if (prefix) R.push(prefix);
2934
+ R.push(...objs);
2924
2935
  } catch (e) {
2925
- console.error(`getDesc exp:${e.message}`);
2936
+ console.error(`formatArgs exp:${e.message}`);
2926
2937
  }
2927
2938
 
2928
2939
  return R
2929
2940
  }
2930
2941
 
2931
- /** @param {...any} args - params */
2942
+ /** @param {...any} args */
2932
2943
  log(...args) {
2933
- const _ = this;
2934
- const last = args.at(-1);
2935
- // clear fn
2936
- if (args.length === 1 && typeof last === 'object' && last.fn) _.fn = '';
2937
- else {
2938
- const desc = _.getDesc(args);
2939
- console.log(desc, ...args);
2940
- }
2944
+ // 生产环境安全剔除 (需 Rspack DefinePlugin 注入 __DEV__)
2945
+ if (isDev) console.log(...this.formatArgs(args));
2941
2946
  }
2942
2947
 
2943
- /** @param {...any} args - params */
2948
+ /** @param {...any} args */
2944
2949
  debug(...args) {
2945
- const _ = this;
2946
- const desc = _.getDesc(args);
2947
- if (desc) console.log(desc, ...args);
2948
- else console.log(...args);
2950
+ if (isDev) console.debug(...this.formatArgs(args));
2949
2951
  }
2950
2952
 
2951
- /** @param {...any} args - params */
2953
+ /** @param {...any} args */
2952
2954
  info(...args) {
2953
- const _ = this;
2954
- const desc = _.getDesc(args);
2955
- if (desc) console.info(desc, ...args);
2956
- else console.log(...args);
2955
+ if (isDev) console.info(...this.formatArgs(args));
2957
2956
  }
2958
2957
 
2959
- /** @param {...any} args - params */
2958
+ /** @param {...any} args */
2960
2959
  warn(...args) {
2961
- const _ = this;
2962
- const { desc, arg } = _.getDesc(args);
2963
- if (desc) console.warn(desc, ...arg);
2964
- else console.log(...args);
2960
+ if (isDev) console.warn(...this.formatArgs(args));
2965
2961
  }
2966
2962
 
2967
- /** @param {...any} args - params */
2963
+ /** @param {...any} args */
2968
2964
  trace(...args) {
2969
- const _ = this;
2970
- const { desc, arg } = _.getDesc(args);
2971
- if (desc) console.trace(desc, ...arg);
2972
- else console.trace(...args);
2965
+ if (isDev) console.trace(...this.formatArgs(args));
2973
2966
  }
2974
2967
 
2975
- /** @param {...any} args - params */
2968
+ /** * error 会在生产环境保留
2969
+ * @param {...any} args
2970
+ */
2976
2971
  error(...args) {
2977
- const _ = this;
2978
- const desc = _.getDesc(args);
2979
- if (desc) console.error(desc, ...args);
2980
- else console.log(...args);
2972
+ console.error(...this.formatArgs(args));
2981
2973
  }
2982
2974
 
2983
2975
  /**
2984
- * 用于 catch(e) log.err(e)
2985
- * @param {...any} args - params */
2976
+ * 用于 catch(e) log.err(e) 或 log.err("提示符", e) 或 log.err(e, "提示符")
2977
+ * 自动对齐前缀 [模块:函数] 提示符: Error对象,并保留堆栈
2978
+ * @param {...any} args
2979
+ */
2986
2980
  err(...args) {
2987
- const _ = this;
2988
- const first = args?.[0];
2989
- if (
2990
- first instanceof Error ||
2991
- (first && first.message && first.cause && first.stack)
2992
- )
2993
- args[0] = { exp: args[0].message };
2994
- _.error(...args);
2981
+ // 生产环境安全输出,不会被 Tree-shaking 摇掉
2982
+ // formatArgs 会自动分离字符串与 Error 对象,并补全冒号
2983
+ const formattedArgs = this.formatArgs(args);
2984
+
2985
+ // 原生打印,保持 stack trace 可点击跳转!
2986
+ console.error(...formattedArgs);
2995
2987
  }
2996
- }
2997
2988
 
2998
- function getDesc(args) {
2999
- let desc = '';
3000
- const last = args.at(-1);
3001
- if (typeof last === 'string') {
3002
- desc = last;
3003
- args.pop();
2989
+ /**
2990
+ * 派生函数级别日志实例 (完美对齐后端 log.fn)
2991
+ * @param {string} name 函数名称
2992
+ * @returns {ScopedLogger}
2993
+ */
2994
+ fn(name) {
2995
+ const scopedLog = new Log(this.m, name);
2996
+
2997
+ /** @type {any} */
2998
+ const R = (...args) => scopedLog.log(...args);
2999
+
3000
+ R.debug = scopedLog.debug.bind(scopedLog);
3001
+ R.info = scopedLog.info.bind(scopedLog);
3002
+ R.warn = scopedLog.warn.bind(scopedLog);
3003
+ R.trace = scopedLog.trace.bind(scopedLog);
3004
+ R.error = scopedLog.error.bind(scopedLog);
3005
+ R.err = scopedLog.err.bind(scopedLog);
3006
+
3007
+ return /** @type {ScopedLogger} */ (R)
3004
3008
  }
3005
- return desc
3006
3009
  }
3007
3010
 
3011
+ // 实例化唯一的全局日志单例
3012
+ const _log = new Log();
3013
+
3008
3014
  /**
3009
3015
  * 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
3010
- * 启用 {f:fn} 标记时,需在函数尾部清除f(log({f:''})),否则会溢出到其他函数
3016
+ * 传入 { m: '模块名' } 构建实例,或直接传入参数输出全局日志
3011
3017
  * @param {...any} args - params
3012
3018
  * returns {*}
3013
3019
  */
3014
3020
  function log(...args) {
3015
3021
  const last = args.at(-1);
3016
3022
 
3017
- // 全局日志
3018
- if (args.length !== 1 || !last?.m) {
3019
- const desc = getDesc(args);
3020
- desc ? console.log(desc, ...args) : console.log(...args);
3021
- return
3022
- }
3023
+ // 1. 如果只有一个参数且带 m 属性,说明是构造 Logger 实例 (如: const log = Log({m: 'User'}))
3024
+ if (args.length === 1 && typeof last === 'object' && last !== null && 'm' in last) {
3025
+ const lg = new Log(last.m);
3023
3026
 
3024
- // 唯一 m 属性,则构造新的 log 实例,这种写法,能被jsDoc识别子属性
3025
- const lg = new Log(last?.m);
3026
- /** @param {*} args2 */
3027
- const R = (...args2) => lg.log(...args2);
3028
- R.debug = lg.debug.bind(lg);
3029
- R.info = lg.info.bind(lg);
3030
- R.warn = lg.warn.bind(lg);
3031
- R.info = lg.info.bind(lg);
3032
- R.trace = lg.trace.bind(lg);
3033
- R.error = lg.error.bind(lg);
3034
- R.err = lg.err.bind(lg);
3027
+ /** @type {any} */
3028
+ const R = (...args2) => lg.log(...args2);
3035
3029
 
3036
- return R
3037
- }
3030
+ R.log = lg.log.bind(lg);
3031
+ R.debug = lg.debug.bind(lg);
3032
+ R.info = lg.info.bind(lg);
3033
+ R.warn = lg.warn.bind(lg);
3034
+ R.trace = lg.trace.bind(lg);
3035
+ R.error = lg.error.bind(lg);
3036
+ R.err = lg.err.bind(lg);
3037
+ R.fn = lg.fn.bind(lg); // 暴露派生函数作用域方法
3038
3038
 
3039
- /**
3040
- * 用于 catch(e) log.err(e)
3041
- * @param {...any} args - params */
3042
- log.err = (...args) => {
3043
- const desc = getDesc(args);
3044
- const first = args?.[0];
3045
- if (
3046
- first instanceof Error ||
3047
- (first && first.message && first.cause && first.stack)
3048
- )
3049
- args[0] = { exp: args[0].message };
3050
- desc ? console.error(desc, ...args) : console.error(...args);
3051
- };
3052
-
3053
- /**
3054
- * @param {...any} args - params */
3055
- log.error = (...args) => {
3056
- const desc = getDesc(args);
3057
- desc ? console.error(desc, ...args) : console.error(...args);
3058
- };
3059
-
3060
- /**
3061
- * @param {...any} args - params */
3062
- log.warn = (...args) => {
3063
- const desc = getDesc(args);
3064
- desc ? console.warn(desc, ...args) : console.warn(...args);
3065
- };
3066
-
3067
- /**
3068
- * @param {...any} args - params */
3069
- log.info = (...args) => {
3070
- const desc = getDesc(args);
3071
- desc ? console.info(desc, ...args) : console.info(...args);
3072
- };
3039
+ return /** @type {LoggerFn} */ (R)
3040
+ }
3073
3041
 
3074
- /**
3075
- * @param {...any} args - params */
3076
- log.debug = (...args) => {
3077
- const desc = getDesc(args);
3078
- desc ? console.log(desc, ...args) : console.log(...args);
3079
- };
3042
+ // 2. 否则作为全局普通日志直接输出
3043
+ _log.log(...args);
3044
+ }
3080
3045
 
3081
- /**
3082
- * @param {...any} args - params */
3083
- log.trace = (...args) => {
3084
- const desc = getDesc(args);
3085
- desc ? console.trace(desc, ...args) : console.trace(...args);
3086
- };
3046
+ // 绑定全局快捷方法,方便直接调用 log.err(e) 等
3047
+ log.err = _log.err.bind(_log);
3048
+ log.error = _log.error.bind(_log);
3049
+ log.warn = _log.warn.bind(_log);
3050
+ log.info = _log.info.bind(_log);
3051
+ log.debug = _log.debug.bind(_log);
3052
+ log.trace = _log.trace.bind(_log);
3087
3053
 
3088
3054
  var Page = function (_Event) {
3089
3055
  function Page(app, name, title, style) {