Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor page filename logic #7983

Merged
merged 3 commits into from
Aug 7, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/early-planets-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix filename generation for `.astro` pages
20 changes: 4 additions & 16 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions packages/astro/test/units/build/static-build.test.js
Original file line number Diff line number Diff line change
@@ -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)
});
});
});