From 9ef94d19f733eca5f457eb30f29d2745152092ce Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 27 Jan 2022 18:02:42 +0100 Subject: [PATCH] docs: add advanced section for module authors (#2950) --- docs/content/3.docs/1.usage/4.nuxt-app.md | 36 --- .../3.docs/2.directory-structure/8.modules.md | 98 -------- docs/content/3.docs/4.advanced/1.nuxt.md | 77 +++++++ docs/content/3.docs/4.advanced/2.modules.md | 214 ++++++++++++++++++ docs/content/3.docs/4.advanced/3.kit.md | 124 ++++++++++ docs/content/3.docs/4.advanced/4.hooks.md | 63 ++++++ 6 files changed, 478 insertions(+), 134 deletions(-) delete mode 100644 docs/content/3.docs/2.directory-structure/8.modules.md create mode 100644 docs/content/3.docs/4.advanced/1.nuxt.md create mode 100644 docs/content/3.docs/4.advanced/2.modules.md create mode 100644 docs/content/3.docs/4.advanced/3.kit.md create mode 100644 docs/content/3.docs/4.advanced/4.hooks.md diff --git a/docs/content/3.docs/1.usage/4.nuxt-app.md b/docs/content/3.docs/1.usage/4.nuxt-app.md index 090498cbf4..a5b9a63fa4 100644 --- a/docs/content/3.docs/1.usage/4.nuxt-app.md +++ b/docs/content/3.docs/1.usage/4.nuxt-app.md @@ -39,39 +39,3 @@ In Nuxt 2 plugins, this was referred to as [inject function](https://nuxtjs.org/ ::alert{icon=👉} It is possible to inject helpers by returning an object with a `provide` key. See the [plugins documentation](/docs/directory-structure/plugins) for more information. :: - -## NuxtApp interface (advanced) - -`nuxtApp` has the following properties: (note: this is an internal interface and some properties might change until stable release) - -```js -const nuxtApp = { - vueApp, // the global Vue application: https://v3.vuejs.org/api/application-api.html - - // These let you call and add runtime NuxtApp hooks - // https://github.com/nuxt/framework/blob/main/packages/nuxt3/src/app/nuxt.ts#L18 - hooks, - hook, - callHook, - - // Only accessible on server-side - ssrContext: { - url, - req, - res, - runtimeConfig, - noSSR, - }, - - // This will be stringified and passed from server to client - payload: { - serverRendered: true, - data: {}, - state: {} - } - - provide: (name: string, value: any) => void -} -``` - -For more information, check out [the source code](https://github.com/nuxt/framework/blob/main/packages/nuxt3/src/app/nuxt.ts#L28-L53). diff --git a/docs/content/3.docs/2.directory-structure/8.modules.md b/docs/content/3.docs/2.directory-structure/8.modules.md deleted file mode 100644 index ece052b5d5..0000000000 --- a/docs/content/3.docs/2.directory-structure/8.modules.md +++ /dev/null @@ -1,98 +0,0 @@ ---- -icon: IconDirectory -title: 'modules' -navigation: false -head.title: Local modules directory ---- - -# Local modules directory - -Nuxt has a powerful modules engine. - -# Creating Modules - -Nuxt provides helper functions (accessed from `@nuxt/kit`) to assist in creating modules that can run on both Nuxt 2 and Nuxt 3. - -```js -import { defineNuxtModule } from '@nuxt/kit' - -export default defineNuxtModule({ - meta: { - // The npm package name of your module - name: '@nuxtjs/sample-module', - // The key in `nuxt.config` that holds your module options - configKey: 'sample', - }, - // Default configuration options for your module - defaults: {}, - hooks: {}, - setup (options, nuxt) {}, -}) -``` - -## Module options - -Nuxt will automatically merge the configuration provided to your module (whether passed directly inline or in a configuration section in `nuxt.config`) with the defaults you provide. - -```js -export default { - buildModules: [ - ['@nuxtjs/sample-module', { sampleOption: true }] - ], - sample: { - anotherOption: 42 - } -} -``` - -## Hooks - -For simple modules, you may be able to implement all you need simply with several hooks in this section. - -## Setup - -If the `hooks` configuration is not enough, you can provide a full setup function, with access to `nuxt`. You will no longer have access to `this` as in the previous Nuxt module specification, but there are dedicated helper functions from `@nuxt/kit` to replace the module container methods from previously. - -```js -import { defineNuxtModule, addPlugin } from '@nuxt/kit' - -export default defineNuxtModule({ - // ... - setup (options, nuxt) { - addPlugin({ - src: path.resolve(__dirname, 'templates/foo.js') - }) - }, -}) -``` - -## Advanced usage - -In some cases you may need access to the Nuxt instance in order to define your module, for example, using other Nuxt options: - -```js -import { defineNuxtModule } from '@nuxt/kit' - -export default defineNuxtModule(nuxt => ({ - // ... - defaults: { - root: nuxt.options.rootDir - }, -})) -``` - -## Module helpers - -A number of helpers are also provided for use within this context (with more coming - feature requests welcome) that ensure compatible behavior across Nuxt versions. - -* addTemplate -* addErrorLayout -* addLayout -* addPlugin -* addServerMiddleware -* extendBuild -* extendRoutes - -Each of these functions provides documentation via IDE hover/autocomplete. - -## Publishing to NPM diff --git a/docs/content/3.docs/4.advanced/1.nuxt.md b/docs/content/3.docs/4.advanced/1.nuxt.md new file mode 100644 index 0000000000..1498fc8395 --- /dev/null +++ b/docs/content/3.docs/4.advanced/1.nuxt.md @@ -0,0 +1,77 @@ +# Nuxt Internals + +Nuxt is a minimal but highly customizable framework to build web applications. This guide helps you better understand Nuxt internals to develop new solutions and module integrations on top of Nuxt. + +## The Nuxt interface + +When you start nuxt in development mode with `nuxi dev` or building a production application with `nuxi build`, +a common context will be created, referred to as `nuxt` internally. It holds normalized options merged with `nuxt.config` file, +some internal state, and a powerful [hooking system](/docs/advanced/hooks) powered by [unjs/hookable](https://github.com/unjs/hookable) +allowing different components to communicate with each other. You can think of it as **Builder Core**. + +This context is globally available to be used with [nuxt/kit](/docs/advanced/kit) composables. +Therefore only one instance of Nuxt is allowed to run per process. + +To extend the Nuxt interface and hook into different stages of the build process, we can use [Nuxt Modules](/docs/advanced/modules). + +For more details, check out [the source code](https://github.com/nuxt/framework/blob/main/packages/nuxt3/src/core/nuxt.ts). + +## The NuxtApp interface + +When rendering a page in Browser or server-side, a shared context will be created, referred to as `nuxtApp`. +This context keeps vue instance, runtime hooks, and internal states like ssrContext and payload for hydration. +You can think of it as **Runtime Core**. + +This context can be accessed using `useNuxtApp()` composable within nuxt plugins and `