1
- import { mkdir , stat } from 'node:fs/promises'
1
+ import { stat } from 'node:fs/promises'
2
2
import {
3
3
defineNuxtModule ,
4
4
createResolver ,
@@ -13,15 +13,15 @@ import {
13
13
import type { Nuxt } from '@nuxt/schema'
14
14
import type { ModuleOptions as MDCModuleOptions } from '@nuxtjs/mdc'
15
15
import { hash } from 'ohash'
16
- import { join , dirname , isAbsolute } from 'pathe'
16
+ import { join , isAbsolute } from 'pathe'
17
17
import htmlTags from '@nuxtjs/mdc/runtime/parser/utils/html-tags-list'
18
18
import { kebabCase , pascalCase } from 'scule'
19
19
import defu from 'defu'
20
20
import { version } from '../package.json'
21
21
import { generateCollectionInsert , generateCollectionTableDefinition } from './utils/collection'
22
22
import { componentsManifestTemplate , contentTypesTemplate , fullDatabaseRawDumpTemplate , manifestTemplate , moduleTemplates } from './utils/templates'
23
23
import type { ResolvedCollection } from './types/collection'
24
- import type { ModuleOptions , SqliteDatabaseConfig } from './types/module'
24
+ import type { ModuleOptions } from './types/module'
25
25
import { getContentChecksum , logger , watchContents , chunks , watchComponents , startSocketServer } from './utils/dev'
26
26
import { loadContentConfig } from './utils/config'
27
27
import { createParser } from './utils/content'
@@ -30,7 +30,7 @@ import { findPreset } from './presets'
30
30
import type { Manifest } from './types/manifest'
31
31
import { setupPreview } from './utils/preview/module'
32
32
import { parseSourceBase } from './utils/source'
33
- import { getLocalDatabase , resolveDatabaseAdapter } from './utils/sqlite '
33
+ import { getLocalDatabase , refineDatabaseConfig , resolveDatabaseAdapter } from './utils/database '
34
34
35
35
// Export public utils
36
36
export * from './utils'
@@ -92,16 +92,16 @@ export default defineNuxtModule<ModuleOptions>({
92
92
}
93
93
94
94
// Create local database
95
- options . _localDatabase ! . filename = isAbsolute ( options . _localDatabase ! . filename )
96
- ? options . _localDatabase ! . filename
97
- : join ( nuxt . options . rootDir , options . _localDatabase ! . filename )
98
- await mkdir ( dirname ( options . _localDatabase ! . filename ) , { recursive : true } ) . catch ( ( ) => { } )
95
+ await refineDatabaseConfig ( options . _localDatabase , nuxt )
96
+ if ( options . _localDatabase ?. type === 'sqlite' ) {
97
+ options . _localDatabase ! . filename = isAbsolute ( options . _localDatabase ! . filename )
98
+ ? options . _localDatabase ! . filename
99
+ : join ( nuxt . options . rootDir , options . _localDatabase ! . filename )
100
+ }
99
101
100
102
// Create sql database
101
- if ( ( options . database as SqliteDatabaseConfig ) . filename ) {
102
- ( options . database as SqliteDatabaseConfig ) . filename = ( options . database as SqliteDatabaseConfig ) . filename
103
- await mkdir ( dirname ( ( options . database as SqliteDatabaseConfig ) . filename ) , { recursive : true } ) . catch ( ( ) => { } )
104
- }
103
+ await refineDatabaseConfig ( options . database , nuxt )
104
+
105
105
const { collections } = await loadContentConfig ( nuxt )
106
106
manifest . collections = collections
107
107
@@ -113,13 +113,14 @@ export default defineNuxtModule<ModuleOptions>({
113
113
version,
114
114
database : options . database ,
115
115
localDatabase : options . _localDatabase ! ,
116
+ integrityCheck : true ,
116
117
} as never
117
118
118
119
nuxt . options . vite . optimizeDeps ||= { }
119
120
nuxt . options . vite . optimizeDeps . exclude ||= [ ]
120
121
nuxt . options . vite . optimizeDeps . exclude . push ( '@sqlite.org/sqlite-wasm' )
121
122
nuxt . options . vite . optimizeDeps . include ||= [ ]
122
- nuxt . options . vite . optimizeDeps . include . push ( 'scule' )
123
+ nuxt . options . vite . optimizeDeps . include . push ( '@nuxt/content > scule' )
123
124
124
125
// Helpers are designed to be enviroment agnostic
125
126
addImports ( [
@@ -163,7 +164,7 @@ export default defineNuxtModule<ModuleOptions>({
163
164
// Load nitro preset and set db adapter
164
165
nuxt . hook ( 'nitro:config' , async ( config ) => {
165
166
const preset = findPreset ( nuxt )
166
- await preset . setupNitro ( config , { manifest, resolver } )
167
+ await preset . setupNitro ( config , { manifest, resolver, moduleOptions : options } )
167
168
168
169
config . alias ||= { }
169
170
config . alias [ '#content/adapter' ] = resolveDatabaseAdapter ( config . runtimeConfig ! . content ! . database ?. type || options . database . type , resolver )
@@ -174,6 +175,14 @@ export default defineNuxtModule<ModuleOptions>({
174
175
route : '/api/content/:collection/query' ,
175
176
handler : resolver . resolve ( './runtime/api/query.post' ) ,
176
177
} )
178
+
179
+ // Handle HMR changes
180
+ if ( nuxt . options . dev ) {
181
+ addPlugin ( { src : resolver . resolve ( './runtime/plugins/websocket.dev' ) , mode : 'client' } )
182
+ await watchComponents ( nuxt )
183
+ const socket = await startSocketServer ( nuxt , options , manifest )
184
+ await watchContents ( nuxt , options , manifest , socket )
185
+ }
177
186
} )
178
187
179
188
// Prerender database.sql routes for each collection to fetch dump
@@ -190,36 +199,24 @@ export default defineNuxtModule<ModuleOptions>({
190
199
return
191
200
}
192
201
193
- const dumpGeneratePromise = processCollectionItems ( nuxt , manifest . collections , options )
194
- . then ( ( fest ) => {
195
- manifest . checksum = fest . checksum
196
- manifest . dump = fest . dump
197
- manifest . components = fest . components
198
-
199
- return updateTemplates ( {
200
- filter : template => [
201
- moduleTemplates . fullRawDump ,
202
- moduleTemplates . fullCompressedDump ,
203
- moduleTemplates . manifest ,
204
- moduleTemplates . components ,
205
- ] . includes ( template . filename ) ,
206
- } )
207
- } )
208
-
209
202
// Generate collections and sql dump to update templates local database
210
- // `app:templates` is triggered for all environments
211
- nuxt . hook ( 'app:templates' , async ( ) => {
212
- await dumpGeneratePromise
213
- } )
214
-
215
- dumpGeneratePromise . then ( async ( ) => {
216
- // Handle HMR changes
217
- if ( nuxt . options . dev ) {
218
- addPlugin ( { src : resolver . resolve ( './runtime/plugins/websocket.dev' ) , mode : 'client' } )
219
- await watchComponents ( nuxt )
220
- const socket = await startSocketServer ( nuxt , options , manifest )
221
- await watchContents ( nuxt , options , manifest , socket )
222
- }
203
+ // `modules:done` is triggered for all environments
204
+ nuxt . hook ( 'modules:done' , async ( ) => {
205
+ const fest = await processCollectionItems ( nuxt , manifest . collections , options )
206
+
207
+ // Update manifest
208
+ manifest . checksum = fest . checksum
209
+ manifest . dump = fest . dump
210
+ manifest . components = fest . components
211
+
212
+ await updateTemplates ( {
213
+ filter : template => [
214
+ moduleTemplates . fullRawDump ,
215
+ moduleTemplates . fullCompressedDump ,
216
+ moduleTemplates . manifest ,
217
+ moduleTemplates . components ,
218
+ ] . includes ( template . filename ) ,
219
+ } )
223
220
224
221
// Handle preview mode
225
222
if ( process . env . NUXT_CONTENT_PREVIEW_API || options . preview ?. api ) {
@@ -237,7 +234,7 @@ export default defineNuxtModule<ModuleOptions>({
237
234
async function processCollectionItems ( nuxt : Nuxt , collections : ResolvedCollection [ ] , options : ModuleOptions ) {
238
235
const collectionDump : Record < string , string [ ] > = { }
239
236
const collectionChecksum : Record < string , string > = { }
240
- const db = await getLocalDatabase ( options . _localDatabase ! . filename )
237
+ const db = await getLocalDatabase ( options . _localDatabase )
241
238
const databaseContents = await db . fetchDevelopmentCache ( )
242
239
243
240
const configHash = hash ( {
0 commit comments