Fix build missing styles (affecting docs) (#2112)

* Revert "remove odd docs regression caused by Astro.fetchContent()"

This reverts commit df47077978.

* Fix build missing styles
This commit is contained in:
Drew Powers 2021-12-06 16:01:32 -07:00 committed by GitHub
parent cc1dae55c8
commit da7b41f5b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 151 additions and 23 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Bugfix: fix missing styles in build

View file

@ -135,6 +135,7 @@ export const SIDEBAR = {
{ text: '新手上路', link: 'zh-TW/getting-started' },
{ text: '快速開始', link: 'zh-TW/quick-start' },
{ text: '安裝', link: 'zh-TW/installation' },
{ text: '佈景主題', link: 'zh-TW/themes' },
],
bg: [
{ text: 'Главни', header: true },

View file

@ -0,0 +1,13 @@
---
import { Markdown } from 'astro/components';
import MainLayout from '~/layouts/MainLayout.astro';
const [content] = Astro.fetchContent('/src/pages/guides/markdown-content.md');
---
<MainLayout content="{content}">
<Markdown>
> Esta página todavía no está disponible en Español. Se muestra la versión en inglés.
</Markdown>
{content.astro.html}
</MainLayout>

View file

@ -0,0 +1,13 @@
---
import { Markdown } from 'astro/components';
import MainLayout from '~/layouts/MainLayout.astro';
const [content] = Astro.fetchContent('/src/pages/guides/pagination.md');
---
<MainLayout content="{content}">
<Markdown>
> Esta página todavía no está disponible en Español. Se muestra la versión en inglés.
</Markdown>
{content.astro.html}
</MainLayout>

View file

@ -0,0 +1,13 @@
---
import { Markdown } from 'astro/components';
import MainLayout from '~/layouts/MainLayout.astro';
const [content] = Astro.fetchContent('/src/pages/guides/publish-to-npm.md');
---
<MainLayout content="{content}">
<Markdown>
> Esta página todavía no está disponible en Español. Se muestra la versión en inglés.
</Markdown>
{content.astro.html}
</MainLayout>

View file

@ -0,0 +1,13 @@
---
import { Markdown } from 'astro/components';
import MainLayout from '~/layouts/MainLayout.astro';
const [content] = Astro.fetchContent('/src/pages/guides/styling.md');
---
<MainLayout content="{content}">
<Markdown>
> Esta página todavía no está disponible en Español. Se muestra la versión en inglés.
</Markdown>
{content.astro.html}
</MainLayout>

View file

@ -0,0 +1,13 @@
---
import { Markdown } from 'astro/components';
import MainLayout from '~/layouts/MainLayout.astro';
const [content] = Astro.fetchContent('/src/pages/reference/renderer-reference.md');
---
<MainLayout content="{content}">
<Markdown>
> Esta página todavía no está disponible en Español. Se muestra la versión en inglés.
</Markdown>
{content.astro.html}
</MainLayout>

View file

@ -0,0 +1,52 @@
---
import Layout from '../../layouts/MainLayout.astro';
import Card from '../../components/Card.astro';
import {Markdown} from 'astro/components';
import themes from '../../data/themes.json';
import components from '../../data/components.json';
---
<style>
.card-grid {
display: grid;
grid-column-gap: 15px;
grid-row-gap: 15px;
grid-auto-flow: dense;
grid-template-columns: repeat(auto-fit,minmax(300px,1fr))
}
</style>
<Layout content={{title: 'Themes'}} hideRightSidebar>
<Markdown>
## 精選佈景主題
</Markdown>
<div class="card-grid">
{themes.featured.map((item)=>(<Card data={item} />))}
</div>
<Markdown>
## 官方佈景主題
Astro 維護的文件網站、作品集⋯等官方佈景主題。
</Markdown>
<div class="card-grid">
{themes.official.map((item)=>(<Card data={item} />))}
</div>
<Markdown>
## 社群佈景主題
趕緊來看看社群開發的佈景主題!
</Markdown>
<div class="card-grid">
{themes.community.map((item)=>(<Card data={item} />))}
</div>
<Markdown>
## 精選套件
我們的套件生態持續成長!所有精選社群套件都可以在 [npm](https://www.npmjs.com/search?q=keywords%3Aastro-component) 發掘。
</Markdown>
<div class="card-grid">
{components.community.map((item)=>(<Card data={item} />))}
</div>
<Markdown>
> 想要讓自己的作品成為精選嗎?[在 Discord 分享!](https://astro.build/chat)
我們常在 `#showcase` 頻道取材,把深受喜愛的在這裡發布。
</Markdown>
</Layout>

View file

@ -1,2 +1,2 @@
[build]
ignore = "git diff --quiet $COMMIT_REF $CACHED_COMMIT_REF -- docs/ www/"
ignore = "git diff --quiet $COMMIT_REF $CACHED_COMMIT_REF -- packages/astro/src docs/ www/"

View file

@ -1,34 +1,40 @@
import type vite from '../vite';
import path from 'path';
import { viteifyURL } from '../util.js';
import { viteID } from '../util.js';
// https://vitejs.dev/guide/features.html#css-pre-processors
export const STYLE_EXTENSIONS = new Set(['.css', '.pcss', '.postcss', '.scss', '.sass', '.styl', '.stylus', '.less']);
/** find unloaded styles */
/**
* getStylesForURL
* Given a filePath URL, crawl Vites module graph to find style files
*/
export function getStylesForURL(filePath: URL, viteServer: vite.ViteDevServer): Set<string> {
const css = new Set<string>();
const rootID = viteifyURL(filePath);
// recursively crawl module graph to get all style files imported by parent id
function crawlCSS(entryModule: string, scanned = new Set<string>()) {
const moduleName = viteServer.moduleGraph.urlToModuleMap.get(entryModule);
function crawlCSS(id: string, scanned = new Set<string>()) {
// note: use .idToModuleMap() for lookups (.urlToModuleMap() may produce different
// URLs for modules depending on conditions, making resolution difficult)
const moduleName = viteServer.moduleGraph.idToModuleMap.get(id);
if (!moduleName || !moduleName.id) return;
// mark the entrypoint as scanned to avoid an infinite loop
scanned.add(moduleName.url);
scanned.add(moduleName.id);
// scan importedModules
for (const importedModule of moduleName.importedModules) {
if (!importedModule.url || scanned.has(importedModule.url)) continue;
if (!importedModule.id || scanned.has(importedModule.id)) continue;
const ext = path.extname(importedModule.url.toLowerCase());
if (STYLE_EXTENSIONS.has(ext)) {
css.add(importedModule.url); // if style file, add to list
} else {
crawlCSS(importedModule.url, scanned); // otherwise, crawl file to see if it imports any CSS
css.add(importedModule.url); // note: return `url`s for HTML (not .id, which will break Windows)
}
scanned.add(importedModule.url);
crawlCSS(importedModule.id, scanned);
scanned.add(importedModule.id);
}
}
crawlCSS(rootID);
crawlCSS(viteID(filePath));
return css;
}

View file

@ -23,7 +23,7 @@ import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { renderPage, renderSlot } from '../../runtime/server/index.js';
import { canonicalURL as getCanonicalURL, codeFrame, resolveDependency, viteifyURL } from '../util.js';
import { canonicalURL as getCanonicalURL, codeFrame, resolveDependency } from '../util.js';
import { getStylesForURL } from './css.js';
import { injectTags } from './html.js';
import { generatePaginateFunction } from './paginate.js';
@ -133,7 +133,7 @@ export async function preload({ astroConfig, filePath, viteServer }: SSROptions)
// Important: This needs to happen first, in case a renderer provides polyfills.
const renderers = await resolveRenderers(viteServer, astroConfig);
// Load the module from the Vite SSR Runtime.
const mod = (await viteServer.ssrLoadModule(viteifyURL(filePath))) as ComponentInstance;
const mod = (await viteServer.ssrLoadModule(fileURLToPath(filePath))) as ComponentInstance;
return [renderers, mod];
}

View file

@ -74,12 +74,11 @@ export function resolveDependency(dep: string, astroConfig: AstroConfig) {
}
/**
* Vite-ify URL
* Given a file URL, return an ID that matches Vites module graph. Needed for resolution and stack trace fixing.
* Must match the following format:
* Linux/Mac: /@fs/Users/astro/code/my-project/src/pages/index.astro
* Windows: /@fs/C:/Users/astro/code/my-project/src/pages/index.astro
* Convert file URL to ID for viteServer.moduleGraph.idToModuleMap.get(:viteID)
* Format:
* Linux/Mac: /Users/astro/code/my-project/src/pages/index.astro
* Windows: C:/Users/astro/code/my-project/src/pages/index.astro
*/
export function viteifyURL(filePath: URL): string {
return `/@fs${slash(fileURLToPath(filePath)).replace(/^\/?/, '/')}`;
export function viteID(filePath: URL): string {
return slash(fileURLToPath(filePath));
}