Skip to content

Commit 5078126

Browse files
committed
Minor improvements
1 parent 43a601c commit 5078126

File tree

6 files changed

+94
-24
lines changed

6 files changed

+94
-24
lines changed

README.md

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@ Clone this repo and install the bundle in your project.
1111
Install Storybook in your project:
1212

1313
```shell
14-
yarn add @storybook/cli
15-
yarn run sb init -t server
14+
yarn add -D @storybook/cli @storybook/addon-essentials @storybook/addon-links @storybook/blocks react react-dom
1615
```
1716

1817
Add the bundled NPM package for Symfony integration:
1918

2019
```shell
21-
yarn add link:vendor/sensiolabs/storybook-bundle/storybook
20+
yarn add -D link:vendor/sensiolabs/storybook-bundle/storybook
2221
```
2322

24-
Remove the auto generated `src/stories` directory, and replace the content of `.storybook/main.js|ts` with:
23+
Create storybook configuration in `.storybook/`:
2524

2625
```ts
2726
// .storybook/main.ts
@@ -58,9 +57,50 @@ const config: StorybookConfig = {
5857
export default config;
5958
```
6059

60+
```ts
61+
// .storybook/preset.ts
62+
63+
import { Preview } from '@storybook/server';
64+
65+
const preview: Preview = {
66+
parameters: {
67+
actions: { argTypesRegex: "^on[A-Z].*" },
68+
controls: {
69+
matchers: {
70+
color: /(background|color)$/i,
71+
date: /Date$/i,
72+
},
73+
},
74+
},
75+
};
76+
77+
export default preview;
78+
79+
```
80+
81+
Register the route for Storybook in your symfony project:
82+
83+
```yaml
84+
# config/routes/storybook.yaml
85+
86+
storybook:
87+
resource: '@StorybookBundle/config/routes.php'
88+
```
89+
6190
Run Storybook with:
6291
```shell
63-
yarn run storybook
92+
yarn run sb dev -p 6006
93+
```
94+
95+
Additionally, you can add custom scripts to your `package.json` file:
96+
97+
```json
98+
{
99+
"scripts": {
100+
"storybook": "sb dev -p 6006",
101+
"build-storybook": "sb build"
102+
}
103+
}
64104
```
65105

66106
## Symfony configuration
@@ -69,17 +109,17 @@ yarn run storybook
69109

70110
As the Symfony integration relies on the Storybook's server renderer, it makes requests to your Symfony server to render Twig components. These requests are cross origins, so you have to configure Symfony to accept them from your Storybook instance.
71111

72-
There is two options to achieve this. You can either configure the Storybook host in the bundle, or use the popular NelmioCORS bundle.
112+
There is two options to achieve this. You can either configure the Storybook host in the bundle, or use the popular [NelmioCorsBundle](https://symfony.com/bundles/NelmioCorsBundle/current/index.html).
73113

74-
In the bundle:
114+
In the StorybookBundle:
75115

76116
```yaml
77117
# config/storybook.yaml
78118
storybook:
79119
server: http://localhost:6006
80120
```
81121
82-
With NelmioCORS:
122+
With NelmioCorsBundle:
83123
```yaml
84124
# config/storybook.yaml
85125
storybook:
@@ -160,6 +200,40 @@ export const Secondary = {
160200
template: Button, // You can pass a specific template for a story
161201
};
162202

203+
```
204+
# Troubleshooting
205+
206+
## Conflicting `string-width` module
207+
208+
When you install storybook, you might encounter the following warnings:
209+
210+
```
211+
warning Pattern ["string-width-cjs@npm:string-width@^4.2.0"] is trying to unpack in the same destination "/home/john/.cache/yarn/v6/npm-string-width-cjs-4.2.3-269c7117d27b05ad2e536830a8ec895ef9c6d010-integrity/node_modules/string-width-cjs" as pattern ["string-width@^4.2.0"]. This could result in non-deterministic behavior, skipping.
212+
warning Pattern ["strip-ansi-cjs@npm:strip-ansi@^6.0.1"] is trying to unpack in the same destination "/home/john/.cache/yarn/v6/npm-strip-ansi-cjs-6.0.1-9e26c63d30f53443e9489495b2105d37b67a85d9-integrity/node_modules/strip-ansi-cjs" as pattern ["strip-ansi@^6.0.0"]. This could result in non-deterministic behavior, skipping.
213+
warning Pattern ["string-width@^4.1.0"] is trying to unpack in the same destination "/home/john/.cache/yarn/v6/npm-string-width-cjs-4.2.3-269c7117d27b05ad2e536830a8ec895ef9c6d010-integrity/node_modules/string-width-cjs" as pattern ["string-width@^4.2.0"]. This could result in non-deterministic behavior, skipping.
214+
```
215+
216+
217+
When trying to run storybook, if you get the following error:
218+
```
219+
🔴 Error: It looks like you are having a known issue with package hoisting.
220+
Please check the following issue for details and solutions: https://github.com/storybookjs/storybook/issues/22431#issuecomment-1630086092
221+
222+
223+
/home/john/Projects/storybook/node_modules/cli-table3/src/utils.js:1
224+
const stringWidth = require('string-width');
225+
^
226+
227+
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/john/Projects/storybook/node_modules/string-width/index.js from /home/john/Projects/storybook/node_modules/cli-table3/src/utils.js not supported.
228+
229+
```
230+
231+
Then you have to clean the yarn cache for the conflicting module and reinstall:
232+
233+
```
234+
yarn cache clean string-width-cjs@npm:string-width@^4.2.0
235+
rm -rf node_modules yarn.lock
236+
yarn install --force
163237
```
164238

165239
# License

config/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
44

55
return function (RoutingConfigurator $routes) {
6-
$routes->add('storybook_render', '/render/{id}')
6+
$routes->add('storybook_render', '/_storybook/render/{id}')
77
->requirements([
88
'id' => '.+',
99
])

src/Command/DumpImportMapCommand.php renamed to src/Command/DumpImportmapModuleCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
1010

11-
#[AsCommand('storybook:dump-importmap', hidden: true)]
1211
/**
12+
* Dump a JavaScript module that imports assets declared in the importmap.
13+
* Note this command is hidden and only intended to be run by the Storybook builder, not the user.
14+
*
1315
* @author Nicolas Rigaud <[email protected]>
1416
*/
15-
class DumpImportMapCommand extends Command
17+
#[AsCommand('storybook:dump-importmap', hidden: true)]
18+
class DumpImportmapModuleCommand extends Command
1619
{
1720
public function __construct(private readonly ImportMapConfigReader $importMapConfigReader)
1821
{

src/DependencyInjection/StorybookExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Storybook\DependencyInjection;
44

55
use Storybook\Attributes\AsStorybookLoader;
6-
use Storybook\Command\DumpImportMapCommand;
6+
use Storybook\Command\DumpImportmapModuleCommand;
77
use Storybook\Controller\StorybookController;
88
use Storybook\EventListener\CorsListener;
99
use Storybook\EventListener\ExceptionListener;
@@ -57,7 +57,7 @@ static function (ChildDefinition $definition, AsStorybookLoader $attributeInstan
5757
->addTag('twig.loader');
5858

5959
if (class_exists(AssetMapper::class)) {
60-
$container->register('storybook.dump_importmap_command', DumpImportMapCommand::class)
60+
$container->register('storybook.dump_importmap_command', DumpImportmapModuleCommand::class)
6161
->setArgument(0, new Reference('asset_mapper.importmap.config_reader'))
6262
->addTag('console.command', ['name' => 'storybook:dump-importmap'])
6363
;

src/EventListener/ExceptionListener.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@ public function __invoke(ExceptionEvent $event): void
1515
{
1616
$th = $event->getThrowable();
1717

18-
match (true) {
19-
$th instanceof TemplateNotFoundException => $this->onTemplateNotFound($event),
20-
default => null,
21-
};
22-
}
23-
24-
private function onTemplateNotFound(ExceptionEvent $event): void
25-
{
26-
$event->setThrowable(new NotFoundHttpException($event->getThrowable()->getMessage(), $event->getThrowable()));
18+
if ($th instanceof TemplateNotFoundException) {
19+
$event->setThrowable(new NotFoundHttpException($event->getThrowable()->getMessage(), $event->getThrowable()));
20+
}
2721
}
2822
}

tests/Fixtures/Kernel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ protected function configureContainer(ContainerConfigurator $container): void
7878

7979
protected function configureRoutes(RoutingConfigurator $routes): void
8080
{
81-
$routes->import('@StorybookBundle/config/routes.php')
82-
->prefix('_storybook');
81+
$routes->import('@StorybookBundle/config/routes.php');
8382
}
8483
}

0 commit comments

Comments
 (0)