Fix astro:build:setup hook updateConfig utility (#7438)

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
Bjorn Lu 2023-06-21 21:56:25 +08:00 committed by GitHub
parent f275d058fc
commit 30bb363713
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix `astro:build:setup` hook `updateConfig` utility, where the configuration wasn't correctly updated when the hook was fired.

View file

@ -207,7 +207,7 @@ async function ssrBuild(
base: settings.config.base, base: settings.config.base,
}; };
await runHookBuildSetup({ const updatedViteBuildConfig = await runHookBuildSetup({
config: settings.config, config: settings.config,
pages: internals.pagesByComponent, pages: internals.pagesByComponent,
vite: viteBuildConfig, vite: viteBuildConfig,
@ -215,7 +215,7 @@ async function ssrBuild(
logging: opts.logging, logging: opts.logging,
}); });
return await vite.build(viteBuildConfig); return await vite.build(updatedViteBuildConfig);
} }
async function clientBuild( async function clientBuild(

View file

@ -286,7 +286,9 @@ export async function runHookBuildSetup({
pages: Map<string, PageBuildData>; pages: Map<string, PageBuildData>;
target: 'server' | 'client'; target: 'server' | 'client';
logging: LogOptions; logging: LogOptions;
}) { }): Promise<InlineConfig> {
let updatedConfig = vite;
for (const integration of config.integrations) { for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:setup']) { if (integration?.hooks?.['astro:build:setup']) {
await withTakingALongTimeMsg({ await withTakingALongTimeMsg({
@ -296,13 +298,15 @@ export async function runHookBuildSetup({
pages, pages,
target, target,
updateConfig: (newConfig) => { updateConfig: (newConfig) => {
mergeConfig(vite, newConfig); updatedConfig = mergeConfig(updatedConfig, newConfig);
}, },
}), }),
logging, logging,
}); });
} }
} }
return updatedConfig;
} }
export async function runHookBuildSsr({ export async function runHookBuildSsr({

View file

@ -0,0 +1,30 @@
import { expect } from 'chai';
import { runHookBuildSetup } from '../../../dist/integrations/index.js';
describe('Integration API', () => {
it('runHookBuildSetup should work', async () => {
const updatedViteConfig = await runHookBuildSetup({
config: {
integrations: [
{
name: 'test',
hooks: {
'astro:build:setup'({ updateConfig }) {
updateConfig({
define: {
foo: 'bar',
},
});
},
},
},
],
},
vite: {},
logging: {},
pages: new Map(),
target: 'server',
});
expect(updatedViteConfig).to.haveOwnProperty('define');
});
});