From 9bec6bc410f324a41c67e5d185fa86f78d7625f2 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 2 Feb 2023 14:21:32 -0500 Subject: [PATCH 01/12] Prevent eager rendering of head content in multi-level MDX layout (#6107) * Prevent eager rendering of head content in multi-level MDX layout * Adding a changeset * Remove old comment * Keep track of slot position as well --- .changeset/lucky-hounds-rhyme.md | 5 +++++ packages/astro/src/@types/astro.ts | 4 ++++ packages/astro/src/core/render/result.ts | 1 + packages/astro/src/runtime/server/jsx.ts | 2 ++ .../astro/src/runtime/server/render/astro/factory.ts | 2 ++ packages/astro/src/runtime/server/render/head.ts | 10 +++++++++- packages/astro/src/runtime/server/render/slot.ts | 6 +++++- packages/astro/src/runtime/server/render/util.ts | 6 ++++++ packages/integrations/mdx/test/css-head-mdx.test.js | 4 ++-- 9 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 .changeset/lucky-hounds-rhyme.md diff --git a/.changeset/lucky-hounds-rhyme.md b/.changeset/lucky-hounds-rhyme.md new file mode 100644 index 000000000..51b1c012b --- /dev/null +++ b/.changeset/lucky-hounds-rhyme.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes head contents being placed in body in MDX components diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 7fab1556f..ea3e5cd3c 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1449,6 +1449,10 @@ export interface SSRResult { ): AstroGlobal; resolve: (s: string) => Promise; response: ResponseInit; + // Bits 1 = astro, 2 = jsx, 4 = slot + // As rendering occurs these bits are manipulated to determine where content + // is within a slot. This is used for head injection. + scope: number; _metadata: SSRMetadata; } diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 06c5e0698..3a6b0bf96 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -156,6 +156,7 @@ export function createResult(args: CreateResultArgs): SSRResult { propagation: args.propagation ?? new Map(), propagators: new Map(), extraHead: [], + scope: 0, cookies, /** This function returns the `Astro` faux-global */ createAstro( diff --git a/packages/astro/src/runtime/server/jsx.ts b/packages/astro/src/runtime/server/jsx.ts index 651ccc945..365345a56 100644 --- a/packages/astro/src/runtime/server/jsx.ts +++ b/packages/astro/src/runtime/server/jsx.ts @@ -11,6 +11,7 @@ import { voidElementNames, } from './index.js'; import { HTMLParts } from './render/common.js'; +import { ScopeFlags } from './render/util.js'; import type { ComponentIterable } from './render/component'; const ClientOnlyPlaceholder = 'astro-client-only'; @@ -94,6 +95,7 @@ Did you forget to import the component or is it possible there is a typo?`); props[key] = value; } } + result.scope |= ScopeFlags.JSX; return markHTMLString(await renderToString(result, vnode.type as any, props, slots)); } case !vnode.type && (vnode.type as any) !== 0: diff --git a/packages/astro/src/runtime/server/render/astro/factory.ts b/packages/astro/src/runtime/server/render/astro/factory.ts index 0d8ffb787..a1e7d0611 100644 --- a/packages/astro/src/runtime/server/render/astro/factory.ts +++ b/packages/astro/src/runtime/server/render/astro/factory.ts @@ -5,6 +5,7 @@ import type { RenderTemplateResult } from './render-template'; import { HTMLParts } from '../common.js'; import { isHeadAndContent } from './head-and-content.js'; import { renderAstroTemplateResult } from './render-template.js'; +import { ScopeFlags } from '../util.js'; export type AstroFactoryReturnValue = RenderTemplateResult | Response | HeadAndContent; @@ -27,6 +28,7 @@ export async function renderToString( props: any, children: any ): Promise { + result.scope |= ScopeFlags.Astro; const factoryResult = await componentFactory(result, props, children); if (factoryResult instanceof Response) { diff --git a/packages/astro/src/runtime/server/render/head.ts b/packages/astro/src/runtime/server/render/head.ts index ade6a7355..72be58623 100644 --- a/packages/astro/src/runtime/server/render/head.ts +++ b/packages/astro/src/runtime/server/render/head.ts @@ -1,7 +1,7 @@ import type { SSRResult } from '../../../@types/astro'; import { markHTMLString } from '../escape.js'; -import { renderElement } from './util.js'; +import { renderElement, ScopeFlags } from './util.js'; // Filter out duplicate elements in our set const uniqueElements = (item: any, index: number, all: any[]) => { @@ -52,6 +52,14 @@ export function* maybeRenderHead(result: SSRResult) { return; } + // Don't render the head inside of a JSX component that's inside of an Astro component + // as the Astro component will be the one to render the head. + switch(result.scope) { + case ScopeFlags.JSX | ScopeFlags.Slot | ScopeFlags.Astro: { + return; + } + } + // This is an instruction informing the page rendering that head might need rendering. // This allows the page to deduplicate head injections. yield { type: 'head', result } as const; diff --git a/packages/astro/src/runtime/server/render/slot.ts b/packages/astro/src/runtime/server/render/slot.ts index cd9225be4..1e2e946c3 100644 --- a/packages/astro/src/runtime/server/render/slot.ts +++ b/packages/astro/src/runtime/server/render/slot.ts @@ -3,6 +3,7 @@ import type { RenderInstruction } from './types.js'; import { HTMLString, markHTMLString } from '../escape.js'; import { renderChild } from './any.js'; +import { ScopeFlags } from './util.js'; const slotString = Symbol.for('astro:slot-string'); @@ -20,8 +21,9 @@ export function isSlotString(str: string): str is any { return !!(str as any)[slotString]; } -export async function renderSlot(_result: any, slotted: string, fallback?: any): Promise { +export async function renderSlot(result: SSRResult, slotted: string, fallback?: any): Promise { if (slotted) { + result.scope |= ScopeFlags.Slot; let iterator = renderChild(slotted); let content = ''; let instructions: null | RenderInstruction[] = null; @@ -35,6 +37,8 @@ export async function renderSlot(_result: any, slotted: string, fallback?: any): content += chunk; } } + // Remove the flag since we are now outside of the scope. + result.scope &= ~ScopeFlags.Slot; return markHTMLString(new SlotString(content, instructions)); } return fallback; diff --git a/packages/astro/src/runtime/server/render/util.ts b/packages/astro/src/runtime/server/render/util.ts index a95ef16f8..3986f9d50 100644 --- a/packages/astro/src/runtime/server/render/util.ts +++ b/packages/astro/src/runtime/server/render/util.ts @@ -128,3 +128,9 @@ export function renderElement( } return `<${name}${internalSpreadAttributes(props, shouldEscape)}>${children}`; } + +export const ScopeFlags = { + Astro: 1 << 0, + JSX: 1 << 1, + Slot: 1 << 2 +}; diff --git a/packages/integrations/mdx/test/css-head-mdx.test.js b/packages/integrations/mdx/test/css-head-mdx.test.js index a6492c3ba..2b1dcdfe7 100644 --- a/packages/integrations/mdx/test/css-head-mdx.test.js +++ b/packages/integrations/mdx/test/css-head-mdx.test.js @@ -23,10 +23,10 @@ describe('Head injection w/ MDX', () => { const html = await fixture.readFile('/indexThree/index.html'); const { document } = parseHTML(html); - const links = document.querySelectorAll('link[rel=stylesheet]'); + const links = document.querySelectorAll('head link[rel=stylesheet]'); expect(links).to.have.a.lengthOf(1); - const scripts = document.querySelectorAll('script[type=module]'); + const scripts = document.querySelectorAll('head script[type=module]'); expect(scripts).to.have.a.lengthOf(1); }); }); From 10550f5ee2f63fe0bba5a6f6533244a021b468ad Mon Sep 17 00:00:00 2001 From: matthewp Date: Thu, 2 Feb 2023 19:23:44 +0000 Subject: [PATCH 02/12] [ci] format --- packages/astro/src/runtime/server/jsx.ts | 4 ++-- packages/astro/src/runtime/server/render/astro/factory.ts | 2 +- packages/astro/src/runtime/server/render/head.ts | 2 +- packages/astro/src/runtime/server/render/slot.ts | 6 +++++- packages/astro/src/runtime/server/render/util.ts | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/astro/src/runtime/server/jsx.ts b/packages/astro/src/runtime/server/jsx.ts index 365345a56..8b81a641e 100644 --- a/packages/astro/src/runtime/server/jsx.ts +++ b/packages/astro/src/runtime/server/jsx.ts @@ -11,8 +11,8 @@ import { voidElementNames, } from './index.js'; import { HTMLParts } from './render/common.js'; -import { ScopeFlags } from './render/util.js'; import type { ComponentIterable } from './render/component'; +import { ScopeFlags } from './render/util.js'; const ClientOnlyPlaceholder = 'astro-client-only'; @@ -95,7 +95,7 @@ Did you forget to import the component or is it possible there is a typo?`); props[key] = value; } } - result.scope |= ScopeFlags.JSX; + result.scope |= ScopeFlags.JSX; return markHTMLString(await renderToString(result, vnode.type as any, props, slots)); } case !vnode.type && (vnode.type as any) !== 0: diff --git a/packages/astro/src/runtime/server/render/astro/factory.ts b/packages/astro/src/runtime/server/render/astro/factory.ts index a1e7d0611..1f85fe45c 100644 --- a/packages/astro/src/runtime/server/render/astro/factory.ts +++ b/packages/astro/src/runtime/server/render/astro/factory.ts @@ -3,9 +3,9 @@ import type { HeadAndContent } from './head-and-content'; import type { RenderTemplateResult } from './render-template'; import { HTMLParts } from '../common.js'; +import { ScopeFlags } from '../util.js'; import { isHeadAndContent } from './head-and-content.js'; import { renderAstroTemplateResult } from './render-template.js'; -import { ScopeFlags } from '../util.js'; export type AstroFactoryReturnValue = RenderTemplateResult | Response | HeadAndContent; diff --git a/packages/astro/src/runtime/server/render/head.ts b/packages/astro/src/runtime/server/render/head.ts index 72be58623..0f39fe219 100644 --- a/packages/astro/src/runtime/server/render/head.ts +++ b/packages/astro/src/runtime/server/render/head.ts @@ -54,7 +54,7 @@ export function* maybeRenderHead(result: SSRResult) { // Don't render the head inside of a JSX component that's inside of an Astro component // as the Astro component will be the one to render the head. - switch(result.scope) { + switch (result.scope) { case ScopeFlags.JSX | ScopeFlags.Slot | ScopeFlags.Astro: { return; } diff --git a/packages/astro/src/runtime/server/render/slot.ts b/packages/astro/src/runtime/server/render/slot.ts index 1e2e946c3..32d0a2dc1 100644 --- a/packages/astro/src/runtime/server/render/slot.ts +++ b/packages/astro/src/runtime/server/render/slot.ts @@ -21,7 +21,11 @@ export function isSlotString(str: string): str is any { return !!(str as any)[slotString]; } -export async function renderSlot(result: SSRResult, slotted: string, fallback?: any): Promise { +export async function renderSlot( + result: SSRResult, + slotted: string, + fallback?: any +): Promise { if (slotted) { result.scope |= ScopeFlags.Slot; let iterator = renderChild(slotted); diff --git a/packages/astro/src/runtime/server/render/util.ts b/packages/astro/src/runtime/server/render/util.ts index 3986f9d50..7e5ca9a5d 100644 --- a/packages/astro/src/runtime/server/render/util.ts +++ b/packages/astro/src/runtime/server/render/util.ts @@ -132,5 +132,5 @@ export function renderElement( export const ScopeFlags = { Astro: 1 << 0, JSX: 1 << 1, - Slot: 1 << 2 + Slot: 1 << 2, }; From 81615c50047a8c815ea7211eaf8e6fd31eba16be Mon Sep 17 00:00:00 2001 From: "Houston (Bot)" <108291165+astrobot-houston@users.noreply.github.com> Date: Thu, 2 Feb 2023 12:38:04 -0800 Subject: [PATCH 03/12] [ci] release (#6094) Co-authored-by: github-actions[bot] --- .changeset/cold-maps-wash.md | 5 -- .changeset/lucky-hounds-rhyme.md | 5 -- .changeset/wild-seas-happen.md | 5 -- examples/basics/package.json | 2 +- examples/blog/package.json | 2 +- examples/component/package.json | 2 +- examples/deno/package.json | 2 +- examples/docs/package.json | 2 +- examples/framework-alpine/package.json | 2 +- examples/framework-lit/package.json | 2 +- examples/framework-multiple/package.json | 4 +- examples/framework-preact/package.json | 2 +- examples/framework-react/package.json | 2 +- examples/framework-solid/package.json | 4 +- examples/framework-svelte/package.json | 2 +- examples/framework-vue/package.json | 2 +- examples/hackernews/package.json | 2 +- examples/integration/package.json | 2 +- examples/minimal/package.json | 2 +- examples/non-html-pages/package.json | 2 +- examples/portfolio/package.json | 2 +- examples/ssr/package.json | 2 +- examples/with-markdown-plugins/package.json | 2 +- examples/with-markdown-shiki/package.json | 2 +- examples/with-mdx/package.json | 2 +- examples/with-nanostores/package.json | 2 +- examples/with-tailwindcss/package.json | 2 +- examples/with-vite-plugin-pwa/package.json | 2 +- examples/with-vitest/package.json | 2 +- packages/astro/CHANGELOG.md | 6 ++ packages/astro/package.json | 2 +- packages/integrations/cloudflare/package.json | 2 +- packages/integrations/deno/package.json | 2 +- packages/integrations/image/package.json | 2 +- packages/integrations/netlify/package.json | 2 +- packages/integrations/node/package.json | 2 +- packages/integrations/solid/CHANGELOG.md | 6 ++ packages/integrations/solid/package.json | 2 +- packages/integrations/svelte/package.json | 2 +- packages/integrations/tailwind/package.json | 2 +- packages/integrations/vercel/CHANGELOG.md | 9 +++ packages/integrations/vercel/package.json | 4 +- packages/integrations/vue/package.json | 2 +- pnpm-lock.yaml | 56 +++++++++---------- 44 files changed, 89 insertions(+), 83 deletions(-) delete mode 100644 .changeset/cold-maps-wash.md delete mode 100644 .changeset/lucky-hounds-rhyme.md delete mode 100644 .changeset/wild-seas-happen.md diff --git a/.changeset/cold-maps-wash.md b/.changeset/cold-maps-wash.md deleted file mode 100644 index 406ce84f9..000000000 --- a/.changeset/cold-maps-wash.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@astrojs/solid-js': patch ---- - -Bump vitefu for peerDep warning with Vite 4 diff --git a/.changeset/lucky-hounds-rhyme.md b/.changeset/lucky-hounds-rhyme.md deleted file mode 100644 index 51b1c012b..000000000 --- a/.changeset/lucky-hounds-rhyme.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'astro': patch ---- - -Fixes head contents being placed in body in MDX components diff --git a/.changeset/wild-seas-happen.md b/.changeset/wild-seas-happen.md deleted file mode 100644 index 8db21e433..000000000 --- a/.changeset/wild-seas-happen.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@astrojs/vercel': patch ---- - -Added second build step through esbuild, to allow framework defined build (vite build) and target defined bundling (esbuilt step) diff --git a/examples/basics/package.json b/examples/basics/package.json index 9dffd8bd0..3b1ad504d 100644 --- a/examples/basics/package.json +++ b/examples/basics/package.json @@ -11,6 +11,6 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" } } diff --git a/examples/blog/package.json b/examples/blog/package.json index 87758e519..05e470780 100644 --- a/examples/blog/package.json +++ b/examples/blog/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "@astrojs/mdx": "^0.16.0", "@astrojs/rss": "^2.1.0", "@astrojs/sitemap": "^1.0.1" diff --git a/examples/component/package.json b/examples/component/package.json index 85a9226a0..7805f0d35 100644 --- a/examples/component/package.json +++ b/examples/component/package.json @@ -15,7 +15,7 @@ ], "scripts": {}, "devDependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" }, "peerDependencies": { "astro": "^2.0.0-beta.0" diff --git a/examples/deno/package.json b/examples/deno/package.json index 27413f456..b8ef2344f 100644 --- a/examples/deno/package.json +++ b/examples/deno/package.json @@ -10,7 +10,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" }, "devDependencies": { "@astrojs/deno": "^4.0.0" diff --git a/examples/docs/package.json b/examples/docs/package.json index b04aa7adf..4f93b0ef7 100644 --- a/examples/docs/package.json +++ b/examples/docs/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "preact": "^10.7.3", "react": "^18.1.0", "react-dom": "^18.1.0", diff --git a/examples/framework-alpine/package.json b/examples/framework-alpine/package.json index 1ce785196..d8c56040c 100644 --- a/examples/framework-alpine/package.json +++ b/examples/framework-alpine/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "alpinejs": "^3.10.2", "@astrojs/alpinejs": "^0.1.3", "@types/alpinejs": "^3.7.0" diff --git a/examples/framework-lit/package.json b/examples/framework-lit/package.json index 70c787dca..ee9c84e9d 100644 --- a/examples/framework-lit/package.json +++ b/examples/framework-lit/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "lit": "^2.2.5", "@astrojs/lit": "^1.1.2", "@webcomponents/template-shadowroot": "^0.1.0" diff --git a/examples/framework-multiple/package.json b/examples/framework-multiple/package.json index 5896c198a..25cc08b83 100644 --- a/examples/framework-multiple/package.json +++ b/examples/framework-multiple/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "preact": "^10.7.3", "react": "^18.1.0", "react-dom": "^18.1.0", @@ -20,7 +20,7 @@ "vue": "^3.2.37", "@astrojs/preact": "^2.0.1", "@astrojs/react": "^2.0.2", - "@astrojs/solid-js": "^2.0.1", + "@astrojs/solid-js": "^2.0.2", "@astrojs/svelte": "^2.0.1", "@astrojs/vue": "^2.0.1" } diff --git a/examples/framework-preact/package.json b/examples/framework-preact/package.json index e93413c48..e9056bca6 100644 --- a/examples/framework-preact/package.json +++ b/examples/framework-preact/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "preact": "^10.7.3", "@astrojs/preact": "^2.0.1", "@preact/signals": "^1.1.0" diff --git a/examples/framework-react/package.json b/examples/framework-react/package.json index 94cac639e..bd8f92499 100644 --- a/examples/framework-react/package.json +++ b/examples/framework-react/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "react": "^18.1.0", "react-dom": "^18.1.0", "@astrojs/react": "^2.0.2", diff --git a/examples/framework-solid/package.json b/examples/framework-solid/package.json index 81d474e6f..8c6531572 100644 --- a/examples/framework-solid/package.json +++ b/examples/framework-solid/package.json @@ -11,8 +11,8 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "solid-js": "^1.4.3", - "@astrojs/solid-js": "^2.0.1" + "@astrojs/solid-js": "^2.0.2" } } diff --git a/examples/framework-svelte/package.json b/examples/framework-svelte/package.json index fcc6ae58f..7ead21391 100644 --- a/examples/framework-svelte/package.json +++ b/examples/framework-svelte/package.json @@ -13,6 +13,6 @@ "dependencies": { "svelte": "^3.48.0", "@astrojs/svelte": "^2.0.1", - "astro": "^2.0.5" + "astro": "^2.0.6" } } diff --git a/examples/framework-vue/package.json b/examples/framework-vue/package.json index f0085d7d4..163ae9cd2 100644 --- a/examples/framework-vue/package.json +++ b/examples/framework-vue/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "vue": "^3.2.37", "@astrojs/vue": "^2.0.1" } diff --git a/examples/hackernews/package.json b/examples/hackernews/package.json index b344dd59b..354490854 100644 --- a/examples/hackernews/package.json +++ b/examples/hackernews/package.json @@ -12,6 +12,6 @@ }, "dependencies": { "@astrojs/node": "^5.0.2", - "astro": "^2.0.5" + "astro": "^2.0.6" } } diff --git a/examples/integration/package.json b/examples/integration/package.json index f0139095a..456e057c8 100644 --- a/examples/integration/package.json +++ b/examples/integration/package.json @@ -15,7 +15,7 @@ ], "scripts": {}, "devDependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" }, "peerDependencies": { "astro": "^2.0.0-beta.0" diff --git a/examples/minimal/package.json b/examples/minimal/package.json index 8c42d3011..ac96c9421 100644 --- a/examples/minimal/package.json +++ b/examples/minimal/package.json @@ -11,6 +11,6 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" } } diff --git a/examples/non-html-pages/package.json b/examples/non-html-pages/package.json index 12967c666..c71776849 100644 --- a/examples/non-html-pages/package.json +++ b/examples/non-html-pages/package.json @@ -11,6 +11,6 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" } } diff --git a/examples/portfolio/package.json b/examples/portfolio/package.json index c7b52087e..bc8919d2c 100644 --- a/examples/portfolio/package.json +++ b/examples/portfolio/package.json @@ -11,6 +11,6 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" } } diff --git a/examples/ssr/package.json b/examples/ssr/package.json index 6b876c0ce..87a20eae0 100644 --- a/examples/ssr/package.json +++ b/examples/ssr/package.json @@ -12,7 +12,7 @@ "server": "node dist/server/entry.mjs" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "svelte": "^3.48.0", "@astrojs/svelte": "^2.0.1", "@astrojs/node": "^5.0.2", diff --git a/examples/with-markdown-plugins/package.json b/examples/with-markdown-plugins/package.json index 7f78949c7..1cd521f88 100644 --- a/examples/with-markdown-plugins/package.json +++ b/examples/with-markdown-plugins/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "@astrojs/markdown-remark": "^2.0.1", "hast-util-select": "5.0.1", "rehype-autolink-headings": "^6.1.1", diff --git a/examples/with-markdown-shiki/package.json b/examples/with-markdown-shiki/package.json index 502397bf0..8d118f884 100644 --- a/examples/with-markdown-shiki/package.json +++ b/examples/with-markdown-shiki/package.json @@ -11,6 +11,6 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5" + "astro": "^2.0.6" } } diff --git a/examples/with-mdx/package.json b/examples/with-mdx/package.json index d7d98fded..fd46013e3 100644 --- a/examples/with-mdx/package.json +++ b/examples/with-mdx/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "preact": "^10.6.5", "@astrojs/preact": "^2.0.1", "@astrojs/mdx": "^0.16.0" diff --git a/examples/with-nanostores/package.json b/examples/with-nanostores/package.json index feb42be03..931c51190 100644 --- a/examples/with-nanostores/package.json +++ b/examples/with-nanostores/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "preact": "^10.7.3", "@astrojs/preact": "^2.0.1", "nanostores": "^0.5.12", diff --git a/examples/with-tailwindcss/package.json b/examples/with-tailwindcss/package.json index 5a7872712..1b82fc4a9 100644 --- a/examples/with-tailwindcss/package.json +++ b/examples/with-tailwindcss/package.json @@ -14,7 +14,7 @@ "@astrojs/mdx": "^0.16.0", "@astrojs/tailwind": "^3.0.1", "@types/canvas-confetti": "^1.4.3", - "astro": "^2.0.5", + "astro": "^2.0.6", "autoprefixer": "^10.4.7", "canvas-confetti": "^1.5.1", "postcss": "^8.4.14", diff --git a/examples/with-vite-plugin-pwa/package.json b/examples/with-vite-plugin-pwa/package.json index 7fe378a68..22d1e6f32 100644 --- a/examples/with-vite-plugin-pwa/package.json +++ b/examples/with-vite-plugin-pwa/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "vite-plugin-pwa": "0.11.11", "workbox-window": "^6.5.3" } diff --git a/examples/with-vitest/package.json b/examples/with-vitest/package.json index 921404bbf..f5db2f873 100644 --- a/examples/with-vitest/package.json +++ b/examples/with-vitest/package.json @@ -12,7 +12,7 @@ "test": "vitest" }, "dependencies": { - "astro": "^2.0.5", + "astro": "^2.0.6", "vitest": "^0.20.3" } } diff --git a/packages/astro/CHANGELOG.md b/packages/astro/CHANGELOG.md index a301de2d6..c20c4e628 100644 --- a/packages/astro/CHANGELOG.md +++ b/packages/astro/CHANGELOG.md @@ -1,5 +1,11 @@ # astro +## 2.0.6 + +### Patch Changes + +- [#6107](https://github.com/withastro/astro/pull/6107) [`9bec6bc41`](https://github.com/withastro/astro/commit/9bec6bc410f324a41c67e5d185fa86f78d7625f2) Thanks [@matthewp](https://github.com/matthewp)! - Fixes head contents being placed in body in MDX components + ## 2.0.5 ### Patch Changes diff --git a/packages/astro/package.json b/packages/astro/package.json index 5284ba51c..9b189a73b 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -1,6 +1,6 @@ { "name": "astro", - "version": "2.0.5", + "version": "2.0.6", "description": "Astro is a modern site builder with web best practices, performance, and DX front-of-mind.", "type": "module", "author": "withastro", diff --git a/packages/integrations/cloudflare/package.json b/packages/integrations/cloudflare/package.json index a2e8ad1da..8f90141ac 100644 --- a/packages/integrations/cloudflare/package.json +++ b/packages/integrations/cloudflare/package.json @@ -38,7 +38,7 @@ "tiny-glob": "^0.2.9" }, "peerDependencies": { - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "devDependencies": { "astro": "workspace:*", diff --git a/packages/integrations/deno/package.json b/packages/integrations/deno/package.json index 528bc266f..f32b5d341 100644 --- a/packages/integrations/deno/package.json +++ b/packages/integrations/deno/package.json @@ -32,7 +32,7 @@ "esbuild": "^0.15.18" }, "peerDependencies": { - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "devDependencies": { "astro": "workspace:*", diff --git a/packages/integrations/image/package.json b/packages/integrations/image/package.json index 6c84fdcad..532ed0b9e 100644 --- a/packages/integrations/image/package.json +++ b/packages/integrations/image/package.json @@ -63,7 +63,7 @@ "vite": "^4.0.3" }, "peerDependencies": { - "astro": "workspace:^2.0.5", + "astro": "workspace:^2.0.6", "sharp": ">=0.31.0" }, "peerDependenciesMeta": { diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index bba5f7709..25695ea79 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -39,7 +39,7 @@ "esbuild": "^0.15.18" }, "peerDependencies": { - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "devDependencies": { "@netlify/edge-handler-types": "^0.34.1", diff --git a/packages/integrations/node/package.json b/packages/integrations/node/package.json index 57ffc50ae..39060dcf3 100644 --- a/packages/integrations/node/package.json +++ b/packages/integrations/node/package.json @@ -34,7 +34,7 @@ "send": "^0.18.0" }, "peerDependencies": { - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "devDependencies": { "@types/send": "^0.17.1", diff --git a/packages/integrations/solid/CHANGELOG.md b/packages/integrations/solid/CHANGELOG.md index 52ed6418c..b8c1c84da 100644 --- a/packages/integrations/solid/CHANGELOG.md +++ b/packages/integrations/solid/CHANGELOG.md @@ -1,5 +1,11 @@ # @astrojs/solid-js +## 2.0.2 + +### Patch Changes + +- [#6104](https://github.com/withastro/astro/pull/6104) [`8c80e78dd`](https://github.com/withastro/astro/commit/8c80e78dd5ebfe0528390f42222aadf4786a90fe) Thanks [@yasserhennawi](https://github.com/yasserhennawi)! - Bump vitefu for peerDep warning with Vite 4 + ## 2.0.1 ### Patch Changes diff --git a/packages/integrations/solid/package.json b/packages/integrations/solid/package.json index 2faec55ea..b4c4595ea 100644 --- a/packages/integrations/solid/package.json +++ b/packages/integrations/solid/package.json @@ -1,6 +1,6 @@ { "name": "@astrojs/solid-js", - "version": "2.0.1", + "version": "2.0.2", "description": "Use Solid components within Astro", "type": "module", "types": "./dist/index.d.ts", diff --git a/packages/integrations/svelte/package.json b/packages/integrations/svelte/package.json index 68184f838..fd0063d76 100644 --- a/packages/integrations/svelte/package.json +++ b/packages/integrations/svelte/package.json @@ -44,7 +44,7 @@ }, "peerDependencies": { "svelte": "^3.54.0", - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "engines": { "node": ">=16.12.0" diff --git a/packages/integrations/tailwind/package.json b/packages/integrations/tailwind/package.json index 555a522f5..4ae57a28e 100644 --- a/packages/integrations/tailwind/package.json +++ b/packages/integrations/tailwind/package.json @@ -41,7 +41,7 @@ }, "peerDependencies": { "tailwindcss": "^3.0.24", - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "pnpm": { "peerDependencyRules": { diff --git a/packages/integrations/vercel/CHANGELOG.md b/packages/integrations/vercel/CHANGELOG.md index d50c0feac..66b72eb4b 100644 --- a/packages/integrations/vercel/CHANGELOG.md +++ b/packages/integrations/vercel/CHANGELOG.md @@ -1,5 +1,14 @@ # @astrojs/vercel +## 3.0.1 + +### Patch Changes + +- [#6085](https://github.com/withastro/astro/pull/6085) [`b236b5cc8`](https://github.com/withastro/astro/commit/b236b5cc8eb9e078758d9c6cb77d88c39bb1fc3d) Thanks [@AirBorne04](https://github.com/AirBorne04)! - Added second build step through esbuild, to allow framework defined build (vite build) and target defined bundling (esbuilt step) + +- Updated dependencies [[`9bec6bc41`](https://github.com/withastro/astro/commit/9bec6bc410f324a41c67e5d185fa86f78d7625f2)]: + - astro@2.0.6 + ## 3.0.0 ### Major Changes diff --git a/packages/integrations/vercel/package.json b/packages/integrations/vercel/package.json index ca4c48346..99e55da68 100644 --- a/packages/integrations/vercel/package.json +++ b/packages/integrations/vercel/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/vercel", "description": "Deploy your site to Vercel", - "version": "3.0.0", + "version": "3.0.1", "type": "module", "author": "withastro", "license": "MIT", @@ -50,7 +50,7 @@ "set-cookie-parser": "^2.5.1" }, "peerDependencies": { - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "devDependencies": { "@types/set-cookie-parser": "^2.4.2", diff --git a/packages/integrations/vue/package.json b/packages/integrations/vue/package.json index fe0d438e4..af4d4429e 100644 --- a/packages/integrations/vue/package.json +++ b/packages/integrations/vue/package.json @@ -51,7 +51,7 @@ }, "peerDependencies": { "vue": "^3.2.30", - "astro": "workspace:^2.0.5" + "astro": "workspace:^2.0.6" }, "engines": { "node": ">=16.12.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e3f8194aa..1c199181e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -63,7 +63,7 @@ importers: examples/basics: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 dependencies: astro: link:../../packages/astro @@ -72,7 +72,7 @@ importers: '@astrojs/mdx': ^0.16.0 '@astrojs/rss': ^2.1.0 '@astrojs/sitemap': ^1.0.1 - astro: ^2.0.5 + astro: ^2.0.6 dependencies: '@astrojs/mdx': link:../../packages/integrations/mdx '@astrojs/rss': link:../../packages/astro-rss @@ -81,14 +81,14 @@ importers: examples/component: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 devDependencies: astro: link:../../packages/astro examples/deno: specifiers: '@astrojs/deno': ^4.0.0 - astro: ^2.0.5 + astro: ^2.0.6 dependencies: astro: link:../../packages/astro devDependencies: @@ -104,7 +104,7 @@ importers: '@types/node': ^18.0.0 '@types/react': ^17.0.45 '@types/react-dom': ^18.0.0 - astro: ^2.0.5 + astro: ^2.0.6 html-escaper: ^3.0.3 preact: ^10.7.3 react: ^18.1.0 @@ -130,7 +130,7 @@ importers: '@astrojs/alpinejs': ^0.1.3 '@types/alpinejs': ^3.7.0 alpinejs: ^3.10.2 - astro: ^2.0.5 + astro: ^2.0.6 dependencies: '@astrojs/alpinejs': link:../../packages/integrations/alpinejs '@types/alpinejs': 3.7.1 @@ -141,7 +141,7 @@ importers: specifiers: '@astrojs/lit': ^1.1.2 '@webcomponents/template-shadowroot': ^0.1.0 - astro: ^2.0.5 + astro: ^2.0.6 lit: ^2.2.5 dependencies: '@astrojs/lit': link:../../packages/integrations/lit @@ -153,10 +153,10 @@ importers: specifiers: '@astrojs/preact': ^2.0.1 '@astrojs/react': ^2.0.2 - '@astrojs/solid-js': ^2.0.1 + '@astrojs/solid-js': ^2.0.2 '@astrojs/svelte': ^2.0.1 '@astrojs/vue': ^2.0.1 - astro: ^2.0.5 + astro: ^2.0.6 preact: ^10.7.3 react: ^18.1.0 react-dom: ^18.1.0 @@ -181,7 +181,7 @@ importers: specifiers: '@astrojs/preact': ^2.0.1 '@preact/signals': ^1.1.0 - astro: ^2.0.5 + astro: ^2.0.6 preact: ^10.7.3 dependencies: '@astrojs/preact': link:../../packages/integrations/preact @@ -194,7 +194,7 @@ importers: '@astrojs/react': ^2.0.2 '@types/react': ^18.0.10 '@types/react-dom': ^18.0.5 - astro: ^2.0.5 + astro: ^2.0.6 react: ^18.1.0 react-dom: ^18.1.0 dependencies: @@ -207,8 +207,8 @@ importers: examples/framework-solid: specifiers: - '@astrojs/solid-js': ^2.0.1 - astro: ^2.0.5 + '@astrojs/solid-js': ^2.0.2 + astro: ^2.0.6 solid-js: ^1.4.3 dependencies: '@astrojs/solid-js': link:../../packages/integrations/solid @@ -218,7 +218,7 @@ importers: examples/framework-svelte: specifiers: '@astrojs/svelte': ^2.0.1 - astro: ^2.0.5 + astro: ^2.0.6 svelte: ^3.48.0 dependencies: '@astrojs/svelte': link:../../packages/integrations/svelte @@ -228,7 +228,7 @@ importers: examples/framework-vue: specifiers: '@astrojs/vue': ^2.0.1 - astro: ^2.0.5 + astro: ^2.0.6 vue: ^3.2.37 dependencies: '@astrojs/vue': link:../../packages/integrations/vue @@ -238,32 +238,32 @@ importers: examples/hackernews: specifiers: '@astrojs/node': ^5.0.2 - astro: ^2.0.5 + astro: ^2.0.6 dependencies: '@astrojs/node': link:../../packages/integrations/node astro: link:../../packages/astro examples/integration: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 devDependencies: astro: link:../../packages/astro examples/minimal: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 dependencies: astro: link:../../packages/astro examples/non-html-pages: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 dependencies: astro: link:../../packages/astro examples/portfolio: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 dependencies: astro: link:../../packages/astro @@ -271,7 +271,7 @@ importers: specifiers: '@astrojs/node': ^5.0.2 '@astrojs/svelte': ^2.0.1 - astro: ^2.0.5 + astro: ^2.0.6 concurrently: ^7.2.1 svelte: ^3.48.0 unocss: ^0.15.6 @@ -288,7 +288,7 @@ importers: examples/with-markdown-plugins: specifiers: '@astrojs/markdown-remark': ^2.0.1 - astro: ^2.0.5 + astro: ^2.0.6 hast-util-select: 5.0.1 rehype-autolink-headings: ^6.1.1 rehype-slug: ^5.0.1 @@ -305,7 +305,7 @@ importers: examples/with-markdown-shiki: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 dependencies: astro: link:../../packages/astro @@ -313,7 +313,7 @@ importers: specifiers: '@astrojs/mdx': ^0.16.0 '@astrojs/preact': ^2.0.1 - astro: ^2.0.5 + astro: ^2.0.6 preact: ^10.6.5 dependencies: '@astrojs/mdx': link:../../packages/integrations/mdx @@ -325,7 +325,7 @@ importers: specifiers: '@astrojs/preact': ^2.0.1 '@nanostores/preact': ^0.1.3 - astro: ^2.0.5 + astro: ^2.0.6 nanostores: ^0.5.12 preact: ^10.7.3 dependencies: @@ -340,7 +340,7 @@ importers: '@astrojs/mdx': ^0.16.0 '@astrojs/tailwind': ^3.0.1 '@types/canvas-confetti': ^1.4.3 - astro: ^2.0.5 + astro: ^2.0.6 autoprefixer: ^10.4.7 canvas-confetti: ^1.5.1 postcss: ^8.4.14 @@ -357,7 +357,7 @@ importers: examples/with-vite-plugin-pwa: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 vite-plugin-pwa: 0.11.11 workbox-window: ^6.5.3 dependencies: @@ -367,7 +367,7 @@ importers: examples/with-vitest: specifiers: - astro: ^2.0.5 + astro: ^2.0.6 vitest: ^0.20.3 dependencies: astro: link:../../packages/astro From f9babc38b48049f73a3a282f48d8cb26969cb0a0 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 2 Feb 2023 16:41:10 -0500 Subject: [PATCH 04/12] Upgrade preact's dependencies to fix security issue (#6108) --- .changeset/breezy-scissors-attack.md | 5 ++++ packages/integrations/preact/package.json | 2 +- pnpm-lock.yaml | 34 +++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 .changeset/breezy-scissors-attack.md diff --git a/.changeset/breezy-scissors-attack.md b/.changeset/breezy-scissors-attack.md new file mode 100644 index 000000000..d34c0566a --- /dev/null +++ b/.changeset/breezy-scissors-attack.md @@ -0,0 +1,5 @@ +--- +'@astrojs/preact': patch +--- + +Upgrade babel dependency to fix security vuln diff --git a/packages/integrations/preact/package.json b/packages/integrations/preact/package.json index 47408fa59..ab7efa4cb 100644 --- a/packages/integrations/preact/package.json +++ b/packages/integrations/preact/package.json @@ -34,7 +34,7 @@ "dependencies": { "@babel/core": ">=7.0.0-0 <8.0.0", "@babel/plugin-transform-react-jsx": "^7.17.12", - "babel-plugin-module-resolver": "^4.1.0", + "babel-plugin-module-resolver": "^5.0.0", "preact-render-to-string": "^5.2.4", "@preact/signals": "^1.1.0" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c199181e..ac4eedde9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3151,14 +3151,14 @@ importers: '@preact/signals': ^1.1.0 astro: workspace:* astro-scripts: workspace:* - babel-plugin-module-resolver: ^4.1.0 + babel-plugin-module-resolver: ^5.0.0 preact: ^10.7.3 preact-render-to-string: ^5.2.4 dependencies: '@babel/core': 7.20.12 '@babel/plugin-transform-react-jsx': 7.20.13_@babel+core@7.20.12 '@preact/signals': 1.1.3_preact@10.11.3 - babel-plugin-module-resolver: 4.1.0 + babel-plugin-module-resolver: 5.0.0 preact-render-to-string: 5.2.6_preact@10.11.3 devDependencies: astro: link:../../astro @@ -7912,6 +7912,17 @@ packages: resolve: 1.22.1 dev: false + /babel-plugin-module-resolver/5.0.0: + resolution: {integrity: sha512-g0u+/ChLSJ5+PzYwLwP8Rp8Rcfowz58TJNCe+L/ui4rpzE/mg//JVX0EWBUYoxaextqnwuGHzfGp2hh0PPV25Q==} + engines: {node: '>= 16'} + dependencies: + find-babel-config: 2.0.0 + glob: 8.1.0 + pkg-up: 3.1.0 + reselect: 4.1.7 + resolve: 1.22.1 + dev: false + /babel-plugin-polyfill-corejs2/0.3.3_@babel+core@7.20.12: resolution: {integrity: sha512-8hOdmFYFSZhqg2C/JgLUQ+t52o5nirNwaWM2B9LWteozwIvM14VSwdsCAUET10qT+kmySAlseadmfeeSWFCy+Q==} peerDependencies: @@ -9883,6 +9894,14 @@ packages: path-exists: 3.0.0 dev: false + /find-babel-config/2.0.0: + resolution: {integrity: sha512-dOKT7jvF3hGzlW60Gc3ONox/0rRZ/tz7WCil0bqA1In/3I8f1BctpXahRnEKDySZqci7u+dqq93sZST9fOJpFw==} + engines: {node: '>=16.0.0'} + dependencies: + json5: 2.2.3 + path-exists: 4.0.0 + dev: false + /find-up/3.0.0: resolution: {integrity: sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==} engines: {node: '>=6'} @@ -10145,6 +10164,17 @@ packages: once: 1.4.0 path-is-absolute: 1.0.1 + /glob/8.1.0: + resolution: {integrity: sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==} + engines: {node: '>=12'} + dependencies: + fs.realpath: 1.0.0 + inflight: 1.0.6 + inherits: 2.0.4 + minimatch: 5.1.6 + once: 1.4.0 + dev: false + /global-agent/3.0.0: resolution: {integrity: sha512-PT6XReJ+D07JvGoxQMkT6qji/jVNfX/h364XHZOWeRzy64sSFr+xJ5OX7LI3b4MPQzdL4H8Y8M0xzPpsVMwA8Q==} engines: {node: '>=10.0'} From 67ccec9e168f241318d9dac40096016982d89b7b Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 2 Feb 2023 19:10:16 -0500 Subject: [PATCH 05/12] Node adapter: handle prerendering and serving with query params (#6110) * Node adapter: handle prerendering and serving with query params * Adding a changeset --- .changeset/shy-cats-heal.md | 5 ++ packages/integrations/node/package.json | 4 +- packages/integrations/node/src/http-server.ts | 5 +- packages/integrations/node/src/index.ts | 2 +- packages/integrations/node/src/server.ts | 1 + packages/integrations/node/src/standalone.ts | 6 +- .../node/test/fixtures/prerender/package.json | 9 +++ .../fixtures/prerender/src/pages/one.astro | 10 ++++ .../fixtures/prerender/src/pages/two.astro | 11 ++++ .../integrations/node/test/prerender.test.js | 60 +++++++++++++++++++ pnpm-lock.yaml | 12 ++++ 11 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 .changeset/shy-cats-heal.md create mode 100644 packages/integrations/node/test/fixtures/prerender/package.json create mode 100644 packages/integrations/node/test/fixtures/prerender/src/pages/one.astro create mode 100644 packages/integrations/node/test/fixtures/prerender/src/pages/two.astro create mode 100644 packages/integrations/node/test/prerender.test.js diff --git a/.changeset/shy-cats-heal.md b/.changeset/shy-cats-heal.md new file mode 100644 index 000000000..33d752751 --- /dev/null +++ b/.changeset/shy-cats-heal.md @@ -0,0 +1,5 @@ +--- +'@astrojs/node': patch +--- + +Fixes support for prerendering and query params diff --git a/packages/integrations/node/package.json b/packages/integrations/node/package.json index 39060dcf3..a9a6a9c04 100644 --- a/packages/integrations/node/package.json +++ b/packages/integrations/node/package.json @@ -31,13 +31,15 @@ }, "dependencies": { "@astrojs/webapi": "^2.0.0", - "send": "^0.18.0" + "send": "^0.18.0", + "server-destroy": "^1.0.1" }, "peerDependencies": { "astro": "workspace:^2.0.6" }, "devDependencies": { "@types/send": "^0.17.1", + "@types/server-destroy": "^1.0.1", "astro": "workspace:*", "astro-scripts": "workspace:*", "chai": "^4.3.6", diff --git a/packages/integrations/node/src/http-server.ts b/packages/integrations/node/src/http-server.ts index 8eea3c170..fee30aaec 100644 --- a/packages/integrations/node/src/http-server.ts +++ b/packages/integrations/node/src/http-server.ts @@ -3,6 +3,7 @@ import http from 'http'; import https from 'https'; import send from 'send'; import { fileURLToPath } from 'url'; +import enableDestroy from 'server-destroy'; interface CreateServerOptions { client: URL; @@ -19,6 +20,7 @@ export function createServer( if (req.url) { let pathname = removeBase(req.url); pathname = pathname[0] === '/' ? pathname : '/' + pathname; + pathname = new URL(pathname, `http://${host}:${port}`).pathname; const stream = send(req, encodeURI(decodeURI(pathname)), { root: fileURLToPath(client), dotfiles: pathname.startsWith('/.well-known/') ? 'allow' : 'deny', @@ -63,6 +65,7 @@ export function createServer( httpServer = http.createServer(listener); } httpServer.listen(port, host); + enableDestroy(httpServer); // Resolves once the server is closed const closed = new Promise((resolve, reject) => { @@ -79,7 +82,7 @@ export function createServer( server: httpServer, stop: async () => { await new Promise((resolve, reject) => { - httpServer.close((err) => (err ? reject(err) : resolve(undefined))); + httpServer.destroy((err) => (err ? reject(err) : resolve(undefined))); }); }, }; diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index bbe5e44b4..d882f34fb 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -6,7 +6,7 @@ export function getAdapter(options: Options): AstroAdapter { name: '@astrojs/node', serverEntrypoint: '@astrojs/node/server.js', previewEntrypoint: '@astrojs/node/preview.js', - exports: ['handler'], + exports: ['handler', 'startServer'], args: options, }; } diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts index e0ccf6599..c4c79f6a8 100644 --- a/packages/integrations/node/src/server.ts +++ b/packages/integrations/node/src/server.ts @@ -13,6 +13,7 @@ export function createExports(manifest: SSRManifest, options: Options) { const app = new NodeApp(manifest); return { handler: middleware(app, options.mode), + startServer: () => startServer(app, options) }; } diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index 789269860..d3ab36793 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -55,8 +55,12 @@ export default function startServer(app: NodeApp, options: Options) { ); const protocol = server.server instanceof https.Server ? 'https' : 'http'; + // eslint-disable-next-line no-console console.log(`Server listening on ${protocol}://${host}:${port}`); - return server.closed(); + return { + server, + done: server.closed() + }; } diff --git a/packages/integrations/node/test/fixtures/prerender/package.json b/packages/integrations/node/test/fixtures/prerender/package.json new file mode 100644 index 000000000..350077973 --- /dev/null +++ b/packages/integrations/node/test/fixtures/prerender/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/nodejs-encoded", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/node": "workspace:*" + } +} diff --git a/packages/integrations/node/test/fixtures/prerender/src/pages/one.astro b/packages/integrations/node/test/fixtures/prerender/src/pages/one.astro new file mode 100644 index 000000000..f3a26721d --- /dev/null +++ b/packages/integrations/node/test/fixtures/prerender/src/pages/one.astro @@ -0,0 +1,10 @@ +--- +--- + + + One + + +

One

+ + diff --git a/packages/integrations/node/test/fixtures/prerender/src/pages/two.astro b/packages/integrations/node/test/fixtures/prerender/src/pages/two.astro new file mode 100644 index 000000000..beb6e8d78 --- /dev/null +++ b/packages/integrations/node/test/fixtures/prerender/src/pages/two.astro @@ -0,0 +1,11 @@ +--- +export const prerender = true; +--- + + + Two + + +

Two

+ + diff --git a/packages/integrations/node/test/prerender.test.js b/packages/integrations/node/test/prerender.test.js new file mode 100644 index 000000000..e5c94391f --- /dev/null +++ b/packages/integrations/node/test/prerender.test.js @@ -0,0 +1,60 @@ +import nodejs from '../dist/index.js'; +import { loadFixture, createRequestAndResponse } from './test-utils.js'; +import { expect } from 'chai'; +import * as cheerio from 'cheerio'; +import { fetch } from 'undici'; + +describe('Prerendering', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + let server; + + before(async () => { + process.env.ASTRO_NODE_AUTOSTART = 'disabled'; + fixture = await loadFixture({ + root: './fixtures/prerender/', + output: 'server', + adapter: nodejs({ mode: 'standalone' }), + }); + await fixture.build(); + const { startServer } = await await load(); + let res = startServer(); + server = res.server; + }); + + after(async () => { + await server.stop(); + }); + + async function load() { + const mod = await import('./fixtures/prerender/dist/server/entry.mjs'); + return mod; + } + + it('Can render SSR route', async () => { + const res = await fetch(`http://${server.host}:${server.port}/one`); + const html = await res.text(); + const $ = cheerio.load(html); + + expect(res.status).to.equal(200); + expect($('h1').text()).to.equal('One'); + }); + + it('Can render prerendered route', async () => { + const res = await fetch(`http://${server.host}:${server.port}/two`); + const html = await res.text(); + const $ = cheerio.load(html); + + expect(res.status).to.equal(200); + expect($('h1').text()).to.equal('Two'); + }); + + it('Can render prerendered route with query params', async () => { + const res = await fetch(`http://${server.host}:${server.port}/two?foo=bar`); + const html = await res.text(); + const $ = cheerio.load(html); + + expect(res.status).to.equal(200); + expect($('h1').text()).to.equal('Two'); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ac4eedde9..606d6a752 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3070,6 +3070,7 @@ importers: specifiers: '@astrojs/webapi': ^2.0.0 '@types/send': ^0.17.1 + '@types/server-destroy': ^1.0.1 astro: workspace:* astro-scripts: workspace:* chai: ^4.3.6 @@ -3077,12 +3078,15 @@ importers: mocha: ^9.2.2 node-mocks-http: ^1.11.0 send: ^0.18.0 + server-destroy: ^1.0.1 undici: ^5.14.0 dependencies: '@astrojs/webapi': link:../../webapi send: 0.18.0 + server-destroy: 1.0.1 devDependencies: '@types/send': 0.17.1 + '@types/server-destroy': 1.0.1 astro: link:../../astro astro-scripts: link:../../../scripts chai: 4.3.7 @@ -3115,6 +3119,14 @@ importers: '@astrojs/node': link:../../.. astro: link:../../../../../astro + packages/integrations/node/test/fixtures/prerender: + specifiers: + '@astrojs/node': workspace:* + astro: workspace:* + dependencies: + '@astrojs/node': link:../../.. + astro: link:../../../../../astro + packages/integrations/node/test/fixtures/url-protocol: specifiers: '@astrojs/node': workspace:* From 74c135ef9d3f0c658e1a78d0c982607b2fec9c27 Mon Sep 17 00:00:00 2001 From: matthewp Date: Fri, 3 Feb 2023 00:12:22 +0000 Subject: [PATCH 06/12] [ci] format --- packages/integrations/node/src/http-server.ts | 2 +- packages/integrations/node/src/server.ts | 2 +- packages/integrations/node/src/standalone.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/integrations/node/src/http-server.ts b/packages/integrations/node/src/http-server.ts index fee30aaec..f0dde82d5 100644 --- a/packages/integrations/node/src/http-server.ts +++ b/packages/integrations/node/src/http-server.ts @@ -2,8 +2,8 @@ import fs from 'fs'; import http from 'http'; import https from 'https'; import send from 'send'; -import { fileURLToPath } from 'url'; import enableDestroy from 'server-destroy'; +import { fileURLToPath } from 'url'; interface CreateServerOptions { client: URL; diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts index c4c79f6a8..ed03b68a6 100644 --- a/packages/integrations/node/src/server.ts +++ b/packages/integrations/node/src/server.ts @@ -13,7 +13,7 @@ export function createExports(manifest: SSRManifest, options: Options) { const app = new NodeApp(manifest); return { handler: middleware(app, options.mode), - startServer: () => startServer(app, options) + startServer: () => startServer(app, options), }; } diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index d3ab36793..813174252 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -61,6 +61,6 @@ export default function startServer(app: NodeApp, options: Options) { return { server, - done: server.closed() + done: server.closed(), }; } From a1c0cbe604c9f91cdc421b5606aab574999eba01 Mon Sep 17 00:00:00 2001 From: "Houston (Bot)" <108291165+astrobot-houston@users.noreply.github.com> Date: Thu, 2 Feb 2023 16:43:30 -0800 Subject: [PATCH 07/12] [ci] release (#6109) Co-authored-by: github-actions[bot] --- .changeset/breezy-scissors-attack.md | 5 ----- .changeset/shy-cats-heal.md | 5 ----- examples/docs/package.json | 2 +- examples/framework-multiple/package.json | 2 +- examples/framework-preact/package.json | 2 +- examples/hackernews/package.json | 2 +- examples/ssr/package.json | 2 +- examples/with-mdx/package.json | 2 +- examples/with-nanostores/package.json | 2 +- packages/integrations/node/CHANGELOG.md | 6 ++++++ packages/integrations/node/package.json | 2 +- packages/integrations/preact/CHANGELOG.md | 6 ++++++ packages/integrations/preact/package.json | 2 +- pnpm-lock.yaml | 14 +++++++------- 14 files changed, 28 insertions(+), 26 deletions(-) delete mode 100644 .changeset/breezy-scissors-attack.md delete mode 100644 .changeset/shy-cats-heal.md diff --git a/.changeset/breezy-scissors-attack.md b/.changeset/breezy-scissors-attack.md deleted file mode 100644 index d34c0566a..000000000 --- a/.changeset/breezy-scissors-attack.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@astrojs/preact': patch ---- - -Upgrade babel dependency to fix security vuln diff --git a/.changeset/shy-cats-heal.md b/.changeset/shy-cats-heal.md deleted file mode 100644 index 33d752751..000000000 --- a/.changeset/shy-cats-heal.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@astrojs/node': patch ---- - -Fixes support for prerendering and query params diff --git a/examples/docs/package.json b/examples/docs/package.json index 4f93b0ef7..5a64ea6b3 100644 --- a/examples/docs/package.json +++ b/examples/docs/package.json @@ -16,7 +16,7 @@ "react": "^18.1.0", "react-dom": "^18.1.0", "@astrojs/react": "^2.0.2", - "@astrojs/preact": "^2.0.1", + "@astrojs/preact": "^2.0.2", "@algolia/client-search": "^4.13.1", "@docsearch/css": "^3.1.0", "@docsearch/react": "^3.1.0", diff --git a/examples/framework-multiple/package.json b/examples/framework-multiple/package.json index 25cc08b83..66fc933e7 100644 --- a/examples/framework-multiple/package.json +++ b/examples/framework-multiple/package.json @@ -18,7 +18,7 @@ "solid-js": "^1.4.3", "svelte": "^3.48.0", "vue": "^3.2.37", - "@astrojs/preact": "^2.0.1", + "@astrojs/preact": "^2.0.2", "@astrojs/react": "^2.0.2", "@astrojs/solid-js": "^2.0.2", "@astrojs/svelte": "^2.0.1", diff --git a/examples/framework-preact/package.json b/examples/framework-preact/package.json index e9056bca6..9c8c50075 100644 --- a/examples/framework-preact/package.json +++ b/examples/framework-preact/package.json @@ -13,7 +13,7 @@ "dependencies": { "astro": "^2.0.6", "preact": "^10.7.3", - "@astrojs/preact": "^2.0.1", + "@astrojs/preact": "^2.0.2", "@preact/signals": "^1.1.0" } } diff --git a/examples/hackernews/package.json b/examples/hackernews/package.json index 354490854..ef2169c12 100644 --- a/examples/hackernews/package.json +++ b/examples/hackernews/package.json @@ -11,7 +11,7 @@ "astro": "astro" }, "dependencies": { - "@astrojs/node": "^5.0.2", + "@astrojs/node": "^5.0.3", "astro": "^2.0.6" } } diff --git a/examples/ssr/package.json b/examples/ssr/package.json index 87a20eae0..bad980a61 100644 --- a/examples/ssr/package.json +++ b/examples/ssr/package.json @@ -15,7 +15,7 @@ "astro": "^2.0.6", "svelte": "^3.48.0", "@astrojs/svelte": "^2.0.1", - "@astrojs/node": "^5.0.2", + "@astrojs/node": "^5.0.3", "concurrently": "^7.2.1", "unocss": "^0.15.6", "vite-imagetools": "^4.0.4" diff --git a/examples/with-mdx/package.json b/examples/with-mdx/package.json index fd46013e3..f98107fd4 100644 --- a/examples/with-mdx/package.json +++ b/examples/with-mdx/package.json @@ -13,7 +13,7 @@ "dependencies": { "astro": "^2.0.6", "preact": "^10.6.5", - "@astrojs/preact": "^2.0.1", + "@astrojs/preact": "^2.0.2", "@astrojs/mdx": "^0.16.0" } } diff --git a/examples/with-nanostores/package.json b/examples/with-nanostores/package.json index 931c51190..49b200643 100644 --- a/examples/with-nanostores/package.json +++ b/examples/with-nanostores/package.json @@ -13,7 +13,7 @@ "dependencies": { "astro": "^2.0.6", "preact": "^10.7.3", - "@astrojs/preact": "^2.0.1", + "@astrojs/preact": "^2.0.2", "nanostores": "^0.5.12", "@nanostores/preact": "^0.1.3" } diff --git a/packages/integrations/node/CHANGELOG.md b/packages/integrations/node/CHANGELOG.md index bfa940181..081ce6d6c 100644 --- a/packages/integrations/node/CHANGELOG.md +++ b/packages/integrations/node/CHANGELOG.md @@ -1,5 +1,11 @@ # @astrojs/node +## 5.0.3 + +### Patch Changes + +- [#6110](https://github.com/withastro/astro/pull/6110) [`67ccec9e1`](https://github.com/withastro/astro/commit/67ccec9e168f241318d9dac40096016982d89b7b) Thanks [@matthewp](https://github.com/matthewp)! - Fixes support for prerendering and query params + ## 5.0.2 ### Patch Changes diff --git a/packages/integrations/node/package.json b/packages/integrations/node/package.json index a9a6a9c04..5f9e48751 100644 --- a/packages/integrations/node/package.json +++ b/packages/integrations/node/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/node", "description": "Deploy your site to a Node.js server", - "version": "5.0.2", + "version": "5.0.3", "type": "module", "types": "./dist/index.d.ts", "author": "withastro", diff --git a/packages/integrations/preact/CHANGELOG.md b/packages/integrations/preact/CHANGELOG.md index 21ae688fd..88c99da1c 100644 --- a/packages/integrations/preact/CHANGELOG.md +++ b/packages/integrations/preact/CHANGELOG.md @@ -1,5 +1,11 @@ # @astrojs/preact +## 2.0.2 + +### Patch Changes + +- [#6108](https://github.com/withastro/astro/pull/6108) [`f9babc38b`](https://github.com/withastro/astro/commit/f9babc38b48049f73a3a282f48d8cb26969cb0a0) Thanks [@matthewp](https://github.com/matthewp)! - Upgrade babel dependency to fix security vuln + ## 2.0.1 ### Patch Changes diff --git a/packages/integrations/preact/package.json b/packages/integrations/preact/package.json index ab7efa4cb..11776fda1 100644 --- a/packages/integrations/preact/package.json +++ b/packages/integrations/preact/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/preact", "description": "Use Preact components within Astro", - "version": "2.0.1", + "version": "2.0.2", "type": "module", "types": "./dist/index.d.ts", "author": "withastro", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 606d6a752..350e5a54c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -97,7 +97,7 @@ importers: examples/docs: specifiers: '@algolia/client-search': ^4.13.1 - '@astrojs/preact': ^2.0.1 + '@astrojs/preact': ^2.0.2 '@astrojs/react': ^2.0.2 '@docsearch/css': ^3.1.0 '@docsearch/react': ^3.1.0 @@ -151,7 +151,7 @@ importers: examples/framework-multiple: specifiers: - '@astrojs/preact': ^2.0.1 + '@astrojs/preact': ^2.0.2 '@astrojs/react': ^2.0.2 '@astrojs/solid-js': ^2.0.2 '@astrojs/svelte': ^2.0.1 @@ -179,7 +179,7 @@ importers: examples/framework-preact: specifiers: - '@astrojs/preact': ^2.0.1 + '@astrojs/preact': ^2.0.2 '@preact/signals': ^1.1.0 astro: ^2.0.6 preact: ^10.7.3 @@ -237,7 +237,7 @@ importers: examples/hackernews: specifiers: - '@astrojs/node': ^5.0.2 + '@astrojs/node': ^5.0.3 astro: ^2.0.6 dependencies: '@astrojs/node': link:../../packages/integrations/node @@ -269,7 +269,7 @@ importers: examples/ssr: specifiers: - '@astrojs/node': ^5.0.2 + '@astrojs/node': ^5.0.3 '@astrojs/svelte': ^2.0.1 astro: ^2.0.6 concurrently: ^7.2.1 @@ -312,7 +312,7 @@ importers: examples/with-mdx: specifiers: '@astrojs/mdx': ^0.16.0 - '@astrojs/preact': ^2.0.1 + '@astrojs/preact': ^2.0.2 astro: ^2.0.6 preact: ^10.6.5 dependencies: @@ -323,7 +323,7 @@ importers: examples/with-nanostores: specifiers: - '@astrojs/preact': ^2.0.1 + '@astrojs/preact': ^2.0.2 '@nanostores/preact': ^0.1.3 astro: ^2.0.6 nanostores: ^0.5.12 From ac7fb04d6b162f28a337918138d5737e2c0fffad Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 3 Feb 2023 20:47:20 +0800 Subject: [PATCH 08/12] Fix scanning sourcemap handling (#6114) --- .changeset/lemon-flies-clean.md | 5 +++++ packages/astro/src/vite-plugin-scanner/index.ts | 1 + 2 files changed, 6 insertions(+) create mode 100644 .changeset/lemon-flies-clean.md diff --git a/.changeset/lemon-flies-clean.md b/.changeset/lemon-flies-clean.md new file mode 100644 index 000000000..7deef534a --- /dev/null +++ b/.changeset/lemon-flies-clean.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix sourcemap generation when scanning files diff --git a/packages/astro/src/vite-plugin-scanner/index.ts b/packages/astro/src/vite-plugin-scanner/index.ts index 3976891c7..92d6a16eb 100644 --- a/packages/astro/src/vite-plugin-scanner/index.ts +++ b/packages/astro/src/vite-plugin-scanner/index.ts @@ -29,6 +29,7 @@ export default function astroScannerPlugin({ settings }: { settings: AstroSettin const { meta = {} } = this.getModuleInfo(id) ?? {}; return { code, + map: null, meta: { ...meta, astro: { From 40bf96f05d4ab462de2e7961970f73c24c094709 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 3 Feb 2023 08:04:26 -0500 Subject: [PATCH 09/12] Refactor build into plugins (#6077) * Refactor build into plugins * maybe fix internals * Await post-build hooks * Use extendManualChunks * Remove commented out code --- .../src/content/vite-plugin-content-assets.ts | 14 +++ packages/astro/src/core/build/plugin.ts | 116 ++++++++++++++++++ .../astro/src/core/build/plugins/index.ts | 23 ++++ .../plugin-alias-resolve.ts} | 16 ++- .../plugin-analyzer.ts} | 24 +++- .../plugin-css.ts} | 75 ++++++----- .../plugin-hoisted-scripts.ts} | 23 +++- .../plugin-internals.ts} | 16 ++- .../plugin-pages.ts} | 23 +++- .../core/build/plugins/plugin-prerender.ts | 48 ++++++++ .../plugin-ssr.ts} | 66 +++++++--- packages/astro/src/core/build/plugins/util.ts | 40 ++++++ packages/astro/src/core/build/static-build.ts | 71 ++++++----- packages/astro/src/core/build/types.ts | 5 + .../src/core/build/vite-plugin-prerender.ts | 43 ------- 15 files changed, 461 insertions(+), 142 deletions(-) create mode 100644 packages/astro/src/core/build/plugin.ts create mode 100644 packages/astro/src/core/build/plugins/index.ts rename packages/astro/src/core/build/{vite-plugin-alias-resolve.ts => plugins/plugin-alias-resolve.ts} (81%) rename packages/astro/src/core/build/{vite-plugin-analyzer.ts => plugins/plugin-analyzer.ts} (89%) rename packages/astro/src/core/build/{vite-plugin-css.ts => plugins/plugin-css.ts} (84%) rename packages/astro/src/core/build/{vite-plugin-hoisted-scripts.ts => plugins/plugin-hoisted-scripts.ts} (80%) rename packages/astro/src/core/build/{vite-plugin-internals.ts => plugins/plugin-internals.ts} (83%) rename packages/astro/src/core/build/{vite-plugin-pages.ts => plugins/plugin-pages.ts} (78%) create mode 100644 packages/astro/src/core/build/plugins/plugin-prerender.ts rename packages/astro/src/core/build/{vite-plugin-ssr.ts => plugins/plugin-ssr.ts} (79%) create mode 100644 packages/astro/src/core/build/plugins/util.ts delete mode 100644 packages/astro/src/core/build/vite-plugin-prerender.ts diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index fd73caf47..a5b205151 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -2,6 +2,7 @@ import { pathToFileURL } from 'url'; import type { Plugin } from 'vite'; import { moduleIsTopLevelPage, walkParentInfos } from '../core/build/graph.js'; import { BuildInternals, getPageDataByViteID } from '../core/build/internal.js'; +import { AstroBuildPlugin } from '../core/build/plugin.js'; import type { ModuleLoader } from '../core/module-loader/loader.js'; import { createViteLoader } from '../core/module-loader/vite.js'; import { getStylesForURL } from '../core/render/dev/css.js'; @@ -98,3 +99,16 @@ export function astroContentProdBundlePlugin({ internals }: { internals: BuildIn }, }; } + +export function astroConfigBuildPlugin(internals: BuildInternals): AstroBuildPlugin { + return { + build: 'ssr', + hooks: { + 'build:before': () => { + return { + vitePlugin: astroContentProdBundlePlugin({ internals }) + }; + } + } + }; +} diff --git a/packages/astro/src/core/build/plugin.ts b/packages/astro/src/core/build/plugin.ts new file mode 100644 index 000000000..47c87e334 --- /dev/null +++ b/packages/astro/src/core/build/plugin.ts @@ -0,0 +1,116 @@ +import type { Plugin as VitePlugin } from 'vite'; +import type { BuildInternals } from './internal'; +import type { StaticBuildOptions, ViteBuildReturn } from './types'; + +type RollupOutputArray = Extract>; +type OutputChunkorAsset = RollupOutputArray[number]['output'][number] +type OutputChunk = Extract; + +type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: string) => void; + +export type AstroBuildPlugin = { + build: 'ssr' | 'client' | 'both'; + hooks?: { + 'build:before'?: (opts: { + build: 'ssr' | 'client'; + input: Set; + }) => { + enforce?: 'after-user-plugins'; + vitePlugin: VitePlugin | VitePlugin[] | undefined + }; + 'build:post'?: (opts: {ssrOutputs: RollupOutputArray; clientOutputs: RollupOutputArray; mutate: MutateChunk}) => void | Promise; + } +}; + +export function createPluginContainer(options: StaticBuildOptions, internals: BuildInternals) { + const clientPlugins: AstroBuildPlugin[] = []; + const ssrPlugins: AstroBuildPlugin[] = []; + const allPlugins = new Set(); + + return { + options, + internals, + register(plugin: AstroBuildPlugin) { + allPlugins.add(plugin); + switch(plugin.build) { + case 'client': { + clientPlugins.push(plugin); + break; + } + case 'ssr': { + ssrPlugins.push(plugin); + break; + } + case 'both': { + clientPlugins.push(plugin); + ssrPlugins.push(plugin); + break; + } + } + }, + + // Hooks + runBeforeHook(build: 'ssr' | 'client', input: Set) { + let plugins = build === 'ssr' ? ssrPlugins : clientPlugins; + let vitePlugins: Array = []; + let lastVitePlugins: Array = []; + for(const plugin of plugins) { + if(plugin.hooks?.['build:before']) { + let result = plugin.hooks['build:before']({ build, input }); + if(result.vitePlugin) { + vitePlugins.push(result.vitePlugin); + } + } + } + + return { + vitePlugins, + lastVitePlugins + }; + }, + + async runPostHook(ssrReturn: ViteBuildReturn, clientReturn: ViteBuildReturn | null) { + const mutations = new Map(); + const ssrOutputs: RollupOutputArray = []; + const clientOutputs: RollupOutputArray = []; + + if(Array.isArray(ssrReturn)) { + ssrOutputs.push(...ssrReturn); + } else if('output' in ssrReturn) { + ssrOutputs.push(ssrReturn); + } + + if(Array.isArray(clientReturn)) { + clientOutputs.push(...clientReturn); + } else if(clientReturn && 'output' in clientReturn) { + clientOutputs.push(clientReturn); + } + + const mutate: MutateChunk = (chunk, build, newCode) => { + chunk.code = newCode; + mutations.set(chunk.fileName, { + build, + code: newCode, + }); + }; + + for(const plugin of allPlugins) { + const postHook = plugin.hooks?.['build:post']; + if(postHook) { + await postHook({ + ssrOutputs, + clientOutputs, + mutate + }); + } + } + + return mutations; + } + }; +} + +export type AstroBuildPluginContainer = ReturnType; diff --git a/packages/astro/src/core/build/plugins/index.ts b/packages/astro/src/core/build/plugins/index.ts new file mode 100644 index 000000000..f253fe289 --- /dev/null +++ b/packages/astro/src/core/build/plugins/index.ts @@ -0,0 +1,23 @@ +import type { AstroBuildPluginContainer, AstroBuildPlugin } from '../plugin'; +import type { PageBuildData, StaticBuildOptions } from '../types'; +import { pluginAnalyzer } from './plugin-analyzer.js'; +import { pluginInternals } from './plugin-internals.js'; +import { pluginPages } from './plugin-pages.js'; +import { pluginCSS } from './plugin-css.js'; +import { pluginPrerender } from './plugin-prerender.js'; +import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.js'; +import { pluginSSR } from './plugin-ssr.js'; +import { pluginAliasResolve } from './plugin-alias-resolve.js'; +import { pluginHoistedScripts } from './plugin-hoisted-scripts.js'; + +export function registerAllPlugins({ internals, options, register }: AstroBuildPluginContainer) { + register(pluginAliasResolve(internals)); + register(pluginAnalyzer(internals)); + register(pluginInternals(internals)); + register(pluginPages(options, internals)); + register(pluginCSS(options, internals)); + register(pluginPrerender(options, internals)); + register(astroConfigBuildPlugin(internals)); + register(pluginHoistedScripts(options, internals)); + register(pluginSSR(options, internals)); +} diff --git a/packages/astro/src/core/build/vite-plugin-alias-resolve.ts b/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts similarity index 81% rename from packages/astro/src/core/build/vite-plugin-alias-resolve.ts rename to packages/astro/src/core/build/plugins/plugin-alias-resolve.ts index ac37e66cd..1487ca2b9 100644 --- a/packages/astro/src/core/build/vite-plugin-alias-resolve.ts +++ b/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts @@ -1,5 +1,6 @@ import type { Alias, Plugin as VitePlugin } from 'vite'; -import type { BuildInternals } from '../../core/build/internal.js'; +import type { BuildInternals } from '../internal.js'; +import { AstroBuildPlugin } from '../plugin.js'; /** * `@rollup/plugin-alias` doesn't resolve aliases in Rollup input by default. This plugin fixes it @@ -48,3 +49,16 @@ function matches(pattern: string | RegExp, importee: string) { } return importee.startsWith(pattern + '/'); } + +export function pluginAliasResolve(internals: BuildInternals): AstroBuildPlugin { + return { + build: 'client', + hooks: { + 'build:before': () => { + return { + vitePlugin: vitePluginAliasResolve(internals) + }; + } + } + }; +} diff --git a/packages/astro/src/core/build/vite-plugin-analyzer.ts b/packages/astro/src/core/build/plugins/plugin-analyzer.ts similarity index 89% rename from packages/astro/src/core/build/vite-plugin-analyzer.ts rename to packages/astro/src/core/build/plugins/plugin-analyzer.ts index 0e6a991bd..4214cde8e 100644 --- a/packages/astro/src/core/build/vite-plugin-analyzer.ts +++ b/packages/astro/src/core/build/plugins/plugin-analyzer.ts @@ -1,11 +1,12 @@ import type { PluginContext } from 'rollup'; import type { Plugin as VitePlugin } from 'vite'; -import type { BuildInternals } from '../../core/build/internal.js'; -import type { PluginMetadata as AstroPluginMetadata } from '../../vite-plugin-astro/types'; +import type { BuildInternals } from '../internal.js'; +import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types'; +import type { AstroBuildPlugin } from '../plugin.js'; -import { prependForwardSlash } from '../../core/path.js'; -import { getTopLevelPages } from './graph.js'; -import { getPageDataByViteID, trackClientOnlyPageDatas } from './internal.js'; +import { prependForwardSlash } from '../../path.js'; +import { getTopLevelPages } from '../graph.js'; +import { getPageDataByViteID, trackClientOnlyPageDatas } from '../internal.js'; export function vitePluginAnalyzer(internals: BuildInternals): VitePlugin { function hoistedScriptScanner() { @@ -122,3 +123,16 @@ export function vitePluginAnalyzer(internals: BuildInternals): VitePlugin { }, }; } + +export function pluginAnalyzer(internals: BuildInternals): AstroBuildPlugin { + return { + build: 'ssr', + hooks: { + 'build:before': () => { + return { + vitePlugin: vitePluginAnalyzer(internals) + }; + } + } + }; +} diff --git a/packages/astro/src/core/build/vite-plugin-css.ts b/packages/astro/src/core/build/plugins/plugin-css.ts similarity index 84% rename from packages/astro/src/core/build/vite-plugin-css.ts rename to packages/astro/src/core/build/plugins/plugin-css.ts index 2a9b5b739..b52b5c0fc 100644 --- a/packages/astro/src/core/build/vite-plugin-css.ts +++ b/packages/astro/src/core/build/plugins/plugin-css.ts @@ -2,20 +2,22 @@ import * as crypto from 'node:crypto'; import * as npath from 'node:path'; import type { GetModuleInfo } from 'rollup'; import { Plugin as VitePlugin, ResolvedConfig, transformWithEsbuild } from 'vite'; -import { isCSSRequest } from '../render/util.js'; -import type { BuildInternals } from './internal'; -import type { PageBuildData, StaticBuildOptions } from './types'; +import { isCSSRequest } from '../../render/util.js'; +import type { BuildInternals } from '../internal'; +import type { PageBuildData, StaticBuildOptions } from '../types'; +import type { AstroBuildPlugin } from '../plugin'; -import { PROPAGATED_ASSET_FLAG } from '../../content/consts.js'; -import * as assetName from './css-asset-name.js'; -import { moduleIsTopLevelPage, walkParentInfos } from './graph.js'; +import { PROPAGATED_ASSET_FLAG } from '../../../content/consts.js'; +import * as assetName from '../css-asset-name.js'; +import { moduleIsTopLevelPage, walkParentInfos } from '../graph.js'; import { eachPageData, getPageDataByViteID, getPageDatasByClientOnlyID, getPageDatasByHoistedScriptId, isHoistedScript, -} from './internal.js'; +} from '../internal.js'; +import { extendManualChunks } from './util.js'; interface PluginOptions { internals: BuildInternals; @@ -54,40 +56,30 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] name: 'astro:rollup-plugin-build-css', outputOptions(outputOptions) { - const manualChunks = outputOptions.manualChunks || Function.prototype; const assetFileNames = outputOptions.assetFileNames; const namingIncludesHash = assetFileNames?.toString().includes('[hash]'); const createNameForParentPages = namingIncludesHash ? assetName.shortHashedName : assetName.createSlugger(settings); - outputOptions.manualChunks = function (id, ...args) { - // Defer to user-provided `manualChunks`, if it was provided. - if (typeof manualChunks == 'object') { - if (id in manualChunks) { - return manualChunks[id]; - } - } else if (typeof manualChunks === 'function') { - const outid = manualChunks.call(this, id, ...args); - if (outid) { - return outid; - } - } - // For CSS, create a hash of all of the pages that use it. - // This causes CSS to be built into shared chunks when used by multiple pages. - if (isCSSRequest(id)) { - for (const [pageInfo] of walkParentInfos(id, { - getModuleInfo: args[0].getModuleInfo, - })) { - if (new URL(pageInfo.id, 'file://').searchParams.has(PROPAGATED_ASSET_FLAG)) { - // Split delayed assets to separate modules - // so they can be injected where needed - return createNameHash(id, [id]); + extendManualChunks(outputOptions, { + after(id, meta) { + // For CSS, create a hash of all of the pages that use it. + // This causes CSS to be built into shared chunks when used by multiple pages. + if (isCSSRequest(id)) { + for (const [pageInfo] of walkParentInfos(id, { + getModuleInfo: meta.getModuleInfo, + })) { + if (new URL(pageInfo.id, 'file://').searchParams.has(PROPAGATED_ASSET_FLAG)) { + // Split delayed assets to separate modules + // so they can be injected where needed + return createNameHash(id, [id]); + } } + return createNameForParentPages(id, meta); } - return createNameForParentPages(id, args[0]); } - }; + }); }, async generateBundle(_outputOptions, bundle) { @@ -272,3 +264,22 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] }, ]; } + +export function pluginCSS(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { + return { + build: 'both', + hooks: { + 'build:before': ({ build }) => { + let plugins = rollupPluginAstroBuildCSS({ + buildOptions: options, + internals, + target: build === 'ssr' ? 'server' : 'client' + }); + + return { + vitePlugin: plugins + }; + } + } + } +} diff --git a/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts b/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts similarity index 80% rename from packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts rename to packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts index 65d38ec87..9e21177bd 100644 --- a/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts +++ b/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts @@ -1,8 +1,10 @@ import type { Plugin as VitePlugin } from 'vite'; -import type { AstroSettings } from '../../@types/astro'; -import type { BuildInternals } from '../../core/build/internal.js'; -import { viteID } from '../util.js'; -import { getPageDataByViteID } from './internal.js'; +import type { AstroSettings } from '../../../@types/astro'; +import type { BuildInternals } from '../internal.js'; +import { viteID } from '../../util.js'; +import { getPageDataByViteID } from '../internal.js'; +import { StaticBuildOptions } from '../types'; +import { AstroBuildPlugin } from '../plugin'; function virtualHoistedEntry(id: string) { return id.startsWith('/astro/hoisted.js?q='); @@ -91,3 +93,16 @@ export function vitePluginHoistedScripts( }, }; } + +export function pluginHoistedScripts(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { + return { + build: 'client', + hooks: { + 'build:before': () => { + return { + vitePlugin: vitePluginHoistedScripts(options.settings, internals) + }; + } + } + }; +} diff --git a/packages/astro/src/core/build/vite-plugin-internals.ts b/packages/astro/src/core/build/plugins/plugin-internals.ts similarity index 83% rename from packages/astro/src/core/build/vite-plugin-internals.ts rename to packages/astro/src/core/build/plugins/plugin-internals.ts index dfd346e77..7177d8bef 100644 --- a/packages/astro/src/core/build/vite-plugin-internals.ts +++ b/packages/astro/src/core/build/plugins/plugin-internals.ts @@ -1,5 +1,6 @@ import type { Plugin as VitePlugin, UserConfig } from 'vite'; -import type { BuildInternals } from './internal.js'; +import type { BuildInternals } from '../internal.js'; +import type { AstroBuildPlugin } from '../plugin'; export function vitePluginInternals(input: Set, internals: BuildInternals): VitePlugin { return { @@ -60,3 +61,16 @@ export function vitePluginInternals(input: Set, internals: BuildInternal }, }; } + +export function pluginInternals(internals: BuildInternals): AstroBuildPlugin { + return { + build: 'both', + hooks: { + 'build:before': ({ input }) => { + return { + vitePlugin: vitePluginInternals(input, internals) + }; + } + } + }; +} diff --git a/packages/astro/src/core/build/vite-plugin-pages.ts b/packages/astro/src/core/build/plugins/plugin-pages.ts similarity index 78% rename from packages/astro/src/core/build/vite-plugin-pages.ts rename to packages/astro/src/core/build/plugins/plugin-pages.ts index 1d661865c..ac436a364 100644 --- a/packages/astro/src/core/build/vite-plugin-pages.ts +++ b/packages/astro/src/core/build/plugins/plugin-pages.ts @@ -1,8 +1,10 @@ import type { Plugin as VitePlugin } from 'vite'; -import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../app/index.js'; -import { addRollupInput } from './add-rollup-input.js'; -import { BuildInternals, eachPageData, hasPrerenderedPages } from './internal.js'; -import type { StaticBuildOptions } from './types'; +import type { StaticBuildOptions } from '../types'; +import type { AstroBuildPlugin } from '../plugin'; + +import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../../app/index.js'; +import { addRollupInput } from '../add-rollup-input.js'; +import { BuildInternals, eachPageData, hasPrerenderedPages } from '../internal.js'; export function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { return { @@ -53,3 +55,16 @@ export const renderers = [${rendererItems}];`; }, }; } + +export function pluginPages(opts: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { + return { + build: 'ssr', + hooks: { + 'build:before': () => { + return { + vitePlugin: vitePluginPages(opts, internals) + }; + } + } + }; +} diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts new file mode 100644 index 000000000..340e84ea6 --- /dev/null +++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts @@ -0,0 +1,48 @@ +import type { Plugin as VitePlugin } from 'vite'; +import type { BuildInternals } from '../internal.js'; +import type { AstroBuildPlugin } from '../plugin.js'; +import type { StaticBuildOptions } from '../types'; +import { extendManualChunks } from './util.js'; + +export function vitePluginPrerender( + opts: StaticBuildOptions, + internals: BuildInternals +): VitePlugin { + return { + name: 'astro:rollup-plugin-prerender', + + outputOptions(outputOptions) { + extendManualChunks(outputOptions, { + after(id, meta) { + // Split the Astro runtime into a separate chunk for readability + if (id.includes('astro/dist')) { + return 'astro'; + } + const pageInfo = internals.pagesByViteID.get(id); + if (pageInfo) { + // prerendered pages should be split into their own chunk + // Important: this can't be in the `pages/` directory! + if (meta.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) { + return 'prerender'; + } + // dynamic pages should all go in their own chunk in the pages/* directory + return `pages/all`; + } + } + }); + }, + }; +} + +export function pluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { + return { + build: 'ssr', + hooks: { + 'build:before': () => { + return { + vitePlugin: vitePluginPrerender(opts, internals) + }; + } + } + }; +} diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts similarity index 79% rename from packages/astro/src/core/build/vite-plugin-ssr.ts rename to packages/astro/src/core/build/plugins/plugin-ssr.ts index 817881c36..289703858 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -1,21 +1,22 @@ import type { Plugin as VitePlugin } from 'vite'; -import type { AstroAdapter } from '../../@types/astro'; -import type { SerializedRouteInfo, SerializedSSRManifest } from '../app/types'; -import type { BuildInternals } from './internal.js'; -import type { StaticBuildOptions } from './types'; +import type { AstroAdapter } from '../../../@types/astro'; +import type { SerializedRouteInfo, SerializedSSRManifest } from '../../app/types'; +import type { BuildInternals } from '../internal.js'; +import type { StaticBuildOptions } from '../types'; import glob from 'fast-glob'; import * as fs from 'fs'; import { fileURLToPath } from 'url'; -import { getContentPaths } from '../../content/index.js'; -import { runHookBuildSsr } from '../../integrations/index.js'; -import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js'; -import { pagesVirtualModuleId } from '../app/index.js'; -import { removeLeadingForwardSlash, removeTrailingForwardSlash } from '../path.js'; -import { serializeRouteData } from '../routing/index.js'; -import { addRollupInput } from './add-rollup-input.js'; -import { getOutFile, getOutFolder } from './common.js'; -import { eachPrerenderedPageData, eachServerPageData, sortedCSS } from './internal.js'; +import { getContentPaths } from '../../../content/index.js'; +import { runHookBuildSsr } from '../../../integrations/index.js'; +import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js'; +import { pagesVirtualModuleId } from '../../app/index.js'; +import { removeLeadingForwardSlash, removeTrailingForwardSlash } from '../../path.js'; +import { serializeRouteData } from '../../routing/index.js'; +import { addRollupInput } from '../add-rollup-input.js'; +import { getOutFile, getOutFolder } from '../common.js'; +import { eachPrerenderedPageData, eachServerPageData, sortedCSS } from '../internal.js'; +import { AstroBuildPlugin } from '../plugin'; export const virtualModuleId = '@astrojs-ssr-virtual-entry'; const resolvedVirtualModuleId = '\0' + virtualModuleId; @@ -29,7 +30,7 @@ export function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter): options(opts) { return addRollupInput(opts, [virtualModuleId]); }, - resolveId(id) { + resolveId(id, parent) { if (id === virtualModuleId) { return resolvedVirtualModuleId; } @@ -114,12 +115,10 @@ export async function injectManifest(buildOpts: StaticBuildOptions, internals: B const chunk = internals.ssrEntryChunk; const code = chunk.code; - chunk.code = code.replace(replaceExp, () => { + + return code.replace(replaceExp, () => { return JSON.stringify(manifest); }); - const serverEntryURL = new URL(buildOpts.buildConfig.serverEntry, buildOpts.buildConfig.server); - await fs.promises.mkdir(new URL('./', serverEntryURL), { recursive: true }); - await fs.promises.writeFile(serverEntryURL, chunk.code, 'utf-8'); } function buildManifest( @@ -220,3 +219,34 @@ function buildManifest( return ssrManifest; } + +export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { + const ssr = options.settings.config.output === 'server'; + return { + build: 'ssr', + hooks: { + 'build:before': () => { + let vitePlugin = ssr ? vitePluginSSR(internals, options.settings.adapter!) : undefined; + + return { + enforce: 'after-user-plugins', + vitePlugin + } + }, + 'build:post': async ({ mutate }) => { + if(!ssr) { + return; + } + + if(!internals.ssrEntryChunk) { + throw new Error(`Did not generate an entry chunk for SSR`); + } + // Mutate the filename + internals.ssrEntryChunk.fileName = options.settings.config.build.serverEntry; + + const code = await injectManifest(options, internals); + mutate(internals.ssrEntryChunk, 'server', code); + } + } + }; +} diff --git a/packages/astro/src/core/build/plugins/util.ts b/packages/astro/src/core/build/plugins/util.ts new file mode 100644 index 000000000..6129aa1e1 --- /dev/null +++ b/packages/astro/src/core/build/plugins/util.ts @@ -0,0 +1,40 @@ +import type { Plugin as VitePlugin } from 'vite'; + +// eslint-disable-next-line @typescript-eslint/ban-types +type OutputOptionsHook = Extract; +type OutputOptions = Parameters[0]; + +type ExtendManualChunksHooks = { + before?: (id: string, meta: any) => string | undefined; + after?: (id: string, meta: any) => string | undefined; +} + +export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendManualChunksHooks) { + const manualChunks = outputOptions.manualChunks; + outputOptions.manualChunks = function(id, meta) { + if(hooks.before) { + let value = hooks.before(id, meta); + if(value) { + return value; + } + } + + // Defer to user-provided `manualChunks`, if it was provided. + if (typeof manualChunks == 'object') { + if (id in manualChunks) { + let value = manualChunks[id]; + return value[0]; + } + } else if (typeof manualChunks === 'function') { + const outid = manualChunks.call(this, id, meta); + if (outid) { + return outid; + } + } + + if(hooks.after) { + return hooks.after(id, meta) || null; + } + return null; + }; +} diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index b2fab0a14..726c1db88 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -4,7 +4,6 @@ import fs from 'fs'; import { bgGreen, bgMagenta, black, dim } from 'kleur/colors'; import { fileURLToPath } from 'url'; import * as vite from 'vite'; -import { astroContentProdBundlePlugin } from '../../content/index.js'; import { BuildInternals, createBuildInternals, @@ -23,14 +22,8 @@ import { generatePages } from './generate.js'; import { trackPageData } from './internal.js'; import type { PageBuildData, StaticBuildOptions } from './types'; import { getTimeStat } from './util.js'; -import { vitePluginAliasResolve } from './vite-plugin-alias-resolve.js'; -import { vitePluginAnalyzer } from './vite-plugin-analyzer.js'; -import { rollupPluginAstroBuildCSS } from './vite-plugin-css.js'; -import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js'; -import { vitePluginInternals } from './vite-plugin-internals.js'; -import { vitePluginPages } from './vite-plugin-pages.js'; -import { vitePluginPrerender } from './vite-plugin-prerender.js'; -import { injectManifest, vitePluginSSR } from './vite-plugin-ssr.js'; +import { AstroBuildPluginContainer, createPluginContainer } from './plugin.js'; +import { registerAllPlugins } from './plugins/index.js'; export async function staticBuild(opts: StaticBuildOptions) { const { allPages, settings } = opts; @@ -70,10 +63,15 @@ export async function staticBuild(opts: StaticBuildOptions) { // condition, so we are doing it ourselves emptyDir(settings.config.outDir, new Set('.git')); + // Register plugins + const container = createPluginContainer(opts, internals); + registerAllPlugins(container); + + // Build your project (SSR application code, assets, client JS, etc.) timer.ssr = performance.now(); info(opts.logging, 'build', `Building ${settings.config.output} entrypoints...`); - await ssrBuild(opts, internals, pageInput); + const ssrOutput = await ssrBuild(opts, internals, pageInput, container); info(opts.logging, 'build', dim(`Completed in ${getTimeStat(timer.ssr, performance.now())}.`)); const rendererClientEntrypoints = settings.renderers @@ -93,9 +91,11 @@ export async function staticBuild(opts: StaticBuildOptions) { // Run client build first, so the assets can be fed into the SSR rendered version. timer.clientBuild = performance.now(); - await clientBuild(opts, internals, clientInput); + const clientOutput = await clientBuild(opts, internals, clientInput, container); timer.generate = performance.now(); + await runPostBuildHooks(container, ssrOutput, clientOutput); + switch (settings.config.output) { case 'static': { await generatePages(opts, internals); @@ -103,7 +103,6 @@ export async function staticBuild(opts: StaticBuildOptions) { return; } case 'server': { - await injectManifest(opts, internals); await generatePages(opts, internals); await cleanStaticOutput(opts, internals); info(opts.logging, null, `\n${bgMagenta(black(' finalizing server assets '))}\n`); @@ -113,11 +112,13 @@ export async function staticBuild(opts: StaticBuildOptions) { } } -async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set) { +async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set, container: AstroBuildPluginContainer) { const { settings, viteConfig } = opts; const ssr = settings.config.output === 'server'; const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir); + const { lastVitePlugins, vitePlugins } = container.runBeforeHook('ssr', input); + const viteBuildConfig: vite.InlineConfig = { ...viteConfig, mode: viteConfig.mode || 'production', @@ -155,19 +156,9 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp reportCompressedSize: false, }, plugins: [ - vitePluginAnalyzer(internals), - vitePluginInternals(input, internals), - vitePluginPages(opts, internals), - rollupPluginAstroBuildCSS({ - buildOptions: opts, - internals, - target: 'server', - }), - vitePluginPrerender(opts, internals), + ...vitePlugins, ...(viteConfig.plugins || []), - astroContentProdBundlePlugin({ internals }), - // SSR needs to be last - ssr && vitePluginSSR(internals, settings.adapter!), + ...lastVitePlugins ], envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', base: settings.config.base, @@ -187,7 +178,8 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp async function clientBuild( opts: StaticBuildOptions, internals: BuildInternals, - input: Set + input: Set, + container: AstroBuildPluginContainer ) { const { settings, viteConfig } = opts; const timer = performance.now(); @@ -204,8 +196,10 @@ async function clientBuild( return null; } + const { lastVitePlugins, vitePlugins } = container.runBeforeHook('client', input); info(opts.logging, null, `\n${bgGreen(black(' building client '))}`); + const viteBuildConfig: vite.InlineConfig = { ...viteConfig, mode: viteConfig.mode || 'production', @@ -229,15 +223,9 @@ async function clientBuild( }, }, plugins: [ - vitePluginAliasResolve(internals), - vitePluginInternals(input, internals), - vitePluginHoistedScripts(settings, internals), - rollupPluginAstroBuildCSS({ - buildOptions: opts, - internals, - target: 'client', - }), + ...vitePlugins, ...(viteConfig.plugins || []), + ...lastVitePlugins, ], envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', base: settings.config.base, @@ -256,6 +244,21 @@ async function clientBuild( return buildResult; } +async function runPostBuildHooks( + container: AstroBuildPluginContainer, + ssrReturn: Awaited>, + clientReturn: Awaited> +) { + const mutations = await container.runPostHook(ssrReturn, clientReturn); + const buildConfig = container.options.settings.config.build; + for(const [fileName, mutation] of mutations) { + const root = mutation.build === 'server' ? buildConfig.server : buildConfig.client; + const fileURL = new URL(fileName, root); + await fs.promises.mkdir(new URL('./', fileURL), { recursive: true }); + await fs.promises.writeFile(fileURL, mutation.code, 'utf-8'); + } +} + /** * For each statically prerendered page, replace their SSR file with a noop. * This allows us to run the SSR build only once, but still remove dependencies for statically rendered routes. diff --git a/packages/astro/src/core/build/types.ts b/packages/astro/src/core/build/types.ts index 0c4d9a069..a9ca08a0e 100644 --- a/packages/astro/src/core/build/types.ts +++ b/packages/astro/src/core/build/types.ts @@ -11,6 +11,7 @@ import type { } from '../../@types/astro'; import type { LogOptions } from '../logger/core'; import type { RouteCache } from '../render/route-cache'; +import type { default as vite } from 'vite'; export type ComponentPath = string; export type ViteID = string; @@ -44,3 +45,7 @@ export interface SingleFileBuiltModule { pageMap: Map; renderers: SSRLoadedRenderer[]; } + +export type ViteBuildReturn = Awaited>; +export type RollupOutput = Extract>>, { output: any }>; +export type OutputChunk = Extract; diff --git a/packages/astro/src/core/build/vite-plugin-prerender.ts b/packages/astro/src/core/build/vite-plugin-prerender.ts deleted file mode 100644 index fdc505378..000000000 --- a/packages/astro/src/core/build/vite-plugin-prerender.ts +++ /dev/null @@ -1,43 +0,0 @@ -import type { Plugin as VitePlugin } from 'vite'; -import type { BuildInternals } from './internal.js'; -import type { StaticBuildOptions } from './types'; - -export function vitePluginPrerender( - opts: StaticBuildOptions, - internals: BuildInternals -): VitePlugin { - return { - name: 'astro:rollup-plugin-prerender', - - outputOptions(outputOptions) { - const manualChunks = outputOptions.manualChunks || Function.prototype; - outputOptions.manualChunks = function (id, api, ...args) { - // Defer to user-provided `manualChunks`, if it was provided. - if (typeof manualChunks == 'object') { - if (id in manualChunks) { - return manualChunks[id]; - } - } else if (typeof manualChunks === 'function') { - const outid = manualChunks.call(this, id, api, ...args); - if (outid) { - return outid; - } - } - // Split the Astro runtime into a separate chunk for readability - if (id.includes('astro/dist')) { - return 'astro'; - } - const pageInfo = internals.pagesByViteID.get(id); - if (pageInfo) { - // prerendered pages should be split into their own chunk - // Important: this can't be in the `pages/` directory! - if (api.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) { - return `prerender`; - } - // dynamic pages should all go in their own chunk in the pages/* directory - return `pages/all`; - } - }; - }, - }; -} From a0efdc8f763b35e287ce1f1a371866a1c12731a8 Mon Sep 17 00:00:00 2001 From: matthewp Date: Fri, 3 Feb 2023 13:06:16 +0000 Subject: [PATCH 10/12] [ci] format --- .../src/content/vite-plugin-content-assets.ts | 6 +-- packages/astro/src/core/build/plugin.ts | 54 ++++++++++--------- .../astro/src/core/build/plugins/index.ts | 11 ++-- .../build/plugins/plugin-alias-resolve.ts | 6 +-- .../src/core/build/plugins/plugin-analyzer.ts | 8 +-- .../src/core/build/plugins/plugin-css.ts | 19 ++++--- .../build/plugins/plugin-hoisted-scripts.ts | 15 +++--- .../core/build/plugins/plugin-internals.ts | 6 +-- .../src/core/build/plugins/plugin-pages.ts | 8 +-- .../core/build/plugins/plugin-prerender.ts | 13 +++-- .../src/core/build/plugins/plugin-ssr.ts | 18 ++++--- packages/astro/src/core/build/plugins/util.ts | 12 ++--- packages/astro/src/core/build/static-build.ts | 27 ++++------ packages/astro/src/core/build/types.ts | 8 +-- 14 files changed, 111 insertions(+), 100 deletions(-) diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index a5b205151..89e417a3c 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -106,9 +106,9 @@ export function astroConfigBuildPlugin(internals: BuildInternals): AstroBuildPlu hooks: { 'build:before': () => { return { - vitePlugin: astroContentProdBundlePlugin({ internals }) + vitePlugin: astroContentProdBundlePlugin({ internals }), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugin.ts b/packages/astro/src/core/build/plugin.ts index 47c87e334..e7b1fbc4a 100644 --- a/packages/astro/src/core/build/plugin.ts +++ b/packages/astro/src/core/build/plugin.ts @@ -3,7 +3,7 @@ import type { BuildInternals } from './internal'; import type { StaticBuildOptions, ViteBuildReturn } from './types'; type RollupOutputArray = Extract>; -type OutputChunkorAsset = RollupOutputArray[number]['output'][number] +type OutputChunkorAsset = RollupOutputArray[number]['output'][number]; type OutputChunk = Extract; type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: string) => void; @@ -11,15 +11,16 @@ type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: str export type AstroBuildPlugin = { build: 'ssr' | 'client' | 'both'; hooks?: { - 'build:before'?: (opts: { - build: 'ssr' | 'client'; - input: Set; - }) => { + 'build:before'?: (opts: { build: 'ssr' | 'client'; input: Set }) => { enforce?: 'after-user-plugins'; - vitePlugin: VitePlugin | VitePlugin[] | undefined + vitePlugin: VitePlugin | VitePlugin[] | undefined; }; - 'build:post'?: (opts: {ssrOutputs: RollupOutputArray; clientOutputs: RollupOutputArray; mutate: MutateChunk}) => void | Promise; - } + 'build:post'?: (opts: { + ssrOutputs: RollupOutputArray; + clientOutputs: RollupOutputArray; + mutate: MutateChunk; + }) => void | Promise; + }; }; export function createPluginContainer(options: StaticBuildOptions, internals: BuildInternals) { @@ -32,7 +33,7 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu internals, register(plugin: AstroBuildPlugin) { allPlugins.add(plugin); - switch(plugin.build) { + switch (plugin.build) { case 'client': { clientPlugins.push(plugin); break; @@ -54,10 +55,10 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu let plugins = build === 'ssr' ? ssrPlugins : clientPlugins; let vitePlugins: Array = []; let lastVitePlugins: Array = []; - for(const plugin of plugins) { - if(plugin.hooks?.['build:before']) { + for (const plugin of plugins) { + if (plugin.hooks?.['build:before']) { let result = plugin.hooks['build:before']({ build, input }); - if(result.vitePlugin) { + if (result.vitePlugin) { vitePlugins.push(result.vitePlugin); } } @@ -65,27 +66,30 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu return { vitePlugins, - lastVitePlugins + lastVitePlugins, }; }, async runPostHook(ssrReturn: ViteBuildReturn, clientReturn: ViteBuildReturn | null) { - const mutations = new Map(); + const mutations = new Map< + string, + { + build: 'server' | 'client'; + code: string; + } + >(); const ssrOutputs: RollupOutputArray = []; const clientOutputs: RollupOutputArray = []; - if(Array.isArray(ssrReturn)) { + if (Array.isArray(ssrReturn)) { ssrOutputs.push(...ssrReturn); - } else if('output' in ssrReturn) { + } else if ('output' in ssrReturn) { ssrOutputs.push(ssrReturn); } - if(Array.isArray(clientReturn)) { + if (Array.isArray(clientReturn)) { clientOutputs.push(...clientReturn); - } else if(clientReturn && 'output' in clientReturn) { + } else if (clientReturn && 'output' in clientReturn) { clientOutputs.push(clientReturn); } @@ -97,19 +101,19 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu }); }; - for(const plugin of allPlugins) { + for (const plugin of allPlugins) { const postHook = plugin.hooks?.['build:post']; - if(postHook) { + if (postHook) { await postHook({ ssrOutputs, clientOutputs, - mutate + mutate, }); } } return mutations; - } + }, }; } diff --git a/packages/astro/src/core/build/plugins/index.ts b/packages/astro/src/core/build/plugins/index.ts index f253fe289..29591185d 100644 --- a/packages/astro/src/core/build/plugins/index.ts +++ b/packages/astro/src/core/build/plugins/index.ts @@ -1,14 +1,13 @@ -import type { AstroBuildPluginContainer, AstroBuildPlugin } from '../plugin'; -import type { PageBuildData, StaticBuildOptions } from '../types'; +import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.js'; +import type { AstroBuildPluginContainer } from '../plugin'; +import { pluginAliasResolve } from './plugin-alias-resolve.js'; import { pluginAnalyzer } from './plugin-analyzer.js'; +import { pluginCSS } from './plugin-css.js'; +import { pluginHoistedScripts } from './plugin-hoisted-scripts.js'; import { pluginInternals } from './plugin-internals.js'; import { pluginPages } from './plugin-pages.js'; -import { pluginCSS } from './plugin-css.js'; import { pluginPrerender } from './plugin-prerender.js'; -import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.js'; import { pluginSSR } from './plugin-ssr.js'; -import { pluginAliasResolve } from './plugin-alias-resolve.js'; -import { pluginHoistedScripts } from './plugin-hoisted-scripts.js'; export function registerAllPlugins({ internals, options, register }: AstroBuildPluginContainer) { register(pluginAliasResolve(internals)); diff --git a/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts b/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts index 1487ca2b9..7b0aba7e8 100644 --- a/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts +++ b/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts @@ -56,9 +56,9 @@ export function pluginAliasResolve(internals: BuildInternals): AstroBuildPlugin hooks: { 'build:before': () => { return { - vitePlugin: vitePluginAliasResolve(internals) + vitePlugin: vitePluginAliasResolve(internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-analyzer.ts b/packages/astro/src/core/build/plugins/plugin-analyzer.ts index 4214cde8e..6fd2ef7b2 100644 --- a/packages/astro/src/core/build/plugins/plugin-analyzer.ts +++ b/packages/astro/src/core/build/plugins/plugin-analyzer.ts @@ -1,7 +1,7 @@ import type { PluginContext } from 'rollup'; import type { Plugin as VitePlugin } from 'vite'; -import type { BuildInternals } from '../internal.js'; import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types'; +import type { BuildInternals } from '../internal.js'; import type { AstroBuildPlugin } from '../plugin.js'; import { prependForwardSlash } from '../../path.js'; @@ -130,9 +130,9 @@ export function pluginAnalyzer(internals: BuildInternals): AstroBuildPlugin { hooks: { 'build:before': () => { return { - vitePlugin: vitePluginAnalyzer(internals) + vitePlugin: vitePluginAnalyzer(internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-css.ts b/packages/astro/src/core/build/plugins/plugin-css.ts index b52b5c0fc..acd59746a 100644 --- a/packages/astro/src/core/build/plugins/plugin-css.ts +++ b/packages/astro/src/core/build/plugins/plugin-css.ts @@ -4,8 +4,8 @@ import type { GetModuleInfo } from 'rollup'; import { Plugin as VitePlugin, ResolvedConfig, transformWithEsbuild } from 'vite'; import { isCSSRequest } from '../../render/util.js'; import type { BuildInternals } from '../internal'; -import type { PageBuildData, StaticBuildOptions } from '../types'; import type { AstroBuildPlugin } from '../plugin'; +import type { PageBuildData, StaticBuildOptions } from '../types'; import { PROPAGATED_ASSET_FLAG } from '../../../content/consts.js'; import * as assetName from '../css-asset-name.js'; @@ -78,7 +78,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] } return createNameForParentPages(id, meta); } - } + }, }); }, @@ -265,7 +265,10 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] ]; } -export function pluginCSS(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { +export function pluginCSS( + options: StaticBuildOptions, + internals: BuildInternals +): AstroBuildPlugin { return { build: 'both', hooks: { @@ -273,13 +276,13 @@ export function pluginCSS(options: StaticBuildOptions, internals: BuildInternals let plugins = rollupPluginAstroBuildCSS({ buildOptions: options, internals, - target: build === 'ssr' ? 'server' : 'client' + target: build === 'ssr' ? 'server' : 'client', }); return { - vitePlugin: plugins + vitePlugin: plugins, }; - } - } - } + }, + }, + }; } diff --git a/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts b/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts index 9e21177bd..da40ede65 100644 --- a/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts +++ b/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts @@ -1,10 +1,10 @@ import type { Plugin as VitePlugin } from 'vite'; import type { AstroSettings } from '../../../@types/astro'; -import type { BuildInternals } from '../internal.js'; import { viteID } from '../../util.js'; +import type { BuildInternals } from '../internal.js'; import { getPageDataByViteID } from '../internal.js'; -import { StaticBuildOptions } from '../types'; import { AstroBuildPlugin } from '../plugin'; +import { StaticBuildOptions } from '../types'; function virtualHoistedEntry(id: string) { return id.startsWith('/astro/hoisted.js?q='); @@ -94,15 +94,18 @@ export function vitePluginHoistedScripts( }; } -export function pluginHoistedScripts(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { +export function pluginHoistedScripts( + options: StaticBuildOptions, + internals: BuildInternals +): AstroBuildPlugin { return { build: 'client', hooks: { 'build:before': () => { return { - vitePlugin: vitePluginHoistedScripts(options.settings, internals) + vitePlugin: vitePluginHoistedScripts(options.settings, internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-internals.ts b/packages/astro/src/core/build/plugins/plugin-internals.ts index 7177d8bef..982d958c3 100644 --- a/packages/astro/src/core/build/plugins/plugin-internals.ts +++ b/packages/astro/src/core/build/plugins/plugin-internals.ts @@ -68,9 +68,9 @@ export function pluginInternals(internals: BuildInternals): AstroBuildPlugin { hooks: { 'build:before': ({ input }) => { return { - vitePlugin: vitePluginInternals(input, internals) + vitePlugin: vitePluginInternals(input, internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-pages.ts b/packages/astro/src/core/build/plugins/plugin-pages.ts index ac436a364..f66494b28 100644 --- a/packages/astro/src/core/build/plugins/plugin-pages.ts +++ b/packages/astro/src/core/build/plugins/plugin-pages.ts @@ -1,6 +1,6 @@ import type { Plugin as VitePlugin } from 'vite'; -import type { StaticBuildOptions } from '../types'; import type { AstroBuildPlugin } from '../plugin'; +import type { StaticBuildOptions } from '../types'; import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../../app/index.js'; import { addRollupInput } from '../add-rollup-input.js'; @@ -62,9 +62,9 @@ export function pluginPages(opts: StaticBuildOptions, internals: BuildInternals) hooks: { 'build:before': () => { return { - vitePlugin: vitePluginPages(opts, internals) + vitePlugin: vitePluginPages(opts, internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts index 340e84ea6..911f153ba 100644 --- a/packages/astro/src/core/build/plugins/plugin-prerender.ts +++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts @@ -28,21 +28,24 @@ export function vitePluginPrerender( // dynamic pages should all go in their own chunk in the pages/* directory return `pages/all`; } - } + }, }); }, }; } -export function pluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { +export function pluginPrerender( + opts: StaticBuildOptions, + internals: BuildInternals +): AstroBuildPlugin { return { build: 'ssr', hooks: { 'build:before': () => { return { - vitePlugin: vitePluginPrerender(opts, internals) + vitePlugin: vitePluginPrerender(opts, internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 289703858..cfc58d71b 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -5,7 +5,6 @@ import type { BuildInternals } from '../internal.js'; import type { StaticBuildOptions } from '../types'; import glob from 'fast-glob'; -import * as fs from 'fs'; import { fileURLToPath } from 'url'; import { getContentPaths } from '../../../content/index.js'; import { runHookBuildSsr } from '../../../integrations/index.js'; @@ -220,7 +219,10 @@ function buildManifest( return ssrManifest; } -export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { +export function pluginSSR( + options: StaticBuildOptions, + internals: BuildInternals +): AstroBuildPlugin { const ssr = options.settings.config.output === 'server'; return { build: 'ssr', @@ -230,15 +232,15 @@ export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals return { enforce: 'after-user-plugins', - vitePlugin - } + vitePlugin, + }; }, 'build:post': async ({ mutate }) => { - if(!ssr) { + if (!ssr) { return; } - if(!internals.ssrEntryChunk) { + if (!internals.ssrEntryChunk) { throw new Error(`Did not generate an entry chunk for SSR`); } // Mutate the filename @@ -246,7 +248,7 @@ export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals const code = await injectManifest(options, internals); mutate(internals.ssrEntryChunk, 'server', code); - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/util.ts b/packages/astro/src/core/build/plugins/util.ts index 6129aa1e1..50f5e0705 100644 --- a/packages/astro/src/core/build/plugins/util.ts +++ b/packages/astro/src/core/build/plugins/util.ts @@ -7,14 +7,14 @@ type OutputOptions = Parameters[0]; type ExtendManualChunksHooks = { before?: (id: string, meta: any) => string | undefined; after?: (id: string, meta: any) => string | undefined; -} +}; export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendManualChunksHooks) { const manualChunks = outputOptions.manualChunks; - outputOptions.manualChunks = function(id, meta) { - if(hooks.before) { + outputOptions.manualChunks = function (id, meta) { + if (hooks.before) { let value = hooks.before(id, meta); - if(value) { + if (value) { return value; } } @@ -31,8 +31,8 @@ export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendMa return outid; } } - - if(hooks.after) { + + if (hooks.after) { return hooks.after(id, meta) || null; } return null; diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 726c1db88..4e3238978 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -20,10 +20,10 @@ import { info } from '../logger/core.js'; import { getOutDirWithinCwd } from './common.js'; import { generatePages } from './generate.js'; import { trackPageData } from './internal.js'; -import type { PageBuildData, StaticBuildOptions } from './types'; -import { getTimeStat } from './util.js'; import { AstroBuildPluginContainer, createPluginContainer } from './plugin.js'; import { registerAllPlugins } from './plugins/index.js'; +import type { PageBuildData, StaticBuildOptions } from './types'; +import { getTimeStat } from './util.js'; export async function staticBuild(opts: StaticBuildOptions) { const { allPages, settings } = opts; @@ -67,7 +67,6 @@ export async function staticBuild(opts: StaticBuildOptions) { const container = createPluginContainer(opts, internals); registerAllPlugins(container); - // Build your project (SSR application code, assets, client JS, etc.) timer.ssr = performance.now(); info(opts.logging, 'build', `Building ${settings.config.output} entrypoints...`); @@ -112,7 +111,12 @@ export async function staticBuild(opts: StaticBuildOptions) { } } -async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set, container: AstroBuildPluginContainer) { +async function ssrBuild( + opts: StaticBuildOptions, + internals: BuildInternals, + input: Set, + container: AstroBuildPluginContainer +) { const { settings, viteConfig } = opts; const ssr = settings.config.output === 'server'; const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir); @@ -155,11 +159,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp modulePreload: { polyfill: false }, reportCompressedSize: false, }, - plugins: [ - ...vitePlugins, - ...(viteConfig.plugins || []), - ...lastVitePlugins - ], + plugins: [...vitePlugins, ...(viteConfig.plugins || []), ...lastVitePlugins], envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', base: settings.config.base, }; @@ -199,7 +199,6 @@ async function clientBuild( const { lastVitePlugins, vitePlugins } = container.runBeforeHook('client', input); info(opts.logging, null, `\n${bgGreen(black(' building client '))}`); - const viteBuildConfig: vite.InlineConfig = { ...viteConfig, mode: viteConfig.mode || 'production', @@ -222,11 +221,7 @@ async function clientBuild( preserveEntrySignatures: 'exports-only', }, }, - plugins: [ - ...vitePlugins, - ...(viteConfig.plugins || []), - ...lastVitePlugins, - ], + plugins: [...vitePlugins, ...(viteConfig.plugins || []), ...lastVitePlugins], envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', base: settings.config.base, }; @@ -251,7 +246,7 @@ async function runPostBuildHooks( ) { const mutations = await container.runPostHook(ssrReturn, clientReturn); const buildConfig = container.options.settings.config.build; - for(const [fileName, mutation] of mutations) { + for (const [fileName, mutation] of mutations) { const root = mutation.build === 'server' ? buildConfig.server : buildConfig.client; const fileURL = new URL(fileName, root); await fs.promises.mkdir(new URL('./', fileURL), { recursive: true }); diff --git a/packages/astro/src/core/build/types.ts b/packages/astro/src/core/build/types.ts index a9ca08a0e..151398c54 100644 --- a/packages/astro/src/core/build/types.ts +++ b/packages/astro/src/core/build/types.ts @@ -1,4 +1,4 @@ -import type { InlineConfig } from 'vite'; +import type { default as vite, InlineConfig } from 'vite'; import type { AstroConfig, AstroSettings, @@ -11,7 +11,6 @@ import type { } from '../../@types/astro'; import type { LogOptions } from '../logger/core'; import type { RouteCache } from '../render/route-cache'; -import type { default as vite } from 'vite'; export type ComponentPath = string; export type ViteID = string; @@ -47,5 +46,8 @@ export interface SingleFileBuiltModule { } export type ViteBuildReturn = Awaited>; -export type RollupOutput = Extract>>, { output: any }>; +export type RollupOutput = Extract< + Extract>>, + { output: any } +>; export type OutputChunk = Extract; From 79fca438e97a5d0f25defd28676835ab3885149b Mon Sep 17 00:00:00 2001 From: "Houston (Bot)" <108291165+astrobot-houston@users.noreply.github.com> Date: Fri, 3 Feb 2023 06:09:54 -0800 Subject: [PATCH 11/12] [ci] update lockfile (#6115) Co-authored-by: FredKSchott --- pnpm-lock.yaml | 98 +++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 350e5a54c..1d156cfaa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -472,7 +472,7 @@ importers: '@astrojs/webapi': link:../webapi '@babel/core': 7.20.12 '@babel/generator': 7.20.14 - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@babel/plugin-transform-react-jsx': 7.20.13_@babel+core@7.20.12 '@babel/traverse': 7.20.13 '@babel/types': 7.20.7 @@ -513,8 +513,8 @@ importers: typescript: 4.9.5 unist-util-visit: 4.1.2 vfile: 5.3.6 - vite: 4.1.0_sass@1.58.0 - vitefu: 0.2.4_vite@4.1.0 + vite: 4.1.1_sass@1.58.0 + vitefu: 0.2.4_vite@4.1.1 yargs-parser: 21.1.1 zod: 3.20.2 devDependencies: @@ -581,7 +581,7 @@ importers: kleur: ^4.1.5 mocha: ^9.2.2 dependencies: - fast-xml-parser: 4.1.0 + fast-xml-parser: 4.1.1 kleur: 4.1.5 devDependencies: '@types/chai': 4.3.4 @@ -2725,7 +2725,7 @@ importers: mocha: 9.2.2 rollup-plugin-copy: 3.4.0 sharp: 0.31.3 - vite: 4.1.0 + vite: 4.1.1 packages/integrations/image/test/fixtures/background-color-image: specifiers: @@ -2933,7 +2933,7 @@ importers: remark-rehype: 10.1.0 remark-shiki-twoslash: 3.1.0 remark-toc: 8.0.1 - vite: 4.1.0 + vite: 4.1.1 packages/integrations/mdx/test/fixtures/mdx-frontmatter-injection: specifiers: @@ -3034,7 +3034,7 @@ importers: chai: 4.3.7 cheerio: 1.0.0-rc.12 mocha: 9.2.2 - vite: 4.1.0_@types+node@14.18.36 + vite: 4.1.1_@types+node@14.18.36 packages/integrations/netlify/test/edge-functions/fixtures/dynimport: specifiers: @@ -3286,13 +3286,13 @@ importers: svelte2tsx: ^0.5.11 vite: ^4.0.3 dependencies: - '@sveltejs/vite-plugin-svelte': 2.0.2_svelte@3.55.1+vite@4.1.0 + '@sveltejs/vite-plugin-svelte': 2.0.2_svelte@3.55.1+vite@4.1.1 svelte2tsx: 0.5.23_svelte@3.55.1 devDependencies: astro: link:../../astro astro-scripts: link:../../../scripts svelte: 3.55.1 - vite: 4.1.0 + vite: 4.1.1 packages/integrations/tailwind: specifiers: @@ -3313,7 +3313,7 @@ importers: astro: link:../../astro astro-scripts: link:../../../scripts tailwindcss: 3.2.4_postcss@8.4.21 - vite: 4.1.0 + vite: 4.1.1 packages/integrations/turbolinks: specifiers: @@ -3372,8 +3372,8 @@ importers: vite: ^4.0.3 vue: ^3.2.37 dependencies: - '@vitejs/plugin-vue': 4.0.0_vite@4.1.0+vue@3.2.47 - '@vitejs/plugin-vue-jsx': 3.0.0_vite@4.1.0+vue@3.2.47 + '@vitejs/plugin-vue': 4.0.0_vite@4.1.1+vue@3.2.47 + '@vitejs/plugin-vue-jsx': 3.0.0_vite@4.1.1+vue@3.2.47 '@vue/babel-plugin-jsx': 1.1.1 '@vue/compiler-sfc': 3.2.47 devDependencies: @@ -3383,7 +3383,7 @@ importers: chai: 4.3.7 linkedom: 0.14.21 mocha: 9.2.2 - vite: 4.1.0 + vite: 4.1.1 vue: 3.2.47 packages/integrations/vue/test/fixtures/app-entrypoint: @@ -3946,7 +3946,7 @@ packages: '@babel/helper-compilation-targets': 7.20.7_@babel+core@7.20.12 '@babel/helper-module-transforms': 7.20.11 '@babel/helpers': 7.20.13 - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@babel/template': 7.20.7 '@babel/traverse': 7.20.13 '@babel/types': 7.20.7 @@ -4221,8 +4221,8 @@ packages: chalk: 2.4.2 js-tokens: 4.0.0 - /@babel/parser/7.20.13: - resolution: {integrity: sha512-gFDLKMfpiXCsjt4za2JA9oTMn70CeseCehb11kRZgvd7+F67Hih3OHOK24cRrWECJ/ljfPGac6ygXAs/C8kIvw==} + /@babel/parser/7.20.15: + resolution: {integrity: sha512-DI4a1oZuf8wC+oAJA9RW6ga3Zbe8RZFt7kD9i4qAspz3I/yHet1VvC3DiSy/fsUvv5pvJuNPh0LPOdCcqinDPg==} engines: {node: '>=6.0.0'} hasBin: true dependencies: @@ -4750,8 +4750,8 @@ packages: '@babel/helper-plugin-utils': 7.20.2 dev: false - /@babel/plugin-transform-block-scoping/7.20.14_@babel+core@7.20.12: - resolution: {integrity: sha512-sMPepQtsOs5fM1bwNvuJJHvaCfOEQfmc01FGw0ELlTpTJj5Ql/zuNRRldYhAPys4ghXdBIQJbRVYi44/7QflQQ==} + /@babel/plugin-transform-block-scoping/7.20.15_@babel+core@7.20.12: + resolution: {integrity: sha512-Vv4DMZ6MiNOhu/LdaZsT/bsLRxgL94d269Mv4R/9sp6+Mp++X/JqypZYypJXLlM4mlL352/Egzbzr98iABH1CA==} engines: {node: '>=6.9.0'} peerDependencies: '@babel/core': ^7.0.0-0 @@ -5247,7 +5247,7 @@ packages: '@babel/plugin-transform-arrow-functions': 7.20.7_@babel+core@7.20.12 '@babel/plugin-transform-async-to-generator': 7.20.7_@babel+core@7.20.12 '@babel/plugin-transform-block-scoped-functions': 7.18.6_@babel+core@7.20.12 - '@babel/plugin-transform-block-scoping': 7.20.14_@babel+core@7.20.12 + '@babel/plugin-transform-block-scoping': 7.20.15_@babel+core@7.20.12 '@babel/plugin-transform-classes': 7.20.7_@babel+core@7.20.12 '@babel/plugin-transform-computed-properties': 7.20.7_@babel+core@7.20.12 '@babel/plugin-transform-destructuring': 7.20.7_@babel+core@7.20.12 @@ -5314,7 +5314,7 @@ packages: engines: {node: '>=6.9.0'} dependencies: '@babel/code-frame': 7.18.6 - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@babel/types': 7.20.7 dev: false @@ -5328,7 +5328,7 @@ packages: '@babel/helper-function-name': 7.19.0 '@babel/helper-hoist-variables': 7.18.6 '@babel/helper-split-export-declaration': 7.18.6 - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@babel/types': 7.20.7 debug: 4.3.4 globals: 11.12.0 @@ -6776,7 +6776,7 @@ packages: string.prototype.matchall: 4.0.8 dev: false - /@sveltejs/vite-plugin-svelte/2.0.2_svelte@3.55.1+vite@4.1.0: + /@sveltejs/vite-plugin-svelte/2.0.2_svelte@3.55.1+vite@4.1.1: resolution: {integrity: sha512-xCEan0/NNpQuL0l5aS42FjwQ6wwskdxC3pW1OeFtEKNZwRg7Evro9lac9HesGP6TdFsTv2xMes5ASQVKbCacxg==} engines: {node: ^14.18.0 || >= 16} peerDependencies: @@ -6792,8 +6792,8 @@ packages: magic-string: 0.27.0 svelte: 3.55.1 svelte-hmr: 0.15.1_svelte@3.55.1 - vite: 4.1.0 - vitefu: 0.2.4_vite@4.1.0 + vite: 4.1.1 + vitefu: 0.2.4_vite@4.1.1 transitivePeerDependencies: - supports-color dev: false @@ -6827,7 +6827,7 @@ packages: /@types/babel__core/7.20.0: resolution: {integrity: sha512-+n8dL/9GWblDO0iU6eZAwEIJVr5DWigtle+Q6HLOrh/pdbXOhOtqzq8VPPE2zvNJzSKY4vH/z3iT3tn0A3ypiQ==} dependencies: - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@babel/types': 7.20.7 '@types/babel__generator': 7.6.4 '@types/babel__template': 7.4.1 @@ -6842,7 +6842,7 @@ packages: /@types/babel__template/7.4.1: resolution: {integrity: sha512-azBFKemX6kMg5Io+/rdGT0dkGreboUVR0Cdm3fz9QJWpaQGJRQXl7C+6hOTCZcMll7KFyEQpgbYI2lHdsS4U7g==} dependencies: - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@babel/types': 7.20.7 dev: false @@ -7446,7 +7446,7 @@ packages: - supports-color dev: false - /@vitejs/plugin-vue-jsx/3.0.0_vite@4.1.0+vue@3.2.47: + /@vitejs/plugin-vue-jsx/3.0.0_vite@4.1.1+vue@3.2.47: resolution: {integrity: sha512-vurkuzgac5SYuxd2HUZqAFAWGTF10diKBwJNbCvnWijNZfXd+7jMtqjPFbGt7idOJUn584fP1Ar9j/GN2jQ3Ew==} engines: {node: ^14.18.0 || >=16.0.0} peerDependencies: @@ -7459,13 +7459,13 @@ packages: '@babel/core': 7.20.12 '@babel/plugin-transform-typescript': 7.20.13_@babel+core@7.20.12 '@vue/babel-plugin-jsx': 1.1.1_@babel+core@7.20.12 - vite: 4.1.0 + vite: 4.1.1 vue: 3.2.47 transitivePeerDependencies: - supports-color dev: false - /@vitejs/plugin-vue/4.0.0_vite@4.1.0+vue@3.2.47: + /@vitejs/plugin-vue/4.0.0_vite@4.1.1+vue@3.2.47: resolution: {integrity: sha512-e0X4jErIxAB5oLtDqbHvHpJe/uWNkdpYV83AOG2xo2tEVSzCzewgJMtREZM30wXnM5ls90hxiOtAuVU6H5JgbA==} engines: {node: ^14.18.0 || >=16.0.0} peerDependencies: @@ -7475,7 +7475,7 @@ packages: vite: optional: true dependencies: - vite: 4.1.0 + vite: 4.1.1 vue: 3.2.47 dev: false @@ -7534,7 +7534,7 @@ packages: /@vue/compiler-core/3.2.47: resolution: {integrity: sha512-p4D7FDnQb7+YJmO2iPEv0SQNeNzcbHdGByJDsT4lynf63AFkOTFN07HsiRSvjGo0QrxR/o3d0hUyNCUnBU2Tig==} dependencies: - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@vue/shared': 3.2.47 estree-walker: 2.0.2 source-map: 0.6.1 @@ -7548,7 +7548,7 @@ packages: /@vue/compiler-sfc/3.2.47: resolution: {integrity: sha512-rog05W+2IFfxjMcFw10tM9+f7i/+FFpZJJ5XHX72NP9eC2uRD+42M3pYcQqDXVYoj74kHMSEdQ/WmCjt8JFksQ==} dependencies: - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@vue/compiler-core': 3.2.47 '@vue/compiler-dom': 3.2.47 '@vue/compiler-ssr': 3.2.47 @@ -7568,7 +7568,7 @@ packages: /@vue/reactivity-transform/3.2.47: resolution: {integrity: sha512-m8lGXw8rdnPVVIdIFhf0LeQ/ixyHkH5plYuS83yop5n7ggVJU+z5v0zecwEnX7fa7HNLBhh2qngJJkxpwEEmYA==} dependencies: - '@babel/parser': 7.20.13 + '@babel/parser': 7.20.15 '@vue/compiler-core': 3.2.47 '@vue/shared': 3.2.47 estree-walker: 2.0.2 @@ -8112,7 +8112,7 @@ packages: hasBin: true dependencies: caniuse-lite: 1.0.30001450 - electron-to-chromium: 1.4.284 + electron-to-chromium: 1.4.285 node-releases: 2.0.9 update-browserslist-db: 1.0.10_browserslist@4.21.5 @@ -8952,8 +8952,8 @@ packages: jake: 10.8.5 dev: false - /electron-to-chromium/1.4.284: - resolution: {integrity: sha512-M8WEXFuKXMYMVr45fo8mq0wUrrJHheiKZf6BArTKk9ZBYCKJEOU5H8cdWgDT+qCVZf7Na4lVUaZsA+h6uA9+PA==} + /electron-to-chromium/1.4.285: + resolution: {integrity: sha512-47o4PPgxfU1KMNejz+Dgaodf7YTcg48uOfV1oM6cs3adrl2+7R+dHkt3Jpxqo0LRCbGJEzTKMUt0RdvByb/leg==} /emmet/2.3.6: resolution: {integrity: sha512-pLS4PBPDdxuUAmw7Me7+TcHbykTsBKN/S9XJbUOMFQrNv9MoshzyMFK/R57JBm94/6HSL4vHnDeEmxlC82NQ4A==} @@ -9840,8 +9840,8 @@ packages: resolution: {integrity: sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==} dev: true - /fast-xml-parser/4.1.0: - resolution: {integrity: sha512-IuFUr5zpPwsoy6NRtgLK1pEuJe89Sd0gYS0U4dIP5tRbpzALraOcxJVQT6ZBNheMVdX7nBkNpeagHVpOYnLcmQ==} + /fast-xml-parser/4.1.1: + resolution: {integrity: sha512-4gAP5PvNyrqePBOIIcpaEeE+nKBry1n6qTQiJsE59sLP0OC+YwhU7/XVmLLEMexbiluFQX1yEYm82Pk9B7xEiw==} hasBin: true dependencies: strnum: 1.0.5 @@ -10636,8 +10636,8 @@ packages: sharp: 0.31.3 dev: false - /immutable/4.2.2: - resolution: {integrity: sha512-fTMKDwtbvO5tldky9QZ2fMX7slR0mYpY5nbnFWYp0fOzDhHqhgIw9KoYgxLWsoNTS9ZHGauHj18DTyEw6BK3Og==} + /immutable/4.2.3: + resolution: {integrity: sha512-IHpmvaOIX4VLJwPOuQr1NpeBr2ZG6vpIj3blsLVxXRWJscLioaJRStqC+NcBsLeCDsnGlPpXd5/WZmnE7MbsKA==} /import-fresh/3.3.0: resolution: {integrity: sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==} @@ -13769,7 +13769,7 @@ packages: hasBin: true dependencies: chokidar: 3.5.3 - immutable: 4.2.2 + immutable: 4.2.3 source-map-js: 1.0.2 /sax/1.2.4: @@ -15148,8 +15148,8 @@ packages: fsevents: 2.3.2 dev: false - /vite/4.1.0: - resolution: {integrity: sha512-YoUKE/9bbK4C8ZeSYMDDEF8H5aypmWUq4WisftDhntR1gkI2zt2SGT/5Wd2xu6ZoVXkCyO3U4844KWG9e4nFoQ==} + /vite/4.1.1: + resolution: {integrity: sha512-LM9WWea8vsxhr782r9ntg+bhSFS06FJgCvvB0+8hf8UWtvaiDagKYWXndjfX6kGl74keHJUcpzrQliDXZlF5yg==} engines: {node: ^14.18.0 || >=16.0.0} hasBin: true peerDependencies: @@ -15180,8 +15180,8 @@ packages: optionalDependencies: fsevents: 2.3.2 - /vite/4.1.0_@types+node@14.18.36: - resolution: {integrity: sha512-YoUKE/9bbK4C8ZeSYMDDEF8H5aypmWUq4WisftDhntR1gkI2zt2SGT/5Wd2xu6ZoVXkCyO3U4844KWG9e4nFoQ==} + /vite/4.1.1_@types+node@14.18.36: + resolution: {integrity: sha512-LM9WWea8vsxhr782r9ntg+bhSFS06FJgCvvB0+8hf8UWtvaiDagKYWXndjfX6kGl74keHJUcpzrQliDXZlF5yg==} engines: {node: ^14.18.0 || >=16.0.0} hasBin: true peerDependencies: @@ -15214,8 +15214,8 @@ packages: fsevents: 2.3.2 dev: true - /vite/4.1.0_sass@1.58.0: - resolution: {integrity: sha512-YoUKE/9bbK4C8ZeSYMDDEF8H5aypmWUq4WisftDhntR1gkI2zt2SGT/5Wd2xu6ZoVXkCyO3U4844KWG9e4nFoQ==} + /vite/4.1.1_sass@1.58.0: + resolution: {integrity: sha512-LM9WWea8vsxhr782r9ntg+bhSFS06FJgCvvB0+8hf8UWtvaiDagKYWXndjfX6kGl74keHJUcpzrQliDXZlF5yg==} engines: {node: ^14.18.0 || >=16.0.0} hasBin: true peerDependencies: @@ -15257,7 +15257,7 @@ packages: optional: true dev: false - /vitefu/0.2.4_vite@4.1.0: + /vitefu/0.2.4_vite@4.1.1: resolution: {integrity: sha512-fanAXjSaf9xXtOOeno8wZXIhgia+CZury481LsDaV++lSvcU2R9Ch2bPh3PYFyoHW+w9LqAeYRISVQjUIew14g==} peerDependencies: vite: ^3.0.0 || ^4.0.0 @@ -15265,7 +15265,7 @@ packages: vite: optional: true dependencies: - vite: 4.1.0_sass@1.58.0 + vite: 4.1.1_sass@1.58.0 dev: false /vitest/0.20.3: From 45b41d98f50dc9f76a5004a8b3346f393f1a6cb6 Mon Sep 17 00:00:00 2001 From: Nacho Vazquez Date: Fri, 3 Feb 2023 12:19:44 -0300 Subject: [PATCH 12/12] fix: use the root of the project as the functions location (#6075) * fix: use the root of the project as the functions location * test: add test to check where the functions folder is added --- .changeset/honest-beds-flow.md | 5 +++++ packages/integrations/cloudflare/src/index.ts | 2 +- .../integrations/cloudflare/test/directory.test.js | 13 +++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 .changeset/honest-beds-flow.md diff --git a/.changeset/honest-beds-flow.md b/.changeset/honest-beds-flow.md new file mode 100644 index 000000000..ddf54699f --- /dev/null +++ b/.changeset/honest-beds-flow.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': patch +--- + +Uses config root path as location for Cloudflare Pages Functions diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 3433cf46d..7ba1cc631 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -203,7 +203,7 @@ export default function createIntegration(args?: Options): AstroIntegration { } if (isModeDirectory) { - const functionsUrl = new URL(`file://${process.cwd()}/functions/`); + const functionsUrl = new URL('functions', _config.root); await fs.promises.mkdir(functionsUrl, { recursive: true }); const directoryUrl = new URL('[[path]].js', functionsUrl); await fs.promises.rename(finalBuildUrl, directoryUrl); diff --git a/packages/integrations/cloudflare/test/directory.test.js b/packages/integrations/cloudflare/test/directory.test.js index 7c299f526..e5b520574 100644 --- a/packages/integrations/cloudflare/test/directory.test.js +++ b/packages/integrations/cloudflare/test/directory.test.js @@ -1,20 +1,21 @@ -import { loadFixture, runCLI } from './test-utils.js'; +import { loadFixture } from './test-utils.js'; import { expect } from 'chai'; -import * as cheerio from 'cheerio'; import cloudflare from '../dist/index.js'; +/** @type {import('./test-utils').Fixture} */ describe('mode: "directory"', () => { - /** @type {import('./test-utils').Fixture} */ let fixture; before(async () => { fixture = await loadFixture({ root: './fixtures/basics/', + output: 'server', adapter: cloudflare({ mode: 'directory' }), }); - }); - - it('Builds', async () => { await fixture.build(); }); + + it('generates functions folder inside the project root', async () => { + expect(await fixture.pathExists('../functions')).to.be.true; + }); });