fix output path logging for build.format: preserve (#12918)

* fix output path logging for build.format: preserve

* add tests for output logging

* code format
This commit is contained in:
lam eu ler 2025-01-08 21:57:53 +08:00 committed by GitHub
parent 0c0c66bf0d
commit fd12a26ac6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a bug where the logged output path does not match the actual output path when using `build.format: 'preserve'`

View file

@ -181,7 +181,7 @@ async function generatePage(
const timeStart = performance.now();
pipeline.logger.debug('build', `Generating: ${path}`);
const filePath = getOutputFilename(config, path, pageData.route.type);
const filePath = getOutputFilename(config, path, pageData.route);
const lineIcon =
(index === paths.length - 1 && !isConcurrent) || paths.length === 1 ? '└─' : '├─';

View file

@ -3,7 +3,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { AstroSettings } from '../types/astro.js';
import type { AstroConfig } from '../types/public/config.js';
import type { RouteType } from '../types/public/internal.js';
import type { RouteData } from '../types/public/internal.js';
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './constants.js';
import { removeQueryString, removeTrailingForwardSlash, slash } from './path.js';
@ -43,8 +43,8 @@ const STATUS_CODE_PAGES = new Set(['/404', '/500']);
* Handles both "/foo" and "foo" `name` formats.
* Handles `/404` and `/` correctly.
*/
export function getOutputFilename(astroConfig: AstroConfig, name: string, type: RouteType) {
if (type === 'endpoint') {
export function getOutputFilename(astroConfig: AstroConfig, name: string, routeData: RouteData) {
if (routeData.type === 'endpoint') {
return name;
}
if (name === '/' || name === '') {
@ -53,6 +53,9 @@ export function getOutputFilename(astroConfig: AstroConfig, name: string, type:
if (astroConfig.build.format === 'file' || STATUS_CODE_PAGES.has(name)) {
return `${removeTrailingForwardSlash(name || 'index')}.html`;
}
if (astroConfig.build.format === 'preserve' && !routeData.isIndex) {
return `${removeTrailingForwardSlash(name || 'index')}.html`;
}
return path.posix.join(name, 'index.html');
}

View file

@ -1,11 +1,14 @@
import assert from 'node:assert/strict';
import { Writable } from 'node:stream';
import { before, describe, it } from 'node:test';
import { Logger } from '../dist/core/logger/core.js';
import { loadFixture } from './test-utils.js';
describe('build format', () => {
describe('build.format: file', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
const logs = [];
before(async () => {
fixture = await loadFixture({
@ -14,7 +17,18 @@ describe('build format', () => {
format: 'file',
},
});
await fixture.build();
await fixture.build({
logger: new Logger({
level: 'info',
dest: new Writable({
objectMode: true,
write(event, _, callback) {
logs.push(event);
callback();
},
}),
}),
});
});
it('outputs', async () => {
@ -22,11 +36,22 @@ describe('build format', () => {
assert.ok(await fixture.readFile('/nested-md.html'));
assert.ok(await fixture.readFile('/nested-astro.html'));
});
it('logs correct output paths', () => {
assert.ok(logs.find((log) => log.level === 'info' && log.message.includes('/client.html')));
assert.ok(
logs.find((log) => log.level === 'info' && log.message.includes('/nested-md.html')),
);
assert.ok(
logs.find((log) => log.level === 'info' && log.message.includes('/nested-astro.html')),
);
});
});
describe('build.format: preserve', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
const logs = [];
before(async () => {
fixture = await loadFixture({
@ -35,7 +60,18 @@ describe('build format', () => {
format: 'preserve',
},
});
await fixture.build();
await fixture.build({
logger: new Logger({
level: 'info',
dest: new Writable({
objectMode: true,
write(event, _, callback) {
logs.push(event);
callback();
},
}),
}),
});
});
it('outputs', async () => {
@ -43,5 +79,17 @@ describe('build format', () => {
assert.ok(await fixture.readFile('/nested-md/index.html'));
assert.ok(await fixture.readFile('/nested-astro/index.html'));
});
it('logs correct output paths', () => {
assert.ok(logs.find((log) => log.level === 'info' && log.message.includes('/client.html')));
assert.ok(
logs.find((log) => log.level === 'info' && log.message.includes('/nested-md/index.html')),
);
assert.ok(
logs.find(
(log) => log.level === 'info' && log.message.includes('/nested-astro/index.html'),
),
);
});
});
});