[ci] format

This commit is contained in:
matthewp 2022-08-25 19:23:09 +00:00 committed by fredkbot
parent cd154e447b
commit b680c3eb97
3 changed files with 60 additions and 48 deletions

View file

@ -517,13 +517,13 @@ export interface AstroUserConfig {
* } * }
* } * }
* ``` * ```
* *
* #### Effect on Astro.url * #### Effect on Astro.url
* Setting `build.format` controls what `Astro.url` is set to during the build. When it is: * Setting `build.format` controls what `Astro.url` is set to during the build. When it is:
* - `directory` - The `Astro.url.pathname` will include a trailing slash to mimic folder behavior; ie `/foo/`. * - `directory` - The `Astro.url.pathname` will include a trailing slash to mimic folder behavior; ie `/foo/`.
* - `file` - The `Astro.url.pathname` will include `.html`; ie `/foo.html`. * - `file` - The `Astro.url.pathname` will include `.html`; ie `/foo.html`.
* *
* This means that when you create relative URLs using `new URL('./relative', Astro.url)`, you will get consistent behavior between dev and build. * This means that when you create relative URLs using `new URL('./relative', Astro.url)`, you will get consistent behavior between dev and build.
*/ */
format?: 'file' | 'directory'; format?: 'file' | 'directory';
}; };

View file

@ -244,7 +244,13 @@ function addPageName(pathname: string, opts: StaticBuildOptions): void {
opts.pageNames.push(pathname.replace(/^\//, '')); opts.pageNames.push(pathname.replace(/^\//, ''));
} }
function getUrlForPath(pathname: string, base: string, origin: string, format: 'directory' | 'file', routeType: RouteType): URL { function getUrlForPath(
pathname: string,
base: string,
origin: string,
format: 'directory' | 'file',
routeType: RouteType
): URL {
/** /**
* Examples: * Examples:
* pathname: /, /foo * pathname: /, /foo
@ -252,13 +258,14 @@ function getUrlForPath(pathname: string, base: string, origin: string, format: '
*/ */
const ending = format === 'directory' ? '/' : '.html'; const ending = format === 'directory' ? '/' : '.html';
let buildPathname: string; let buildPathname: string;
if(pathname === '/' || pathname === '') { if (pathname === '/' || pathname === '') {
buildPathname = base; buildPathname = base;
} else if(routeType === 'endpoint') { } else if (routeType === 'endpoint') {
const buildPathRelative = removeLeadingForwardSlash(pathname); const buildPathRelative = removeLeadingForwardSlash(pathname);
buildPathname = base + buildPathRelative; buildPathname = base + buildPathRelative;
} else { } else {
const buildPathRelative = removeTrailingForwardSlash(removeLeadingForwardSlash(pathname)) + ending; const buildPathRelative =
removeTrailingForwardSlash(removeLeadingForwardSlash(pathname)) + ending;
buildPathname = base + buildPathRelative; buildPathname = base + buildPathRelative;
} }
const url = new URL(buildPathname, origin); const url = new URL(buildPathname, origin);
@ -312,8 +319,13 @@ async function generatePath(
} }
const ssr = opts.astroConfig.output === 'server'; const ssr = opts.astroConfig.output === 'server';
const url = getUrlForPath(pathname, opts.astroConfig.base, origin, const url = getUrlForPath(
opts.astroConfig.build.format, pageData.route.type); pathname,
opts.astroConfig.base,
origin,
opts.astroConfig.build.format,
pageData.route.type
);
const options: RenderOptions = { const options: RenderOptions = {
adapterName: undefined, adapterName: undefined,
links, links,

View file

@ -3,50 +3,50 @@ import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js'; import { loadFixture } from './test-utils.js';
describe('build.format', () => { describe('build.format', () => {
describe('directory', () => { describe('directory', () => {
/** @type {import('./test-utils').Fixture} */ /** @type {import('./test-utils').Fixture} */
let fixture; let fixture;
before(async () => { before(async () => {
fixture = await loadFixture({ fixture = await loadFixture({
root: './fixtures/page-format/', root: './fixtures/page-format/',
}); });
}); });
describe('Build', () => { describe('Build', () => {
before(async () => { before(async () => {
await fixture.build(); await fixture.build();
}); });
it('relative urls created point to sibling folders', async () => { it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/nested/page/index.html'); let html = await fixture.readFile('/nested/page/index.html');
let $ = cheerio.load(html); let $ = cheerio.load(html);
expect($('#another').attr('href')).to.equal('/nested/page/another/'); expect($('#another').attr('href')).to.equal('/nested/page/another/');
}); });
}); });
}); });
describe('file', () => { describe('file', () => {
/** @type {import('./test-utils').Fixture} */ /** @type {import('./test-utils').Fixture} */
let fixture; let fixture;
before(async () => { before(async () => {
fixture = await loadFixture({ fixture = await loadFixture({
root: './fixtures/page-format/', root: './fixtures/page-format/',
build: { build: {
format: 'file', format: 'file',
}, },
}); });
}); });
describe('Build', () => { describe('Build', () => {
before(async () => { before(async () => {
await fixture.build(); await fixture.build();
}); });
it('relative urls created point to sibling folders', async () => { it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/nested/page.html'); let html = await fixture.readFile('/nested/page.html');
let $ = cheerio.load(html); let $ = cheerio.load(html);
expect($('#another').attr('href')).to.equal('/nested/another/'); expect($('#another').attr('href')).to.equal('/nested/another/');
}); });
}); });
}); });
}); });