-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmodule.ts
More file actions
175 lines (153 loc) Β· 5.46 KB
/
module.ts
File metadata and controls
175 lines (153 loc) Β· 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { resolve } from 'path'
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin, addComponentsDir, addServerHandler, addImportsDir, useLogger, isNuxt2 } from '@nuxt/kit'
import { defu } from 'defu'
import { createPageGenerateHook, createGenerateDoneHook } from './hooks'
import type { CrawlerPage, CrawlerHooks, CrawlerOptions } from './hooks'
import { InstantSearchThemes, type ModuleBaseOptions } from './types'
import { resolveModulePath } from 'exsolve'
const MODULE_NAME = '@nuxtjs/algolia'
const logger = useLogger(MODULE_NAME)
function throwError (message: string) {
throw new Error(`\`[${MODULE_NAME}]\` ${message}`)
}
export interface ModuleOptions extends ModuleBaseOptions {
crawler?: CrawlerOptions
};
export interface ModuleHooks extends CrawlerHooks {}
export * from './types'
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@nuxtjs/algolia',
configKey: 'algolia',
compatibility: {
nuxt: '>=3.0.0-rc.9 || ^2.16.0',
bridge: true
}
},
defaults: {
applicationId: process.env.ALGOLIA_APPLICATION_ID || '',
apiKey: process.env.ALGOLIA_API_KEY || '',
globalIndex: '',
lite: true,
cache: false,
instantSearch: false,
docSearch: {},
useFetch: false,
crawler: {
apiKey: '',
indexName: '',
include: () => true,
meta: ['title', 'description']
}
},
setup (options, nuxt) {
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.build.transpile.push(runtimeDir)
const notRunningInPrepareScript = !nuxt.options._prepare
if (notRunningInPrepareScript && (!options.apiKey || !options.applicationId)) {
console.warn('Missing `apiKey` or `applicationId` in `nuxt.config.js`')
return
}
if (options.crawler!.apiKey || options.crawler!.indexName) {
if (!options.crawler!.apiKey && notRunningInPrepareScript) {
throwError('Missing `crawler.apiKey`')
}
if (!options.crawler!.indexName && notRunningInPrepareScript) {
throwError('Missing `crawler.indexName`')
}
const pages: CrawlerPage[] = []
const pageGenerator = createPageGenerateHook(nuxt, options, pages)
const doneGenerator = createGenerateDoneHook(nuxt, options, pages)
if (isNuxt2(nuxt)) {
nuxt.addHooks({
// Nuxt 2 only hook
'generate:page': createPageGenerateHook(nuxt, options, pages),
'generate:done': createGenerateDoneHook(nuxt, options, pages)
})
} else {
nuxt.hooks.hookOnce('nitro:init', (nitro) => {
nitro.hooks.hookOnce('prerender:routes', () => {
nitro.hooks.hook('prerender:route', async ({ route, contents }) => {
await pageGenerator(contents, route)
})
nitro.hooks.hookOnce('close', async () => {
await doneGenerator()
})
})
})
}
}
if (Object.keys(options.docSearch!).length) {
addComponentsDir({
path: resolve(runtimeDir, 'components'),
pathPrefix: false,
prefix: '',
// @ts-ignore
level: 999,
global: true
})
}
if (isNuxt2() && !nuxt?.options?.runtimeConfig?.public?.algolia) {
// Nuxt 2
// @ts-ignore
nuxt.options.publicRuntimeConfig.algolia = defu(nuxt.options.publicRuntimeConfig.algolia, {
apiKey: options.apiKey,
applicationId: options.applicationId,
lite: options.lite,
instantSearch: options.instantSearch,
docSearch: options.docSearch,
recommend: options.recommend,
globalIndex: options.globalIndex,
useFetch: options.useFetch
})
}
// Nuxt 3
// Workaround for rc.14 only
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {}
// @ts-ignore
nuxt.options.runtimeConfig.public.algolia = defu(nuxt.options.runtimeConfig.algolia, {
apiKey: options.apiKey,
applicationId: options.applicationId,
lite: options.lite,
cache: options.cache,
instantSearch: options.instantSearch,
docSearch: options.docSearch,
recommend: options.recommend,
globalIndex: options.globalIndex,
useFetch: options.useFetch
})
if (options.instantSearch) {
nuxt.options.build.transpile.push('vue-instantsearch/vue3')
if (typeof options.instantSearch === 'object') {
const { theme } = options.instantSearch
if (theme) {
if (theme in InstantSearchThemes) {
nuxt.options.css.push(`instantsearch.css/themes/${theme}.css`)
} else {
logger.error('Invalid theme:', theme)
}
}
}
}
// Polyfilling server packages for SSR support
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
if (isClient) {
(config as any).resolve.alias['@algolia/requester-node-http'] = resolveModulePath('mocked-exports/empty', { from: import.meta.url })
}
})
addPlugin(resolve(runtimeDir, 'plugin'))
addImportsDir(resolve(runtimeDir, 'composables'))
if (options?.indexer && Object.keys(options?.indexer).length) {
const cmsProvider = Object.keys(options.indexer)[0]
nuxt.options.runtimeConfig.algoliaIndexer = defu(nuxt.options.runtimeConfig.algoliaIndexer, {
// @ts-ignore
[cmsProvider]: options.indexer[cmsProvider]
})
addServerHandler({
route: '/api/indexer',
handler: resolve(runtimeDir, `server/api/${cmsProvider}`)
})
}
}
})