Skip to content
Closed
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
15 changes: 15 additions & 0 deletions docs/content/4.api/3.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ export default defineNuxtConfig({
})
```

## `components`

- Type: `string[]`{lang=ts}
- Default: `[]`{lang=ts}

Specify the list of components which are not in `components/content/` or not defined as global component to add them in the main bundle (included in all pages). Useful for components used in UI library.

```ts [nuxt.config.ts]
export default defineNuxtConfig({
content: {
components: ['UButton']
}
})
```

## `watch`

- Type: `object | false`{lang=ts}
Expand Down
3 changes: 3 additions & 0 deletions playground/basic/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p>Hello from not-content component.</p>
</template>
4 changes: 3 additions & 1 deletion playground/basic/content/0.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ title: Index
- [Playground](/playground)
- [Query Builder](/query-playground)
- [Custom 404 page](/404)
- [Not Found Content](/not-found-content)
- [Not Found Content](/not-found-content)

:hello-world
1 change: 1 addition & 0 deletions playground/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resolve } from 'pathe'
export default defineNuxtConfig({
extends: ['../shared'],
content: {
components: ['hello-world'],
sources: {
'translation-fa': {
prefix: '/fa',
Expand Down
27 changes: 26 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
addComponentsDir,
addTemplate,
extendViteConfig,
installModule
installModule,
addPluginTemplate
} from '@nuxt/kit'
import { genDynamicImport, genImport, genSafeVariableName } from 'knitwork'
import type { ListenOptions } from 'listhen'
Expand All @@ -20,6 +21,7 @@ import type { Lang as ShikiLang, Theme as ShikiTheme } from 'shiki-es'
import { listen } from 'listhen'
import { type WatchEvent, createStorage } from 'unstorage'
import { joinURL, withLeadingSlash, withTrailingSlash } from 'ufo'
import { pascalCase } from 'scule'
import type { Component } from '@nuxt/schema'
import { name, version } from '../package.json'
import { makeIgnored } from './runtime/utils/config'
Expand Down Expand Up @@ -58,6 +60,12 @@ export interface ModuleOptions {
*/
baseURL: string
}
/**
* List the components that will be used in markdown.
*
* @default []
*/
components: string[]
/**
* Disable content watcher and hot content reload.
* Note: Watcher is a development feature and will not includes in the production.
Expand Down Expand Up @@ -267,6 +275,7 @@ export default defineNuxtModule<ModuleOptions>({
showURL: false
}
},
components: [],
Copy link
Member

@farnabaz farnabaz Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about reusing markdown.tags option for this feature? We can extract all components from markdown.tags map and mark them as global.

!!! This will be a bit confusing if we want to mark a component as global without changing its name.

tags: {
  UButton: 'UButton'
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is a different feature than the tags itself.

We could use markdown.tags to get the extra components as synced though

sources: {},
ignores: [],
locales: [],
Expand Down Expand Up @@ -611,6 +620,22 @@ export default defineNuxtModule<ModuleOptions>({
// @ts-ignore
await nuxt.callHook('content:context', contentContext)

// Add sync components to use not global components
const components = (options.components || []).map(pascalCase)

addPluginTemplate({
Copy link
Member

@farnabaz farnabaz Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if we mark these components as global instead of registering a custom plugin. Marking them as global will help other modules to list these components as global ones. Modules like nuxt-component-meta

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is to avoid using global components and put them in the main bundle of the application actually. I want to see if this improves the performance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, Nuxt registers lazy version of global components, but here the goal is to include it directly inside main bundle.

https://github.com/nuxt/nuxt/blob/ce9aecca4a39e1d5e36e6b92c77e706f8f577445/packages/nuxt/src/components/templates.ts#L50-L63

filename: 'plugins/content-components.ts',
getContents: () => {
return `import { defineNuxtPlugin } from '#app/nuxt'
${genImport('#components', components)}

export default defineNuxtPlugin((nuxtApp) => {
${components.map(name => `nuxtApp.vueApp.component('${name}', ${name});`).join('\n')}
})
`
}
})

contentContext.defaultLocale = contentContext.defaultLocale || contentContext.locales[0]

// Generate cache integrity based on content context
Expand Down