From e54645553c950f2dac0cc670b2e2d9abd1cfedb6 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 7 Aug 2023 10:58:53 -0400 Subject: [PATCH 1/2] fix(#7561): refactor astro page filename logic --- packages/astro/src/core/build/static-build.ts | 20 ++----- .../test/units/build/static-build.test.js | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 packages/astro/test/units/build/static-build.test.js diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 8e3cf8fa8802..95027fd5b5d2 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -445,22 +445,10 @@ export function makeAstroPageEntryPointFileName( facadeModuleId: string, routes: RouteData[] ) { - const pageModuleId = facadeModuleId - .replace(prefix, '') - .replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.'); - let route = routes.find((routeData) => { - return routeData.route === pageModuleId; - }); - let name = pageModuleId; - if (route) { - name = route.route; - } - if (name.endsWith('/')) name += 'index'; - const fileName = `${name.replaceAll('[', '_').replaceAll(']', '_').replaceAll('...', '---')}.mjs`; - if (name.startsWith('..')) { - return `pages${fileName}`; - } - return fileName; + const pageModuleId = facadeModuleId.replace(prefix, '').replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.'); + const route = routes.find((routeData) => routeData.component === pageModuleId); + const name = route?.route ?? pageModuleId; + return `pages${name.replace(/\/$/, '/index').replaceAll(/[\[\]]/g, '_').replaceAll('...', '---')}.astro.mjs`; } /** diff --git a/packages/astro/test/units/build/static-build.test.js b/packages/astro/test/units/build/static-build.test.js new file mode 100644 index 000000000000..632b1c429411 --- /dev/null +++ b/packages/astro/test/units/build/static-build.test.js @@ -0,0 +1,58 @@ +import { expect } from 'chai'; +import { makeAstroPageEntryPointFileName } from '../../../dist/core/build/static-build.js'; + +describe('astro/src/core/build', () => { + describe('makeAstroPageEntryPointFileName', () => { + const routes = [ + { + route: '/', + component: 'src/pages/index.astro', + pathname: '/', + }, + { + route: '/injected', + component: '../node_modules/my-dep/injected.astro', + pathname: '/injected', + }, + { + route: '/injected-workspace', + component: '../../packages/demo/[...all].astro', + pathname: undefined, + }, + { + route: '/blog/[year]/[...slug]', + component: 'src/pages/blog/[year]/[...slug].astro', + pathname: undefined, + }, + ] + + it('handles local pages', async () => { + const input = '@astro-page:src/pages/index@_@astro'; + const output = 'pages/index.astro.mjs'; + const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes); + expect(result).to.equal(output) + }); + + it('handles dynamic pages', async () => { + const input = '@astro-page:src/pages/blog/[year]/[...slug]@_@astro'; + const output = 'pages/blog/_year_/_---slug_.astro.mjs'; + const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes); + expect(result).to.equal(output) + }); + + it('handles node_modules pages', async () => { + const input = '@astro-page:../node_modules/my-dep/injected@_@astro'; + const output = 'pages/injected.astro.mjs'; + const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes); + expect(result).to.equal(output) + }); + + // Fix #7561 + it('handles local workspace pages', async () => { + const input = '@astro-page:../../packages/demo/[...all]@_@astro'; + const output = 'pages/injected-workspace.astro.mjs'; + const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes); + expect(result).to.equal(output) + }); + }); +}); From a668214b01d0893bfb914df9e54720fb2ee181cc Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 7 Aug 2023 11:11:08 -0400 Subject: [PATCH 2/2] chore: add changeset --- .changeset/early-planets-knock.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/early-planets-knock.md diff --git a/.changeset/early-planets-knock.md b/.changeset/early-planets-knock.md new file mode 100644 index 000000000000..378e2f4b2fd1 --- /dev/null +++ b/.changeset/early-planets-knock.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix filename generation for `.astro` pages