简体   繁体   English

Webpack 动态导入的循环依赖

[英]Webpack Circular dependencies with dynamic import

Hi i am building an app where i found myslef doing circular dependency but webpack does not give me circular deps error.嗨,我正在构建一个应用程序,在该应用程序中我发现 myslef 执行循环依赖,但 webpack 没有给我循环 deps 错误。 so i wanted to understand what actually is happening here.所以我想了解这里实际发生了什么。

so here is how my module imports are done所以这是我的模块导入的完成方式

1. root.router.config.js root.router.config.js

import React from 'react'

const AsyncAdmin = React.lazy(()=>import('./Modules/Admin'));

export ROUTER_CONFIG = {
   admin: {
      component: AsyncAdmin,
      path: '/'
   } 
}

2. Modules/Admin/index.js 2. Modules/Admin/index.js

import {Router} from 'react-router-dom'
import MODULE_ROUTER from './Admin.router.config'

 // renders router from this MODULE_ROUTER

  1. Modules/Admin/Admin.router.config.js
import React from 'react'
import {ROUTER_CONFIG as ROOT_ROUTER_CONFIG} from '../../root.router.config' // should be cyclic import error but its not

const base = ROOT_ROUTER_CONFIG['admin'].path

const AsyncDashboard = React.lazy(()=>import('./Routes/Dashboard'));

export ROUTER_CONFIG = {
   dashbaord: {
      component: Dashboard,
      path: base + 'dashboard'
   } 
}

if you see the imports here如果你在这里看到进口

Modules/Admin/Admin.router.config.js =imports> root.router.config =imports(lazy)=> Modules/Admin/Admin.router.config.js Modules/Admin/Admin.router.config.js =imports> root.router.config =imports(lazy)=> Modules/Admin/Admin.router.config.js

this will be a definite circular import if i am not using lazy/dynamic import.如果我不使用惰性/动态导入,这将是一个明确的循环导入。

i want to know why webpack treats it differently and why its not a circular import error.我想知道为什么 webpack 对待它的方式不同,为什么它不是循环导入错误。

should i avoid it and move to a better approach where there is no chance of circular imports?我应该避免它并转向没有循环进口机会的更好方法吗?

and if i continue to do this.如果我继续这样做。 will it have any impact on my bundled code?它会对我的捆绑代码有任何影响吗?

Abstract the "path" to a constants folder/file and it should resolve the issue.将“路径”抽象为常量文件夹/文件,它应该可以解决问题。 In general, webpack aside, you should try avoiding circular dependencies.一般来说,除了 webpack,你应该尽量避免循环依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM