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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/2.guide/2.directory-structure/1.modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
navigation.icon: IconDirectory
title: 'modules'
head.title: 'modules/'
description: Use the modules/ directory to automatically register local modules within your application.
---

# Modules Directory

Nuxt automatically reads the top-level files in your `modules/` directory and loads them before starting. It is a good place to place any local modules you develop while building your application.

These files are auto-registered, so you do not need to add them to your `nuxt.config` separately.

::alert{type=warning}
Only files at the top level of the `modules/` directory will be automatically registered.
::

:ReadMore{link="/docs/guide/going-further/modules"}
8 changes: 7 additions & 1 deletion packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join, normalize, resolve } from 'pathe'
import { createHooks, createDebugger } from 'hookable'
import type { LoadNuxtOptions } from '@nuxt/kit'
import { loadNuxtConfig, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule, addPlugin } from '@nuxt/kit'
import { resolveFiles, loadNuxtConfig, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule, addPlugin } from '@nuxt/kit'

import escapeRE from 'escape-string-regexp'
import fse from 'fs-extra'
Expand Down Expand Up @@ -127,6 +127,12 @@ async function initNuxt (nuxt: Nuxt) {
...nuxt.options._modules
]

// Automatically register user modules
for (const config of nuxt.options._layers.map(layer => layer.config)) {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
const userModules = await resolveFiles(config.srcDir, `${config.dir?.modules || 'modules'}/*{${nuxt.options.extensions.join(',')}}`)
modulesToInstall.push(...userModules)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
}

// Add <NuxtWelcome>
addComponent({
name: 'NuxtWelcome',
Expand Down
5 changes: 5 additions & 0 deletions packages/schema/src/config/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ export default defineUntypedSchema({
*/
middleware: 'middleware',

/**
* The modules directory, each file in which will be auto-registered as a Nuxt module.
*/
modules: 'modules',

/**
* The directory which will be processed to auto-generate your application page routes.
*/
Expand Down
7 changes: 7 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ describe('route rules', () => {
})
})

describe('modules', () => {
it('should auto-register modules in ~/modules', async () => {
const result = await $fetch('/auto-registered-module')
expect(result).toEqual('handler added by auto-registered module')
})
})

describe('pages', () => {
it('render index', async () => {
const html = await $fetch('/')
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/basic/modules/auto-registered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createResolver, defineNuxtModule, useNuxt } from '@nuxt/kit'

export default defineNuxtModule({
meta: {
name: 'auto-registered-module'
},
setup () {
const nuxt = useNuxt()
const resolver = createResolver(import.meta.url)

nuxt.options.nitro.handlers = nuxt.options.nitro.handlers || []
nuxt.options.nitro.handlers.push({
handler: resolver.resolve('./runtime/handler'),
route: '/auto-registered-module'
})
}
})
1 change: 1 addition & 0 deletions test/fixtures/basic/modules/runtime/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default defineEventHandler(() => 'handler added by auto-registered module')