From e9a1f48485a1bad49248721c265ae0d2dc42b531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20Guilloux?= Date: Mon, 13 Jun 2022 14:18:47 +0200 Subject: [PATCH 1/3] feat(config): allow ws config --- docs/content/4.api/3.configuration.md | 14 ++++++++++++-- src/module.ts | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/docs/content/4.api/3.configuration.md b/docs/content/4.api/3.configuration.md index 2908b95da..6b5b15e5e 100644 --- a/docs/content/4.api/3.configuration.md +++ b/docs/content/4.api/3.configuration.md @@ -16,7 +16,6 @@ export default defineNuxtConfig({ }) ``` - ## `base` - Type: `String`{lang=ts} @@ -179,7 +178,7 @@ Nuxt Content uses [Shiki](https://github.com/shikijs/shiki) to provide syntax hi ### `highlight` options -| Option | Default | Description | +| Option | Type | Description | | ----------------- | :--------: | :-------- | | `theme` | `ShikiTheme` | The [color theme](https://github.com/shikijs/shiki/blob/main/docs/themes.md) to use | | `preload` | `ShikiLang[]` | The [preloaded languages](https://github.com/shikijs/shiki/blob/main/docs/languages.md) available for highlighting. | @@ -211,3 +210,14 @@ List of locale codes. This codes will be used to detect contents locale. - Default: `undefined`{lang=ts} Default locale for top level contents. Module will use first locale code from `locales` array if this option is not defined. + +## `ws` + +Nuxt Content uses a WebSocket server in development to allow hot reloading of your content files. + +### `ws` options + +| Option | Default | Description | +| ----------------- | :--------: | :-------- | +| `port` | `4000` | Select the port used for the WebSocket server. | +| `showUrl` | `false` | Toggle URL display in dev server boot message. | diff --git a/src/module.ts b/src/module.ts index dfbeaa392..8e19f92ec 100644 --- a/src/module.ts +++ b/src/module.ts @@ -158,6 +158,19 @@ export interface ModuleOptions { * @default undefined */ defaultLocale: string + /** + * WebSocket server configuration. + */ + ws: { + /** + * @default 4000 + */ + port: number + /** + * @default false + */ + showUrl: boolean + } } interface ContentContext extends ModuleOptions { @@ -181,6 +194,10 @@ export default defineNuxtModule({ defaults: { base: '_content', watch: true, + ws: { + port: 4000, + showUrl: false + }, sources: ['content'], ignores: ['\\.', '-'], locales: [], @@ -416,7 +433,7 @@ export default defineNuxtModule({ }) // Listen dev server - const { server, url } = await listen(() => 'Nuxt Content', { port: 4000, showURL: false }) + const { server, url } = await listen(() => 'Nuxt Content', options.ws) server.on('upgrade', ws.serve) // Register ws url From d1d40e8e2a59181f2bea228af3bbbb5be9a13979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20Guilloux?= Date: Mon, 13 Jun 2022 17:59:07 +0200 Subject: [PATCH 2/3] feat(ws): allow ws configuration from `watch` key --- docs/content/4.api/3.configuration.md | 57 +++++++++++++++++---------- playground/content/_test.md | 4 ++ playground/pages/sandbox.vue | 2 - src/module.ts | 36 +++++++---------- 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/docs/content/4.api/3.configuration.md b/docs/content/4.api/3.configuration.md index 6b5b15e5e..f2210f3c5 100644 --- a/docs/content/4.api/3.configuration.md +++ b/docs/content/4.api/3.configuration.md @@ -33,18 +33,44 @@ export default defineNuxtConfig({ ## `watch` -- Type: `Boolean`{lang=ts} -- Default: `true`{lang=ts} +- Type: `Object | false`{lang=ts} +- Default: `{ port: 4000, showUrl: true }`{lang=ts} -Disable content watcher and hot content reload. Watcher is a development feature and will not includes in the production. +Disable content watcher and hot content reload. -```ts [nuxt.config.ts] -export default defineNuxtConfig({ - content: { - watch: true - } -}) -``` +The watcher is a development feature and will not be included in production. + +::code-group + + ```ts [Enabled] + export default defineNuxtConfig({ + content: { + watch: { + ws: { + port: 4000, + showUrl: true + } + } + } + }) + ``` + + ```ts [Disabled] + export default defineNuxtConfig({ + content: { + watch: false + } + }) + ``` + +:: + +### `ws` options + +| Option | Default | Description | +| ----------------- | :--------: | :-------- | +| `port` | `4000` | Select the port used for the WebSocket server. | +| `showUrl` | `false` | Toggle URL display in dev server boot message. | ## `sources` @@ -210,14 +236,3 @@ List of locale codes. This codes will be used to detect contents locale. - Default: `undefined`{lang=ts} Default locale for top level contents. Module will use first locale code from `locales` array if this option is not defined. - -## `ws` - -Nuxt Content uses a WebSocket server in development to allow hot reloading of your content files. - -### `ws` options - -| Option | Default | Description | -| ----------------- | :--------: | :-------- | -| `port` | `4000` | Select the port used for the WebSocket server. | -| `showUrl` | `false` | Toggle URL display in dev server boot message. | diff --git a/playground/content/_test.md b/playground/content/_test.md index 66ef82ff4..568bf1d05 100644 --- a/playground/content/_test.md +++ b/playground/content/_test.md @@ -5,3 +5,7 @@ navigation: false # Hello World! This is a test page! + +Hello + +HEllo hello diff --git a/playground/pages/sandbox.vue b/playground/pages/sandbox.vue index e06fd2925..5f954037a 100644 --- a/playground/pages/sandbox.vue +++ b/playground/pages/sandbox.vue @@ -22,7 +22,5 @@ - - diff --git a/src/module.ts b/src/module.ts index 8e19f92ec..15531e0b7 100644 --- a/src/module.ts +++ b/src/module.ts @@ -10,6 +10,7 @@ import { useLogger, addTemplate } from '@nuxt/kit' +import type { ListenOptions } from 'listhen' // eslint-disable-next-line import/no-named-as-default import defu from 'defu' import { hash } from 'ohash' @@ -48,7 +49,9 @@ export interface ModuleOptions { * * @default true */ - watch: boolean + watch: false | { + ws: Partial + } /** * Contents can located in multiple places, in multiple directories or even in remote git repositories. * Using sources option you can tell Content module where to look for contents. @@ -158,19 +161,6 @@ export interface ModuleOptions { * @default undefined */ defaultLocale: string - /** - * WebSocket server configuration. - */ - ws: { - /** - * @default 4000 - */ - port: number - /** - * @default false - */ - showUrl: boolean - } } interface ContentContext extends ModuleOptions { @@ -193,10 +183,11 @@ export default defineNuxtModule({ }, defaults: { base: '_content', - watch: true, - ws: { - port: 4000, - showUrl: false + watch: { + ws: { + port: 4000, + showURL: false + } }, sources: ['content'], ignores: ['\\.', '-'], @@ -420,11 +411,11 @@ export default defineNuxtModule({ } // Setup content dev module - if (!nuxt.options.dev || !options.watch) { - return - } + if (!nuxt.options.dev) { return } nuxt.hook('nitro:init', async (nitro) => { + if (!options.watch || !options.watch.ws) { return } + const ws = createWebSocket() // Dispose storage on nuxt close @@ -433,7 +424,8 @@ export default defineNuxtModule({ }) // Listen dev server - const { server, url } = await listen(() => 'Nuxt Content', options.ws) + const { server, url } = await listen(() => 'Nuxt Content', options.watch.ws) + server.on('upgrade', ws.serve) // Register ws url From e97622c1eac4cd9fc4660edb8db458452b4bfcb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20Guilloux?= Date: Mon, 13 Jun 2022 18:00:31 +0200 Subject: [PATCH 3/3] chore(playground): prune playground content --- playground/content/_test.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/playground/content/_test.md b/playground/content/_test.md index 568bf1d05..66ef82ff4 100644 --- a/playground/content/_test.md +++ b/playground/content/_test.md @@ -5,7 +5,3 @@ navigation: false # Hello World! This is a test page! - -Hello - -HEllo hello