From 938ad514cd75c09756cd24223346159172f5fd60 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 8 Feb 2023 17:15:33 -0500 Subject: [PATCH] Ensure base configuration appended to content collection styles (#6182) * Fix, base appended to propagated scripts * Test scripts --- .changeset/late-poets-own.md | 5 ++++ .../src/content/vite-plugin-content-assets.ts | 5 ++-- .../astro/test/content-collections.test.js | 23 +++++++++++++++++++ .../content-collections-base/astro.config.mjs | 13 +++++++++++ .../content-collections-base/package.json | 9 ++++++++ .../src/components/One.astro | 7 ++++++ .../src/content/config.ts | 12 ++++++++++ .../src/content/docs/one.mdx | 11 +++++++++ .../src/pages/docs.astro | 17 ++++++++++++++ pnpm-lock.yaml | 8 +++++++ 10 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 .changeset/late-poets-own.md create mode 100644 packages/astro/test/fixtures/content-collections-base/astro.config.mjs create mode 100644 packages/astro/test/fixtures/content-collections-base/package.json create mode 100644 packages/astro/test/fixtures/content-collections-base/src/components/One.astro create mode 100644 packages/astro/test/fixtures/content-collections-base/src/content/config.ts create mode 100644 packages/astro/test/fixtures/content-collections-base/src/content/docs/one.mdx create mode 100644 packages/astro/test/fixtures/content-collections-base/src/pages/docs.astro diff --git a/.changeset/late-poets-own.md b/.changeset/late-poets-own.md new file mode 100644 index 000000000000..1055bc71af05 --- /dev/null +++ b/.changeset/late-poets-own.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Ensure base configuration appended to content collection styles diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index 74378f4e0f50..c09162177eda 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -98,6 +98,7 @@ export function astroConfigBuildPlugin( }, 'build:post': ({ ssrOutputs, clientOutputs, mutate }) => { const outputs = ssrOutputs.flatMap((o) => o.output); + const prependBase = (src: string) => prependForwardSlash(npath.posix.join(options.settings.config.base, src)); for (const chunk of outputs) { if ( chunk.type === 'chunk' && @@ -133,7 +134,7 @@ export function astroConfigBuildPlugin( if (entryCSS.size) { newCode = newCode.replace( JSON.stringify(LINKS_PLACEHOLDER), - JSON.stringify([...entryCSS]) + JSON.stringify(Array.from(entryCSS).map(prependBase)) ); } if (entryScripts.size) { @@ -153,7 +154,7 @@ export function astroConfigBuildPlugin( JSON.stringify( [...entryFileNames].map((src) => ({ props: { - src: prependForwardSlash(npath.posix.join(options.settings.config.base, src)), + src: prependBase(src), type: 'module', }, children: '', diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js index c8105184e83b..148fa01cad4f 100644 --- a/packages/astro/test/content-collections.test.js +++ b/packages/astro/test/content-collections.test.js @@ -243,4 +243,27 @@ describe('Content Collections', () => { } }); }); + + describe('Base configuration', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/content-collections-base/', + }); + await fixture.build(); + }); + + it('Includes base in links', async () => { + const html = await fixture.readFile('/docs/index.html'); + const $ = cheerio.load(html); + expect($('link').attr('href')).to.satisfies(a => a.startsWith('/docs')) + }); + + it('Includes base in hoisted scripts', async () => { + const html = await fixture.readFile('/docs/index.html'); + const $ = cheerio.load(html); + expect($('script').attr('src')).to.satisfies(a => a.startsWith('/docs')) + }); + }); }); diff --git a/packages/astro/test/fixtures/content-collections-base/astro.config.mjs b/packages/astro/test/fixtures/content-collections-base/astro.config.mjs new file mode 100644 index 000000000000..b11c49cea26b --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-base/astro.config.mjs @@ -0,0 +1,13 @@ +import { defineConfig } from 'astro/config'; +import mdx from '@astrojs/mdx'; + +// https://astro.build/config +export default defineConfig({ + base: '/docs', + integrations: [mdx()], + vite: { + build: { + assetsInlineLimit: 0 + } + } +}); diff --git a/packages/astro/test/fixtures/content-collections-base/package.json b/packages/astro/test/fixtures/content-collections-base/package.json new file mode 100644 index 000000000000..152a02919dae --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-base/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/content-collections-base", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/mdx": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/content-collections-base/src/components/One.astro b/packages/astro/test/fixtures/content-collections-base/src/components/One.astro new file mode 100644 index 000000000000..69dda4a61ecd --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-base/src/components/One.astro @@ -0,0 +1,7 @@ + + +
Testing
diff --git a/packages/astro/test/fixtures/content-collections-base/src/content/config.ts b/packages/astro/test/fixtures/content-collections-base/src/content/config.ts new file mode 100644 index 000000000000..439a3533a9c6 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-base/src/content/config.ts @@ -0,0 +1,12 @@ +import { z, defineCollection } from 'astro:content'; + + +const docs = defineCollection({ + schema: z.object({ + title: z.string(), + }), +}); + +export const collections = { + docs, +} diff --git a/packages/astro/test/fixtures/content-collections-base/src/content/docs/one.mdx b/packages/astro/test/fixtures/content-collections-base/src/content/docs/one.mdx new file mode 100644 index 000000000000..223b22a409b5 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-base/src/content/docs/one.mdx @@ -0,0 +1,11 @@ +--- +title: One +--- + +import One from '../../components/One.astro'; + +# Title + +stuff + + diff --git a/packages/astro/test/fixtures/content-collections-base/src/pages/docs.astro b/packages/astro/test/fixtures/content-collections-base/src/pages/docs.astro new file mode 100644 index 000000000000..6b352ce371a8 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-base/src/pages/docs.astro @@ -0,0 +1,17 @@ +--- +import { getEntryBySlug } from 'astro:content'; +const entry = await getEntryBySlug('docs', 'one'); +const { Content } = await entry.render(); +--- + + + + + It's content time! + + +
+ +
+ + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a296a150d318..3e1a40bf7ea0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1663,6 +1663,14 @@ importers: '@astrojs/mdx': link:../../../../integrations/mdx astro: link:../../.. + packages/astro/test/fixtures/content-collections-base: + specifiers: + '@astrojs/mdx': workspace:* + astro: workspace:* + dependencies: + '@astrojs/mdx': link:../../../../integrations/mdx + astro: link:../../.. + packages/astro/test/fixtures/content-ssr-integration: specifiers: '@astrojs/mdx': workspace:*