diff --git a/packages/astro/src/core/build/internal.ts b/packages/astro/src/core/build/internal.ts index 7561e17e4..ad838fac8 100644 --- a/packages/astro/src/core/build/internal.ts +++ b/packages/astro/src/core/build/internal.ts @@ -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; } } diff --git a/packages/astro/src/vite-plugin-build-css/index.ts b/packages/astro/src/vite-plugin-build-css/index.ts index 02e46c109..0ff5f58eb 100644 --- a/packages/astro/src/vite-plugin-build-css/index.ts +++ b/packages/astro/src/vite-plugin-build-css/index.ts @@ -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()): Generator { + function* walkParentInfos( + id: string, + ctx: { getModuleInfo: GetModuleInfo }, + seen = new Set() + ): Generator { 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 { - for(const info of walkParentInfos(id, ctx)) { + function* getTopLevelPages( + id: string, + ctx: { getModuleInfo: GetModuleInfo } + ): Generator { + 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 { - for(const info of walkParentInfos(id, ctx)) { - yield * getPageDatasByClientOnlyID(internals, info.id); + function* getParentClientOnlys( + id: string, + ctx: { getModuleInfo: GetModuleInfo } + ): Generator { + 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; importedCss: Set; - } + }; 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; }); } } - } + }, }; } diff --git a/packages/astro/test/0-css.test.js b/packages/astro/test/0-css.test.js index 7ad68227f..3d91b6135 100644 --- a/packages/astro/test/0-css.test.js +++ b/packages/astro/test/0-css.test.js @@ -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', () => { diff --git a/packages/astro/test/astro-client-only.test.js b/packages/astro/test/astro-client-only.test.js index 1067dba1b..c8b0ca793 100644 --- a/packages/astro/test/astro-client-only.test.js +++ b/packages/astro/test/astro-client-only.test.js @@ -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/, '')); diff --git a/packages/astro/test/config-vite.test.js b/packages/astro/test/config-vite.test.js index ef32602de..f910bc30b 100644 --- a/packages/astro/test/config-vite.test.js +++ b/packages/astro/test/config-vite.test.js @@ -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/); }); }); diff --git a/packages/astro/test/css-assets.test.js b/packages/astro/test/css-assets.test.js index 18eeb234d..6e36521e6 100644 --- a/packages/astro/test/css-assets.test.js +++ b/packages/astro/test/css-assets.test.js @@ -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; } diff --git a/packages/astro/test/postcss.test.js b/packages/astro/test/postcss.test.js index 4ef4cd936..d5360ba1d 100644 --- a/packages/astro/test/postcss.test.js +++ b/packages/astro/test/postcss.test.js @@ -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;}`); }); diff --git a/packages/astro/test/tailwindcss.test.js b/packages/astro/test/tailwindcss.test.js index 4fa3973c3..15723c456 100644 --- a/packages/astro/test/tailwindcss.test.js +++ b/packages/astro/test/tailwindcss.test.js @@ -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{/);