-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hoist Astro.globbed hoisted scripts in dev (#3930)
* Hoist Astro.globbed hoisted scripts in dev * Adds a changeset * Increase the timeout for the HMR test * Fix e2e tests * Refactor test
- Loading branch information
Showing
11 changed files
with
276 additions
and
189 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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Include hoisted scripts inside Astro.glob in dev |
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,44 @@ | ||
import type { AstroConfig, SSRElement } from '../../../@types/astro'; | ||
import type { ModuleInfo } from 'rollup'; | ||
import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types'; | ||
import vite from 'vite'; | ||
import slash from 'slash'; | ||
import { viteID } from '../../util.js'; | ||
import { createModuleScriptElementWithSrc } from '../ssr-element.js'; | ||
import { crawlGraph } from './vite.js'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
export async function getScriptsForURL( | ||
filePath: URL, | ||
astroConfig: AstroConfig, | ||
viteServer: vite.ViteDevServer, | ||
): Promise<Set<SSRElement>> { | ||
const elements = new Set<SSRElement>(); | ||
const rootID = viteID(filePath); | ||
let rootProjectFolder = slash(fileURLToPath(astroConfig.root)); | ||
const modInfo = viteServer.pluginContainer.getModuleInfo(rootID); | ||
addHoistedScripts(elements, modInfo, rootProjectFolder); | ||
for await(const moduleNode of crawlGraph(viteServer, rootID, true)) { | ||
const id = moduleNode.id; | ||
if(id) { | ||
const info = viteServer.pluginContainer.getModuleInfo(id); | ||
addHoistedScripts(elements, info, rootProjectFolder); | ||
} | ||
} | ||
|
||
return elements; | ||
} | ||
|
||
function addHoistedScripts(set: Set<SSRElement>, info: ModuleInfo | null, rootProjectFolder: string) { | ||
if(!info?.meta?.astro) { | ||
return; | ||
} | ||
|
||
let id = info.id; | ||
const astro = info?.meta?.astro as AstroPluginMetadata['astro']; | ||
for(let i = 0; i < astro.scripts.length; i++) { | ||
const scriptId = `${id}?astro&type=script&index=${i}&lang.ts`; | ||
const element = createModuleScriptElementWithSrc(scriptId); | ||
set.add(element); | ||
} | ||
} |
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,68 @@ | ||
import vite from 'vite'; | ||
import npath from 'path'; | ||
import { unwrapId } from '../../util.js'; | ||
|
||
/** | ||
* List of file extensions signalling we can (and should) SSR ahead-of-time | ||
* See usage below | ||
*/ | ||
const fileExtensionsToSSR = new Set(['.astro', '.md']); | ||
|
||
/** recursively crawl the module graph to get all style files imported by parent id */ | ||
export async function * crawlGraph( | ||
viteServer: vite.ViteDevServer, | ||
_id: string, | ||
isFile: boolean, | ||
scanned = new Set<string>() | ||
) : AsyncGenerator<vite.ModuleNode, void, unknown> { | ||
const id = unwrapId(_id); | ||
const importedModules = new Set<vite.ModuleNode>(); | ||
const moduleEntriesForId = isFile | ||
? // If isFile = true, then you are at the root of your module import tree. | ||
// The `id` arg is a filepath, so use `getModulesByFile()` to collect all | ||
// nodes for that file. This is needed for advanced imports like Tailwind. | ||
viteServer.moduleGraph.getModulesByFile(id) ?? new Set() | ||
: // Otherwise, you are following an import in the module import tree. | ||
// You are safe to use getModuleById() here because Vite has already | ||
// resolved the correct `id` for you, by creating the import you followed here. | ||
new Set([viteServer.moduleGraph.getModuleById(id)]); | ||
|
||
// Collect all imported modules for the module(s). | ||
for (const entry of moduleEntriesForId) { | ||
// Handle this in case an module entries weren't found for ID | ||
// This seems possible with some virtual IDs (ex: `astro:markdown/*.md`) | ||
if (!entry) { | ||
continue; | ||
} | ||
if (id === entry.id) { | ||
scanned.add(id); | ||
for (const importedModule of entry.importedModules) { | ||
// some dynamically imported modules are *not* server rendered in time | ||
// to only SSR modules that we can safely transform, we check against | ||
// a list of file extensions based on our built-in vite plugins | ||
if (importedModule.id) { | ||
// use URL to strip special query params like "?content" | ||
const { pathname } = new URL(`file://${importedModule.id}`); | ||
if (fileExtensionsToSSR.has(npath.extname(pathname))) { | ||
const mod = viteServer.moduleGraph.getModuleById(importedModule.id); | ||
if(!mod?.ssrModule) { | ||
await viteServer.ssrLoadModule(importedModule.id); | ||
} | ||
} | ||
} | ||
importedModules.add(importedModule); | ||
} | ||
} | ||
} | ||
|
||
// scan imported modules for CSS imports & add them to our collection. | ||
// Then, crawl that file to follow and scan all deep imports as well. | ||
for (const importedModule of importedModules) { | ||
if (!importedModule.id || scanned.has(importedModule.id)) { | ||
continue; | ||
} | ||
|
||
yield importedModule; | ||
yield * crawlGraph(viteServer, importedModule.id, false, scanned); | ||
} | ||
} |
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
Oops, something went wrong.