@@ -11,6 +11,7 @@ import {
11
11
updateTemplates ,
12
12
addComponent ,
13
13
installModule ,
14
+ addVitePlugin ,
14
15
} from '@nuxt/kit'
15
16
import type { Nuxt } from '@nuxt/schema'
16
17
import type { ModuleOptions as MDCModuleOptions } from '@nuxtjs/mdc'
@@ -24,7 +25,7 @@ import { generateCollectionInsert, generateCollectionTableDefinition } from './u
24
25
import { componentsManifestTemplate , contentTypesTemplate , fullDatabaseRawDumpTemplate , manifestTemplate , moduleTemplates } from './utils/templates'
25
26
import type { ResolvedCollection } from './types/collection'
26
27
import type { ModuleOptions } from './types/module'
27
- import { getContentChecksum , logger , watchContents , chunks , watchComponents , startSocketServer } from './utils/dev'
28
+ import { getContentChecksum , logger , chunks , NuxtContentHMRUnplugin } from './utils/dev'
28
29
import { loadContentConfig } from './utils/config'
29
30
import { createParser } from './utils/content'
30
31
import { installMDCModule } from './utils/mdc'
@@ -53,15 +54,7 @@ export default defineNuxtModule<ModuleOptions>({
53
54
filename : '.data/content/contents.sqlite' ,
54
55
} ,
55
56
preview : { } ,
56
- watch : {
57
- enabled : true ,
58
- port : {
59
- port : 4000 ,
60
- portRange : [ 4000 , 4040 ] ,
61
- } ,
62
- hostname : 'localhost' ,
63
- showURL : false ,
64
- } ,
57
+ watch : { enabled : true } ,
65
58
renderer : {
66
59
alias : { } ,
67
60
anchorLinks : {
@@ -96,12 +89,12 @@ export default defineNuxtModule<ModuleOptions>({
96
89
// Detect installed validators and them into content context
97
90
await initiateValidatorsContext ( )
98
91
99
- const { collections } = await loadContentConfig ( nuxt )
92
+ const { collections } = await loadContentConfig ( nuxt , options )
100
93
manifest . collections = collections
101
94
102
- nuxt . options . vite . optimizeDeps ||= { }
103
- nuxt . options . vite . optimizeDeps . exclude ||= [ ]
104
- nuxt . options . vite . optimizeDeps . exclude . push ( '@sqlite.org/sqlite-wasm' )
95
+ nuxt . options . vite . optimizeDeps = defu ( nuxt . options . vite . optimizeDeps , {
96
+ exclude : [ '@sqlite.org/sqlite-wasm' ] ,
97
+ } )
105
98
106
99
// Ignore content directory files in building
107
100
nuxt . options . ignore = [ ...( nuxt . options . ignore || [ ] ) , 'content/**' ]
@@ -122,16 +115,18 @@ export default defineNuxtModule<ModuleOptions>({
122
115
addComponent ( { name : 'ContentRenderer' , filePath : resolver . resolve ( './runtime/components/ContentRenderer.vue' ) } )
123
116
124
117
// Add Templates & aliases
125
- nuxt . options . nitro . alias = nuxt . options . nitro . alias || { }
126
118
addTemplate ( fullDatabaseRawDumpTemplate ( manifest ) )
127
- nuxt . options . alias [ '#content/components' ] = addTemplate ( componentsManifestTemplate ( manifest ) ) . dst
128
- nuxt . options . alias [ '#content/manifest' ] = addTemplate ( manifestTemplate ( manifest ) ) . dst
119
+ nuxt . options . alias = defu ( nuxt . options . alias , {
120
+ '#content/components' : addTemplate ( componentsManifestTemplate ( manifest ) ) . dst ,
121
+ '#content/manifest' : addTemplate ( manifestTemplate ( manifest ) ) . dst ,
122
+ } )
129
123
130
124
// Add content types to Nuxt and Nitro
131
125
const typesTemplateDst = addTypeTemplate ( contentTypesTemplate ( manifest . collections ) ) . dst
132
- nuxt . options . nitro . typescript ||= { }
133
- nuxt . options . nitro . typescript . tsConfig = defu ( nuxt . options . nitro . typescript . tsConfig , {
134
- include : [ typesTemplateDst ] ,
126
+ nuxt . options . nitro . typescript = defu ( nuxt . options . nitro . typescript , {
127
+ tsConfig : {
128
+ include : [ typesTemplateDst ] ,
129
+ } ,
135
130
} )
136
131
137
132
// Register user components
@@ -195,11 +190,13 @@ export default defineNuxtModule<ModuleOptions>({
195
190
} )
196
191
197
192
// Handle HMR changes
198
- if ( nuxt . options . dev ) {
193
+ if ( nuxt . options . dev && options . watch ?. enabled !== false ) {
199
194
addPlugin ( { src : resolver . resolve ( './runtime/plugins/websocket.dev' ) , mode : 'client' } )
200
- await watchComponents ( nuxt )
201
- const socket = await startSocketServer ( nuxt , options , manifest )
202
- await watchContents ( nuxt , options , manifest , socket )
195
+ addVitePlugin ( NuxtContentHMRUnplugin . vite ( {
196
+ nuxt,
197
+ moduleOptions : options ,
198
+ manifest,
199
+ } ) )
203
200
}
204
201
} )
205
202
0 commit comments