Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过 circular-dependency-plugin 插件检测存在循环依赖 #3319

Closed
chenbin92 opened this issue Jul 3, 2020 · 3 comments
Closed

通过 circular-dependency-plugin 插件检测存在循环依赖 #3319

chenbin92 opened this issue Jul 3, 2020 · 3 comments
Assignees
Labels
Milestone

Comments

@chenbin92
Copy link
Collaborator

问题

如下配置

const CircularDependencyPlugin = require('circular-dependency-plugin')

module.exports = ({ onGetWebpackConfig }) => {
    onGetWebpackConfig((config) => {
        config.plugin('CircularDependencyPlugin')
          .use(CircularDependencyPlugin, [{
              // exclude detection of files based on a RegExp
              exclude: /node_modules/,
              // include specific files based on a RegExp
              // include: /src/,
              // add errors to webpack instead of warnings
              failOnError: false,
              // allow import cycles that include an asyncronous import,
              // e.g. via import(/* webpackMode: "weak" */ './file.js')
              // allowAsyncCycles: false,
              // set the current working directory for displaying module paths
              cwd: process.cwd(),
          }]);
    });
}

路由使用

import { lazy } from 'ice';
import BasicLayout from '@/layouts/BasicLayout';

import Home from '@/pages/Home';

const routerConfig = [
  {
    path: '/',
    component: BasicLayout,
    children: [
      { path: '/channels', component: Home }
    ],
  },
];
export default routerConfig;

通过 circular-dependency-plugin 插件检测存在循环问题

image

预期

解决循环依赖问题

@LanceZhu
Copy link
Collaborator

LanceZhu commented Jul 16, 2020

问题定位:
plugin-router 中引用 src/routes.ts 文件。
产生循环依赖:src/app.tsx -> .ice/index.ts -> plugin-router -> src/routes.ts -> .ice/index.ts

相关代码:

import defaultRoutes from '$ice/routes';

TEM_ROUTER_SETS.forEach(i => {

解决方案:

  1. routes.ts 提升至 app.tsx
    示例代码:

    // src/app.tsx
    import routes from '$ice/routes';
    
    const appConfig: IAppConfig = {
    	router: {
    +		routes
    	}
    }
  2. 路由相关组件,lazyLink 等与 createApp 分离。

    原有:

    import { createApp, lazy, Link } from 'ice';

    修改后:

    import { createApp } from 'ice';
    
    import { Link } from 'ice/Router';
    import { lazy } from 'ice/lazy';

@LanceZhu
Copy link
Collaborator

针对解决方案2:
想法:通过 babel 更换 src/pages/*src/routes 中引入的 路由 相关组件,如 Link, lazy 的原有导入方式。

原有导入:
import { Link } from 'ice';

修改后:
import { Link } from 'ice/routes/index.ts';

实现参考:plugin-store babelPluginReplacePath

@chenbin92
Copy link
Collaborator Author

已处理

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants