[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;
if (pagesByClientOnly.size) {
const pathname = `/@fs${prependForwardSlash(viteid)}`;
const pageBuildDatas = pagesByClientOnly.get(pathname)
if(pageBuildDatas) {
for(const pageData of pageBuildDatas) {
const pageBuildDatas = pagesByClientOnly.get(pathname);
if (pageBuildDatas) {
for (const pageData of pageBuildDatas) {
yield pageData;
}
}

View file

@ -17,44 +17,54 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
const { internals } = options;
// 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);
const info = ctx.getModuleInfo(id);
if(info) {
if (info) {
yield info;
}
const importers = (info?.importers || []).concat(info?.dynamicImporters || []);
for(const imp of importers) {
if(seen.has(imp)) {
for (const imp of importers) {
if (seen.has(imp)) {
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 could be a .astro page or a .md page.
function* getTopLevelPages(id: string, ctx: {getModuleInfo: GetModuleInfo}): Generator<string, void, unknown> {
for(const info of walkParentInfos(id, ctx)) {
function* getTopLevelPages(
id: string,
ctx: { getModuleInfo: GetModuleInfo }
): Generator<string, void, unknown> {
for (const info of walkParentInfos(id, ctx)) {
const importers = (info?.importers || []).concat(info?.dynamicImporters || []);
if(importers.length <= 2 && importers[0] === resolvedPagesVirtualModuleId) {
if (importers.length <= 2 && importers[0] === resolvedPagesVirtualModuleId) {
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 hash = crypto.createHash('sha256');
for(const page of parents) {
for (const page of parents) {
hash.update(page, 'utf-8');
}
return hash.digest('hex').slice(0, 8);
}
function* getParentClientOnlys(id: string, ctx: {getModuleInfo: GetModuleInfo}): Generator<PageBuildData, void, unknown> {
for(const info of walkParentInfos(id, ctx)) {
yield * getPageDatasByClientOnlyID(internals, info.id);
function* getParentClientOnlys(
id: string,
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
// server JS.
const renderChunk = viteCSSPost.renderChunk;
if(renderChunk) {
viteCSSPost.renderChunk = async function(...args) {
if (renderChunk) {
viteCSSPost.renderChunk = async function (...args) {
const minifyOption = resolvedConfig.build.minify;
if(minifyOption === false) {
if (minifyOption === false) {
resolvedConfig.build.minify = 'esbuild';
}
const result = await renderChunk.apply(this, args);
if(typeof result === 'string') {
if (typeof result === 'string') {
return {
code: result
code: result,
};
}
resolvedConfig.build.minify = minifyOption;
@ -103,15 +113,15 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
outputOptions(outputOptions) {
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.
if(typeof manualChunks == 'object') {
if(id in manualChunks) {
if (typeof manualChunks == 'object') {
if (id in manualChunks) {
return manualChunks[id];
}
} else if(typeof manualChunks === 'function') {
} else if (typeof manualChunks === 'function') {
const outid = manualChunks.call(this, id, ...args);
if(outid) {
if (outid) {
return outid;
}
}
@ -128,23 +138,23 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
type ViteMetadata = {
importedAssets: Set<string>;
importedCss: Set<string>;
}
};
for (const [_, chunk] of Object.entries(bundle)) {
if(chunk.type === 'chunk') {
if (chunk.type === 'chunk') {
const c = chunk;
if('viteMetadata' in chunk) {
if ('viteMetadata' in chunk) {
const meta = chunk['viteMetadata'] as ViteMetadata;
// 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
// 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.
if(options.target === 'client') {
for(const [id] of Object.entries(c.modules)) {
for(const pageData of getParentClientOnlys(id, this)) {
for(const importedCssImport of meta.importedCss) {
if (options.target === 'client') {
for (const [id] of Object.entries(c.modules)) {
for (const pageData of getParentClientOnlys(id, this)) {
for (const importedCssImport of meta.importedCss) {
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(const [id] of Object.entries(c.modules)) {
for(const pageViteID of getTopLevelPages(id, this)) {
for (const [id] of Object.entries(c.modules)) {
for (const pageViteID of getTopLevelPages(id, this)) {
const pageData = getPageDataByViteID(internals, pageViteID);
for(const importedCssImport of meta.importedCss) {
for (const importedCssImport of meta.importedCss) {
pageData?.css.add(importedCssImport);
}
}
@ -165,15 +175,18 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
}
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
// 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) => {
return begin + '"' + chunkPath + '"' + end;
});
}
}
}
},
};
}

View file

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

View file

@ -65,7 +65,7 @@ describe('Client only components subpath', () => {
it('Adds the CSS to the page', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html);
const href = $('link[rel=stylesheet]').attr('href');
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 () => {
const html = await fixture.readFile('/index.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/',
vite: {
build: {
assetsInlineLimit: 0
}
}
assetsInlineLimit: 0,
},
},
});
await fixture.build();
});
@ -20,7 +20,7 @@ describe('Assets in CSS', () => {
function getAllMatches(re, text) {
let count = 0;
while (re.exec(text) !== null) {
++count;
++count;
}
return count;
}

View file

@ -19,7 +19,8 @@ describe('PostCSS', () => {
const $ = cheerio.load(html);
const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href');
bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/')))
.replace(/\s/g, '').replace('/n', '');
.replace(/\s/g, '')
.replace('/n', '');
});
it('works in Astro page styles', () => {
@ -43,7 +44,10 @@ describe('PostCSS', () => {
});
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
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{/);
// tailwind escapes brackets, `font-[900]` compiles to `font-\[900\]`
expect(bundledCSS, 'supports arbitrary value classes').to.match(
/\.font-\\\[900\\\]{/
);
expect(bundledCSS, 'supports arbitrary value classes').to.match(/\.font-\\\[900\\\]{/);
// custom theme colors were included
expect(bundledCSS, 'includes custom theme colors').to.match(/\.text-midnight{/);