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

feat(nuxt): auto-register modules in ~/modules #19394

Merged
merged 19 commits into from Mar 3, 2023
Merged

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Mar 2, 2023

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This adds support for scanning ~/modules and automatically registering any local modules there. The directory is configurable via dir.modules and supports local modules in layers.

It also adds support for automatically restarting Nuxt when these local module files are updated, resolving #12350. Note that at the moment, they are cached and a soft restart does not load an updated module - we should likely investigate the cause of this so we do not need a hard restart in this case.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link

codesandbox bot commented Mar 2, 2023

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

packages/nuxt/src/core/nuxt.ts Outdated Show resolved Hide resolved
Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea! As a complement we might also introduce nuxt/kit since users have only nuxt dependency.

@danielroe danielroe added this to the v3.3 milestone Mar 2, 2023
Copy link
Contributor

@Baroshem Baroshem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome feature idea!

I can easily see using it in my projects :)

@Atinux
Copy link
Member

Atinux commented Mar 3, 2023

I would like to update the behaviour to scanning ~/modules/<module-name>/module.ts so user can have the same structure as https://github.com/nuxt/starter/tree/module/src

Also, modules tends to include files so a directory is better for separation of concerns.

This way it's easier to extract when then want to publish their package to the Nuxt community

@Atinux
Copy link
Member

Atinux commented Mar 3, 2023

Well actually I would prefer~/modules/<module-name>/index.ts which makes the most sense.

Why did we decide to go with module.ts on https://github.com/nuxt/starter/tree/module/src ?

@pi0
Copy link
Member

pi0 commented Mar 3, 2023

For module builder package template, top-level index is reserved for util exports.

BTW for local scanning, {name}/{index,module}.ts seems a good idea since it is not a package with such reservations.

packages/nuxt/src/core/nuxt.ts Outdated Show resolved Hide resolved
Copy link
Member

Atinux commented Mar 3, 2023

I will try to keep it simple to modules/{name}/index.ts then 😊

Explaining to rename to module.ts when packaging in order to let them export utils makes perfect sense.

I guess we could also support top-level file for very simple modules?

@pi0
Copy link
Member

pi0 commented Mar 3, 2023

I'm okay with either convention in local dir. Each has cons and pros.

Co-authored-by: pooya parsa <pyapar@gmail.com>
packages/nuxt/src/core/nuxt.ts Outdated Show resolved Hide resolved
packages/nuxt/src/core/nuxt.ts Outdated Show resolved Hide resolved
@HigherOrderLogic
Copy link
Contributor

I'm against this idea. Although auto registering modules inside ~/modules folder is convenient, not everytime the developer would want to load the modules in alphabetical order.

@pi0
Copy link
Member

pi0 commented Mar 3, 2023

not everytime the developer would want to load the modules in alphabetical order.

I fully agree, and because of that, when you manually specify them in modules[] in configuration, they will be kept in your custom order.

packages/nuxt/src/core/nuxt.ts Outdated Show resolved Hide resolved
@danielroe danielroe requested a review from pi0 March 3, 2023 17:27
@danielroe danielroe merged commit 3681bdd into main Mar 3, 2023
@danielroe danielroe deleted the feat/modules-dir branch March 3, 2023 17:52
Copy link
Member

Atinux commented Mar 4, 2023

@HigherOrderLogic well you can do:

modules/
  1.c-module/
  2.d-module/

Similar to what we have in Nuxt Content for ordering links in the generated navigation.

Copy link
Member

Atinux commented Mar 4, 2023

Might be worth adding to the docs?

@summer-0609
Copy link

How to ignore local modules that I don't want to be automatically registered

Copy link
Member Author

You can use a different directory, nest them in a folder under ~/modules, or change dir.modules.

@ineshbose
Copy link
Member

Regarding ordering, from the PR I understand that local modules load after nuxt.options.modules (maybe need to mention that in the docs) - is there a possibility to have one before (like the first loaded module possibly)? Not sure if adding the local module path to nuxt.options.modules could double-load it (if in dir.modules).

How to ignore local modules that I don't want to be automatically registered

We can also add functionality for prepending filename with . to ignore through a PR you think? πŸ™‚

actually I would prefer~/modules//index.ts which makes the most sense.

The thing (for me at least) was to have consistent file-scanning in Nuxt (like #18418) because when the behaviour is different, it tends to confuse (me at least); though this PR has been long-released now. πŸ˜…

(Sorry for continuing the thread!)

Copy link
Member Author

danielroe commented May 6, 2023

I think we already respect current ignore option when scanning modules.

And if local modules are added to nuxt.options.modules then they will run at that point and not be rerun later on (just like with nuxt.options.plugins.

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

Successfully merging this pull request may close these issues.

None yet

9 participants