Skip to content

Commit

Permalink
feat: add multi-app support for global var __NUXT__
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayot committed May 21, 2024
1 parent 6ec39d4 commit 37ac042
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export async function getVitestConfigFromNuxt(
test: {
dir: process.cwd(),
environmentOptions: {
// @ts-expect-error will be available from nuxt@3.12.0
appId: options.nuxt.options.appId,
experimental: defu({}, options.nuxt.options.experimental),
future: defu({}, options.nuxt.options.future),
nuxtRuntimeConfig: applyEnv(structuredClone(options.nuxt.options.runtimeConfig), {
prefix: 'NUXT_',
env: await setupDotenv(defu(loadNuxtOptions.dotenv, {
Expand Down
12 changes: 11 additions & 1 deletion src/environments/vitest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default <Environment>{

win.__NUXT_VITEST_ENVIRONMENT__ = true

win.__NUXT__ = {
const __NUXT__ = {
serverRendered: false,
config: {
public: {},
Expand All @@ -43,6 +43,16 @@ export default <Environment>{
state: {},
}

const multiApp = (environmentOptions?.future.multiApp as boolean) || false
if (!multiApp) {
win.__NUXT__ = __NUXT__
} else {
const appId = environmentOptions?.appId || 'nuxt-app'
win.__NUXT__ = {
[appId]: __NUXT__
}
}

const app = win.document.createElement('div')
// this is a workaround for a happy-dom bug with ids beginning with _
app.id = environmentOptions.nuxt.rootId
Expand Down

0 comments on commit 37ac042

Please sign in to comment.