Fix markdoc test fail in main (#7459)

* Early out if `importedModule` has no `id`

We can do this as line 97 also has this guard for each `importedModules`

* Simplify `isPropagationStoppingPoint`

* Fix actual bug

* chore: changeset

---------

Co-authored-by: bholmesdev <hey@bholmes.dev>
This commit is contained in:
Bjorn Lu 2023-06-24 05:00:19 +08:00 committed by GitHub
parent dfa70357d3
commit 869197aafd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 45 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix missing styles for Markdoc files in development

View file

@ -46,44 +46,45 @@ export async function* crawlGraph(
const entryIsStyle = isCSSRequest(id); const entryIsStyle = isCSSRequest(id);
for (const importedModule of entry.importedModules) { for (const importedModule of entry.importedModules) {
// A propagation stopping point is a module with the ?astroPropagatedAssets flag. if (!importedModule.id) continue;
// When we encounter one of these modules we don't want to continue traversing.
let isPropagationStoppingPoint = false;
// some dynamically imported modules are *not* server rendered in time // some dynamically imported modules are *not* server rendered in time
// to only SSR modules that we can safely transform, we check against // to only SSR modules that we can safely transform, we check against
// a list of file extensions based on our built-in vite plugins // a list of file extensions based on our built-in vite plugins
if (importedModule.id) {
// Strip special query params like "?content". // Strip special query params like "?content".
// NOTE: Cannot use `new URL()` here because not all IDs will be valid paths. // NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
// For example, `virtual:image-loader` if you don't have the plugin installed. // For example, `virtual:image-loader` if you don't have the plugin installed.
const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, ''); const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, '');
// If the entry is a style, skip any modules that are not also styles. // If the entry is a style, skip any modules that are not also styles.
// Tools like Tailwind might add HMR dependencies as `importedModules` // Tools like Tailwind might add HMR dependencies as `importedModules`
// but we should skip them--they aren't really imported. Without this, // but we should skip them--they aren't really imported. Without this,
// every hoisted script in the project is added to every page! // every hoisted script in the project is added to every page!
if (entryIsStyle && !isCSSRequest(importedModulePathname)) { if (entryIsStyle && !isCSSRequest(importedModulePathname)) {
continue; continue;
} }
const isFileTypeNeedingSSR = fileExtensionsToSSR.has(
npath.extname(importedModulePathname) const isFileTypeNeedingSSR = fileExtensionsToSSR.has(npath.extname(importedModulePathname));
); // A propagation stopping point is a module with the ?astroPropagatedAssets flag.
isPropagationStoppingPoint = ASTRO_PROPAGATED_ASSET_REGEX.test(importedModule.id); // When we encounter one of these modules we don't want to continue traversing.
if ( const isPropagationStoppingPoint = ASTRO_PROPAGATED_ASSET_REGEX.test(importedModule.id);
isFileTypeNeedingSSR && if (
// Should not SSR a module with ?astroPropagatedAssets isFileTypeNeedingSSR &&
!isPropagationStoppingPoint // Should not SSR a module with ?astroPropagatedAssets
) { !isPropagationStoppingPoint
const mod = loader.getModuleById(importedModule.id); ) {
if (!mod?.ssrModule) { const mod = loader.getModuleById(importedModule.id);
try { if (!mod?.ssrModule) {
await loader.import(importedModule.id); try {
} catch { await loader.import(importedModule.id);
/** Likely an out-of-date module entry! Silently continue. */ } catch {
} /** Likely an out-of-date module entry! Silently continue. */
} }
} }
} }
if (urlDeps.includes(urlId(importedModule.url)) && !isPropagationStoppingPoint) {
// Make sure the `importedModule` traversed is explicitly imported by the user, and not by HMR
if (urlDeps.includes(importedModule.url) && !isPropagationStoppingPoint) {
importedModules.add(importedModule); importedModules.add(importedModule);
} }
} }
@ -102,18 +103,10 @@ export async function* crawlGraph(
} }
} }
// Virtual modules URL should start with /@id/ but do not
function urlId(url: string) {
if (url.startsWith('astro:scripts')) {
return '/@id/' + url;
}
return url;
}
function getDepsFromEntry(entry: ModuleNode) { function getDepsFromEntry(entry: ModuleNode) {
let deps = entry.ssrTransformResult?.deps ?? []; let deps = entry.ssrTransformResult?.deps ?? [];
if (entry.ssrTransformResult?.dynamicDeps) { if (entry.ssrTransformResult?.dynamicDeps) {
return deps.concat(entry.ssrTransformResult.dynamicDeps); deps = deps.concat(entry.ssrTransformResult.dynamicDeps);
} }
return deps; return deps.map((dep) => unwrapId(dep));
} }

View file

@ -93,11 +93,14 @@ export function viteID(filePath: URL): string {
} }
export const VALID_ID_PREFIX = `/@id/`; export const VALID_ID_PREFIX = `/@id/`;
export const NULL_BYTE_PLACEHOLDER = `__x00__`;
// Strip valid id prefix. This is prepended to resolved Ids that are // Strip valid id prefix and replace null byte placeholder. Both are prepended to resolved ids
// not valid browser import specifiers by the importAnalysis plugin. // as they are not valid browser import specifiers (by the Vite's importAnalysis plugin)
export function unwrapId(id: string): string { export function unwrapId(id: string): string {
return id.startsWith(VALID_ID_PREFIX) ? id.slice(VALID_ID_PREFIX.length) : id; return id.startsWith(VALID_ID_PREFIX)
? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0')
: id;
} }
export function resolvePages(config: AstroConfig) { export function resolvePages(config: AstroConfig) {