/** * Node 兼容 shim(uni-app Vue2 工具链专用) * * 背景:Node 23+ 移除了历史遗留的 `util.isArray` / `util.isRegExp` 等 API, * 而 uni-app Vue2 编译链依赖的 postcss-urlrewrite 等老包仍在调用它们, * 在 Node 22+ 上执行 app-plus 构建会抛 `util.isRegExp is not a function`。 * * 用法:通过 NODE_OPTIONS=--require=./scripts/node-compat.cjs 注入(见 package.json scripts)。 * 在 Node 16/18 下运行时所有 API 均存在,本文件自动变成空操作,无副作用。 */ 'use strict' const util = require('util') const toString = Object.prototype.toString const shims = { isArray: Array.isArray, isBoolean: (v) => typeof v === 'boolean', isBuffer: (v) => Buffer.isBuffer(v), isDate: (v) => v instanceof Date, isError: (v) => v instanceof Error || toString.call(v) === '[object Error]', isFunction: (v) => typeof v === 'function', isNull: (v) => v === null, isNullOrUndefined: (v) => v === null || v === undefined, isNumber: (v) => typeof v === 'number', isObject: (v) => v !== null && typeof v === 'object', isPrimitive: (v) => v === null || (typeof v !== 'object' && typeof v !== 'function'), isRegExp: (v) => v instanceof RegExp, isString: (v) => typeof v === 'string', isSymbol: (v) => typeof v === 'symbol', isUndefined: (v) => v === undefined } Object.keys(shims).forEach((name) => { if (typeof util[name] !== 'function') { util[name] = shims[name] } }) if (typeof util._extend !== 'function') { util._extend = function _extend (target, source) { if (source) { for (const key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key] } } } return target } }