代码之家  ›  专栏  ›  技术社区  ›  Harkirat Saluja

使用babel模块解析器从单个文件导出时出现循环依赖问题

  •  6
  • Harkirat Saluja  · 技术社区  · 6 年前

    here

    从这里我意识到有什么地方不对,循环依赖正在被创建。

    我决定放弃 madge

    现在由于我的项目是相当大的调试,这是一个相当大的任务,所以我创建了一个包含单个文件夹的项目的小版本。

    我创建了一个utils文件夹,其中有4个文件:-

    1. 公用事业/索引.js
    2. 实用程序/设备-助手.js
    3. 公用事业/初始化.js
    4. 索引.js

    对于我正在使用的导入 babel-module-resolver

    公用事业/初始化.js

    import {deviceInfo} from "utils";
    
    export const init = () => {
      // initialising app and calling backend API with device info
    };
    

    实用程序/设备-助手.js

    import DeviceInfo from "react-native-device-info";
    
    const API_LEVEL = "v0";
    
    export const deviceInfo = () => {
      try {
        return Object.assign({}, {
          apiLevel: API_LEVEL,
          deviceId: DeviceInfo.getUniqueID(),
          device: DeviceInfo.getDeviceName(),
          model: DeviceInfo.getModel(),
          osVersion: DeviceInfo.getSystemVersion(),
          product: DeviceInfo.getBrand(),
          country: DeviceInfo.getDeviceCountry(),
          appVersion: DeviceInfo.getVersion(),
          manufacturer: DeviceInfo.getManufacturer(),
          userAgent: DeviceInfo.getUserAgent(),
          buildNumber: DeviceInfo.getBuildNumber(),
          bundleId: DeviceInfo.getBundleId()
        });
      } catch (e) {
        //  TODO: Report to Bugsnag
        return {};
      }
    };
    

    export * from "./init";
    export * from "./device-info-helper";
    

    索引.js

    export * from "./utils"; 
    

    运行后 madge 我得到的命令如下:-

    tests-MBP:madge-test harkirat$ madge --circular  index.js
    Processed 4 files (684ms)
    
    ✖ Found 1 circular dependency!
    
    1) utils/index.js > utils/init.js
    

    公用事业/初始化.js

    import {deviceInfo} from "./device-helpers";
    
    
    export const init = () => {
      // initialising app and calling backend API with device info
    };
    

    我无法理解这种循环依赖的原因。有人能帮忙吗?

    Here

    1 回复  |  直到 6 年前
        1
  •  0
  •   Dirk    6 年前

    我看不到 .babelrc 但我认为:

    1. utils/init.js 导入时使用:

    import {deviceInfo} from "utils";

    这与:

    import {deviceInfo} from "./utils/index";

    1. utils/index.js 你做一个 export * from "./init" . 这个 export from 基本上先导入 ./utils/init 再出口。

    所以:

    • 公用事业/初始化.js 进口自 ./utils/index
    • ./utils/index.js 进口自

    这就是你的循环依赖。

    推荐文章