nitrojs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.build
MIT License
6.24k stars 514 forks source link

Support import.meta.glob #1671

Open IlyaSemenov opened 1 year ago

IlyaSemenov commented 1 year ago

Describe the feature

Following https://github.com/nuxt/nuxt/issues/22106, I would like to have import.meta.glob("./migrations/*.ts") available in Nitro apps, in particular in Nuxt server handlers.

As I elaborated in #1670, this should be lazy by default, repeating how Vite works. An option to pass { eager: true }(same as in Vite) would be nice but not critical, as this could be achieved in user-land.

Additional information

pi0 commented 1 year ago

Hi. This is a Vite feature but we might be able to replicate it with a rollup plugin 👍🏼


Side-note: As for laziness, please strictly consider to avoid any side-effect logics inside js modules even when they are dynamically imported. defineLazyHandler can do the trick!

so1ve commented 1 year ago

Try https://github.com/sxzz/unplugin-glob

If you prefer vite-style glob imports, I'm willing to create an unplugin for that

IlyaSemenov commented 1 year ago

@so1ve I'd prefer to have vite-style imports to reuse code between Vite and Nuxt. My use-case is a lot of graphql resolvers in individual modules which I gather with import.meta.glob in Vite. In dev mode, I'd like these handlers to be served by Nuxt dev server, and for that I need it to understand vite import syntax.

tschni commented 7 months ago

@pi0 have there been any updates regarding the use of import.meta.glob in the meantime?

pi0 commented 7 months ago

@tschni it is likely we will support it only with nitro v3 via vite bundler.

In the meantime, i think an unplugin like the one @sxzz made unplugin-glob should make it doable.

so1ve commented 7 months ago

Oh this one is made by @sxzz :)

IlyaSemenov commented 7 months ago

@so1ve Did you actually try that plugin with Nuxt? I'm struggling to make it work. When I add this to nuxt.config.ts:

import UnpluginGlob from 'unplugin-glob/vite';

export default defineNuxtConfig({
  vite: {
    plugins: [UnpluginGlob({})],
  },
});

and then:

import * as all from 'glob:name:./parts/*.ts';

It doesn't work:

[2:51:22 PM]  ERROR  Pre-transform error: Failed to resolve import "glob:name:./parts/*.ts" from "app.vue". Does the file exist?

See minimal reproduction: https://stackblitz.com/edit/nuxt-starter-farkra

zhouHaoZhuang commented 1 week ago

关于这个红色提示的问题我在 tsconfig.json 文件中添加 "compilerOptions": { "module": "esnext", // 主要是这个

}, 获得了解决