-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Move .storybook to build/storybook, add config-aware build support. #38683
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
Open
goodov
wants to merge
3
commits into
master
Choose a base branch
from
storybook-build-move
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−54
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
| // Copyright (c) 2026 The Brave Authors. All rights reserved. | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| // Check environment before doing anything. | ||
| import '../lib/checkEnvironment.js' | ||
|
|
||
| import { Argument, program } from 'commander' | ||
| import { createBuildConfigArgument } from '../lib/commandsUtils.ts' | ||
| import path from 'node:path' | ||
| import config from '../lib/config.ts' | ||
| import util from '../lib/util.js' | ||
| import { storybookCommand } from '../../storybook/storybook_command.ts' | ||
|
|
||
| program | ||
| .description( | ||
| 'Build Storybook generated deps, then run Storybook (dev or static build)', | ||
| ) | ||
| .addArgument( | ||
| new Argument('<mode>', 'Storybook mode').choices(['dev', 'build']), | ||
| ) | ||
| .addArgument(createBuildConfigArgument()) | ||
| .option('-C <build_dir>', 'build directory, relative to out/ or absolute') | ||
| .option('--target_arch <target_arch>', 'target architecture') | ||
| .option('--target_os <target_os>', 'target OS') | ||
| .allowExcessArguments(true) | ||
| .allowUnknownOption(true) | ||
| .action(async (mode, buildConfig, options) => { | ||
| const buildConfigProvided = | ||
| buildConfig !== undefined | ||
| || options.C !== undefined | ||
| || options.target_arch !== undefined | ||
| || options.target_os !== undefined | ||
|
|
||
| if (!buildConfigProvided) { | ||
| // If no build config was provided, use the build output path from | ||
| // guessConfig.js to set the build directory. This is a fallback for CI to | ||
| // run storybook without specifying a build config, but still use the | ||
| // right build output path. | ||
| await import('../lib/guessConfig.js').then(({ outputPath }) => { | ||
| console.log(`Using build output path from guessConfig: ${outputPath}`) | ||
| options.C = outputPath | ||
| }) | ||
| } else { | ||
| config.buildConfig = buildConfig || config.defaultBuildConfig | ||
| } | ||
|
|
||
| config.update(options) | ||
| config.buildTargets = ['brave/build/storybook:storybook_deps'] | ||
|
|
||
| await util.buildTargets(config.buildTargets, config.defaultOptions) | ||
|
|
||
| const storybookArgs = [ | ||
| mode, | ||
| `--root-gen-dir=${path.join(config.outputDir, 'gen')}`, | ||
| ] | ||
|
|
||
| if (mode === 'build') { | ||
| // If no build config was provided, use brave/.storybook-out directory. | ||
| // This is a fallback for CI to generate storybook at the CI-expected | ||
| // output path. | ||
| const outputDir = buildConfigProvided | ||
| ? path.join(config.outputDir, 'storybook') | ||
| : path.join(config.braveCoreDir, '.storybook-out') | ||
| storybookArgs.push(`--output-dir=${outputDir}`) | ||
| } | ||
|
|
||
| if (mode === 'dev') { | ||
| // Pass through any additional arguments in dev mode. | ||
| storybookArgs.push(...program.args) | ||
| } | ||
|
|
||
| await storybookCommand.parseAsync(storybookArgs, { from: 'user' }) | ||
| }) | ||
| .parseAsync() | ||
This file contains hidden or 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 hidden or 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 hidden or 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
File renamed without changes.
This file contains hidden or 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,86 @@ | ||
| // Copyright (c) 2026 The Brave Authors. All rights reserved. | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| import { spawnSync } from 'node:child_process' | ||
| import fs from 'node:fs' | ||
| import path from 'node:path' | ||
| import process from 'node:process' | ||
| import { Argument, Command } from 'commander' | ||
|
|
||
| const storybookConfigDir = import.meta.dirname | ||
| const braveCoreDir = path.resolve(storybookConfigDir, '../..') | ||
| const storybookCli = path.join( | ||
| braveCoreDir, | ||
| 'node_modules', | ||
| 'storybook', | ||
| 'bin', | ||
| 'index.cjs', | ||
| ) | ||
|
|
||
| export const storybookCommand = new Command('storybook') | ||
| .description('Run Storybook (dev or static build)') | ||
| .addArgument( | ||
| new Argument('<mode>', 'Storybook mode').choices(['dev', 'build']), | ||
| ) | ||
| .requiredOption( | ||
| '--root-gen-dir <path>', | ||
| 'Path to the build gen directory (out/<config>/gen)', | ||
| ) | ||
| .option( | ||
| '--output-dir <path>', | ||
| 'Static Storybook output directory (required for build mode)', | ||
| ) | ||
| .allowExcessArguments(true) | ||
| .allowUnknownOption(true) | ||
| .action((mode, options, program) => { | ||
| const rootGenDir = path.resolve(options.rootGenDir) | ||
| const outputDir = options.outputDir | ||
| ? path.resolve(options.outputDir) | ||
| : undefined | ||
|
|
||
| if (mode === 'build' && !outputDir) { | ||
| program.error('--output-dir is required when --mode=build') | ||
| } | ||
|
|
||
| if (!fs.existsSync(rootGenDir)) { | ||
| program.error( | ||
| `Failed to find build output 'gen' folder at '${rootGenDir}'. ` | ||
| + 'Have you run a brave-core build yet with the specified ' | ||
| + '(or default) configuration?', | ||
| ) | ||
| } | ||
|
|
||
| const nodeArgs = [ | ||
| '--max-old-space-size=8192', | ||
| storybookCli, | ||
| mode, | ||
| '-c', | ||
| storybookConfigDir, | ||
| ] | ||
|
|
||
| if (mode === 'build' && outputDir) { | ||
| nodeArgs.push('-o', outputDir) | ||
| } | ||
|
|
||
| // Pass through any additional arguments. | ||
| nodeArgs.push(...program.args) | ||
|
|
||
| const env: NodeJS.ProcessEnv = { | ||
| ...process.env, | ||
| ROOT_GEN_DIR: rootGenDir, | ||
| } | ||
|
|
||
| console.log(`Using gen directory: ${rootGenDir}`) | ||
|
|
||
| const result = spawnSync(process.execPath, nodeArgs, { | ||
| env, | ||
| stdio: 'inherit', | ||
| cwd: braveCoreDir, | ||
| }) | ||
|
|
||
| if (result.status !== 0) { | ||
| process.exit(result.status ?? 1) | ||
| } | ||
| }) |
This file contains hidden or 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,15 @@ | ||
| { | ||
| "extends": "../../tsconfig", | ||
| "include": [ | ||
| "../../components/definitions/*", | ||
| "../../components/**/storybook", | ||
| "../../components/**/stories/*.tsx", | ||
| "../../components/**/*.stories.tsx", | ||
| ], | ||
| "compilerOptions": { | ||
| "allowJs": true, | ||
| "skipLibCheck": true, | ||
| "noImplicitAny": true, | ||
| "outDir": "../../out/storybook_dist", | ||
| }, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we add an option to skip this, e.g.
--skip-gn-deps? If I'm running the command many times in a session and I know I haven't changed any types then I would like to skip the GN time if it takes more than a couple of seconds to realize it doesn't need to compile. Restarting storybool can be necessary when adding new files or dependencies.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.
disabled the build by default in
devmode and added-Bflag to enable it. lmk what you think, the default can be the other way around, but since you mentioned you may restart storybook frequently, maybe it's better to have the default to be "disable gn build".