Vuejs 3 - what's the alternative of require.context in vitejs

Viewed 2874

I'm looking for way to make the same logic of require.context of webpack in vitejs, I've found this plugin named vite-plugin-import-context, I tried it out but there's something that I didn't understand which is import dynamicImport from '../src/index' in the basic usage :

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*/)],
  };
};
3 Answers

Yep, that example is directly taken from examples folder in the repo so it works only in that repo.

If you install the plugin via npm or yarn, the line should look like import dynamicImport from 'vite-plugin-import-context'

require should never be used in source code when using Vite. It's ESM only. For Vite v2, import.meta.globEager can be used. For Vite >v2, import.meta.globEager is deprecated. Use import.meta.glob('*', { eager: true }) instead.

Related