Add vite.build.cssTarget support for CSS build (#4155)

* Add `vite.build.cssTarget` support for CSS build

* chore: update lockfile

Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
kagankan 2022-08-05 04:39:43 +09:00 committed by GitHub
parent 3362ec2478
commit 81c9ad9a6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Add `vite.build.cssTaregt` support for CSS build

View file

@ -153,6 +153,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
rollupPluginAstroBuildCSS({
internals,
target: 'server',
astroConfig,
}),
...(viteConfig.plugins || []),
// SSR needs to be last
@ -234,6 +235,7 @@ async function clientBuild(
rollupPluginAstroBuildCSS({
internals,
target: 'client',
astroConfig,
}),
...(viteConfig.plugins || []),
],

View file

@ -8,10 +8,12 @@ import { Plugin as VitePlugin } from 'vite';
import { getTopLevelPages, walkParentInfos } from '../core/build/graph.js';
import { getPageDataByViteID, getPageDatasByClientOnlyID } from '../core/build/internal.js';
import { isCSSRequest } from '../core/render/util.js';
import { AstroConfig } from '../@types/astro';
interface PluginOptions {
internals: BuildInternals;
target: 'client' | 'server';
astroConfig: AstroConfig;
}
export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
@ -118,9 +120,11 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
for (const [, output] of Object.entries(bundle)) {
if (output.type === 'asset') {
if (output.name?.endsWith('.css') && typeof output.source === 'string') {
const cssTarget = options.astroConfig.vite.build?.cssTarget;
const { code: minifiedCSS } = await esbuild.transform(output.source, {
loader: 'css',
minify: true,
...(cssTarget ? { target: cssTarget } : {}),
});
output.source = minifiedCSS;
}

View file

@ -0,0 +1,39 @@
/**
* css-target
*/
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
let fixture;
describe('CSS', function () {
before(async () => {
fixture = await loadFixture({ root: './fixtures/config-vite-css-target/' });
});
describe('build', () => {
let $;
let html;
let bundledCSS;
before(async () => {
await fixture.build();
// get bundled CSS (will be hashed, hence DOM query)
html = await fixture.readFile('/index.html');
$ = cheerio.load(html);
const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href');
bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/')))
.replace(/\s/g, '')
.replace('/n', '');
});
it('vite.build.cssTarget is respected', async () => {
expect(bundledCSS).to.match(
new RegExp('.class\\:where\\(.astro-[^{]*{top:0;right:0;bottom:0;left:0}')
);
});
});
});

View file

@ -0,0 +1,9 @@
import { defineConfig } from 'astro/config';
export default defineConfig({
vite: {
build: {
cssTarget: "safari14",
}
}
})

View file

@ -0,0 +1,8 @@
{
"name": "@test/config-vite-css-target",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,18 @@
<html>
<head>
<title>css-target</title>
</head>
<body>
<h1>css-target</h1>
<div class="class"></div>
</body>
</html>
<style>
.class {
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>

View file

@ -1389,6 +1389,12 @@ importers:
dependencies:
astro: link:../../..
packages/astro/test/fixtures/config-vite-css-target:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/css-assets:
specifiers:
'@astrojs/test-font-awesome-package': file:packages/font-awesome