[ci] format

This commit is contained in:
matthewp 2022-05-26 18:01:35 +00:00 committed by github-actions[bot]
parent ac3c60d48d
commit edf2838a66
8 changed files with 68 additions and 52 deletions

View file

@ -119,9 +119,9 @@ export function* getPageDatasByClientOnlyID(
const pagesByClientOnly = internals.pagesByClientOnly; const pagesByClientOnly = internals.pagesByClientOnly;
if (pagesByClientOnly.size) { if (pagesByClientOnly.size) {
const pathname = `/@fs${prependForwardSlash(viteid)}`; const pathname = `/@fs${prependForwardSlash(viteid)}`;
const pageBuildDatas = pagesByClientOnly.get(pathname) const pageBuildDatas = pagesByClientOnly.get(pathname);
if(pageBuildDatas) { if (pageBuildDatas) {
for(const pageData of pageBuildDatas) { for (const pageData of pageBuildDatas) {
yield pageData; yield pageData;
} }
} }

View file

@ -17,44 +17,54 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
const { internals } = options; const { internals } = options;
// This walks up the dependency graph and yields out each ModuleInfo object. // This walks up the dependency graph and yields out each ModuleInfo object.
function* walkParentInfos(id: string, ctx: {getModuleInfo: GetModuleInfo}, seen = new Set<string>()): Generator<ModuleInfo, void, unknown> { function* walkParentInfos(
id: string,
ctx: { getModuleInfo: GetModuleInfo },
seen = new Set<string>()
): Generator<ModuleInfo, void, unknown> {
seen.add(id); seen.add(id);
const info = ctx.getModuleInfo(id); const info = ctx.getModuleInfo(id);
if(info) { if (info) {
yield info; yield info;
} }
const importers = (info?.importers || []).concat(info?.dynamicImporters || []); const importers = (info?.importers || []).concat(info?.dynamicImporters || []);
for(const imp of importers) { for (const imp of importers) {
if(seen.has(imp)) { if (seen.has(imp)) {
continue; continue;
} }
yield * walkParentInfos(imp, ctx, seen); yield* walkParentInfos(imp, ctx, seen);
} }
} }
// This function walks the dependency graph, going up until it finds a page component. // This function walks the dependency graph, going up until it finds a page component.
// This could be a .astro page or a .md page. // This could be a .astro page or a .md page.
function* getTopLevelPages(id: string, ctx: {getModuleInfo: GetModuleInfo}): Generator<string, void, unknown> { function* getTopLevelPages(
for(const info of walkParentInfos(id, ctx)) { id: string,
ctx: { getModuleInfo: GetModuleInfo }
): Generator<string, void, unknown> {
for (const info of walkParentInfos(id, ctx)) {
const importers = (info?.importers || []).concat(info?.dynamicImporters || []); const importers = (info?.importers || []).concat(info?.dynamicImporters || []);
if(importers.length <= 2 && importers[0] === resolvedPagesVirtualModuleId) { if (importers.length <= 2 && importers[0] === resolvedPagesVirtualModuleId) {
yield info.id; yield info.id;
} }
} }
} }
function createHashOfPageParents(id: string, ctx: {getModuleInfo: GetModuleInfo}): string { function createHashOfPageParents(id: string, ctx: { getModuleInfo: GetModuleInfo }): string {
const parents = Array.from(getTopLevelPages(id, ctx)).sort(); const parents = Array.from(getTopLevelPages(id, ctx)).sort();
const hash = crypto.createHash('sha256'); const hash = crypto.createHash('sha256');
for(const page of parents) { for (const page of parents) {
hash.update(page, 'utf-8'); hash.update(page, 'utf-8');
} }
return hash.digest('hex').slice(0, 8); return hash.digest('hex').slice(0, 8);
} }
function* getParentClientOnlys(id: string, ctx: {getModuleInfo: GetModuleInfo}): Generator<PageBuildData, void, unknown> { function* getParentClientOnlys(
for(const info of walkParentInfos(id, ctx)) { id: string,
yield * getPageDatasByClientOnlyID(internals, info.id); ctx: { getModuleInfo: GetModuleInfo }
): Generator<PageBuildData, void, unknown> {
for (const info of walkParentInfos(id, ctx)) {
yield* getPageDatasByClientOnlyID(internals, info.id);
} }
} }
@ -74,16 +84,16 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
// We do this instead of setting minification globally to avoid minifying // We do this instead of setting minification globally to avoid minifying
// server JS. // server JS.
const renderChunk = viteCSSPost.renderChunk; const renderChunk = viteCSSPost.renderChunk;
if(renderChunk) { if (renderChunk) {
viteCSSPost.renderChunk = async function(...args) { viteCSSPost.renderChunk = async function (...args) {
const minifyOption = resolvedConfig.build.minify; const minifyOption = resolvedConfig.build.minify;
if(minifyOption === false) { if (minifyOption === false) {
resolvedConfig.build.minify = 'esbuild'; resolvedConfig.build.minify = 'esbuild';
} }
const result = await renderChunk.apply(this, args); const result = await renderChunk.apply(this, args);
if(typeof result === 'string') { if (typeof result === 'string') {
return { return {
code: result code: result,
}; };
} }
resolvedConfig.build.minify = minifyOption; resolvedConfig.build.minify = minifyOption;
@ -103,15 +113,15 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
outputOptions(outputOptions) { outputOptions(outputOptions) {
const manualChunks = outputOptions.manualChunks || Function.prototype; const manualChunks = outputOptions.manualChunks || Function.prototype;
outputOptions.manualChunks = function(id, ...args) { outputOptions.manualChunks = function (id, ...args) {
// Defer to user-provided `manualChunks`, if it was provided. // Defer to user-provided `manualChunks`, if it was provided.
if(typeof manualChunks == 'object') { if (typeof manualChunks == 'object') {
if(id in manualChunks) { if (id in manualChunks) {
return manualChunks[id]; return manualChunks[id];
} }
} else if(typeof manualChunks === 'function') { } else if (typeof manualChunks === 'function') {
const outid = manualChunks.call(this, id, ...args); const outid = manualChunks.call(this, id, ...args);
if(outid) { if (outid) {
return outid; return outid;
} }
} }
@ -128,23 +138,23 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
type ViteMetadata = { type ViteMetadata = {
importedAssets: Set<string>; importedAssets: Set<string>;
importedCss: Set<string>; importedCss: Set<string>;
} };
for (const [_, chunk] of Object.entries(bundle)) { for (const [_, chunk] of Object.entries(bundle)) {
if(chunk.type === 'chunk') { if (chunk.type === 'chunk') {
const c = chunk; const c = chunk;
if('viteMetadata' in chunk) { if ('viteMetadata' in chunk) {
const meta = chunk['viteMetadata'] as ViteMetadata; const meta = chunk['viteMetadata'] as ViteMetadata;
// Chunks that have the viteMetadata.importedCss are CSS chunks // Chunks that have the viteMetadata.importedCss are CSS chunks
if(meta.importedCss.size) { if (meta.importedCss.size) {
// For the client build, client:only styles need to be mapped // For the client build, client:only styles need to be mapped
// over to their page. For this chunk, determine if it's a child of a // over to their page. For this chunk, determine if it's a child of a
// client:only component and if so, add its CSS to the page it belongs to. // client:only component and if so, add its CSS to the page it belongs to.
if(options.target === 'client') { if (options.target === 'client') {
for(const [id] of Object.entries(c.modules)) { for (const [id] of Object.entries(c.modules)) {
for(const pageData of getParentClientOnlys(id, this)) { for (const pageData of getParentClientOnlys(id, this)) {
for(const importedCssImport of meta.importedCss) { for (const importedCssImport of meta.importedCss) {
pageData.css.add(importedCssImport); pageData.css.add(importedCssImport);
} }
} }
@ -152,10 +162,10 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
} }
// For this CSS chunk, walk parents until you find a page. Add the CSS to that page. // For this CSS chunk, walk parents until you find a page. Add the CSS to that page.
for(const [id] of Object.entries(c.modules)) { for (const [id] of Object.entries(c.modules)) {
for(const pageViteID of getTopLevelPages(id, this)) { for (const pageViteID of getTopLevelPages(id, this)) {
const pageData = getPageDataByViteID(internals, pageViteID); const pageData = getPageDataByViteID(internals, pageViteID);
for(const importedCssImport of meta.importedCss) { for (const importedCssImport of meta.importedCss) {
pageData?.css.add(importedCssImport); pageData?.css.add(importedCssImport);
} }
} }
@ -165,15 +175,18 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
} }
if (chunk.type === 'chunk') { if (chunk.type === 'chunk') {
// This simply replaces single quotes with double quotes because the vite:css-post // This simply replaces single quotes with double quotes because the vite:css-post
// plugin only works with single for some reason. This code can be removed // plugin only works with single for some reason. This code can be removed
// When the Vite bug is fixed: https://github.com/vitejs/vite/issues/8330 // When the Vite bug is fixed: https://github.com/vitejs/vite/issues/8330
const exp = new RegExp(`(\\bimport\\s*)[']([^']*(?:[a-z]+\.[0-9a-z]+\.m?js))['](;\n?)`, 'g'); const exp = new RegExp(
`(\\bimport\\s*)[']([^']*(?:[a-z]+\.[0-9a-z]+\.m?js))['](;\n?)`,
'g'
);
chunk.code = chunk.code.replace(exp, (_match, begin, chunkPath, end) => { chunk.code = chunk.code.replace(exp, (_match, begin, chunkPath, end) => {
return begin + '"' + chunkPath + '"' + end; return begin + '"' + chunkPath + '"' + end;
}); });
} }
} }
} },
}; };
} }

View file

@ -29,7 +29,8 @@ describe('CSS', function () {
$ = cheerio.load(html); $ = cheerio.load(html);
const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href'); const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href');
bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/'))) bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/')))
.replace(/\s/g, '').replace('/n', ''); .replace(/\s/g, '')
.replace('/n', '');
}); });
describe('Astro Styles', () => { describe('Astro Styles', () => {

View file

@ -65,7 +65,7 @@ describe('Client only components subpath', () => {
it('Adds the CSS to the page', async () => { it('Adds the CSS to the page', async () => {
const html = await fixture.readFile('/index.html'); const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html); const $ = cheerioLoad(html);
const href = $('link[rel=stylesheet]').attr('href'); const href = $('link[rel=stylesheet]').attr('href');
const css = await fixture.readFile(href.replace(/\/blog/, '')); const css = await fixture.readFile(href.replace(/\/blog/, ''));

View file

@ -13,6 +13,6 @@ describe('Vite Config', async () => {
it('Allows overriding bundle naming options in the build', async () => { it('Allows overriding bundle naming options in the build', async () => {
const html = await fixture.readFile('/index.html'); const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html); const $ = cheerio.load(html);
expect($('link').attr('href')).to.match(/\/assets\/testing-[a-z0-9]+\.css/) expect($('link').attr('href')).to.match(/\/assets\/testing-[a-z0-9]+\.css/);
}); });
}); });

View file

@ -10,9 +10,9 @@ describe('Assets in CSS', () => {
root: './fixtures/css-assets/', root: './fixtures/css-assets/',
vite: { vite: {
build: { build: {
assetsInlineLimit: 0 assetsInlineLimit: 0,
} },
} },
}); });
await fixture.build(); await fixture.build();
}); });
@ -20,7 +20,7 @@ describe('Assets in CSS', () => {
function getAllMatches(re, text) { function getAllMatches(re, text) {
let count = 0; let count = 0;
while (re.exec(text) !== null) { while (re.exec(text) !== null) {
++count; ++count;
} }
return count; return count;
} }

View file

@ -19,7 +19,8 @@ describe('PostCSS', () => {
const $ = cheerio.load(html); const $ = cheerio.load(html);
const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href'); const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href');
bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/'))) bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/')))
.replace(/\s/g, '').replace('/n', ''); .replace(/\s/g, '')
.replace('/n', '');
}); });
it('works in Astro page styles', () => { it('works in Astro page styles', () => {
@ -43,7 +44,10 @@ describe('PostCSS', () => {
}); });
it('ignores CSS in public/', async () => { it('ignores CSS in public/', async () => {
const publicCSS = (await fixture.readFile('/global.css')).trim().replace(/\s/g, '').replace('/n', ''); const publicCSS = (await fixture.readFile('/global.css'))
.trim()
.replace(/\s/g, '')
.replace('/n', '');
// neither minified nor prefixed // neither minified nor prefixed
expect(eol.lf(publicCSS)).to.equal(`.global{appearance:none;}`); expect(eol.lf(publicCSS)).to.equal(`.global{appearance:none;}`);
}); });

View file

@ -38,9 +38,7 @@ describe('Tailwind', () => {
expect(bundledCSS, 'includes responsive classes').to.match(/\.lg\\:py-3{/); expect(bundledCSS, 'includes responsive classes').to.match(/\.lg\\:py-3{/);
// tailwind escapes brackets, `font-[900]` compiles to `font-\[900\]` // tailwind escapes brackets, `font-[900]` compiles to `font-\[900\]`
expect(bundledCSS, 'supports arbitrary value classes').to.match( expect(bundledCSS, 'supports arbitrary value classes').to.match(/\.font-\\\[900\\\]{/);
/\.font-\\\[900\\\]{/
);
// custom theme colors were included // custom theme colors were included
expect(bundledCSS, 'includes custom theme colors').to.match(/\.text-midnight{/); expect(bundledCSS, 'includes custom theme colors').to.match(/\.text-midnight{/);