Ian VanSchooten 994a26df8f
Vite: Allow specifying path to vite.config file (#20681)
Issue: #20552

## What I did

I added a new builder option for the Vite builder named `viteConfigPath`, which is a path to a custom config relative to the cwd.  If the path does not resolve, a clear error will be thrown with the absolute path being checked.

I also added a new utility, `getBuilderOptions()`, which is helpful because these options can be specified two different ways, with `core.builder.options` or `framework.options.builder`.  The utility gives an easy way to check both.  It gives preference to ~`core.builder.options`~ `framework.options.builder`, since that's the newer way to define the options.

## How to test

1. `yarn task --task sandbox --start-from auto --template react-vite/default-ts`
2. Move the `vite.config.ts` file into `.storybook`
3. Add an alias, like:
```ts
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      stories: path.resolve(__dirname, '../src/stories'),
    },
  },
});
```
4. Change one of the stories to use this alias, such as the Button story:
```ts
import { Button } from 'stories/Button';
```
5. Start Storybook, it will fail
6. Add `viteConfigPath: ".storybook/vite.config.ts"` to the builder options in `.storybook/main.ts`
7. Restart storybook, and it should work.


## Checklist

<!-- Please check (put an "x" inside the "[ ]") the applicable items below to make sure your PR is ready to be reviewed. -->

- [ ] Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
- [X] Make sure to add/update documentation regarding your changes
- [ ] If you are deprecating/removing a feature, make sure to update
      [MIGRATION.MD](https://github.com/storybookjs/storybook/blob/next/MIGRATION.md)

Is there a way to create an automated test for this?  I couldn't think of a good way to do it.

#### Maintainers

- [ ] If this PR should be tested against many or all sandboxes,
      make sure to add the `ci:merged` or `ci:daily` GH label to it.
- [X] Make sure this PR contains **one** of the labels below.

`["cleanup", "BREAKING CHANGE", "feature request", "bug", "documentation", "maintenance", "dependencies", "other"]`

<!--

Everybody: Please submit all PRs to the `next` branch unless they are specific to the current release. Storybook maintainers cherry-pick bug and documentation fixes into the `main` branch as part of the release process, so you shouldn't need to worry about this. For additional guidance: https://storybook.js.org/docs/react/contribute/how-to-contribute

-->
2023-01-20 09:17:29 -05:00
..
2022-07-21 11:24:07 +02:00
2022-11-14 14:18:39 +01:00

Storybook Core-Common

Common utilities used across @storybook/core-server (manager UI configuration) and @storybook/builder-webpack{4,5} (preview configuration).

This is a lot of code extracted for convenience, not because it made sense.

Supporting multiple version of webpack and this duplicating a large portion of code that was never meant to be generic caused this.

At some point we'll refactor this, it's likely a lot of this code is dead or barely used.