nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Support import.meta.glob

Open IlyaSemenov opened this issue 2 years ago • 11 comments

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

  • [ ] Would you be willing to help implement this feature?

IlyaSemenov avatar Aug 30 '23 02:08 IlyaSemenov

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!

pi0 avatar Sep 01 '23 17:09 pi0

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

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

so1ve avatar Sep 03 '23 03:09 so1ve

@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.

IlyaSemenov avatar Sep 26 '23 15:09 IlyaSemenov

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

tschni avatar Mar 22 '24 14:03 tschni

@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.

pi0 avatar Mar 22 '24 14:03 pi0

Oh this one is made by @sxzz :)

so1ve avatar Mar 23 '24 02:03 so1ve

@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

IlyaSemenov avatar Apr 13 '24 07:04 IlyaSemenov

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

}, 获得了解决

zhouHaoZhuang avatar Nov 06 '24 07:11 zhouHaoZhuang