diff --git a/packages/vite-plugin-nitro/src/lib/utils/get-content-files.ts b/packages/vite-plugin-nitro/src/lib/utils/get-content-files.ts index a6ac08818..6709aca1c 100644 --- a/packages/vite-plugin-nitro/src/lib/utils/get-content-files.ts +++ b/packages/vite-plugin-nitro/src/lib/utils/get-content-files.ts @@ -15,7 +15,7 @@ export function getMatchingContentFilesWithFrontMatter( const fm = require('front-matter'); const root = normalizePath(path.resolve(workspaceRoot, rootDir)); - const resolvedDir = normalizePath(path.resolve(root, glob)); + const resolvedDir = normalizePath(path.relative(root, path.join(root, glob))); const contentFiles: string[] = fg.sync([`${root}/${resolvedDir}/*`], { dot: true, }); diff --git a/packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts b/packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts index ad3bbea04..cb666a29f 100644 --- a/packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts +++ b/packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts @@ -184,65 +184,72 @@ describe('nitro', () => { ); }); - it('should build the server with content dir routes', async () => { - // Arrange - const { - buildSSRAppImportSpy, - buildServerImportSpy, - buildSitemapImportSpy, - } = await mockBuildFunctions(); - const prerenderRoutes = { - prerender: { - routes: [ - '/blog', - '/about', + describe('should build the server with content dir routes', () => { + [ + '/packages/vite-plugin-nitro/test-data/content', + 'packages/vite-plugin-nitro/test-data/content', + ].forEach((contentDir) => { + it(`contentDir: ${contentDir}`, async () => { + // Arrange + const { + buildSSRAppImportSpy, + buildServerImportSpy, + buildSitemapImportSpy, + } = await mockBuildFunctions(); + const prerenderRoutes = { + prerender: { + routes: [ + '/blog', + '/about', + { + contentDir, + transform: (file: PrerenderContentFile) => { + if (file.attributes['draft']) { + return false; + } + const slug = file.attributes['slug'] || file.name; + return `/blog/${slug}`; + }, + }, + ], + sitemap: { host: 'example.com' }, + }, + }; + const plugin = nitro({ + ssr: true, + ...prerenderRoutes, + }); + + // Act + await runConfigAndCloseBundle(plugin); + + // Assert + expect(buildSSRAppImportSpy).toHaveBeenCalledWith( + {}, + { ssr: true, ...prerenderRoutes } + ); + + expect(buildServerImportSpy).toHaveBeenCalledWith( + { ssr: true, ...prerenderRoutes }, { - contentDir: '/packages/vite-plugin-nitro/test-data/content', - transform: (file: PrerenderContentFile) => { - if (file.attributes['draft']) { - return false; - } - const slug = file.attributes['slug'] || file.name; - return `/blog/${slug}`; + ...mockNitroConfig, + prerender: { + routes: ['/blog', '/about', '/blog/first', '/blog/02-second'], }, - }, - ], - sitemap: { host: 'example.com' }, - }, - }; - const plugin = nitro({ - ssr: true, - ...prerenderRoutes, + alias: expect.anything(), + rollupConfig: expect.anything(), + handlers: expect.anything(), + } + ); + + expect(buildSitemapImportSpy).toHaveBeenCalledWith( + {}, + { host: 'example.com' }, + ['/blog', '/about', '/blog/first', '/blog/02-second'], + expect.anything() + ); + }); }); - - // Act - await runConfigAndCloseBundle(plugin); - - // Assert - expect(buildSSRAppImportSpy).toHaveBeenCalledWith( - {}, - { ssr: true, ...prerenderRoutes } - ); - - expect(buildServerImportSpy).toHaveBeenCalledWith( - { ssr: true, ...prerenderRoutes }, - { - ...mockNitroConfig, - prerender: { - routes: ['/blog', '/about', '/blog/first', '/blog/02-second'], - }, - alias: expect.anything(), - rollupConfig: expect.anything(), - handlers: expect.anything(), - } - ); - - expect(buildSitemapImportSpy).toHaveBeenCalledWith( - {}, - { host: 'example.com' }, - ['/blog', '/about', '/blog/first', '/blog/02-second'], - expect.anything() - ); }); });