wheatjs / vite-plugin-vue-type-imports

Import types in Vue SFC for defineProps
223 stars 16 forks source link

Nuxt 3 + Vitest and `@nuxt/test-utils-edge` package, test error — TypeError: Invalid URL #24

Closed mrauhu closed 1 year ago

mrauhu commented 1 year ago

Hello, @wheatjs.

Found strange behavior: the vite-plugin-vue-type-imports/nuxt module is breaks the Vitest with @nuxt/test-utils-edge package, but it's works if a same code is loaded from module/ directory:

https://github.com/wheatjs/vite-plugin-vue-type-imports/blob/370fcea9c7a09a0961bdf6e29b0795963034eb1e/src/nuxt.ts#L3-L8

See: https://github.com/nuxt/nuxt.js/issues/14645

Maybe, also the Nuxt module is missing the defineNuxtModule() with setup() script, I rewrote it locally like this and the code is works:

modules/vue-type-imports.ts

import { defineNuxtModule } from '@nuxt/kit';
import VitePluginVueTypeImports from 'vite-plugin-vue-type-imports';

// noinspection JSUnusedGlobalSymbols
export default defineNuxtModule({
  async setup(_options, nuxt) {
    nuxt.hook('vite:extend', async (vite) => {
      vite.config.plugins = vite.config.plugins || [];
      vite.config.plugins.unshift(VitePluginVueTypeImports());
    });
  },
});

Best wishes, Sergey.

Zolyn commented 1 year ago

Can you give a reproduction link/repo?

mrauhu commented 1 year ago

@Zolyn, from https://github.com/nuxt/nuxt.js/issues/14645:


Reproduction

How to recreate

For every link:

  1. Open a link.
  2. Wait for a development server is start and the Nuxt Welcome page is loaded.
  3. Press Ctrl + C in Terminal.
  4. Run npm test.

Broken

https://stackblitz.com/edit/github-owtwqh-82agcd?file=nuxt.config.ts,modules%2Fvite-plugin-vue-type-imports.ts,app.vue

Load a bundled Nuxt module from the node_modules directory
export default defineNuxtConfig({
  modules: [
    'vite-plugin-vue-type-imports/nuxt',
  ],
});

Working

https://stackblitz.com/edit/github-owtwqh-dyj8f8?file=nuxt.config.ts,modules%2Fvite-plugin-vue-type-imports.ts,app.vue

Load a local Nuxt module from the modules/ directory
export default defineNuxtConfig({
  modules: [
    '~/modules/vite-plugin-vue-type-imports.ts',
  ],
});

Also, example with defineNuxtModule():

https://stackblitz.com/edit/github-owtwqh-b4r36h?file=nuxt.config.ts,modules%2Fvite-plugin-vue-type-imports.ts,app.vue

Zolyn commented 1 year ago

I'm not familiar with Nuxt. I tried to build the plugin in the latest commit. Fortunately, the test passed. So it might have something to do with the version of tsup.

It will be fixed in the next release.

mrauhu commented 1 year ago

By default, the vite-plugin-vue-type-imports/nuxt module works in Nuxt 3. But, it doesn't work with test via Vitest and @nuxt/test-utils-edge.

Zolyn commented 1 year ago

It should be fixed in v0.2.1