Skip to content

Commit

Permalink
feat(nuxt3): create root component (nuxt#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 12, 2021
1 parent b35b111 commit 9cb9bb6
Show file tree
Hide file tree
Showing 35 changed files with 66 additions and 184 deletions.
4 changes: 2 additions & 2 deletions docs/content/3.docs/1.usage/1.data-fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default () => {
}
```

```vue [pages/index.vue]
```vue [app.vue]
<script setup>
const { data } = await useAsyncData('count', () => $fetch('/api/count'))
</script>
Expand Down Expand Up @@ -68,7 +68,7 @@ Available options:
### Example
```vue [pages/index.vue]
```vue [app.vue]
<script setup>
const { data } = await useFetch('/api/count')
</script>
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example-pages",
"name": "example-use-async-data",
"private": true,
"devDependencies": {
"nuxt3": "latest"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example-meta",
"name": "example-use-meta",
"private": true,
"devDependencies": {
"nuxt3": "latest"
Expand Down
File renamed without changes.
12 changes: 0 additions & 12 deletions examples/wasm/package.json

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example-async-data",
"name": "example-with-pages",
"private": true,
"devDependencies": {
"nuxt3": "latest"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions examples/with-vite/app.vue

This file was deleted.

5 changes: 0 additions & 5 deletions examples/with-vite/nuxt.config.ts

This file was deleted.

14 changes: 0 additions & 14 deletions examples/with-vue-content-loader/package.json

This file was deleted.

76 changes: 0 additions & 76 deletions examples/with-vue-content-loader/pages/index.vue

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example-with-vite",
"name": "example-with-wasm",
"private": true,
"devDependencies": {
"nuxt3": "latest"
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/kit/src/types/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export interface NuxtPlugin {
}

export interface NuxtApp {
main?: string
mainComponent?: string
rootComponent?: string
dir: string
extensions: string[]
plugins: NuxtPlugin[]
Expand Down
5 changes: 5 additions & 0 deletions packages/nuxt3/src/app/components/nuxt-root.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<Suspense>
<App />
</Suspense>
</template>
10 changes: 7 additions & 3 deletions packages/nuxt3/src/app/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import '#build/css'
// @ts-ignore
import _plugins from '#build/plugins'
// @ts-ignore
import App from '#build/app'
import RootComponent from '#build/root-component.mjs'
// @ts-ignore
import AppComponent from '#build/app-component.mjs'

let entry: Function

const plugins = normalizePlugins(_plugins)

if (process.server) {
entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext'] = {}) {
const app = createApp(App)
const app = createApp(RootComponent)
app.component('App', AppComponent)

const nuxt = createNuxtApp({ app, ssrContext })

Expand All @@ -35,7 +38,8 @@ if (process.client) {

entry = async function initApp () {
const isSSR = Boolean(window.__NUXT__?.serverRendered)
const app = isSSR ? createSSRApp(App) : createApp(App)
const app = isSSR ? createSSRApp(RootComponent) : createApp(RootComponent)
app.component('App', AppComponent)

const nuxt = createNuxtApp({ app })

Expand Down
14 changes: 11 additions & 3 deletions packages/nuxt3/src/app/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ type TemplateContext = {
app: NuxtApp;
}

export const appTemplate = {
filename: 'app.mjs',
// TODO: Use an alias
export const appComponentTemplate = {
filename: 'app-component.mjs',
getContents (ctx: TemplateContext) {
return `export { default } from '${ctx.app.main}'`
return `export { default } from '${ctx.app.mainComponent}'`
}
}
// TODO: Use an alias
export const rootComponentTemplate = {
filename: 'root-component.mjs',
getContents (ctx: TemplateContext) {
return `export { default } from '${ctx.app.rootComponent}'`
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/nuxt3/src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
}

// Resolve main (app.vue)
if (!app.main) {
app.main = tryResolvePath('~/App', resolveOptions) || tryResolvePath('~/app', resolveOptions)
if (!app.mainComponent) {
app.mainComponent = tryResolvePath('~/App', resolveOptions) || tryResolvePath('~/app', resolveOptions)
}
if (!app.main) {
app.main = resolve(nuxt.options.appDir, 'components/nuxt-welcome.vue')
if (!app.mainComponent) {
app.mainComponent = resolve(nuxt.options.appDir, 'components/nuxt-welcome.vue')
}

// Default root component
app.rootComponent = resolve(nuxt.options.appDir, 'components/nuxt-root.vue')

// Resolve plugins
app.plugins = [
...nuxt.options.plugins,
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt3/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function build (nuxt: Nuxt) {
nuxt.hook('builder:watch', async (event, path) => {
if (event !== 'change' && /app|plugins/i.test(path)) {
if (path.match(/app/i)) {
app.main = null
app.mainComponent = null
}
await generateApp(nuxt, app)
}
Expand Down
8 changes: 5 additions & 3 deletions packages/nuxt3/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export default defineNuxtModule({
}
})

// Add default layout for pages
nuxt.hook('app:resolve', (app) => {
if (app.main.includes('nuxt-welcome')) {
app.main = resolve(runtimeDir, 'app.vue')
// Remove default root with Suspense
app.rootComponent = resolve(runtimeDir, 'root.vue')
// Add default layout for pages
if (app.mainComponent.includes('nuxt-welcome')) {
app.mainComponent = resolve(runtimeDir, 'app.vue')
}
})

Expand Down
3 changes: 3 additions & 0 deletions packages/nuxt3/src/pages/runtime/root.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<App />
</template>
9 changes: 8 additions & 1 deletion playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<template>
<div>
<NuxtPage />
<img src="~/assets/logo.svg">
<br>
Hello Nuxt 3
</div>
</template>

<script>
export default defineNuxtComponent({
})
</script>
12 changes: 0 additions & 12 deletions playground/pages/index.vue

This file was deleted.

Loading

0 comments on commit 9cb9bb6

Please sign in to comment.