Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extends in defineConfig #8964

Closed
4 tasks done
lsdsjy opened this issue Jul 7, 2022 · 5 comments
Closed
4 tasks done

extends in defineConfig #8964

lsdsjy opened this issue Jul 7, 2022 · 5 comments

Comments

@lsdsjy
Copy link
Contributor

lsdsjy commented Jul 7, 2022

Description

I'd like to add a field extends for defineConfig like this:

import commonConfig from '@company/vite-config-common'

export default defineConfig({
  extends: [commonConfig]
})

It would help us unify and simplify the vite config files in our projects.

Suggested solution

The semantics of extends can be identical to mergeConfig.
We can allow using package name directly in extends for convenience.

Alternative

  1. mergeConfig
    Adding extends field is more simple and constrained than letting users use mergeConfig directly in their config files. Besides, mergeConfig is an undocumented API.

  2. abstract over vite defineConfig
    Wrap around vite like:

// @mycompany/wrapped-vite/index.ts
import { mergeConfig, defineConfig } from 'vite'
const commonConfig = ...
export const defineConfig: typeof defineConfig = (...args) => mergeConfig(commonConfig, defineConfig(...args))

// user-project/vite.config.ts
import { defineConfig } from '@mycompany/wrapped-vite'
export default defineConfig({...})

It works but we think it's better to add this ability directly to vite rather than wrap it.

Additional context

No response

Validations

@lmiller1990
Copy link

Can't you just do a merge?

import { common } from './common.config'

export default defineConfig({
   ...common,
   {
      // other config
  }
})

Then you can selectively override things. The obvious complexity with extends is how things are merged - since this is JS module, not json, I don't think extends makes sense. extends is normally in json configs, where you cannot do a merge, since the code is not a module.

@lsdsjy
Copy link
Contributor Author

lsdsjy commented Jul 14, 2022

Using object spread syntax would overwrite fields, e.g.

const commonConfig = {
    resolve: {
        alias: [
            {
                find: '@',
                replacement: path.resolve(__dirname, 'src'),
            },
        ]
    }
}
export default defineConfig({
    ...commonConfig,
    resolve: {
        alias: [...]
    }
})

The whole resolve field in the commonConfig would be lost. The semantics of extends is more like deepMerge.
In fact, merging configs is already implemented in mergeConfig which is exported by vite. All we need to do is make it public, in a more declarative and constrained fashion.

@lmiller1990
Copy link

lmiller1990 commented Jul 14, 2022

Right, you'd need to do more effort:

const commonConfig = {
    resolve: {
        alias: [
            {
                find: '@',
                replacement: path.resolve(__dirname, 'src'),
            },
        ]
    }
}
export default defineConfig({
    ...commonConfig,
    resolve: {
        alias: [...commonConfig.alias, ...newAliases]
    }
})

Not clean, for sure. I have not looked, but how does Vite implement merging under the hood? Is it just something like https://www.npmjs.com/package/deepmerge? I wonder if that would work for you, if the Vite team doesn't want to expose and document extends.

@lsdsjy
Copy link
Contributor Author

lsdsjy commented Jul 14, 2022

#8999 It seems that Vite now documents mergeConfig as an public API. Though I still don't want users of my common config uses mergeConfig directly, this feature request is more unnecessary now.😅

@bluwy
Copy link
Member

bluwy commented Jul 14, 2022

mergeConfig and many exposed APIs are always public, but we expose a lot of APIs to fit in the docs so it only shows the commonly used ones. I think using mergeConfig is the way to go here. If we implement extends it'll very likely be the same as mergeConfig, and it's better to have one way of doings things. Plus doing it in-code makes it easier to reason how data is passed around.

@lsdsjy lsdsjy closed this as completed Jul 14, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants