Skip to content
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

Nested module's 'modules:done' doesn't work like expected #947

Open
AndreyYolkin opened this issue Oct 28, 2023 · 5 comments
Open

Nested module's 'modules:done' doesn't work like expected #947

AndreyYolkin opened this issue Oct 28, 2023 · 5 comments

Comments

@AndreyYolkin
Copy link
Contributor

AndreyYolkin commented Oct 28, 2023

Environment


  • Operating System: Linux
  • Node Version: v18.18.0
  • Nuxt Version: 2.17.1
  • CLI Version: 3.9.1
  • Nitro Version: 2.7.1
  • Package Manager: npm@9.4.2
  • Builder: webpack
  • User Config: modules, serverHandlers, devServerHandlers, devServer, typescript, nitro, bridge, buildModules
  • Runtime Modules: ~/modules/nested
  • Build Modules: (), @nuxt/bridge@3.0.0-rc.2-28307913.ff6077a

Reproduction

Nuxt 2b:

Direct module install (working as expected):

https://stackblitz.com/edit/github-dwx287-kpyejp?file=nuxt.config.ts

Sync installModule (broken):

https://stackblitz.com/edit/github-dwx287-eqecsd?file=nuxt.config.ts

Async installModule (working as a little bit unexpected):

https://stackblitz.com/edit/github-dwx287-7mgh1y?file=nuxt.config.ts

Nuxt 3:

Sync installModule (working as expected):

https://stackblitz.com/edit/github-dwx287-ogyhr8?file=nuxt.config.ts

Describe the bug

Current behaviour is not fully aligned with nuxt 3. According to this: https://github.com/vuejs/pinia/blob/1151e67f5eb2f5221273f9a8566e3e5ba9154a1e/packages/nuxt/src/module.ts#L78-L84

Pinia module relies on 'modules:done' hook, which is fired after all modules were installed. @pinia/nuxt's setup() is also synchronous.

I expect pinia to add needed plugins when calling sync installModule function, as it works in nuxt 3 or in direct usage. I got pinia undefined, because module didn't receive 'modules:done' hook

Additional context

No response

Logs

No response

@AndreyYolkin AndreyYolkin changed the title Nested modules 'modules:done' doesn't work like expected Nested module's 'modules:done' doesn't work like expected Oct 28, 2023
@wattanx
Copy link
Collaborator

wattanx commented Oct 29, 2023

With async await it seems to be working as expected.
https://stackblitz.com/edit/github-dwx287-8jrhpi

import { defineNuxtModule, installModule } from '@nuxt/kit';

export default defineNuxtModule({
  meta: {
    name: 'pinia-installer',
    compatibility: {
      nuxt: '^2.17.0 || ^3.0.0',
      bridge: true,
    },
  },
  // add async
  async setup() {
    // add await
    await installModule('@pinia/nuxt');
  },
});

@AndreyYolkin
Copy link
Contributor Author

It's not clear, because nested module setup in sync. This is why I provided 4 different setups above: similar code without async/await works in Nuxt 3 and should work in Nuxt 2b too, I suppose.

I've created another example where nested module have only one hook: https://stackblitz.com/edit/github-dwx287-swerdg?file=nuxt.config.ts,modules%2Fsecondary%2Findex.ts,modules%2Fprimary%2Findex.ts

I still think we should align the behaviour with nuxt 3 (To compare: https://stackblitz.com/edit/github-dwx287-vi8c7r?file=nuxt.config.ts,modules%2Fsecondary%2Findex.ts,modules%2Fprimary%2Findex.ts)

@wattanx
Copy link
Collaborator

wattanx commented Oct 29, 2023

Oh, sorry. I didn't realize there was already a reproduction 🙏
It may be difficult to adapt the processing of modules to Nuxt 3, as it would require modifications to Nuxt 2 itself.
(If this is a problem with the behavior of modules)

@AndreyYolkin
Copy link
Contributor Author

AndreyYolkin commented Oct 29, 2023

It's ok) I didn't go deep inside Nuxt internals to realise the causes of this behaviour. I can suggest to label this issue with workaround available until future investigations

@AndreyYolkin
Copy link
Contributor Author

I made a small research and I can declare this is not fully fixable.

Nuxt 3 utilises kit's installModule hook for each module which makes possible to use any nested installModule possible: https://github.com/nuxt/nuxt/blob/7cce0ef6e6e0733b5bdee368e8015d955623df19/packages/nuxt/src/core/nuxt.ts#L385

However, Nuxt 2 relies on its own addModule implementation. so even with https://github.com/nuxt/nuxt/blob/5eb1b32f62a0ad92bfa6f37641489c35caa4b791/packages/core/src/module.js#L22 so even if we push nested module in nuxt.options._modules inside kit, next nested module will lost modules:done hook again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants