微信小程序线上根据Source Map文件快速定位错误代码

不废话,直接上流程

1 先登录自己的微信小程序后台将Source Map文件下载到本地;

2 解压下载的文件,可得到以下目录

在这里插入图片描述

该目录对应的就是我们小程序分包

在这里插入图片描述

也就是说,如果我们的页面在packageDynamic包下,我们定位的时候就要使用packageDynamic下的文件

如果我们的小程序没有分包的话下载下来直接解压可看到下面如下目录

点击解压后的文件夹出现的就是下面的这个图片。

3 添加一个定位页面,代码入下,copy过去直接运行即可

  
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SourceMap 查看工具</title>
    <script src="https://cdn.staticfile.org/jquery/3.4.1/jquery.js"></script>
    <script src="https://unpkg.com/source-map@0.7.3/dist/source-map.js"></script>
    <script>
        sourceMap.SourceMapConsumer.initialize({
            "lib/mappings.wasm": "https://unpkg.com/source-map@0.7.3/lib/mappings.wasm"
        });
    </script>
</head>
<body>
<form id="myForm">
    <p>
        <label for="line-column">line:column </label><input id="line-column" name="line-column">
    </p>
    <p>
        <label for="sourceMapFile">sourceMapFile </label><input id="sourceMapFile" type="file" name="sourceMapFile">
    </p>
    <button type="submit" name="submit">确定</button>
</form>
<table id="result" border="1" cellspacing="0" cellpadding="10">
    <tr>
        <th>line:column</th>
        <th> ==></th>
        <th>source</th>
        <th>line</th>
        <th>column</th>
        <th>name</th>
    </tr>
    <!--<tr>-->
    <!--<td>line:column</td><td> ==> </td><td>line</td><td>column</td><td>source</td><td>name</td>-->
    <!--</tr>-->
</table>
<script>
    $(function () {
        const form = $('#myForm');
        form.on('submit', function (e) {
            e.preventDefault();
            const dataArray = form.serializeArray();
            const dataObj = dataArray.reduce((obj, item) => {
                obj[item.name] = item.value;
                return obj;
            }, {});
            let [line, column] = dataObj['line-column'].split(':');

            // 读取文件
            let file = $('#sourceMapFile').get(0).files[0];
            const fileReader = new FileReader();
            fileReader.onloadend = function () {
                const rawSourceMap = fileReader.result;
                // 查找
                sourceMap.SourceMapConsumer.with(rawSourceMap, null, consumer => {
                    const result = consumer.originalPositionFor({
                        source: "./",
                        line: +line,
                        column: +column
                    });
                    $('#result').append($(`
                        <tr>
                            <td>${line}:${column}</td><td> ==> </td><td>${result.source}</td><td>${result.line}</td><td>${result.column}</td><td>${result.name}</td>
                        </tr>
                    `));
                });
            };
            fileReader.readAsText(file);
            return false;
        });
    });
</script>
</body>
</html>

4 运行copy好的代码 

在这里插入图片描述

看到如下界面就可以开始定位了

5 查看小程序后台日志错误提示

在这里插入图片描述

6 将相应的报错行数复制到工具页面,并选择相应文件夹下的文件即可

在这里插入图片描述

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_流年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值