@@ -11,6 +11,7 @@ import {
1111 updateTemplates ,
1212 addComponent ,
1313 installModule ,
14+ addVitePlugin ,
1415} from '@nuxt/kit'
1516import type { Nuxt } from '@nuxt/schema'
1617import type { ModuleOptions as MDCModuleOptions } from '@nuxtjs/mdc'
@@ -24,7 +25,7 @@ import { generateCollectionInsert, generateCollectionTableDefinition } from './u
2425import { componentsManifestTemplate , contentTypesTemplate , fullDatabaseRawDumpTemplate , manifestTemplate , moduleTemplates } from './utils/templates'
2526import type { ResolvedCollection } from './types/collection'
2627import 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'
2829import { loadContentConfig } from './utils/config'
2930import { createParser } from './utils/content'
3031import { installMDCModule } from './utils/mdc'
@@ -53,15 +54,7 @@ export default defineNuxtModule<ModuleOptions>({
5354 filename : '.data/content/contents.sqlite' ,
5455 } ,
5556 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 } ,
6558 renderer : {
6659 alias : { } ,
6760 anchorLinks : {
@@ -96,12 +89,12 @@ export default defineNuxtModule<ModuleOptions>({
9689 // Detect installed validators and them into content context
9790 await initiateValidatorsContext ( )
9891
99- const { collections } = await loadContentConfig ( nuxt )
92+ const { collections } = await loadContentConfig ( nuxt , options )
10093 manifest . collections = collections
10194
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+ } )
10598
10699 // Ignore content directory files in building
107100 nuxt . options . ignore = [ ...( nuxt . options . ignore || [ ] ) , 'content/**' ]
@@ -122,16 +115,18 @@ export default defineNuxtModule<ModuleOptions>({
122115 addComponent ( { name : 'ContentRenderer' , filePath : resolver . resolve ( './runtime/components/ContentRenderer.vue' ) } )
123116
124117 // Add Templates & aliases
125- nuxt . options . nitro . alias = nuxt . options . nitro . alias || { }
126118 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+ } )
129123
130124 // Add content types to Nuxt and Nitro
131125 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+ } ,
135130 } )
136131
137132 // Register user components
@@ -195,11 +190,13 @@ export default defineNuxtModule<ModuleOptions>({
195190 } )
196191
197192 // Handle HMR changes
198- if ( nuxt . options . dev ) {
193+ if ( nuxt . options . dev && options . watch ?. enabled !== false ) {
199194 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+ } ) )
203200 }
204201 } )
205202
0 commit comments