更新
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
import type { TransformResult } from 'vite';
|
||||
import type * as tsTypes from 'typescript';
|
||||
export declare function rewriteConsoleExpr(method: string, id: string, filename: string, code: string, sourceMap?: boolean): TransformResult;
|
||||
export declare function restoreConsoleExpr(code: string): string;
|
||||
export declare function appendConsoleExpr(filename: string, code: string, ts: typeof tsTypes): string;
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.appendConsoleExpr = exports.restoreConsoleExpr = exports.rewriteConsoleExpr = void 0;
|
||||
const magic_string_1 = __importDefault(require("magic-string"));
|
||||
const utils_1 = require("../utils");
|
||||
function rewriteConsoleExpr(method, id, filename, code, sourceMap = false) {
|
||||
filename = (0, utils_1.normalizePath)(filename);
|
||||
const re = /(console\.(log|info|debug|warn|error))\s*\(([^)]+)\)/g;
|
||||
const locate = getLocator(code);
|
||||
const s = new magic_string_1.default(code);
|
||||
let match;
|
||||
while ((match = re.exec(code))) {
|
||||
const [, expr, type] = match;
|
||||
s.overwrite(match.index, match.index + expr.length + 1, method + `('${type}','at ${filename}:${locate(match.index).line + 1}',`);
|
||||
}
|
||||
if (s.hasChanged()) {
|
||||
return {
|
||||
code: s.toString(),
|
||||
map: sourceMap ? s.generateMap({ hires: true }) : { mappings: '' },
|
||||
};
|
||||
}
|
||||
return { code, map: { mappings: '' } };
|
||||
}
|
||||
exports.rewriteConsoleExpr = rewriteConsoleExpr;
|
||||
function restoreConsoleExpr(code) {
|
||||
return code.replace(/(?:uni\.)?__f__\('([^']+)','at ([^:]+):(\d+)',/g, 'console.$1(');
|
||||
}
|
||||
exports.restoreConsoleExpr = restoreConsoleExpr;
|
||||
function getLocator(source) {
|
||||
const originalLines = source.split('\n');
|
||||
const lineOffsets = [];
|
||||
for (let i = 0, pos = 0; i < originalLines.length; i++) {
|
||||
lineOffsets.push(pos);
|
||||
pos += originalLines[i].length + 1;
|
||||
}
|
||||
return function locate(index) {
|
||||
let i = 0;
|
||||
let j = lineOffsets.length;
|
||||
while (i < j) {
|
||||
const m = (i + j) >> 1;
|
||||
if (index < lineOffsets[m]) {
|
||||
j = m;
|
||||
}
|
||||
else {
|
||||
i = m + 1;
|
||||
}
|
||||
}
|
||||
const line = i - 1;
|
||||
const column = index - lineOffsets[line];
|
||||
return { line, column };
|
||||
};
|
||||
}
|
||||
const methods = ['log', 'info', 'debug', 'warn', 'error'];
|
||||
function appendConsoleExpr(filename, code, ts) {
|
||||
if (!ts) {
|
||||
return code;
|
||||
}
|
||||
const s = new magic_string_1.default(code);
|
||||
const sourceFile = ts.createSourceFile(filename, code, ts.ScriptTarget.Latest);
|
||||
// 遍历sourceFile,查找console的方法调用
|
||||
const traverse = (node) => {
|
||||
ts.forEachChild(node, (node) => traverse(node));
|
||||
if (ts.isCallExpression(node) &&
|
||||
node.arguments.length > 0 &&
|
||||
ts.isPropertyAccessExpression(node.expression)) {
|
||||
const propertyAccess = node.expression;
|
||||
if (ts.isIdentifier(propertyAccess.expression) &&
|
||||
propertyAccess.expression.text === 'console' &&
|
||||
ts.isIdentifier(propertyAccess.name) &&
|
||||
methods.includes(propertyAccess.name.text)) {
|
||||
const lastArg = node.arguments[node.arguments.length - 1];
|
||||
if (lastArg) {
|
||||
const { line } = sourceFile.getLineAndCharacterOfPosition(propertyAccess.name.end);
|
||||
// 重要,需要用双引号,因为混编的kt,swift,java不能用单引号(char类型)
|
||||
s.prependRight(lastArg.getEnd(), `, " at ${filename}:${line + 1}"`);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
traverse(sourceFile);
|
||||
if (s.hasChanged()) {
|
||||
return s.toString();
|
||||
}
|
||||
return code;
|
||||
}
|
||||
exports.appendConsoleExpr = appendConsoleExpr;
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import type { LogErrorOptions, LogOptions } from 'vite';
|
||||
export interface Formatter<T extends LogOptions = LogOptions> {
|
||||
test: (msg: string, options?: T) => boolean;
|
||||
format: (msg: string, options?: T) => string;
|
||||
}
|
||||
export declare function formatErrMsg(msg: string, options?: LogErrorOptions): string;
|
||||
export declare const removeNVueInfoFormatter: Formatter;
|
||||
export declare function formatInfoMsg(msg: string, options?: LogOptions & {
|
||||
nvue?: boolean;
|
||||
}): string;
|
||||
export declare function formatWarnMsg(msg: string, options?: LogOptions): string;
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.formatWarnMsg = exports.formatInfoMsg = exports.removeNVueInfoFormatter = exports.formatErrMsg = void 0;
|
||||
const uni_shared_1 = require("@dcloudio/uni-shared");
|
||||
const shared_1 = require("@vue/shared");
|
||||
const env_1 = require("../hbx/env");
|
||||
const alias_1 = require("../hbx/alias");
|
||||
const log_1 = require("../hbx/log");
|
||||
const errFormatters = [];
|
||||
const infoFormatters = [];
|
||||
const warnFormatters = [];
|
||||
const initErrFormattersOnce = (0, uni_shared_1.once)(() => {
|
||||
if ((0, env_1.isInHBuilderX)()) {
|
||||
errFormatters.push(alias_1.moduleAliasFormatter);
|
||||
}
|
||||
errFormatters.push(log_1.errorFormatter);
|
||||
});
|
||||
const initInfoFormattersOnce = (0, uni_shared_1.once)(() => {
|
||||
if ((0, env_1.runByHBuilderX)()) {
|
||||
if (
|
||||
// 开发模式下
|
||||
process.env.UNI_PLATFORM === 'h5' &&
|
||||
process.env.NODE_ENV !== 'production') {
|
||||
infoFormatters.push(log_1.h5ServeFormatter);
|
||||
}
|
||||
}
|
||||
infoFormatters.push(log_1.removeInfoFormatter);
|
||||
});
|
||||
const initWarnFormattersOnce = (0, uni_shared_1.once)(() => {
|
||||
warnFormatters.push(log_1.removeWarnFormatter);
|
||||
});
|
||||
function formatErrMsg(msg, options) {
|
||||
if (options && (0, env_1.isInHBuilderX)()) {
|
||||
options.timestamp = false;
|
||||
}
|
||||
initErrFormattersOnce();
|
||||
const formatter = errFormatters.find(({ test }) => test(msg, options));
|
||||
if (formatter) {
|
||||
return formatter.format(msg, options);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
exports.formatErrMsg = formatErrMsg;
|
||||
const REMOVED_NVUE_MSGS = [
|
||||
(msg) => {
|
||||
// vite v2.7.10 building for development... (x2)
|
||||
return msg.includes('vite v') && msg.includes('building ');
|
||||
},
|
||||
];
|
||||
exports.removeNVueInfoFormatter = {
|
||||
test(msg) {
|
||||
return !!REMOVED_NVUE_MSGS.find((m) => (0, shared_1.isString)(m) ? msg.includes(m) : m(msg));
|
||||
},
|
||||
format() {
|
||||
return '';
|
||||
},
|
||||
};
|
||||
const nvueInfoFormatters = [];
|
||||
const initNVueInfoFormattersOnce = (0, uni_shared_1.once)(() => {
|
||||
nvueInfoFormatters.push(exports.removeNVueInfoFormatter);
|
||||
});
|
||||
function formatInfoMsg(msg, options) {
|
||||
if (options && (0, env_1.isInHBuilderX)()) {
|
||||
options.timestamp = false;
|
||||
}
|
||||
initInfoFormattersOnce();
|
||||
const formatter = infoFormatters.find(({ test }) => test(msg, options));
|
||||
if (formatter) {
|
||||
return formatter.format(msg, options);
|
||||
}
|
||||
if (options?.nvue) {
|
||||
initNVueInfoFormattersOnce();
|
||||
const formatter = nvueInfoFormatters.find(({ test }) => test(msg, options));
|
||||
if (formatter) {
|
||||
return formatter.format(msg, options);
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
exports.formatInfoMsg = formatInfoMsg;
|
||||
function formatWarnMsg(msg, options) {
|
||||
if (options && (0, env_1.isInHBuilderX)()) {
|
||||
options.timestamp = false;
|
||||
}
|
||||
initWarnFormattersOnce();
|
||||
msg = log_1.removeDuplicatePluginFormatter.format(msg);
|
||||
const formatter = warnFormatters.find(({ test }) => test(msg, options));
|
||||
if (formatter) {
|
||||
return formatter.format(msg, options);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
exports.formatWarnMsg = formatWarnMsg;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export { formatErrMsg, formatInfoMsg, formatWarnMsg } from './format';
|
||||
type LogType = 'error' | 'warn' | 'info' | 'log';
|
||||
export declare function resetOutput(type: LogType): void;
|
||||
export declare function output(type: LogType, msg: string): void;
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.output = exports.resetOutput = exports.formatWarnMsg = exports.formatInfoMsg = exports.formatErrMsg = void 0;
|
||||
var format_1 = require("./format");
|
||||
Object.defineProperty(exports, "formatErrMsg", { enumerable: true, get: function () { return format_1.formatErrMsg; } });
|
||||
Object.defineProperty(exports, "formatInfoMsg", { enumerable: true, get: function () { return format_1.formatInfoMsg; } });
|
||||
Object.defineProperty(exports, "formatWarnMsg", { enumerable: true, get: function () { return format_1.formatWarnMsg; } });
|
||||
let lastType;
|
||||
let lastMsg;
|
||||
function resetOutput(type) {
|
||||
if (type === lastType) {
|
||||
lastType = undefined;
|
||||
lastMsg = '';
|
||||
}
|
||||
}
|
||||
exports.resetOutput = resetOutput;
|
||||
function output(type, msg) {
|
||||
if (type === lastType && msg === lastMsg) {
|
||||
return;
|
||||
}
|
||||
lastMsg = msg;
|
||||
lastType = type;
|
||||
const method = type === 'info' ? 'log' : type;
|
||||
console[method](msg);
|
||||
}
|
||||
exports.output = output;
|
||||
Reference in New Issue
Block a user