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(config): allow ws config #1249

Merged
merged 3 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 37 additions & 12 deletions docs/content/4.api/3.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default defineNuxtConfig({
})
```


## `base`

- Type: `String`{lang=ts}
Expand All @@ -34,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`

Expand Down Expand Up @@ -179,7 +204,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. |
Expand Down
2 changes: 0 additions & 2 deletions playground/pages/sandbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
</div>
</template>
</ContentDoc>

<ContentList />
</div>
</template>
21 changes: 15 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -48,7 +49,9 @@ export interface ModuleOptions {
*
* @default true
*/
watch: boolean
watch: false | {
ws: Partial<ListenOptions>
}
/**
* 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.
Expand Down Expand Up @@ -180,7 +183,12 @@ export default defineNuxtModule<ModuleOptions>({
},
defaults: {
base: '_content',
watch: true,
watch: {
ws: {
port: 4000,
showURL: false
}
},
sources: ['content'],
ignores: ['\\.', '-'],
locales: [],
Expand Down Expand Up @@ -403,11 +411,11 @@ export default defineNuxtModule<ModuleOptions>({
}

// 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
Expand All @@ -416,7 +424,8 @@ export default defineNuxtModule<ModuleOptions>({
})

// Listen dev server
const { server, url } = await listen(() => 'Nuxt Content', { port: 4000, showURL: false })
const { server, url } = await listen(() => 'Nuxt Content', options.watch.ws)

server.on('upgrade', ws.serve)

// Register ws url
Expand Down