From 2939be5f2d95711a2a891d77824763c7dbbf10d2 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 18 May 2022 15:11:40 -0400 Subject: [PATCH] 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 --- .changeset/eleven-gifts-matter.md | 5 +++++ packages/astro/src/@types/astro.ts | 1 + packages/astro/src/core/build/static-build.ts | 2 ++ packages/astro/src/integrations/index.ts | 9 ++++++++- packages/astro/test/config-vite.test.js | 18 ++++++++++++++++++ .../test/fixtures/config-vite/astro.config.mjs | 14 ++++++++++++++ .../fixtures/config-vite/src/pages/index.astro | 13 +++++++++++++ 7 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .changeset/eleven-gifts-matter.md create mode 100644 packages/astro/test/config-vite.test.js create mode 100644 packages/astro/test/fixtures/config-vite/astro.config.mjs create mode 100644 packages/astro/test/fixtures/config-vite/src/pages/index.astro diff --git a/.changeset/eleven-gifts-matter.md b/.changeset/eleven-gifts-matter.md new file mode 100644 index 000000000..ae6e0323d --- /dev/null +++ b/.changeset/eleven-gifts-matter.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allows vite config to override options used in the build diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 9cd7dd85a..85e3d26c5 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -944,6 +944,7 @@ export interface AstroIntegration { vite: ViteConfigWithSSR; pages: Map; target: 'client' | 'server'; + updateConfig: (newConfig: ViteConfigWithSSR) => void; }) => void | Promise; 'astro:build:done'?: (options: { pages: { pathname: string }[]; diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 2fa7f964c..a2cbe982a 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -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', }, diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 1abbbae3b..47dcfdcc5 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -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); + }, + }); } } } diff --git a/packages/astro/test/config-vite.test.js b/packages/astro/test/config-vite.test.js new file mode 100644 index 000000000..6e69bf4a7 --- /dev/null +++ b/packages/astro/test/config-vite.test.js @@ -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'); + }); +}); diff --git a/packages/astro/test/fixtures/config-vite/astro.config.mjs b/packages/astro/test/fixtures/config-vite/astro.config.mjs new file mode 100644 index 000000000..2e6f18a63 --- /dev/null +++ b/packages/astro/test/fixtures/config-vite/astro.config.mjs @@ -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]' + } + } + } + } +}) diff --git a/packages/astro/test/fixtures/config-vite/src/pages/index.astro b/packages/astro/test/fixtures/config-vite/src/pages/index.astro new file mode 100644 index 000000000..8947fd369 --- /dev/null +++ b/packages/astro/test/fixtures/config-vite/src/pages/index.astro @@ -0,0 +1,13 @@ + + + Testing + + + +

Testing

+ +