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
Vite: Fix config loading - project directory #22240
Conversation
@JReinhold can you re-approve the workflows on this PR, please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, this looks good to me! I think our heuristic for determining where the root actually is might be a bit brittle, but it's the way we've been doing it for a while and we haven't had reports of problems. And we can try to find a better way in the future if we do hear from folks.
Thanks again!
81677e5
to
8ccd904
Compare
@IanVS Just wanted to follow up on this PR. I went ahead and rebased over the latest |
@IanVS Just wanted to follow up on this. |
@nVitius what do you think about Norbert's suggestion to use |
@IanVS I made the change, but it can no longer find:
|
@ndelangen I think this is failing because in our monorepo, the |
This reverts commit d2dc245.
Interestingly this broke our mono repo build. We have the In here the import path from "node:path"
import type { StorybookConfig } from "@storybook/react-vite"
const config: StorybookConfig = {
stories: [path.join(process.cwd(), "src/**/*.stories.@(ts|tsx)")],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions"
],
framework: {
name: "@storybook/react-vite",
options: {
builder: {
viteConfigPath: path.join(process.cwd(), "vite.config.ts")
}
}
},
typescript: {
check: false
}
}
export default config We also use a wrapper for the storybook binary to simplify using this config: const path = require("path")
const { symlinkSync } = require("fs")
const { spawn } = require("child_process")
const configFolder = path.resolve(__dirname, "../config")
const binPath = path.resolve(__dirname, "../node_modules/.bin/storybook")
const ports = {
uikit: 8000,
theme: 8100,
workbench: 9000,
dashboard: 9100
}
;(async () => {
const command = process.argv[2]
const params = [
command,
"--config-dir",
configFolder,
"--disable-telemetry",
...process.argv.slice(3)
]
if (command === "dev") {
const base = path.basename(process.cwd())
params.push("--port", ports[base] ?? 7000)
params.push("--no-open")
}
if (command === "build") {
params.push("--output-dir", "public")
}
try {
const cmd = spawn(binPath, params)
// Reduce noise by not piping over stdout
cmd.stdout.pipe(process.stdout)
cmd.stderr.pipe(process.stdout)
await cmd
} catch (err) {
console.log("Storybook Error:", err)
process.exitCode = 1
}
})() This is mainly meant as a side-node for other people looking for a solution if this also broke for them. |
I haven't filed an issue for this yet. Happy to create one if there is any value in tracking it that way.
What I did
When calling Vite's
loadConfigFromFile
there is an option to specify theconfigRoot
. That is, the directory that contains the package'spackage.json
. By default, this isprocess.cwd()
.If you run storybook from a directory that is not the package root, then this will be the wrong directory. This is a common strategy when running storybook in a monorepo, for example.
Vite uses the
package.json
to determine if it is an ESM and changes its bundling strategy accordingly. I was running into an issue where a package imported into myvite.config.ts
was failing to import correctly when running Storybook, but not when building with Vite directly.This PR adds the correct root directory to
loadConfigFromFile
.How to test
Checklist
MIGRATION.MD
Maintainers
make sure to add the
ci:merged
orci:daily
GH label to it.["cleanup", "BREAKING CHANGE", "feature request", "bug", "documentation", "maintenance", "dependencies", "other"]