diff --git a/nuxt.config.ts b/nuxt.config.ts index 6ebf2d80..663d317f 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -92,37 +92,6 @@ export default defineNuxtConfig({ '@nuxt/content', 'vue-plausible', '~/modules/critters', - function () { - const nuxt = useNuxt() - let dirs: string[] - nuxt.hook('components:dirs', _dirs => { - dirs = _dirs.map(d => (typeof d === 'string' ? d : d.path)) - }) - // Exclude non-prose content components - nuxt.hook('components:extend', components => { - for (const component of components) { - if ( - 'filePath' in component && - component.filePath.includes('@nuxt/content') - ) { - if (!component.filePath.includes('/Prose/')) { - components.splice(components.indexOf(component), 1) - } - } - } - }) - // Use single components chunk - nuxt.hook('vite:extendConfig', config => { - if (Array.isArray(config.build.rollupOptions.output)) return - config.build.rollupOptions.output.manualChunks = id => { - if ( - id.includes('@nuxt/content') && - dirs.some(dir => id.includes(dir)) - ) { - return 'components-chunk' - } - } - }) - }, + '~/modules/components-chunk', ], }) diff --git a/src/modules/components-chunk.ts b/src/modules/components-chunk.ts new file mode 100644 index 00000000..76950277 --- /dev/null +++ b/src/modules/components-chunk.ts @@ -0,0 +1,42 @@ +import { defineNuxtModule, useNuxt } from '@nuxt/kit' + +export default defineNuxtModule({ + meta: { + name: 'components-chunk', + }, + setup() { + const nuxt = useNuxt() + let dirs: string[] + + nuxt.hook('components:dirs', _dirs => { + dirs = _dirs.map(d => (typeof d === 'string' ? d : d.path)) + }) + + // Exclude non-prose content components + nuxt.hook('components:extend', components => { + for (const component of components) { + if ( + 'filePath' in component && + component.filePath.includes('@nuxt/content') + ) { + if (!component.filePath.includes('/Prose/')) { + components.splice(components.indexOf(component), 1) + } + } + } + }) + + // Use single components chunk + nuxt.hook('vite:extendConfig', config => { + if (Array.isArray(config.build.rollupOptions.output)) return + config.build.rollupOptions.output.manualChunks = id => { + if ( + id.includes('@nuxt/content') && + dirs.some(dir => id.includes(dir)) + ) { + return 'components-chunk' + } + } + }) + }, +})