Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding build vite config options #3392

Merged
merged 9 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-gifts-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allows vite config to override options used in the build
8 changes: 4 additions & 4 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
const ssr = isBuildingToSSR(astroConfig);
const out = ssr ? opts.buildConfig.server : astroConfig.outDir;

const viteBuildConfig: ViteConfigWithSSR = {
const viteBuildConfig: ViteConfigWithSSR = vite.mergeConfig({
logLevel: 'error',
mode: 'production',
css: viteConfig.css,
Expand Down Expand Up @@ -165,7 +165,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
base: astroConfig.base,
ssr: viteConfig.ssr,
resolve: viteConfig.resolve,
};
}, viteConfig);

await runHookBuildSetup({
config: astroConfig,
Expand Down Expand Up @@ -201,7 +201,7 @@ async function clientBuild(
// TODO: use vite.mergeConfig() here?
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);

const viteBuildConfig = {
const viteBuildConfig = vite.mergeConfig({
logLevel: 'info',
mode: 'production',
css: viteConfig.css,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not important, but can css, server, and base be removed here since they'll get merged in from viteConfig?

Expand Down Expand Up @@ -235,7 +235,7 @@ async function clientBuild(
envPrefix: 'PUBLIC_',
server: viteConfig.server,
base: astroConfig.base,
} as ViteConfigWithSSR;
}, viteConfig) as ViteConfigWithSSR;

await runHookBuildSetup({
config: astroConfig,
Expand Down
18 changes: 18 additions & 0 deletions packages/astro/test/config-vite.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Vite Config', async () => {
let fixture;

before(async () => {
fixture = await loadFixture({ root: './fixtures/config-vite/' });
await fixture.build();
});

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.equal('/assets/testing-entry.css');
});
});
14 changes: 14 additions & 0 deletions packages/astro/test/fixtures/config-vite/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from 'astro/config';

export default defineConfig({
vite: {
build: {
rollupOptions: {
output: {
chunkFileNames: 'assets/testing-[name].js',
assetFileNames: 'assets/testing-[name].[ext]'
}
}
}
}
})
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/config-vite/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<title>Testing</title>
<style>
body {
background-color: aquamarine;
}
</style>
</head>
<body>
<h1>Testing</h1>
</body>
</html>