前端 uni-app 重写: 9页面 H5

This commit is contained in:
zhiqui
2026-11-15 15:09:37 +08:00
parent 513b53672e
commit ea7c2529e9
24104 changed files with 3667749 additions and 0 deletions
@@ -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;
@@ -0,0 +1,92 @@
"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");
const x_1 = require("../x");
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: (0, x_1.shouldUseHighResolutionSourceMap)() })
: { mappings: '' },
};
}
return { code, map: null };
}
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;
@@ -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
View File
@@ -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;
@@ -0,0 +1,15 @@
import type { SourceLocation } from '@vue/compiler-core';
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;
export interface CompileLogError extends Error {
loc?: Omit<SourceLocation, 'source'>;
customPrint?: () => void;
}
export interface CompileLogOptions {
plugin?: string;
line?: number;
column?: number;
}
export declare function onCompileLog(type: 'warn' | 'error', error: CompileLogError, code: string, relativeFileName: string, options?: CompileLogOptions): void;
+59
View File
@@ -0,0 +1,59 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.onCompileLog = exports.output = exports.resetOutput = exports.formatWarnMsg = exports.formatInfoMsg = exports.formatErrMsg = void 0;
const picocolors_1 = __importDefault(require("picocolors"));
const constants_1 = require("../constants");
const utils_1 = require("../vite/utils/utils");
const log_1 = require("../hbx/log");
const utils_2 = require("../vite/plugins/vitejs/utils");
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;
function onCompileLog(type, error, code, relativeFileName, options) {
const char = type === 'warn' ? constants_1.SPECIAL_CHARS.WARN_BLOCK : constants_1.SPECIAL_CHARS.ERROR_BLOCK;
if (options?.plugin) {
// CSS 插件格式
const colorFn = type === 'warn' ? picocolors_1.default.yellow : (s) => s;
console[type](char + colorFn(`[plugin:${options.plugin}] ${error.message}`));
let msg = (0, log_1.formatAtFilename)(relativeFileName, options.line, options.column);
if (options.line && options.column) {
msg += `\n${(0, utils_2.generateCodeFrame)(code, {
line: options.line,
column: options.column,
}).replace(/\t/g, ' ')}\n`;
}
console.log(msg + char);
}
else {
console[type](char + type + ': ' + error.message + (error.loc ? '' : char));
if (error.loc) {
const start = error.loc.start;
console.log('at ' + relativeFileName + ':' + start.line + ':' + start.column);
console.log((0, utils_1.generateCodeFrameColumns)(code, error.loc) + char);
}
}
}
exports.onCompileLog = onCompileLog;