Allow overriding build vite config options (#3392)

* Allow overriding build vite config options

* Adds a changeset

* Test svelte

* Move plugins down

* Assign after for the client too

* Spread output options on manually

* Remove .only
This commit is contained in:
Matthew Phillips 2022-05-18 15:11:40 -04:00 committed by GitHub
parent 54aba7231d
commit 2939be5f2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allows vite config to override options used in the build

View file

@ -944,6 +944,7 @@ export interface AstroIntegration {
vite: ViteConfigWithSSR;
pages: Map<string, PageBuildData>;
target: 'client' | 'server';
updateConfig: (newConfig: ViteConfigWithSSR) => void;
}) => void | Promise<void>;
'astro:build:done'?: (options: {
pages: { pathname: string }[];

View file

@ -142,6 +142,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
entryFileNames: opts.buildConfig.serverEntry,
chunkFileNames: 'chunks/chunk.[hash].mjs',
assetFileNames: 'assets/asset.[hash][extname]',
...viteConfig.build?.rollupOptions?.output
},
},
ssr: true,
@ -222,6 +223,7 @@ async function clientBuild(
entryFileNames: 'entry.[hash].js',
chunkFileNames: 'chunks/chunk.[hash].js',
assetFileNames: 'assets/asset.[hash][extname]',
...viteConfig.build?.rollupOptions?.output
},
preserveEntrySignatures: 'exports-only',
},

View file

@ -134,7 +134,14 @@ export async function runHookBuildSetup({
}) {
for (const integration of config.integrations) {
if (integration.hooks['astro:build:setup']) {
await integration.hooks['astro:build:setup']({ vite, pages, target });
await integration.hooks['astro:build:setup']({
vite,
pages,
target,
updateConfig: (newConfig) => {
mergeConfig(vite, newConfig);
},
});
}
}
}

View file

@ -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');
});
});

View file

@ -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]'
}
}
}
}
})

View file

@ -0,0 +1,13 @@
<html>
<head>
<title>Testing</title>
<style>
body {
background-color: aquamarine;
}
</style>
</head>
<body>
<h1>Testing</h1>
</body>
</html>