Gets Astro.fetchContent compilation to work (#1596)

* Gets Astro.fetchContent compilation to work

This fixes Astro.fetchContent so that we handle esbuild transforming the
name of the nested Astro call.

* Remove debugging

* Update the tests

* Remove another debugger
This commit is contained in:
Matthew Phillips 2021-10-19 14:08:39 -04:00 committed by Drew Powers
parent 2ac2559b5d
commit 94a513adc6
7 changed files with 22 additions and 18 deletions

View file

@ -7,6 +7,6 @@ const { title = 'Jeanine White: Personal Site' } = Astro.props;
<title>{title}</title> <title>{title}</title>
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> <link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/_astro/src/scss/global.css"> <link rel="stylesheet" type="text/css" href="/src/scss/global.scss">
<link rel="preconnect" href="https://fonts.gstatic.com"> <link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@200;400;700;900&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@200;400;700;900&display=swap" rel="stylesheet">

View file

@ -7,7 +7,7 @@ import Footer from '../components/Footer/index.jsx';
import PortfolioPreview from '../components/PortfolioPreview/index.jsx'; import PortfolioPreview from '../components/PortfolioPreview/index.jsx';
// Data Fetching: List all Markdown posts in the repo. // Data Fetching: List all Markdown posts in the repo.
const projects = Astro.fetchContent('./project/**/*.md'); const projects = Astro.fetchContent("./project/**/*.md");
const featuredProject = projects[0]; const featuredProject = projects[0];
// Full Astro Component Syntax: // Full Astro Component Syntax:

View file

@ -1,4 +1,4 @@
// export { default as Code } from './Code.astro'; //export { default as Code } from './Code.astro';
// export { default as Debug } from './Debug.astro'; //export { default as Debug } from './Debug.astro';
export { default as Markdown } from './Markdown.astro'; export { default as Markdown } from './Markdown.astro';
// export { default as Prism } from './Prism.astro'; export { default as Prism } from './Prism.astro';

View file

@ -258,10 +258,10 @@ function createFetchContentFn(url: URL) {
} }
const urlSpec = new URL(spec, url).pathname.replace(/[\\/\\\\]/, '/'); const urlSpec = new URL(spec, url).pathname.replace(/[\\/\\\\]/, '/');
return { return {
...mod.frontmatter,
content: mod.metadata, content: mod.metadata,
metadata: mod.frontmatter,
file: new URL(spec, url), file: new URL(spec, url),
url: urlSpec.includes('/pages/') && urlSpec.replace(/^.*\/pages\//, '/').replace(/\.md$/, ''), url: urlSpec.includes('/pages/') && urlSpec.replace(/^.*\/pages\//, '/').replace(/\.md$/, '')
}; };
}) })
.filter(Boolean); .filter(Boolean);

View file

@ -11,6 +11,9 @@ interface AstroPluginOptions {
devServer?: AstroDevServer; devServer?: AstroDevServer;
} }
// esbuild transforms the component-scoped Astro into Astro2, so need to check both.
const validAstroGlobalNames = new Set(['Astro', 'Astro2']);
export default function astro({ config, devServer }: AstroPluginOptions): Plugin { export default function astro({ config, devServer }: AstroPluginOptions): Plugin {
return { return {
name: '@astrojs/vite-plugin-astro-postprocess', name: '@astrojs/vite-plugin-astro-postprocess',
@ -38,7 +41,7 @@ export default function astro({ config, devServer }: AstroPluginOptions): Plugin
if ( if (
path.parent.type !== 'CallExpression' || path.parent.type !== 'CallExpression' ||
path.parent.callee.type !== 'MemberExpression' || path.parent.callee.type !== 'MemberExpression' ||
(path.parent.callee.object as any).name !== 'Astro' || !validAstroGlobalNames.has((path.parent.callee.object as any).name) ||
(path.parent.callee.property as any).name !== 'fetchContent' (path.parent.callee.property as any).name !== 'fetchContent'
) { ) {
return; return;
@ -68,6 +71,7 @@ export default function astro({ config, devServer }: AstroPluginOptions): Plugin
if (!result || !result.code) { if (!result || !result.code) {
return null; return null;
} }
return { code: result.code, map: result.map }; return { code: result.code, map: result.map };
}, },
}; };

View file

@ -18,7 +18,7 @@ describe('RSS Generation', () => {
it('generates RSS correctly', async () => { it('generates RSS correctly', async () => {
const rss = await fixture.readFile('/custom/feed.xml'); const rss = await fixture.readFile('/custom/feed.xml');
expect(rss).to.equal( expect(rss).to.equal(
`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[MF Doomcast]]></title><description><![CDATA[The podcast about the things you find on a picnic, or at a picnic table]]></description><link>https://astro.build/custom/feed.xml</link><language>en-us</language><itunes:author>MF Doom</itunes:author><item><title><![CDATA[Fazers]]></title><link>https://astro.build/episode/fazers/</link><description><![CDATA[Rhapsody ranked Take Me to Your Leader 17th on its list “Hip-Hops Best Albums of the Decade”]]></description><pubDate>Thu, 03 Jul 2003 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>197</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]></title><link>https://astro.build/episode/rap-snitch-knishes/</link><description><![CDATA[Complex named this song the “22nd funniest rap song of all time.”]]></description><pubDate>Tue, 16 Nov 2004 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>172</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]></title><link>https://astro.build/episode/rhymes-like-dimes/</link><description><![CDATA[Operation: Doomsday has been heralded as an underground classic that established MF Doom's rank within the underground hip-hop scene during the early to mid-2000s. `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[MF Doomcast]]></title><description><![CDATA[The podcast about the things you find on a picnic, or at a picnic table]]></description><link>https://astro.build/custom/feed.xml</link><language>en-us</language><itunes:author>MF Doom</itunes:author><item><title><![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]></title><link>https://astro.build/episode/rap-snitch-knishes/</link><description><![CDATA[Complex named this song the “22nd funniest rap song of all time.”]]></description><pubDate>Tue, 16 Nov 2004 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>172</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Fazers]]></title><link>https://astro.build/episode/fazers/</link><description><![CDATA[Rhapsody ranked Take Me to Your Leader 17th on its list “Hip-Hops Best Albums of the Decade”]]></description><pubDate>Thu, 03 Jul 2003 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>197</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]></title><link>https://astro.build/episode/rhymes-like-dimes/</link><description><![CDATA[Operation: Doomsday has been heralded as an underground classic that established MF Doom's rank within the underground hip-hop scene during the early to mid-2000s.
]]></description><pubDate>Tue, 19 Oct 1999 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>259</itunes:duration><itunes:explicit>true</itunes:explicit></item></channel></rss>` ]]></description><pubDate>Tue, 19 Oct 1999 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>259</itunes:duration><itunes:explicit>true</itunes:explicit></item></channel></rss>`
); );
}); });
@ -31,4 +31,4 @@ describe('Sitemap Generation', () => {
`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://astro.build/404/index.html</loc></url><url><loc>https://astro.build/episode/fazers/index.html</loc></url><url><loc>https://astro.build/episode/rap-snitch-knishes/index.html</loc></url><url><loc>https://astro.build/episode/rhymes-like-dimes/index.html</loc></url><url><loc>https://astro.build/episodes/index.html</loc></url></urlset>\n` `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://astro.build/404/index.html</loc></url><url><loc>https://astro.build/episode/fazers/index.html</loc></url><url><loc>https://astro.build/episode/rap-snitch-knishes/index.html</loc></url><url><loc>https://astro.build/episode/rhymes-like-dimes/index.html</loc></url><url><loc>https://astro.build/episodes/index.html</loc></url></urlset>\n`
); );
}); });
}); });

View file

@ -10,14 +10,14 @@ export function getStaticPaths({paginate, rss}) {
}, },
customData: `<language>en-us</language>` + customData: `<language>en-us</language>` +
`<itunes:author>MF Doom</itunes:author>`, `<itunes:author>MF Doom</itunes:author>`,
items: episodes.map(({ metadata, url }) => ({ items: episodes.map((episode) => ({
title: metadata.title, title: episode.title,
link: url, link: episode.url,
description: metadata.description, description: episode.description,
pubDate: metadata.pubDate + 'Z', pubDate: episode.pubDate + 'Z',
customData: `<itunes:episodeType>${metadata.type}</itunes:episodeType>` + customData: `<itunes:episodeType>${episode.type}</itunes:episodeType>` +
`<itunes:duration>${metadata.duration}</itunes:duration>` + `<itunes:duration>${episode.duration}</itunes:duration>` +
`<itunes:explicit>${metadata.explicit || false}</itunes:explicit>`, `<itunes:explicit>${episode.explicit || false}</itunes:explicit>`,
})), })),
dest: '/custom/feed.xml', dest: '/custom/feed.xml',
}); });