Skip to content

Commit

Permalink
Fix astro:build:setup hook updateConfig utility (#7438)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <[email protected]>
  • Loading branch information
bluwy and ematipico authored Jun 21, 2023
1 parent f275d05 commit 30bb363
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-vans-clap.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ async function ssrBuild(
base: settings.config.base,
};

await runHookBuildSetup({
const updatedViteBuildConfig = await runHookBuildSetup({
config: settings.config,
pages: internals.pagesByComponent,
vite: viteBuildConfig,
target: 'server',
logging: opts.logging,
});

return await vite.build(viteBuildConfig);
return await vite.build(updatedViteBuildConfig);
}

async function clientBuild(
Expand Down
8 changes: 6 additions & 2 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ export async function runHookBuildSetup({
pages: Map<string, PageBuildData>;
target: 'server' | 'client';
logging: LogOptions;
}) {
}): Promise<InlineConfig> {
let updatedConfig = vite;

for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:setup']) {
await withTakingALongTimeMsg({
Expand All @@ -296,13 +298,15 @@ export async function runHookBuildSetup({
pages,
target,
updateConfig: (newConfig) => {
mergeConfig(vite, newConfig);
updatedConfig = mergeConfig(updatedConfig, newConfig);
},
}),
logging,
});
}
}

return updatedConfig;
}

export async function runHookBuildSsr({
Expand Down
30 changes: 30 additions & 0 deletions packages/astro/test/units/integrations/api.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit 30bb363

Please sign in to comment.