Skip to content

Commit

Permalink
fix: don't use alias to inject config to preserve runtime reactivity, r…
Browse files Browse the repository at this point in the history
…esolves #192
  • Loading branch information
lihbr committed May 15, 2023
1 parent fb25c7b commit 6846c07
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
34 changes: 18 additions & 16 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import prismicModule from '../src/module'

// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
typescript: {
strict: true
},
modules: [prismicModule],
modules: ['../src/module'],
runtimeConfig: {
public: {
prismic: {
endpoint: '200629-sms-hoy',
clientConfig: {
routes: [
{
type: 'kitchen_sink_2',
path: '/'
},
{
type: 'page',
path: '/'
}
]
}
endpoint: '200629-sms-hoy'
}
}
},
prismic: {
// endpoint: '200629-sms-hoy',
clientConfig: {
routes: [
{
type: 'kitchen_sink_2',
path: '/'
},
{
type: 'page',
path: '/'
}
]

}
}
})
6 changes: 0 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ export default defineNuxtModule<PrismicModuleOptions>({
const moduleOptions: PrismicModuleOptions = defu(nuxt.options.runtimeConfig.public.prismic, options)
nuxt.options.runtimeConfig.public.prismic = moduleOptions

nuxt.options.alias['#prismicOptions'] = addTemplate({
filename: 'prismicOptions.mjs',
write: true,
getContents: () => `export default ${JSON.stringify(moduleOptions, undefined, 2)}`
}).dst || ''

if (!moduleOptions.endpoint) {
logger.warn('Options `endpoint` is required, disabling module...')
return
Expand Down
4 changes: 1 addition & 3 deletions src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { PrismicModuleOptions } from '../types'
import { defineNuxtPlugin, refreshNuxtData, useRuntimeConfig } from '#imports'

import moduleOptions from '#prismicOptions'

export default defineNuxtPlugin((nuxtApp) => {
const options = moduleOptions as PrismicModuleOptions
const options: PrismicModuleOptions = useRuntimeConfig().public.prismic

// Hot reload preview updates
if (options.preview) {
Expand Down
4 changes: 1 addition & 3 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import linkResolver from '#build/prismic/proxy/linkResolver'
// @ts-expect-error vfs cannot be resolved here
import htmlSerializer from '#build/prismic/proxy/htmlSerializer'

import moduleOptions from '#prismicOptions'

export default defineNuxtPlugin((nuxtApp) => {
const options = moduleOptions as PrismicModuleOptions
const options: PrismicModuleOptions = useRuntimeConfig().public.prismic

const prismicPlugin = createPrismic({
...options,
Expand Down
12 changes: 6 additions & 6 deletions test/module-userFileProxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ afterEach(() => {
it('proxies nothing if user files are not available', () => {
mockedPrismicModule({ endpoint: 'qwerty' })

expect(addTemplate).toHaveBeenCalledTimes(4)
expect(vi.mocked(addTemplate).mock.calls.flat().filter((options: any) => !options.filename.startsWith('prismicOptions')).map((options: any) => [options.filename, options.getContents()])).toMatchInlineSnapshot(`
expect(addTemplate).toHaveBeenCalledTimes(3)
expect(vi.mocked(addTemplate).mock.calls.flat().map((options: any) => [options.filename, options.getContents()])).toMatchInlineSnapshot(`
[
[
"prismic/proxy/client.ts",
Expand All @@ -53,8 +53,8 @@ it('proxies user files from default location', () => {

mockedPrismicModule({ endpoint: 'qwerty' })

expect(addTemplate).toHaveBeenCalledTimes(4)
expect(vi.mocked(addTemplate).mock.calls.flat().filter((options: any) => !options.filename.startsWith('prismicOptions')).map((options: any) => [options.filename, options.getContents()])).toMatchInlineSnapshot(`
expect(addTemplate).toHaveBeenCalledTimes(3)
expect(vi.mocked(addTemplate).mock.calls.flat().map((options: any) => [options.filename, options.getContents()])).toMatchInlineSnapshot(`
[
[
"prismic/proxy/client.ts",
Expand Down Expand Up @@ -88,8 +88,8 @@ it('proxies user files from provided location', () => {
htmlSerializer: '~/custom/htmlSerializer'
})

expect(addTemplate).toHaveBeenCalledTimes(4)
expect(vi.mocked(addTemplate).mock.calls.flat().filter((options: any) => !options.filename.startsWith('prismicOptions')).map((options: any) => [options.filename, options.getContents()])).toMatchInlineSnapshot(`
expect(addTemplate).toHaveBeenCalledTimes(3)
expect(vi.mocked(addTemplate).mock.calls.flat().map((options: any) => [options.filename, options.getContents()])).toMatchInlineSnapshot(`
[
[
"prismic/proxy/client.ts",
Expand Down

0 comments on commit 6846c07

Please sign in to comment.