Vuejs 3 - vitejs 中 require.context 的替代品是什么

3 webpack vue.js vuejs3 vite

我正在寻找require.context在 vitejs 中制作与 webpack 相同逻辑的方法,我发现了这个名为vite-plugin-import-context的插件,我尝试了一下,但有一些我不明白的东西,这是import dynamicImport from '../src/index'基本的用法 :

import { UserConfigExport } from 'vite';
import vue from '@vitejs/plugin-vue';

import dynamicImport from '../src/index';// <-- this is not described

export default (): UserConfigExport => {
  return {
    plugins: [vue(), dynamicImport(/*options*/)],
  };
};
Run Code Online (Sandbox Code Playgroud)

小智 5

使用 Vite 时,切勿在源代码中使用 require。这只是 ESM。对于Vite v2,import.meta.globEager可以使用。对于 Vite >v2,import.meta.globEager已弃用。import.meta.glob('*', { eager: true })代替使用。