Skip to content

Commit

Permalink
Allow overriding build vite config options (withastro#3392)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
matthewp committed May 18, 2022
1 parent 54aba72 commit 2939be5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
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
1 change: 1 addition & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }[];
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
},
Expand Down
9 changes: 8 additions & 1 deletion packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
});
}
}
}
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>

0 comments on commit 2939be5

Please sign in to comment.