-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Does vite provide a require.context-like API? #77
Comments
Perhaps an alternative would be to use |
like |
Any update on this Feature? Only good Solution that i found by now is to generate a JSON File (on build) with Is there any other possible Idea for this? |
I am also looking for a substitute for Any advise is much appreciated! |
rollup plugin can be used in build mode, but in dev mode no rollup, how to support this feature? |
I haven't thought of nice resolutions for this. But I have written a transform for this。https://github.com/luxueyan/vite-transform-globby-import. It support both dev and build mode. It only replaces 'globby import' simplely. import routes from '../pages/**/route.ts'
import imgs from '/@/assets/image/**/*.@(jpg|png)'
// These will be replaced to:
/*
* import * as routes0 from '/@/pages/route.ts'
* import * as routes1 from '/@/pages/demo/route.ts'
* ...
* const routes = { routes0, routes1, ... }
* import * as imgs0 from '/@/assets/image/demo.jpg'
* import * as imgs1 from '/@/assets/image/demo/demo.png'
* ...
* const imgs = { imgs0, imgs1, ... }
*/ |
Also interested. Would be useful for autoloading Vuex modules for instance. |
I wrote one that can support dynamic import and other functions https://github.com/vbenjs/vite-plugin-import-context. Welcome to try |
Can anyone give an example of how to use the newly implemented glob import? |
The syntax has been updated https://vitejs.dev/guide/features.html#glob-import |
Oh, that's great! thanks |
牛🍺 ! |
/**
* 路由自动注册
* 遍历views目录下所有 *.route.ts 文件,内部为路由信息
* https://cn.vitejs.dev/guide/features.html#glob-import
*/
import { RouteRecordRaw } from "vue-router"
const modules = import.meta.globEager('/src/views/**/*.route.ts')
const autoRegisteredRoutes: RouteRecordRaw[] = []
for (const path in modules) {
autoRegisteredRoutes.push(modules[path].default)
}
export { autoRegisteredRoutes } import.meta.globEager 这个方法是同步的,等同于require.context 希望对大家能有帮助 |
webpack vs vite glob /** webpack */
// const files = require.context('@/packages', true, /demo\.vue$/);
// files.keys().forEach(component => {
// const componentEntity = files(component).default;
// routes.push({
// path: `/${componentEntity.baseName}`,
// name: componentEntity.baseName,
// component: componentEntity
// });
// });
/** vite */
const modulesPage = import.meta.glob('/src/packages/**/demo.vue');
for (const path in modulesPage) {
let name = (/packages\/(.*)\/demo.vue/.exec(path) as any[])[1];
routes.push({
path: '/' + name,
component: modulesPage[path],
name
});
} |
vuex: webpack vs vite glob /** webpack */
const files = require.context(".", false, /\.js$/);
const modules = {};
files.keys().forEach(key => {
if (key === "./index.js") return;
modules[key.replace(/(\.\/|\.js)/g, "")] = files(key).default;
});
export default modules; /** vite */
const files = import.meta.globEager("./*.js");
const modules = {};
for (const key in files) {
// if (key === "./index.js") continue;
modules[key.replace(/(\.\/|\.js)/g, "")] = files[key].default;
}
console.log(files, modules);
export default modules; |
Hi folks. Recently I implemented a vite plugin that can handle the BTW : contributions are welcome. |
// code produced by vite
const modules = {
'./dir/foo.js': () => import('./dir/foo.js'),
'./dir/bar.js': () => import('./dir/bar.js')
} You can then iterate over the keys of the modules object to access the corresponding modules: for (const path in modules) {
modules[path]().then((mod) => {
console.log(path, mod)
})
} |
This issue has been locked since it has been closed for more than 14 days. If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion. |
Is your feature request related to a problem? Please describe.
I would like to be able to dynamically read the file objects inside specific directory. Webpack provides API like
require.context
in order to achieve this.Describe the solution you'd like
Is there any way within vite that one can perform something similar to
require.context
to read and parse file objectsAdditional context
This would be useful to create dynamic route building with Vue router by reading the contents of the directory.
The text was updated successfully, but these errors were encountered: