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

defineAsyncComponent异步加载Icon无效 #3429

Open
zhengtulymGh opened this issue Jan 21, 2025 · 0 comments
Open

defineAsyncComponent异步加载Icon无效 #3429

zhengtulymGh opened this issue Jan 21, 2025 · 0 comments

Comments

@zhengtulymGh
Copy link

zhengtulymGh commented Jan 21, 2025

项目是基于vite的vue3项目,使用了unplugin-auto-import/viteunplugin-vue-components/vite做按需加载, 发现会将每一个icon都单独打一个包, 导致页面渲染之前需要加载多个icon的js。
我想做的优化是使用defineAsyncComponent异步导入@arco-design/web-vue/es/icon,防止阻塞页面渲染,但是发现没有生效, 依然会阻塞, 但是使用defineAsyncComponent异步导入vue组件是生效的。
请教下有什么解决办法吗?

应用代码:

// asyncImport.ts
export const loadArcoIcon = (iconName: string) => {
  return defineAsyncComponent(async () => {
    console.log('defineAsyncComponent', iconName)
    const module = await import('@arco-design/web-vue/es/icon');
    const component = module[iconName];
    return component;
  });
};

//  vue单文件组件
<template>
  <icon-right />
</template>
<script lang="ts" setup>
  import { onMounted } from 'vue'
  import { loadArcoIcon } from "/@/utils/asyncImport";
  const IconRight = loadArcoIcon('IconRight');
</script>

各依赖的版本:

"@arco-design/web-vue": "^2.55.3"
"unplugin-auto-import": "^0.19.0"
"unplugin-icons": "^0.22.0"
"unplugin-vue-components": "^0.27.4"
"vite": "^2.0.5"

vite的配置:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite';
import { ArcoResolver } from 'unplugin-vue-components/resolvers';
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
        resolvers: [
           ArcoResolver({
              importStyle: false
            }), 
           ElementPlusResolver({
              importStyle: false
            }), 
            IconsResolver({
              prefix: 'Icon',
            })
        ],
      }),
      Components({
        resolvers: [
          ArcoResolver({
            sideEffect: true,
            importStyle: false
          }),
          IconsResolver({
            enabledCollections: ['ep'],
          }),
          ElementPlusResolver({
            importStyle: false
          }),
        ]
      }),
      Icons({
        autoInstall: true,
      })
    ])
  ]
});

表现结果:
打包部署后,从打印上看,确实在mounted后才执行导入
Image

但是从js资源的加上看,全局的js加载完后,接着加载阻塞具体页面渲染的的js,其中icon对应的js依然处于阻塞渲染的js队列
Image
Image

@zhengtulymGh zhengtulymGh changed the title 异步加载Icon组件失效 defineAsyncComponent异步加载Icon组件失效 Jan 21, 2025
@zhengtulymGh zhengtulymGh changed the title defineAsyncComponent异步加载Icon组件失效 defineAsyncComponent异步加载Icon组件无效 Jan 21, 2025
@zhengtulymGh zhengtulymGh changed the title defineAsyncComponent异步加载Icon组件无效 defineAsyncComponent异步加载Icon无效 Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant