Skip to content

Commit

Permalink
feat: add generic page-ssr plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Aug 1, 2022
1 parent f62f05f commit d024fa7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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),
Expand Down
53 changes: 53 additions & 0 deletions packages/astro/src/vite-plugin-scripts/page.ts
Original file line number Diff line number Diff line change
@@ -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(),
}
},
};
}

0 comments on commit d024fa7

Please sign in to comment.