-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): improve nuxt env stub (#443)
* wip * feat: more commented code * wip * fix: WORKS OMG * test(lint): fix * chore: console.log * fix: builds now * fix: nuxt close * feat: onPreview + close nuxt on preview command * feat(config): build.excludeFromVendorsChunk
- Loading branch information
Showing
14 changed files
with
281 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
<script setup> | ||
/* eslint-disable no-undef */ | ||
console.log('useNuxtApp', useNuxtApp()) | ||
console.log('useNuxtApp().$config', useNuxtApp().$config) | ||
const config = useRuntimeConfig() | ||
console.log('useRuntimeConfig', config) | ||
</script> | ||
|
||
<template> | ||
<Story> | ||
<Meow /> | ||
|
||
<h3>Nuxt runtime config</h3> | ||
<pre>{{ config }}</pre> | ||
</Story> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare global { | ||
const __HST_COLLECT__: boolean | ||
} | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { App } from 'h3' | ||
import { createApp } from 'h3' | ||
import { createFetch } from 'ofetch' | ||
|
||
export async function setupNuxtApp () { | ||
const win = window as unknown as Window & { | ||
__app: App | ||
__registry: Set<string> | ||
__NUXT__: any | ||
$fetch: any | ||
fetch: any | ||
IntersectionObserver: any | ||
Headers: any | ||
} | ||
|
||
win.__NUXT__ = { | ||
serverRendered: false, | ||
config: { | ||
public: {}, | ||
app: { baseURL: '/' }, | ||
}, | ||
data: {}, | ||
state: {}, | ||
} | ||
|
||
const app = win.document.createElement('div') | ||
// this is a workaround for a happy-dom bug with ids beginning with _ | ||
app.id = 'nuxt-test' | ||
win.document.body.appendChild(app) | ||
|
||
win.IntersectionObserver = | ||
win.IntersectionObserver || | ||
class IntersectionObserver { | ||
observe () { | ||
// noop | ||
} | ||
} | ||
|
||
const h3App = createApp() | ||
|
||
const registry = new Set<string>() | ||
|
||
if (__HST_COLLECT__) { | ||
const { | ||
toNodeListener, | ||
} = await import('h3') | ||
const { | ||
createCall, | ||
createFetch: createLocalFetch, | ||
} = await import('unenv/runtime/fetch/index') | ||
// @ts-expect-error TODO: fix in h3 | ||
const localCall = createCall(toNodeListener(h3App)) | ||
const localFetch = createLocalFetch(localCall, globalThis.fetch) | ||
|
||
win.fetch = (init: string, options?: any) => { | ||
if (typeof init === 'string' && registry.has(init)) { | ||
init = '/_' + init | ||
} | ||
return localFetch(init, options) | ||
} | ||
} | ||
|
||
win.$fetch = createFetch({ fetch: win.fetch, Headers: win.Headers as any }) | ||
|
||
win.__registry = registry | ||
win.__app = h3App | ||
|
||
// @ts-ignore | ||
await import('#app/entry') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.