mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Fix for shared CSS when using the static build (#2365)
* Fix for shared CSS when using the static build * Adds a changeset
This commit is contained in:
parent
c388e6800f
commit
20d0cce681
12 changed files with 260 additions and 20 deletions
5
.changeset/moody-boats-pull.md
Normal file
5
.changeset/moody-boats-pull.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes shared CSS within the static build
|
|
@ -1,4 +1,5 @@
|
|||
---
|
||||
import '../styles/global.scss';
|
||||
const { title = 'Jeanine White: Personal Site', description = 'The personal site of Jeanine White' } = Astro.props;
|
||||
---
|
||||
|
||||
|
@ -8,6 +9,5 @@ const { title = 'Jeanine White: Personal Site', description = 'The personal site
|
|||
<title>{title}</title>
|
||||
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||
<link rel="stylesheet" type="text/css" href={Astro.resolve('../styles/global.scss')} />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" />
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@200;400;700;900&display=swap" rel="stylesheet" />
|
||||
|
|
|
@ -189,6 +189,7 @@ async function generatePage(output: OutputChunk, opts: StaticBuildOptions, inter
|
|||
}
|
||||
|
||||
let linkIds = internals.facadeIdToAssetsMap.get(facadeId) || [];
|
||||
|
||||
let compiledModule = await import(url.toString());
|
||||
let Component = compiledModule.default;
|
||||
|
||||
|
@ -242,6 +243,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
|
|||
children: '',
|
||||
}))
|
||||
);
|
||||
|
||||
// Override the `resolve` method so that hydrated components are given the
|
||||
// hashed filepath to the component.
|
||||
result.resolve = async (specifier: string) => {
|
||||
|
|
|
@ -13,7 +13,8 @@ const ASTRO_STYLE_PREFIX = '@astro-inline-style';
|
|||
|
||||
const ASTRO_PAGE_STYLE_PREFIX = '@astro-page-all-styles';
|
||||
|
||||
const isCSSRequest = (request: string) => STYLE_EXTENSIONS.has(path.extname(request));
|
||||
const cssRe = new RegExp(`\\.(${Array.from(STYLE_EXTENSIONS).map(s => s.slice(1)).join('|')})($|\\?)`);
|
||||
const isCSSRequest = (request: string): boolean => cssRe.test(request);
|
||||
|
||||
export function getAstroPageStyleId(pathname: string) {
|
||||
let styleId = ASTRO_PAGE_STYLE_PREFIX + pathname;
|
||||
|
@ -109,6 +110,8 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
|||
return null;
|
||||
},
|
||||
|
||||
|
||||
|
||||
async renderChunk(_code, chunk) {
|
||||
let chunkCSS = '';
|
||||
let isPureCSS = true;
|
||||
|
@ -121,7 +124,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
|||
}
|
||||
}
|
||||
|
||||
// if (!chunkCSS) return null; // don’t output empty .css files
|
||||
if (!chunkCSS) return null; // don’t output empty .css files
|
||||
|
||||
if (isPureCSS) {
|
||||
internals.pureCSSChunks.add(chunk);
|
||||
|
@ -139,11 +142,14 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
|||
|
||||
internals.chunkToReferenceIdMap.set(chunk.fileName, referenceId);
|
||||
if (chunk.type === 'chunk') {
|
||||
const facadeId = chunk.facadeModuleId!;
|
||||
if (!internals.facadeIdToAssetsMap.has(facadeId)) {
|
||||
internals.facadeIdToAssetsMap.set(facadeId, []);
|
||||
const fileName = this.getFileName(referenceId);
|
||||
if(chunk.facadeModuleId) {
|
||||
const facadeId = chunk.facadeModuleId!;
|
||||
if (!internals.facadeIdToAssetsMap.has(facadeId)) {
|
||||
internals.facadeIdToAssetsMap.set(facadeId, []);
|
||||
}
|
||||
internals.facadeIdToAssetsMap.get(facadeId)!.push(fileName);
|
||||
}
|
||||
internals.facadeIdToAssetsMap.get(facadeId)!.push(this.getFileName(referenceId));
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -151,16 +157,39 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
|||
|
||||
// Delete CSS chunks so JS is not produced for them.
|
||||
generateBundle(opts, bundle) {
|
||||
if (internals.pureCSSChunks.size) {
|
||||
const pureChunkFilenames = new Set([...internals.pureCSSChunks].map((chunk) => chunk.fileName));
|
||||
const emptyChunkFiles = [...pureChunkFilenames]
|
||||
.map((file) => path.basename(file))
|
||||
.join('|')
|
||||
.replace(/\./g, '\\.');
|
||||
const emptyChunkRE = new RegExp(opts.format === 'es' ? `\\bimport\\s*"[^"]*(?:${emptyChunkFiles})";\n?` : `\\brequire\\(\\s*"[^"]*(?:${emptyChunkFiles})"\\);\n?`, 'g');
|
||||
const hasPureCSSChunks = internals.pureCSSChunks.size;
|
||||
const pureChunkFilenames = new Set([...internals.pureCSSChunks].map((chunk) => chunk.fileName));
|
||||
const emptyChunkFiles = [...pureChunkFilenames]
|
||||
.map((file) => path.basename(file))
|
||||
.join('|')
|
||||
.replace(/\./g, '\\.');
|
||||
const emptyChunkRE = new RegExp(opts.format === 'es' ? `\\bimport\\s*"[^"]*(?:${emptyChunkFiles})";\n?` : `\\brequire\\(\\s*"[^"]*(?:${emptyChunkFiles})"\\);\n?`, 'g');
|
||||
|
||||
for (const [chunkId, chunk] of Object.entries(bundle)) {
|
||||
if (chunk.type === 'chunk') {
|
||||
for (const [chunkId, chunk] of Object.entries(bundle)) {
|
||||
if (chunk.type === 'chunk') {
|
||||
// If the chunk has a facadeModuleId it is an entrypoint chunk.
|
||||
// This find shared chunks of CSS and adds them to the main CSS chunks,
|
||||
// so that shared CSS is added to the page.
|
||||
if(chunk.facadeModuleId) {
|
||||
if(!internals.facadeIdToAssetsMap.has(chunk.facadeModuleId)) {
|
||||
internals.facadeIdToAssetsMap.set(chunk.facadeModuleId, []);
|
||||
}
|
||||
const assets = internals.facadeIdToAssetsMap.get(chunk.facadeModuleId)!;
|
||||
const assetSet = new Set(assets);
|
||||
for(const imp of chunk.imports) {
|
||||
if(internals.chunkToReferenceIdMap.has(imp) && !pureChunkFilenames.has(imp)) {
|
||||
const referenceId = internals.chunkToReferenceIdMap.get(imp)!;
|
||||
const fileName = this.getFileName(referenceId);
|
||||
if(!assetSet.has(fileName)) {
|
||||
assetSet.add(fileName);
|
||||
assets.push(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Removes imports for pure CSS chunks.
|
||||
if(hasPureCSSChunks) {
|
||||
if (internals.pureCSSChunks.has(chunk)) {
|
||||
// Delete pure CSS chunks, these are JavaScript chunks that only import
|
||||
// other CSS files, so are empty at the end of bundling.
|
||||
|
|
11
packages/astro/test/fixtures/static-build/src/components/MainHead.astro
vendored
Normal file
11
packages/astro/test/fixtures/static-build/src/components/MainHead.astro
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
import '../styles/main.scss';
|
||||
const { title = 'Static Build', description = 'This is a demo of the static build' } = Astro.props;
|
||||
---
|
||||
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="description" property="og:description" content={description} />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>{title}</title>
|
||||
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
35
packages/astro/test/fixtures/static-build/src/components/Nav/index.jsx
vendored
Normal file
35
packages/astro/test/fixtures/static-build/src/components/Nav/index.jsx
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { h } from 'preact';
|
||||
import Styles from './styles.module.scss';
|
||||
|
||||
function Nav() {
|
||||
return (
|
||||
<nav className={Styles.nav}>
|
||||
<a className={Styles.logolink} href="/">
|
||||
<div className={Styles.monogram}>JW</div>
|
||||
</a>
|
||||
<a className={Styles.link} href="/projects">
|
||||
Portfolio
|
||||
</a>
|
||||
<a className={Styles.link} href="/about">
|
||||
About
|
||||
</a>
|
||||
<a className={Styles.social} href="https://twitter.com/me">
|
||||
<svg className={Styles.socialicon} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
|
||||
<path d="M5.026 15c6.038 0 9.341-5.003 9.341-9.334 0-.14 0-.282-.006-.422A6.685 6.685 0 0 0 16 3.542a6.658 6.658 0 0 1-1.889.518 3.301 3.301 0 0 0 1.447-1.817 6.533 6.533 0 0 1-2.087.793A3.286 3.286 0 0 0 7.875 6.03a9.325 9.325 0 0 1-6.767-3.429 3.289 3.289 0 0 0 1.018 4.382A3.323 3.323 0 0 1 .64 6.575v.045a3.288 3.288 0 0 0 2.632 3.218 3.203 3.203 0 0 1-.865.115 3.23 3.23 0 0 1-.614-.057 3.283 3.283 0 0 0 3.067 2.277A6.588 6.588 0 0 1 .78 13.58a6.32 6.32 0 0 1-.78-.045A9.344 9.344 0 0 0 5.026 15z" />
|
||||
</svg>
|
||||
</a>
|
||||
<a className={Styles.social} href="https://github.com/me">
|
||||
<svg className={Styles.socialicon} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
|
||||
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
|
||||
</svg>
|
||||
</a>
|
||||
<a className={Styles.social} href="https://dev.to/me">
|
||||
<svg className={Styles.socialicon} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 40" style="enable-background:new 0 0 50 40" xmlSpace="preserve">
|
||||
<path d="M15.7 15.5c-.4-.3-.7-.4-1.1-.4h-1.7v10.1h1.7c.4 0 .8-.1 1.1-.4.4-.3.6-.7.6-1.3v-6.7c0-.6-.2-1-.6-1.3z" />
|
||||
<path d="M47 0H3C1.3 0 0 1.3 0 3v34c0 1.7 1.3 3 3 3h44c1.7 0 3-1.3 3-3V3c0-1.7-1.3-3-3-3zM19.1 23.5c0 1.3-.4 2.4-1.3 3.2-.8.9-1.9 1.3-3.3 1.3h-4.4V12.3h4.5c1.3 0 2.4.4 3.2 1.3.8.8 1.3 1.9 1.3 3.2v6.7zm9.1-8.4h-5.1v3.6h3.1v2.8h-3.1v3.7h5.1V28h-5.9c-.6 0-1-.2-1.4-.6-.4-.4-.6-.8-.6-1.4V14.2c0-.6.2-1 .6-1.4.4-.4.8-.6 1.4-.6h5.9v2.9zM37.5 26c-.6 1.3-1.3 2-2.2 2-.9 0-1.7-.7-2.2-2l-3.7-13.8h3.1L35.3 23l2.8-10.8h3.1L37.5 26z" />
|
||||
</svg>
|
||||
</a>
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
export default Nav;
|
65
packages/astro/test/fixtures/static-build/src/components/Nav/styles.module.scss
vendored
Normal file
65
packages/astro/test/fixtures/static-build/src/components/Nav/styles.module.scss
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
.nav {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding-top: 1rem;
|
||||
padding-right: 2rem;
|
||||
padding-bottom: 1rem;
|
||||
padding-left: 2rem;
|
||||
}
|
||||
|
||||
.logolink {
|
||||
display: block;
|
||||
color: var(--t-fg);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.link {
|
||||
color: var(--t-subdue);
|
||||
display: block;
|
||||
margin-left: 1rem;
|
||||
text-decoration: none;
|
||||
font-size: var(--f-d1);
|
||||
text-transform: uppercase;
|
||||
padding-top: 0.75em;
|
||||
padding-bottom: 0.75em;
|
||||
|
||||
&:focus,
|
||||
&:hover {
|
||||
color: var(--t-active);
|
||||
}
|
||||
}
|
||||
|
||||
.monogram {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 2em;
|
||||
height: 2em;
|
||||
margin-right: 0.5rem;
|
||||
color: var(--c-black);
|
||||
font-weight: 900;
|
||||
letter-spacing: -0.125rem;
|
||||
border: 3px solid currentColor;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.social {
|
||||
display: block;
|
||||
margin-left: auto;
|
||||
|
||||
+ .social {
|
||||
margin-left: 0.75rem;
|
||||
}
|
||||
}
|
||||
|
||||
.socialicon {
|
||||
display: block;
|
||||
width: 1.25rem;
|
||||
height: 1.25rem;
|
||||
fill: var(--t-subdue);
|
||||
transition: fill linear 150ms;
|
||||
|
||||
&:hover {
|
||||
fill: var(--t-active);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,21 @@
|
|||
---
|
||||
import MainHead from '../components/MainHead.astro';
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
<MainHead title="Blog Post" />
|
||||
<style lang="scss">
|
||||
$w-s: 350px;
|
||||
|
||||
.hero {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
|
||||
@media (min-width: $w-s) {
|
||||
height: 45vw;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body><slot></slot></body>
|
||||
</html>
|
||||
|
|
|
@ -1,6 +1,25 @@
|
|||
---
|
||||
import MainHead from '../components/MainHead.astro';
|
||||
import Nav from '../components/Nav/index.jsx';
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
<MainHead title="Testing" />
|
||||
<style lang="scss">
|
||||
$w-s: 750px;
|
||||
|
||||
.hero {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
|
||||
@media (min-width: $w-s) {
|
||||
height: 45vw;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body><h1>Testing</h1></body>
|
||||
<body>
|
||||
<Nav />
|
||||
<h1>Testing</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
|
7
packages/astro/test/fixtures/static-build/src/pages/posts/nested/more.md
vendored
Normal file
7
packages/astro/test/fixtures/static-build/src/pages/posts/nested/more.md
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
layout: ../../../layouts/Main.astro
|
||||
---
|
||||
|
||||
# Post
|
||||
|
||||
Testing here
|
6
packages/astro/test/fixtures/static-build/src/styles/main.scss
vendored
Normal file
6
packages/astro/test/fixtures/static-build/src/styles/main.scss
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
$base-color: blue;
|
||||
|
||||
body {
|
||||
--c: #{$base-color};
|
||||
color: var(--c);
|
||||
}
|
|
@ -2,13 +2,19 @@ import { expect } from 'chai';
|
|||
import cheerio from 'cheerio';
|
||||
import { loadFixture } from './test-utils.js';
|
||||
|
||||
function addLeadingSlash(path) {
|
||||
return path.startsWith('/') ? path : '/' + path;
|
||||
}
|
||||
|
||||
describe('Static build', () => {
|
||||
let fixture;
|
||||
|
||||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
projectRoot: './fixtures/static-build/',
|
||||
renderers: [],
|
||||
renderers: [
|
||||
'@astrojs/renderer-preact'
|
||||
],
|
||||
buildOptions: {
|
||||
experimentalStaticBuild: true,
|
||||
},
|
||||
|
@ -25,4 +31,44 @@ describe('Static build', () => {
|
|||
const html = await fixture.readFile('/posts/thoughts/index.html');
|
||||
expect(html).to.be.a('string');
|
||||
});
|
||||
|
||||
function createFindEvidence(expected) {
|
||||
return async function findEvidence(pathname) {
|
||||
const html = await fixture.readFile(pathname);
|
||||
const $ = cheerio.load(html);
|
||||
const links = $('link[rel=stylesheet]');
|
||||
for(const link of links) {
|
||||
const href = $(link).attr('href');
|
||||
const data = await fixture.readFile(addLeadingSlash(href));
|
||||
if(expected.test(data)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
describe('Shared CSS', () => {
|
||||
const findEvidence = createFindEvidence(/var\(--c\)/);
|
||||
|
||||
it('Included on the index page', async () => {
|
||||
const found = await findEvidence('/index.html');
|
||||
expect(found).to.equal(true, 'Did not find shared CSS on this page');
|
||||
});
|
||||
|
||||
it('Included on a md page', async () => {
|
||||
const found = await findEvidence('/posts/thoughts/index.html');
|
||||
expect(found).to.equal(true, 'Did not find shared CSS on this page');
|
||||
});
|
||||
});
|
||||
|
||||
describe('CSS modules', () => {
|
||||
const findEvidence = createFindEvidence(/var\(--c-black\)/);
|
||||
|
||||
it('Is included in the index CSS', async () => {
|
||||
const found = await findEvidence('/index.html');
|
||||
expect(found).to.equal(true, 'Did not find shared CSS module code');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue