Skip to content

Conversation

@koyopro
Copy link
Contributor

@koyopro koyopro commented Oct 26, 2024

Changes

In the config method of @astro/plugin-middleware, we modified it to return an empty object instead of the received opts itself.

There was an issue where some configs were duplicated when using getViteConfig().
This was caused by @astro/plugin-middleware returning the received opts as the config's return value, resulting in opts being merged with itself.

https://vite.dev/guide/api-plugin.html#config

It can return a partial config object that will be deeply merged into existing config, or directly mutate the config (if the default merging cannot achieve the desired result).

In the config, it is recommended to return only the parts you want to change, rather than the entire modified config.
Since @astro/plugin-middleware does not need to change the config itself, it seems correct to modify it to return an empty object as shown above.

Fixes: When using getViteConfig, the files listed in setupFiles are loaded twice. · Issue #12287 · withastro/astro

Testing

Before the fix, we tested that the return value of Vite's resolveConfig had a resolve.conditions array with duplicate elements like ['astro', 'astro']. After the change, the duplicates were resolved, resulting in ['astro'].

Docs

It is unlikely users rely on the existing buggy behavior, so updating the docs isn't necessary.

@changeset-bot
Copy link

changeset-bot bot commented Oct 26, 2024

🦋 Changeset detected

Latest commit: 13c0fc7

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Oct 26, 2024
@koyopro koyopro force-pushed the fix/get-vite-config branch from b477b4f to e362cdb Compare October 26, 2024 15:22
@koyopro koyopro force-pushed the fix/get-vite-config branch from e362cdb to c0b5899 Compare October 26, 2024 15:31
Comment on lines +27 to +34
describe("getViteConfig", () => {
it("Does not change the default config.", async () => {
const command = "serve";
const mode = "test";
const configFn = getViteConfig({});
const config = await configFn({ command, mode });
const resolvedConfig = await resolveConfig(config, command, mode);
assert.deepStrictEqual(resolvedConfig.resolve.conditions, ["astro"]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the fix, the test fails as follows. (The contents of the array are duplicated)

image

@koyopro koyopro marked this pull request as ready for review October 26, 2024 15:47
@bluwy bluwy merged commit 5642ef9 into withastro:main Oct 29, 2024
13 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Oct 29, 2024
@koyopro koyopro deleted the fix/get-vite-config branch October 30, 2024 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When using getViteConfig, the files listed in setupFiles are loaded twice.

3 participants