Skip to content

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

Merged
danielroe merged 19 commits into
mainfrom
feat/modules-dir
Mar 3, 2023
Merged

feat(nuxt): auto-register modules in ~/modules#19394
danielroe merged 19 commits into
mainfrom
feat/modules-dir

Conversation

@danielroe

@danielroe danielroe commented Mar 2, 2023

Copy link
Copy Markdown
Member

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

@danielroe danielroe added ✨ enhancement New feature or improvement to existing functionality 3.x 🍰 p2-nice-to-have Nice to have, not blocking labels Mar 2, 2023
@danielroe danielroe requested a review from atinux March 2, 2023 15:36
@danielroe danielroe self-assigned this Mar 2, 2023
@codesandbox

codesandbox Bot commented Mar 2, 2023

Copy link
Copy Markdown

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

Comment thread docs/2.guide/2.directory-structure/1.modules.md Outdated
Comment thread packages/nuxt/src/core/nuxt.ts Outdated

@pi0 pi0 left a comment

Copy link
Copy Markdown
Member

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

@Baroshem Baroshem left a comment

Copy link
Copy Markdown
Contributor

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

atinux commented Mar 3, 2023

Copy link
Copy Markdown
Member

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

atinux commented Mar 3, 2023

Copy link
Copy Markdown
Member

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

pi0 commented Mar 3, 2023

Copy link
Copy Markdown
Member

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.

Comment thread packages/nuxt/src/core/nuxt.ts Outdated
@atinux

atinux commented Mar 3, 2023

Copy link
Copy Markdown
Member

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

pi0 commented Mar 3, 2023

Copy link
Copy Markdown
Member

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

Co-authored-by: pooya parsa <pyapar@gmail.com>
Comment thread docs/2.guide/2.directory-structure/1.modules.md Outdated
Comment thread test/fixtures/basic/modules/auto-registered/index.ts Outdated
Comment thread test/fixtures/basic/modules/example.ts Outdated
Comment thread packages/nuxt/src/core/nuxt.ts Outdated
Comment thread packages/nuxt/src/core/nuxt.ts Outdated
@HigherOrderLogic

Copy link
Copy Markdown
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

pi0 commented Mar 3, 2023

Copy link
Copy Markdown
Member

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.

Comment thread packages/nuxt/src/core/nuxt.ts Outdated
@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
@atinux

atinux commented Mar 4, 2023

Copy link
Copy Markdown
Member

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

@atinux

atinux commented Mar 4, 2023

Copy link
Copy Markdown
Member

Might be worth adding to the docs?

@summer-0609

Copy link
Copy Markdown

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

@danielroe

Copy link
Copy Markdown
Member Author

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

@ineshbose

Copy link
Copy Markdown
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!)

@danielroe

danielroe commented May 6, 2023

Copy link
Copy Markdown
Member Author

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

Labels

3.x ✨ enhancement New feature or improvement to existing functionality 🍰 p2-nice-to-have Nice to have, not blocking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants