Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

fix: allow custom globalContext and globalNuxt #80

Merged
merged 1 commit into from
May 27, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"prepublishOnly": "yarn lint && yarn test",
"release": "release-it",
"test": "run-s test:*",
"test:e2e-globals": "GLOBALS=true start-server-and-test fixture:prod 3000 \"testcafe firefox:headless test/e2e\"",
"test:e2e-ssr": "start-server-and-test fixture:prod 3000 \"testcafe firefox:headless test/e2e\"",
"test:e2e-generated": "start-server-and-test fixture:generate 3000 \"GENERATE=true testcafe firefox:headless test/e2e\"",
"test:types": "tsd",
Expand Down
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const withContext = (callback: ContextCallback) => {
const vm = getCurrentInstance()
if (!vm) throw new Error('This must be called within a setup function.')

callback(vm.$nuxt.context)
callback(vm['<%= options.globalNuxt %>' as '$nuxt'].context)
}

/**
Expand All @@ -36,7 +36,7 @@ export const useContext = () => {
if (!vm) throw new Error('This must be called within a setup function.')

return {
...vm.$nuxt.context,
...vm['<%= options.globalNuxt %>' as '$nuxt'].context,
route: computed(() => vm.$route),
query: computed(() => vm.$route.query),
from: computed(() => vm.$route.redirectedFrom),
Expand Down
9 changes: 6 additions & 3 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const fetches = new WeakMap<ComponentInstance, Fetch[]>()

const isSsrHydration = (vm: ComponentInstance) =>
(vm.$vnode?.elm as any)?.dataset?.fetchKey
const nuxtState = process.client && (window as any).__NUXT__
const nuxtState =
process.client && (window as any)['<%= options.globalContext %>']

interface AugmentedComponentInstance extends ComponentInstance {
_fetchKey?: number
Expand All @@ -55,7 +56,7 @@ function registerCallback(vm: ComponentInstance, callback: Fetch) {
async function callFetches(this: AugmentedComponentInstance) {
const fetchesToCall = fetches.get(this)
if (!fetchesToCall) return
;(this.$nuxt as any).nbFetching++
;(this['<%= options.globalNuxt %>' as '$nuxt'] as any).nbFetching++

this.$fetchState.pending = true
this.$fetchState.error = null
Expand All @@ -79,7 +80,9 @@ async function callFetches(this: AugmentedComponentInstance) {
this.$fetchState.pending = false
this.$fetchState.timestamp = Date.now()

this.$nextTick(() => (this.$nuxt as any).nbFetching--)
this.$nextTick(
() => (this['<%= options.globalNuxt %>' as '$nuxt'] as any).nbFetching--
)
}

async function serverPrefetch(vm: AugmentedComponentInstance) {
Expand Down
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,22 @@ const compositionApiModule: Module<any> = function () {
},
})

const globalName = this.options.globalName || 'nuxt'
const globalContextFactory =
this.options.globals?.context ||
(globalName => `__${globalName.toUpperCase()}__`)
const globalNuxtFactory =
this.options.globals?.nuxt || (globalName => `$${globalName}`)
const globalContext = globalContextFactory(globalName)
const globalNuxt = globalNuxtFactory(globalName)

const { dst: entryDst } = this.addTemplate({
src: resolve(libRoot, 'lib', 'entrypoint.js'),
fileName: join('composition-api', 'index.js'),
options: {
globalContext,
globalNuxt,
},
})

this.options.build = this.options.build || {}
Expand Down
8 changes: 6 additions & 2 deletions src/ssr-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => {
}

if (process.client) {
return ref((window as any).__NUXT__?.ssrRefs?.[key] ?? getValue(value))
return ref(
(window as any)['<%= options.globalContext %>']?.ssrRefs?.[key] ??
getValue(value)
)
}

const val = getValue(value)
Expand Down Expand Up @@ -117,7 +120,8 @@ export const shallowSsrRef = <T>(

if (process.client) {
return shallowRef(
(window as any).__NUXT__?.ssrRefs?.[key] ?? getValue(value)
(window as any)['<%= options.globalContext %>']?.ssrRefs?.[key] ??
getValue(value)
)
}

Expand Down
25 changes: 17 additions & 8 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ module.exports = {
head: {
link: [
{
rel: "stylesheet", href: "https://newcss.net/lite.css"
}
]
rel: 'stylesheet',
href: 'https://newcss.net/lite.css',
},
],
},
...(process.env.GLOBALS === 'true'
? {
globalName: 'bob',
globals: {
nuxt: globalName => `$my${globalName}`,
},
}
: {}),
...(process.env.NOW_BUILD === 'true'
? {
generate: {
dir: 'dist/fixture',
routes
routes,
},
router: {
base: '/fixture/',
Expand All @@ -28,10 +37,10 @@ module.exports = {
},
}
: {
generate: {
routes,
}
}),
generate: {
routes,
},
}),
buildModules: [
process.env.NODE_ENV === 'test'
? require('../..').default
Expand Down