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

chore: test bundler module resolution #2474

Merged
merged 3 commits into from
Dec 21, 2023
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
3 changes: 3 additions & 0 deletions .nuxtrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
imports.autoImport=false
typescript.includeWorkspace=true

# enable TypeScript bundler module resolution - https://www.typescriptlang.org/docs/handbook/modules/reference.html#bundler
experimental.typescriptBundlerResolution=true
13 changes: 12 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.runtimeConfig.public.content = defu(nuxt.options.runtimeConfig.public.content, {
locales: options.locales,
defaultLocale: contentContext.defaultLocale,
integrity: buildIntegrity,
integrity: buildIntegrity as number,
experimental: {
stripQueryParameters: options.experimental.stripQueryParameters,
advanceQuery: options.experimental.advanceQuery === true,
Expand Down Expand Up @@ -964,6 +964,17 @@ interface ModulePublicRuntimeConfig {
clientDB: boolean
advanceQuery: boolean
}

api: {
baseURL: string
}

host: string | undefined

trailingSlash: boolean

integrity: number | undefined

respectPathCase: boolean

defaultLocale: ModuleOptions['defaultLocale']
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/composables/client-db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-ignore
import memoryDriver from 'unstorage/drivers/memory'
import { type Storage, createStorage, prefixStorage } from 'unstorage'
import { type Storage, createStorage, prefixStorage, type StorageValue } from 'unstorage'
import { withBase } from 'ufo'
import { createQuery } from '../query/query'
import type { NavItem, ParsedContent, ParsedContentMeta } from '../types'
Expand Down Expand Up @@ -87,7 +87,7 @@ async function initContentDatabase () {

await _contentDatabase.storage.setItem('navigation', navigation)

await _contentDatabase.storage.setItem('integrity', content.integrity)
await _contentDatabase.storage.setItem('integrity', content.integrity as StorageValue)
}

// call `content:storage` hook to allow plugins to fill storage
Expand Down
17 changes: 12 additions & 5 deletions src/runtime/legacy/composables/client-db.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// @ts-ignore
import memoryDriver from 'unstorage/drivers/memory'
import { type Storage, createStorage, prefixStorage } from 'unstorage'
import { type Storage, createStorage, prefixStorage, type StorageValue } from 'unstorage'
import { withBase } from 'ufo'
import { createPipelineFetcherLegacy } from '../../query/match/pipeline-legacy'
import { createQuery } from '../../query/query'
import type { NavItem, ParsedContent, ParsedContentMeta, QueryBuilderParams } from '../../types'
import type { NavItem, ParsedContent, ParsedContentMeta, QueryBuilder, QueryBuilderParams } from '../../types'
import { createNav } from '../../server/navigation'
import { useContentPreview } from '../../composables/preview'
import type { ContentQueryBuilderParams, ContentQueryFetcher } from '../../types/query'
import { useRuntimeConfig, useNuxtApp } from '#imports'

const withContentBase = (url: string) => withBase(url, useRuntimeConfig().public.content.api.baseURL)

export const contentStorage = prefixStorage(createStorage({ driver: memoryDriver() }), '@content')
export const contentStorage: Storage = prefixStorage(createStorage({ driver: memoryDriver() }), '@content')

export function createDB (storage: Storage) {
interface ClientDB {
storage: Storage
fetch: (query: QueryBuilder<ParsedContent>) => Promise<ParsedContent | ParsedContent[]>
query: (query?: QueryBuilderParams) => QueryBuilder<ParsedContent>
}

export function createDB (storage: Storage): ClientDB {
async function getItems () {
const keys = new Set<string>(await storage.getKeys('cache:'))

Expand Down Expand Up @@ -45,6 +51,7 @@ export function createDB (storage: Storage) {
const items = await Promise.all(Array.from(keys).map(key => storage.getItem(key) as Promise<ParsedContent>))
return items
}

return {
storage,
fetch: createPipelineFetcherLegacy(getItems),
Expand Down Expand Up @@ -87,7 +94,7 @@ async function initContentDatabase () {

await _contentDatabase.storage.setItem('navigation', navigation)

await _contentDatabase.storage.setItem('integrity', content.integrity)
await _contentDatabase.storage.setItem('integrity', content.integrity as StorageValue)
}

// call `content:storage` hook to allow plugins to fill storage
Expand Down
4 changes: 1 addition & 3 deletions src/runtime/server/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import { useRuntimeConfig } from '#imports'

export default defineEventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig()
const { ignoredTags, filterQuery, indexed } = runtimeConfig.public.content.search
const { ignoredTags = [], filterQuery, indexed, options } = runtimeConfig.public.content.search!

const files = await serverSearchContent(event, filterQuery)

const sections = files.map(page => splitPageIntoSections(page, { ignoredTags })).flat()

if (indexed) {
const { options } = runtimeConfig.public.content.search

const miniSearch = new MiniSearch(options)
miniSearch.addAll(sections)

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/server/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function createNav (contents: ParsedContentMeta[], configs: Record<string
// Get navigation config from runtimeConfig
const { navigation } = useRuntimeConfig().public.content

if (navigation === false) {
return []
}

// Navigation fields picker
const pickNavigationFields = (content: ParsedContentMeta) => ({
...pick(['title', ...navigation.fields])(content),
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type StorageValue, prefixStorage } from 'unstorage'
import { type StorageValue, prefixStorage, type Storage } from 'unstorage'
import { joinURL, withLeadingSlash, withoutTrailingSlash } from 'ufo'
import { hash as ohash } from 'ohash'
import type { H3Event } from 'h3'
Expand Down Expand Up @@ -33,9 +33,9 @@ interface ParseContentOptions {
[key: string]: any
}

export const sourceStorage = prefixStorage(useStorage(), 'content:source')
export const cacheStorage = prefixStorage(useStorage(), 'cache:content')
export const cacheParsedStorage = prefixStorage(useStorage(), 'cache:content:parsed')
export const sourceStorage: Storage = prefixStorage(useStorage(), 'content:source')
export const cacheStorage: Storage = prefixStorage(useStorage(), 'cache:content')
export const cacheParsedStorage: Storage = prefixStorage(useStorage(), 'cache:content:parsed')

const isProduction = process.env.NODE_ENV === 'production'

Expand Down
10 changes: 5 additions & 5 deletions src/runtime/types/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContentQueryFindResponse, ContentQueryFindOneResponse, ContentQueryCountResponse, ContentQueryWithSurround, ContentQueryWithDirConfig } from './api'
import { ContentQueryCountResponse, ContentQueryResponse, ParsedContentInternalMeta, ParsedContentMeta } from '.'
import type { ContentQueryFindResponse, ContentQueryFindOneResponse, ContentQueryCountResponse, ContentQueryWithSurround, ContentQueryWithDirConfig, ContentQueryResponse } from './api'
import type { ParsedContentInternalMeta, ParsedContentMeta } from '.'
/**
* Query
*/
Expand Down Expand Up @@ -256,7 +256,7 @@ export interface ContentQueryBuilderWhere extends Partial<Record<keyof ParsedCon
**/
$in?: Array<string | number | boolean>

[key: string]: string | number | boolean | RegExp | ContentQueryBuilderWhere | Array<string | number | boolean | ContentQueryBuilderWhere>
[key: string]: string | number | boolean | RegExp | ContentQueryBuilderWhere | Array<string | number | boolean | ContentQueryBuilderWhere> | undefined
}

export interface ContentQueryBuilderParams {
Expand Down Expand Up @@ -313,7 +313,7 @@ export interface ContentQueryBuilder<T = ParsedContentMeta, Y = {}> {
* Retrieve query builder params
* @internal
*/
params: () => readonly ContentQueryBuilderParams
params: () => ContentQueryBuilderParams

/**
* Filter contents based on locale
Expand Down Expand Up @@ -346,7 +346,7 @@ export interface ContentQueryBuilder<T = ParsedContentMeta, Y = {}> {
withDirConfig(): ContentQueryBuilder<T, ContentQueryWithDirConfig>
}

export type ContentQueryFetcher<T> = (query: ContentQueryBuilder<T>) => Promise<ContentQueryResponse>
export type ContentQueryFetcher<T> = (query: ContentQueryBuilder<T>) => Promise<ContentQueryResponse<T>>

// Ensure that a .js file is emitted too
export {}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"lib": [ "es2015" ]
"lib": [ "es2015", "dom" ]
},
"extends": "./playground/basic/.nuxt/tsconfig.json",
"exclude": [
Expand Down