1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
| const fs = require("fs"); const path = require("path"); const { exec } = require("child_process"); const axios = require("axios"); const { default: routerConfig } = require("../config/router.config"); const pluginName = "DependencesAnalysisPlugin"; const ignoreDependenciesArr = Object.keys( require("../package.json").dependencies );
const analysisBreak = (request, issuer) => { return ( (issuer && issuer.includes("node_modules")) || (request && request.includes("node_modules")) ); };
const getRouterInfo = (config) => { if (!config.component && (!config.routes || !config.routes.length)) { return null; } const pagePath = path.resolve(__dirname, "../src/pages/"); const proRoot = path.resolve(__dirname, "../"); let children = undefined; if (config.routes && config.routes.length) { children = config.routes.map(getRouterInfo).filter(Boolean); } return { route: config.path, path: config.component ? path.relative( proRoot, require.resolve(path.resolve(pagePath, config.component)) ) : undefined, children, }; };
class DependencyTree { constructor() { this.uniqNodes = []; }
/** * 遍历树找到对应的结点列表 * @param {*} issuer */ traverseTree(issuer) { const nodes = this.uniqNodes.filter((n) => n.name === issuer); return nodes; }
/** * 为结点列表添加子节点 * @param {*} nodes * @param {*} request */ addChildNode(nodes, request) { let uniqNode = this.uniqNodes.find((n) => n.name === request); if (!uniqNode) { uniqNode = { name: request, }; this.uniqNodes.push(uniqNode); } nodes.forEach((node) => { if (!node.children) { node.children = [uniqNode]; } else { if (!node.children.find((n) => n.name === request)) { node.children.push(uniqNode); } } }); }
/** * 增加一个依赖信息 * @param {*} issuer * @param {*} request */ addDependency(issuer, request) { if (!this.root) { const firstChildNode = { name: request, }; const rootNode = { name: issuer, children: [firstChildNode], }; this.root = rootNode; this.uniqNodes.push(rootNode, firstChildNode); return; } const nodes = this.traverseTree(issuer); this.addChildNode(nodes, request); }
/** * 遍历树,将同一条链路上的重复出现的结点删除,第一次出现的保留 */ clearTree(root, nodes) { if (root.changed) { nodes.forEach((n) => { n.changed = true; }); } if (!root.children) { return; } const indexes = root.children .map((child) => nodes.find((n) => n.name === child.name)) .reduce((iter, cur, index) => iter.concat(cur ? index : []), []); root.children = root.children.reduce((iter, cur, index) => { if (indexes.includes(index)) { return iter; } return iter.concat(cur); }, []); root.children.forEach((child) => { this.clearTree(child, nodes.concat(root)); }); }
getFiles(filePath) { const sum = []; const files = fs.readdirSync(filePath); for (const file of files) { const stat = fs.statSync(path.resolve(filePath, file)); if (stat.isFile()) { sum.push(path.resolve(filePath, file)); } else if (stat.isDirectory()) { sum.push(...this.getFiles(path.resolve(filePath, file))); } } return sum; }
unusedFiles() { const root = path.resolve(__dirname, "../src/"); const proRoot = path.resolve(__dirname, "../"); const proFiles = this.getFiles(root).map((filePath) => path.relative(proRoot, filePath) ); const unused = proFiles.filter( (filePath) => !this.uniqNodes.find((node) => node.name === filePath) ); const filePath = `unused.json`; fs.writeFileSync(filePath, JSON.stringify(unused, null, 2)); }
gitDiffFiles(commitHash, branch) { return axios .get( `xxx` // 获取此次打包的信息 ) .then((res) => res && res.data) .then((res) => { if (!res || !res.results || !res.results.length) { return Promise.reject("当前分支无上线版本,无法进行依赖分析"); } const index = res.results.findIndex( (r) => r.base_commit_hash === commitHash ); return res.results[ index === -1 || index >= res.results.length - 1 ? 0 : index + 1 ].base_commit_hash; }) .then((lastHash) => { console.log("Diff hash from: ", lastHash, " to: ", commitHash); return axios.get( `xxx` // get git diff files from gitlab ); }) .then((res) => res.data) .then((res) => res.diffs.map((diff) => diff.new_path)); }
generate(commitHash, branch) { const router = getRouterInfo(routerConfig[0]); return this.gitDiffFiles(commitHash, branch) .then((filesTemp) => { const files = filesTemp.filter((p) => p.match("^src/")); console.log("Diff files: ", files); return files; }) .then((files) => { this.uniqNodes.forEach((node) => { if (files.includes(node.name)) { node.changed = true; } }); this.clearTree(this.root, []); return this.root; }) .then((data) => axios({ url: "xxx", // upload result method: "POST", data: { body: data, commitHash, router, branch }, headers: { "Content-Type": "application/json" }, }) ) .then((res) => res.data) .then((res) => { if (res.code === 0) { console.log("依赖分析构建成功!"); } else { console.error("依赖分析构建失败:", res.message); } }) .catch((err) => { console.error("依赖分析构建失败:", err); }); } }
const tree = new DependencyTree();
class DependencesAnalysisPlugin { constructor(options = {}) { this.options = options; }
afterResolve(result, callback) { const { resourceResolveData } = result; const { context: { issuer }, path, descriptionFileRoot, } = resourceResolveData; if (!analysisBreak(path, issuer)) { tree.addDependency( issuer && issuer.replace(new RegExp(`^${descriptionFileRoot}/`), ""), path.replace(new RegExp(`^${descriptionFileRoot}/`), "") ); } callback(null, result); }
apply(compiler) { compiler.hooks.normalModuleFactory.tap(pluginName, (nmf) => { nmf.hooks.afterResolve.tapAsync(pluginName, this.afterResolve); }); compiler.hooks.done.tap(pluginName, ( stats /* stats is passed as an argument when done hook is tapped. */ ) => { tree.generate(this.options.commitId, this.options.branch); }); } }
module.exports = DependencesAnalysisPlugin;
|