-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic plugin for
page-ssr
injection (#4049)
* feat: add generic page-ssr plugin * refactor: remove page-specific logic from astro/markdown/mdx plugins * refactor: revert changes to vite-plugin-scripts * fix: handle injected `page` scripts in build * fix: prepend injected `page` scripts with `/@id/` in dev Co-authored-by: Nate Moore <[email protected]>
- Loading branch information
1 parent
9cc3a11
commit b60cc05
Showing
10 changed files
with
93 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'astro': patch | ||
'@astrojs/mdx': patch | ||
--- | ||
|
||
Improve `injectScript` handling for non-Astro pages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Plugin as VitePlugin } from 'vite'; | ||
import { AstroConfig } from '../@types/astro.js'; | ||
import { PAGE_SSR_SCRIPT_ID } from './index.js'; | ||
|
||
import { isPage } 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:page-ssr', | ||
enforce: 'post', | ||
|
||
transform(this, code, id, options) { | ||
if (!options?.ssr) return; | ||
|
||
const hasInjectedScript = config._ctx.scripts.some((s) => s.stage === 'page-ssr'); | ||
if (!hasInjectedScript) 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 fileIsPage = isPage(fileURL, config); | ||
if (!fileIsPage) return; | ||
|
||
const s = new MagicString(code, { filename }); | ||
s.prepend(`import '${PAGE_SSR_SCRIPT_ID}';\n`); | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap(), | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters