zoukankan      html  css  js  c++  java
  • 判断通过es6导入的模块是否存在循环引用

    // 循环依赖
    const fs = require('fs');
    const { basename, dirname, resolve } = require('path');
    const dep_cache = {};
    const reg = /(?<=froms+['"])[./w]+(?=['"])/g;
    function estimateRepeatDep (entry, prePath = '.') {
      let temp = null;
      // 是否为循环依赖
      let flag = false;
      const name = basename(entry);
      const dir = resolve(prePath, dirname(entry));
      const path = resolve(prePath, entry);
      const readFile = Buffer.from(fs.readFileSync(path)).toString();
      const cache = [];
      while ((temp = reg.exec(readFile)) !== null) {
        cache.push(...temp.map(filePath => resolve(dir, ~filePath.indexOf('.js') ? filePath : `${filePath}.js`)));
      }
        dep_cache[path] = {
          name,
          dir,
          path,
          cache
        };
        cache.forEach(c => {
          if (dep_cache[c] && dep_cache[c].cache.includes(resolve(path))) {
            flag = true;
          }
        });
        for (let i = 0, length = cache.length;i < length; ++i) {
          if (flag) {
            break;
          }
          flag = estimateRepeatDep(cache[i], dir)
        }
        return flag;
    }
    console.log(estimateRepeatDep('../d.js'));
    
    
  • 相关阅读:
    在Twrp下删除面具模块
    Windows之批量创建用户、组部署
    H3C之HDLC实验部署
    Linux之防火墙部署
    H3C之Telnet实验部署
    win10 远程桌面 ubuntu
    VMware 虚拟机开机黑屏
    计算机存储单位换算
    TextCNN代码实践
    TextCNN论文解读
  • 原文地址:https://www.cnblogs.com/smallZoro/p/13773499.html
Copyright © 2011-2022 走看看