如何实现Vue项目反编译

3,314 阅读1分钟

场景

描述:缺少最新版本源码,只有最新版本打包后的dist文件,需要拿到最新版本项目的源码

环境部署

安装反编译库 reverse-sourcemap
// 安装反编译库 
npm install -g reverse-sourcemap 
// 执行命令, 反编译打包后的代码为源文件到指定路径下
reverse-sourcemap --output-dir src 0.xxxxxxxx.js.map

批量处理反编译文件

import subprocess from pathlib 
import Path import os 
# 反编译vue 
base_dir = "./" # 打包后的vue项目dist路径 
output_dir = "../src" # 输出反编译后源码路径 
p = Path(base_dir) 
files_list = p.rglob('*.map') 
for file in files_list: 
  file_name = file.name 
  cmd = "reverse-sourcemap --output-dir {} {}".format(output_dir, str(file)) 
  result = subprocess.check_output(cmd, shell=True) 
  print(result)
删除反编译生成的中间文件
# 删除文件名包含特定字符串文件脚本 
import os 
def delete_files_with_string(directory, search_string): 
    for root, dirs, files in os.walk(directory): 
        for file in files: 
          if search_string in file: 
            file_name, file_extenstion = os.path.splitext(file) 
            if len(file_extenstion) > 4: 
              file_path = os.path.join(root, file) 
              print(file_path) os.remove(file_path) 
              print(f"Deleted file: {file_path}") 
# 调用示例 
directory = './src' # 替换成你的目录路径 
suffix = '.vue' # 替换成你要删除的文件包含的字符串 
delete_files_with_string(directory, suffix)
对比老版源码,补充缺少的文件即可