-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19259 from storybookjs/vite/framework-plugins
Issue: storybookjs/builder-vite#498 Closes #19245 ## What I did When we made the change in 7.0 to start using the user's `vite.config.js`, I made the (faulty) assumption that users would already have vite framework plugins (e.g. `@vitejs/plugin-react`) installed. But this isn't always true, for example, a project might just be a collection of components being exported, and there may not be an actual app. So, this PR checks to see if the required framework plugin is already in the config, and if not, adds it. I created a utility function to check for the plugin, but ideally this would live in a `vite-core` instead of duplicated across each framework. This, combined with the duplication in #19216, makes me lean towards creating such a package. But, it can be done any time and these duplications cleaned up at that point, so I don't think it's a blocker. ## How to test Create a sandbox, delete `vite.config.js`, and start storybook. Without this change there would be a crash, but it should work just fine now.
- Loading branch information
Showing
9 changed files
with
100 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { PluginOption } from 'vite'; | ||
|
||
export function readPackageJson(): Record<string, any> | false { | ||
const packageJsonPath = path.resolve('package.json'); | ||
if (!fs.existsSync(packageJsonPath)) { | ||
return false; | ||
} | ||
|
||
const jsonContent = fs.readFileSync(packageJsonPath, 'utf8'); | ||
return JSON.parse(jsonContent); | ||
} | ||
|
||
function checkName(plugin: PluginOption, name: string) { | ||
return typeof plugin === 'object' && 'name' in plugin && plugin.name === name; | ||
} | ||
|
||
export function hasPlugin(plugins: PluginOption[], name: string) { | ||
return Boolean( | ||
plugins.find((p): boolean => { | ||
if (Array.isArray(p)) { | ||
return Boolean(hasPlugin(p, name)); | ||
} | ||
return checkName(p, name); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { PluginOption } from 'vite'; | ||
|
||
function checkName(plugin: PluginOption, name: string) { | ||
return typeof plugin === 'object' && 'name' in plugin && plugin.name === name; | ||
} | ||
|
||
export function hasPlugin(plugins: PluginOption[], name: string) { | ||
return Boolean( | ||
plugins.find((p): boolean => { | ||
if (Array.isArray(p)) { | ||
return Boolean(hasPlugin(p, name)); | ||
} | ||
return checkName(p, name); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { PluginOption } from 'vite'; | ||
|
||
function checkName(plugin: PluginOption, name: string) { | ||
return typeof plugin === 'object' && 'name' in plugin && plugin.name === name; | ||
} | ||
|
||
export function hasPlugin(plugins: PluginOption[], name: string) { | ||
return Boolean( | ||
plugins.find((p): boolean => { | ||
if (Array.isArray(p)) { | ||
return Boolean(hasPlugin(p, name)); | ||
} | ||
return checkName(p, name); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters