From d024fa71bc15522eacd3588ba7a638a8b5858a0b Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 25 Jul 2022 23:08:47 -0400 Subject: [PATCH 1/5] feat: add generic page-ssr plugin --- packages/astro/src/core/create-vite.ts | 2 + .../astro/src/vite-plugin-scripts/page.ts | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 packages/astro/src/vite-plugin-scripts/page.ts diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 7b3a8070f72b..42d736ff6b8e 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -14,6 +14,7 @@ import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-contai import jsxVitePlugin from '../vite-plugin-jsx/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import astroScriptsPlugin from '../vite-plugin-scripts/index.js'; +import astroScriptsPagePlugin from '../vite-plugin-scripts/page.js'; import { createCustomViteLogger } from './errors.js'; import { resolveDependency } from './util.js'; @@ -80,6 +81,7 @@ export async function createVite( jsxVitePlugin({ config: astroConfig, logging }), astroPostprocessVitePlugin({ config: astroConfig }), astroIntegrationsContainerPlugin({ config: astroConfig }), + astroScriptsPagePlugin({ config: astroConfig }), ], publicDir: fileURLToPath(astroConfig.publicDir), root: fileURLToPath(astroConfig.root), diff --git a/packages/astro/src/vite-plugin-scripts/page.ts b/packages/astro/src/vite-plugin-scripts/page.ts new file mode 100644 index 000000000000..5cc09b276d70 --- /dev/null +++ b/packages/astro/src/vite-plugin-scripts/page.ts @@ -0,0 +1,53 @@ +import { Plugin as VitePlugin } from 'vite'; +import { AstroConfig } from '../@types/astro.js'; +import { PAGE_SSR_SCRIPT_ID } from './index.js'; + +import { resolvePages } from '../core/util.js'; +import ancestor from 'common-ancestor-path'; +import MagicString from 'magic-string'; + +export default function astroScriptsPostPlugin({ config }: { config: AstroConfig }): VitePlugin { + function normalizeFilename(filename: string) { + if (filename.startsWith('/@fs')) { + filename = filename.slice('/@fs'.length); + } else if (filename.startsWith('/') && !ancestor(filename, config.root.pathname)) { + filename = new URL('.' + filename, config.root).pathname; + } + return filename; + } + + return { + name: 'astro:scripts:post', + enforce: 'post', + + transform(this, code, id, options) { + if (!options?.ssr) return; + + const filename = normalizeFilename(id); + let fileUrl: URL; + try { + fileUrl = new URL(`file://${filename}`); + } catch (e) { + // If we can't construct a valid URL, exit early + return; + } + + const isPage = fileUrl.pathname.startsWith(resolvePages(config).pathname); + if (!isPage) return; + const parts = fileUrl.pathname.slice(resolvePages(config).pathname.length).split('/'); + for (const part of parts) { + if (part.startsWith('_')) return; + } + const hasInjectedScript = config._ctx.scripts.some((s) => s.stage === 'page-ssr'); + if (!hasInjectedScript) return; + + const s = new MagicString(code, { filename }); + s.prepend(`import '${PAGE_SSR_SCRIPT_ID}';\n`); + + return { + code: s.toString(), + map: s.generateMap(), + } + }, + }; +} From 1b966cfc7a33a5ea5825e4d66c6e1b2eee68dece Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 1 Aug 2022 15:23:36 -0500 Subject: [PATCH 2/5] refactor: remove page-specific logic from astro/markdown/mdx plugins --- .changeset/plenty-hats-grow.md | 6 ++ packages/astro/src/core/create-vite.ts | 4 +- packages/astro/src/core/render/dev/index.ts | 7 ++ packages/astro/src/vite-plugin-astro/index.ts | 14 --- .../astro/src/vite-plugin-markdown/index.ts | 8 +- .../astro/src/vite-plugin-scripts/index.ts | 88 ++++++++++--------- .../{page.ts => page-ssr.ts} | 21 ++--- packages/integrations/mdx/src/index.ts | 7 -- 8 files changed, 71 insertions(+), 84 deletions(-) create mode 100644 .changeset/plenty-hats-grow.md rename packages/astro/src/vite-plugin-scripts/{page.ts => page-ssr.ts} (74%) diff --git a/.changeset/plenty-hats-grow.md b/.changeset/plenty-hats-grow.md new file mode 100644 index 000000000000..d7238f840cac --- /dev/null +++ b/.changeset/plenty-hats-grow.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'@astrojs/mdx': patch +--- + +Improve `injectScript` handling for non-Astro pages diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 42d736ff6b8e..631c1dc2b9c3 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -14,7 +14,7 @@ import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-contai import jsxVitePlugin from '../vite-plugin-jsx/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import astroScriptsPlugin from '../vite-plugin-scripts/index.js'; -import astroScriptsPagePlugin from '../vite-plugin-scripts/page.js'; +import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js'; import { createCustomViteLogger } from './errors.js'; import { resolveDependency } from './util.js'; @@ -81,7 +81,7 @@ export async function createVite( jsxVitePlugin({ config: astroConfig, logging }), astroPostprocessVitePlugin({ config: astroConfig }), astroIntegrationsContainerPlugin({ config: astroConfig }), - astroScriptsPagePlugin({ config: astroConfig }), + astroScriptsPageSSRPlugin({ config: astroConfig }), ], publicDir: fileURLToPath(astroConfig.publicDir), root: fileURLToPath(astroConfig.root), diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index a6643fc359cb..de45a3ae182b 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -9,6 +9,7 @@ import type { SSRElement, SSRLoadedRenderer, } from '../../../@types/astro'; +import { PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js'; import { prependForwardSlash } from '../../../core/path.js'; import { LogOptions } from '../../logger/core.js'; import { isPage } from '../../util.js'; @@ -124,6 +125,7 @@ export async function render( children: '', }); } + // TODO: We should allow adding generic HTML elements to the head, not just scripts for (const script of astroConfig._ctx.scripts) { if (script.stage === 'head-inline') { @@ -131,6 +133,11 @@ export async function render( props: {}, children: script.content, }); + } else if (script.stage === 'page' && isPage(filePath, astroConfig)) { + scripts.add({ + props: { type: 'module', src: PAGE_SCRIPT_ID }, + children: '', + }); } } diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index b4925d0fd66e..f38d5e5ca11b 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -9,8 +9,6 @@ import esbuild from 'esbuild'; import slash from 'slash'; import { fileURLToPath } from 'url'; import { isRelativePath, startsWithForwardSlash } from '../core/path.js'; -import { resolvePages } from '../core/util.js'; -import { PAGE_SCRIPT_ID, PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js'; import { getFileInfo } from '../vite-plugin-utils/index.js'; import { cachedCompilation, CompileProps, getCachedSource } from './compile.js'; import { handleHotUpdate, trackCSSDependencies } from './hmr.js'; @@ -215,14 +213,6 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu } const filename = normalizeFilename(parsedId.filename); - let isPage = false; - try { - const fileUrl = new URL(`file://${filename}`); - isPage = fileUrl.pathname.startsWith(resolvePages(config).pathname); - } catch {} - if (isPage && config._ctx.scripts.some((s) => s.stage === 'page')) { - source += `\n