storybook-addon-dependency-previews 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +251 -1
  2. package/dist/addon/index.cjs +6 -0
  3. package/dist/addon/index.cjs.map +1 -1
  4. package/dist/addon/index.d.cts +2 -1
  5. package/dist/addon/index.d.mts +2 -1
  6. package/dist/addon/index.mjs +4 -1
  7. package/dist/addon/index.mjs.map +1 -1
  8. package/dist/blocks/DependencyPreviews.d.cts +2 -2
  9. package/dist/blocks/DependencyPreviews.d.mts +2 -2
  10. package/dist/cli/sb-deps.cjs +357 -33
  11. package/dist/cli/sb-deps.cjs.map +1 -1
  12. package/dist/cli/sb-deps.mjs +354 -31
  13. package/dist/cli/sb-deps.mjs.map +1 -1
  14. package/dist/cli/scripts/postprocess.cjs +10 -3
  15. package/dist/cli/scripts/postprocess.cjs.map +1 -1
  16. package/dist/cli/scripts/postprocess.mjs +10 -3
  17. package/dist/cli/scripts/postprocess.mjs.map +1 -1
  18. package/dist/components/SbHeading.cjs +1 -3
  19. package/dist/components/SbHeading.cjs.map +1 -1
  20. package/dist/components/SbHeading.mjs +1 -2
  21. package/dist/components/SbHeading.mjs.map +1 -1
  22. package/dist/config.cjs +9 -0
  23. package/dist/config.cjs.map +1 -0
  24. package/dist/config.d.cts +107 -0
  25. package/dist/config.d.mts +107 -0
  26. package/dist/config.mjs +8 -0
  27. package/dist/config.mjs.map +1 -0
  28. package/dist/hooks/useDependencyGraph.cjs +1 -1
  29. package/dist/hooks/useDependencyGraph.cjs.map +1 -1
  30. package/dist/hooks/useDependencyGraph.mjs +1 -1
  31. package/dist/hooks/useDependencyGraph.mjs.map +1 -1
  32. package/dist/index.d.cts +2 -2
  33. package/dist/index.d.mts +2 -2
  34. package/dist/panels/DefaultAutoDocsLayout.d.cts +2 -2
  35. package/dist/panels/DefaultAutoDocsLayout.d.mts +2 -2
  36. package/dist/types.d.cts +14 -1
  37. package/dist/types.d.mts +14 -1
  38. package/package.json +7 -2
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  A plugin for [Storybook](https://storybook.js.org/) that shows the full dependency tree in both directions (built with and used by) the components in your application.
10
10
 
11
- Currently works with **React** and **Svelte**. Angular support is coming soon.
11
+ Currently works with **React**, **Svelte**, and **Angular**.
12
12
 
13
13
  This is what you will see in Storybook after Dependency Previews have been installed and configured:
14
14
 
@@ -32,6 +32,12 @@ The below image demonstrates what you will see when you open up some of the depe
32
32
  - [Svelte rendered example website](https://dependency-previews-demo-site-svelte.netlify.app/)
33
33
  - [Svelte demo source code](https://github.com/Dan503/storybook-addon-dependency-previews/tree/main/example-site/svelte)
34
34
 
35
+ #### Angular demos
36
+
37
+ - [Angular Storybook demo site](https://dependency-previews-storybook-angular.netlify.app/?path=/docs/04-templates-home-template--docs)
38
+ - [Angular rendered example website](https://dependency-previews-demo-site-angular.netlify.app/)
39
+ - [Angular demo source code](https://github.com/Dan503/storybook-addon-dependency-previews/tree/main/example-site/angular)
40
+
35
41
  <!-- TODO: Provide a video/gif of the addon in action -->
36
42
 
37
43
  ## Installation guide
@@ -101,6 +107,91 @@ const config: StorybookConfig = {
101
107
  export default config
102
108
  ```
103
109
 
110
+ #### Register with Angular
111
+
112
+ `@storybook/angular` uses webpack5 exclusively. The addon requires two webpack additions in `webpackFinal`: a `DefinePlugin` to inject `__PROJECT_ROOT__` (needed for the VS Code "open file" feature), and a `NormalModuleFactory` plugin to handle the addon's CSS modules correctly.
113
+
114
+ You will also need a `css-modules-loader.cjs` file alongside your `.storybook/main.ts`. Copy it from the [Angular example source](https://github.com/Dan503/storybook-addon-dependency-previews/blob/main/example-site/angular/.storybook/css-modules-loader.cjs).
115
+
116
+ ```ts
117
+ import type { StorybookConfig } from '@storybook/angular'
118
+ import { createRequire } from 'module'
119
+ import { fileURLToPath } from 'url'
120
+ import path from 'path'
121
+
122
+ const require = createRequire(import.meta.url)
123
+
124
+ const config: StorybookConfig = {
125
+ stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
126
+ addons: [
127
+ // autodocs is required for this addon to work
128
+ '@storybook/addon-docs',
129
+ // the storybook dependency previews addon registration
130
+ 'storybook-addon-dependency-previews/addon',
131
+ ],
132
+ framework: {
133
+ name: '@storybook/angular',
134
+ options: {},
135
+ },
136
+ webpackFinal: async (config) => {
137
+ const sbAngularRequire = createRequire(
138
+ import.meta.resolve('@storybook/angular'),
139
+ )
140
+ const webpack = sbAngularRequire('webpack') as any
141
+
142
+ // Path to the css-modules-loader.cjs file (copy from the Angular example source)
143
+ const cssModulesLoader = fileURLToPath(
144
+ new URL('./css-modules-loader.cjs', import.meta.url),
145
+ )
146
+
147
+ config.plugins = config.plugins ?? []
148
+
149
+ // Inject __PROJECT_ROOT__ so preview.ts can pass it to projectRootPath
150
+ config.plugins.push(
151
+ new webpack.DefinePlugin({
152
+ __PROJECT_ROOT__: JSON.stringify(
153
+ path.resolve(fileURLToPath(new URL('..', import.meta.url))),
154
+ ),
155
+ }),
156
+ )
157
+
158
+ // Handle CSS modules for the addon's dist files
159
+ config.plugins.push({
160
+ apply(compiler: any) {
161
+ compiler.hooks.normalModuleFactory.tap(
162
+ 'AddonCssModules',
163
+ (factory: any) => {
164
+ factory.hooks.afterResolve.tap(
165
+ 'AddonCssModules',
166
+ (resolveData: any) => {
167
+ const resource: string =
168
+ resolveData.createData?.resource ?? ''
169
+ if (
170
+ resource.endsWith('.module.css') &&
171
+ resource.includes(
172
+ 'storybook-addon-dependency-previews',
173
+ )
174
+ ) {
175
+ resolveData.createData.loaders = [
176
+ {
177
+ loader: cssModulesLoader,
178
+ options: {},
179
+ },
180
+ ]
181
+ }
182
+ },
183
+ )
184
+ },
185
+ )
186
+ },
187
+ })
188
+
189
+ return config
190
+ },
191
+ }
192
+ export default config
193
+ ```
194
+
104
195
  ### Bare minimum storybook story example
105
196
 
106
197
  Get a story ready so you have something to test with.
@@ -158,6 +249,34 @@ export const Primary: Story = {
158
249
  <Story name="Primary" />
159
250
  ```
160
251
 
252
+ #### Angular `.stories.ts` file example
253
+
254
+ ```ts
255
+ import type { Meta, StoryObj } from '@storybook/angular'
256
+ import type { StoryParameters } from 'storybook-addon-dependency-previews'
257
+ import { ComponentName } from './ComponentName.component'
258
+
259
+ const meta: Meta<ComponentName> = {
260
+ // You can use spaces here to make the title of the story page more human readable
261
+ title: 'Component Name',
262
+ component: ComponentName,
263
+ // autodocs tag is required
264
+ tags: ['autodocs'],
265
+ // The `__filePath` property must be applied to every story file
266
+ // for the addon to track dependencies effectively
267
+ // `import.meta.url` is a webpack feature available in Angular Storybook
268
+ parameters: {
269
+ __filePath: import.meta.url,
270
+ } satisfies StoryParameters,
271
+ }
272
+
273
+ export default meta
274
+
275
+ type Story = StoryObj<ComponentName>
276
+
277
+ export const Primary: Story = {}
278
+ ```
279
+
161
280
  ### package.json scripts
162
281
 
163
282
  You will need the following scripts in your `package.json` file:
@@ -187,6 +306,16 @@ Don't run any of these yet, you are not finished with the installation.
187
306
 
188
307
  `npm run sb:alt-port` (optional) will run storybook in watch mode and run it using a specific port number.
189
308
 
309
+ #### Angular projects
310
+
311
+ `@storybook/angular@10+` requires Storybook to be launched through the Angular CLI builder rather than the default `storybook dev` command. Pass the Angular CLI target as an argument to `--run-storybook`:
312
+
313
+ ```json
314
+ "sb": "sb-deps --watch --run-storybook \"ng run <project-name>:storybook\""
315
+ ```
316
+
317
+ Replace `<project-name>` with the name of your Angular project as defined in `angular.json`.
318
+
190
319
  ### Create the preview file
191
320
 
192
321
  First run `npm run sb:deps` to generate a fresh `dependency-previews.json` file in the `.storybook` folder.
@@ -236,6 +365,43 @@ const previewConfig: StorybookPreviewConfig = {
236
365
  export default previewConfig
237
366
  ```
238
367
 
368
+ #### Angular `preview.ts`
369
+
370
+ Angular uses webpack, so `import.meta.glob` and Vite-specific APIs are not available. Use the `__PROJECT_ROOT__` value injected via `DefinePlugin` in `main.ts` instead:
371
+
372
+ ```ts
373
+ import type { Preview } from '@storybook/angular'
374
+ import {
375
+ defaultPreviewParameters,
376
+ dependencyPreviewDecorators,
377
+ } from 'storybook-addon-dependency-previews'
378
+ import dependenciesJson from './dependency-previews.json'
379
+
380
+ declare const __PROJECT_ROOT__: string
381
+
382
+ const preview: Preview = {
383
+ parameters: {
384
+ ...defaultPreviewParameters,
385
+ dependencyPreviews: {
386
+ dependenciesJson,
387
+ // Replace this with the URL to your src folder in your git repository.
388
+ // This enables the addon to link to the source code of the component.
389
+ sourceRootUrl:
390
+ 'https://github.com/your-org/your-repo/blob/main/src',
391
+ // __PROJECT_ROOT__ is injected by DefinePlugin in main.ts.
392
+ // This allows Source File Path to open the component file
393
+ // directly in VS Code when running in a local host environment.
394
+ projectRootPath: __PROJECT_ROOT__,
395
+ },
396
+ },
397
+ decorators: [...dependencyPreviewDecorators],
398
+ }
399
+
400
+ export default preview
401
+ ```
402
+
403
+ > **Note:** Angular does not use `storyModules` (no `import.meta.glob`). The addon still generates the full dependency tree via the CLI — only the VS Code "open file" shortcut needs `projectRootPath`.
404
+
239
405
  ### Run it!
240
406
 
241
407
  You should be all set now.
@@ -245,3 +411,87 @@ Try running `npm run sb` to boot up storybook, leave it running as you work.
245
411
  As you create new component files, the component will be auto-scaffolded for you and a matching story file will be created for it automatically. The dependency previews json file will also be auto-updated.
246
412
 
247
413
  This workflow allows you to focus on what matters instead of having to waste time writing lots of boilerplate code every time you want to create a new component.
414
+
415
+ ## Configuration file (optional)
416
+
417
+ The `sb-deps` CLI can be customized via a `sb-deps.config.mjs` file in your project root (alongside `package.json`). Supported formats: `.mjs`, `.js`, or `.cjs`.
418
+
419
+ Use the `defineSbDepsConfig` helper from `storybook-addon-dependency-previews/config` for type safety and editor autocomplete:
420
+
421
+ ```js
422
+ // sb-deps.config.mjs
423
+ import { defineSbDepsConfig } from 'storybook-addon-dependency-previews/config'
424
+
425
+ export default defineSbDepsConfig({
426
+ // options go here
427
+ })
428
+ ```
429
+
430
+ ### `angularSelectorPrefix`
431
+
432
+ _(Angular only)_ The prefix prepended to Angular component selectors when auto-scaffolding new components.
433
+
434
+ **Default:** `'app-'`
435
+
436
+ | Value | Resulting selector |
437
+ | ------------------ | ------------------ |
438
+ | `'app-'` (default) | `app-button-atom` |
439
+ | `'my-'` | `my-button-atom` |
440
+ | `''` | `button-atom` |
441
+
442
+ ```js
443
+ // sb-deps.config.mjs
444
+ import { defineSbDepsConfig } from 'storybook-addon-dependency-previews/config'
445
+
446
+ export default defineSbDepsConfig({
447
+ angularSelectorPrefix: '', // no prefix
448
+ })
449
+ ```
450
+
451
+ ### `scaffold`
452
+
453
+ Override the templates used when `sb-deps` auto-scaffolds new component and story files. Each template function receives a context object with relevant variables and must return the full file content as a string.
454
+
455
+ ```js
456
+ // sb-deps.config.mjs
457
+ import { defineSbDepsConfig } from 'storybook-addon-dependency-previews/config'
458
+
459
+ export default defineSbDepsConfig({
460
+ scaffold: {
461
+ react: {
462
+ /** Customize the generated .tsx component file */
463
+ component: ({ componentName, propsName }) =>
464
+ `export interface ${propsName} {}
465
+
466
+ export function ${componentName}({}: ${propsName}) {
467
+ return <div>${componentName}</div>
468
+ }
469
+ `,
470
+ /** Customize the generated .stories.tsx file */
471
+ story: ({ componentName, propsName, title, tags, base }) => '...',
472
+ },
473
+ svelte: {
474
+ /** Customize the generated .svelte component file */
475
+ component: ({ componentName }) => '...',
476
+ /** Customize the generated .stories.svelte file */
477
+ story: ({ componentName, title, tags }) => '...',
478
+ },
479
+ angular: {
480
+ /** Customize the generated .component.ts file */
481
+ component: ({
482
+ componentName,
483
+ className,
484
+ selector,
485
+ base,
486
+ templateLocation,
487
+ }) => '...',
488
+ /** Customize the generated .component.html file (external templates only) */
489
+ componentHtml: ({ componentName }) => '...',
490
+ /** Customize the generated .stories.ts file */
491
+ story: ({ componentName, className, base, title, tags }) => '...',
492
+ },
493
+ },
494
+ })
495
+ ```
496
+
497
+ All scaffold options are optional — omit any key to keep the default template for that file type.
@@ -1,11 +1,17 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
1
2
  const require_addon_vitePlugin = require('./vitePlugin.cjs');
3
+ let module$1 = require("module");
4
+ module$1 = require_rolldown_runtime.__toESM(module$1);
2
5
 
3
6
  //#region src/addon/index.ts
7
+ const require$1 = (0, module$1.createRequire)(require("url").pathToFileURL(__filename).href);
8
+ const managerEntries = [require$1.resolve("../manager.mjs")];
4
9
  async function viteFinal(config) {
5
10
  config.plugins = [...config.plugins ?? [], require_addon_vitePlugin.sbDepsVitePlugin()];
6
11
  return config;
7
12
  }
8
13
 
9
14
  //#endregion
15
+ exports.managerEntries = managerEntries;
10
16
  exports.viteFinal = viteFinal;
11
17
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["sbDepsVitePlugin"],"sources":["../../src/addon/index.ts"],"sourcesContent":["import { sbDepsVitePlugin } from './vitePlugin'\n\nexport async function viteFinal(config: any) {\n\tconfig.plugins = [...(config.plugins ?? []), sbDepsVitePlugin()]\n\treturn config\n}\n"],"mappings":";;;AAEA,eAAsB,UAAU,QAAa;AAC5C,QAAO,UAAU,CAAC,GAAI,OAAO,WAAW,EAAE,EAAGA,2CAAkB,CAAC;AAChE,QAAO"}
1
+ {"version":3,"file":"index.cjs","names":["require","sbDepsVitePlugin"],"sources":["../../src/addon/index.ts"],"sourcesContent":["import { createRequire } from 'module'\nimport { sbDepsVitePlugin } from './vitePlugin'\n\nconst require = createRequire(import.meta.url)\n\nexport const managerEntries = [require.resolve('../manager.mjs')]\n\nexport async function viteFinal(config: any) {\n\tconfig.plugins = [...(config.plugins ?? []), sbDepsVitePlugin()]\n\treturn config\n}\n"],"mappings":";;;;;;AAGA,MAAMA,sFAAwC;AAE9C,MAAa,iBAAiB,CAACA,UAAQ,QAAQ,iBAAiB,CAAC;AAEjE,eAAsB,UAAU,QAAa;AAC5C,QAAO,UAAU,CAAC,GAAI,OAAO,WAAW,EAAE,EAAGC,2CAAkB,CAAC;AAChE,QAAO"}
@@ -1,5 +1,6 @@
1
1
  //#region src/addon/index.d.ts
2
+ declare const managerEntries: string[];
2
3
  declare function viteFinal(config: any): Promise<any>;
3
4
  //#endregion
4
- export { viteFinal };
5
+ export { managerEntries, viteFinal };
5
6
  //# sourceMappingURL=index.d.cts.map
@@ -1,5 +1,6 @@
1
1
  //#region src/addon/index.d.ts
2
+ declare const managerEntries: string[];
2
3
  declare function viteFinal(config: any): Promise<any>;
3
4
  //#endregion
4
- export { viteFinal };
5
+ export { managerEntries, viteFinal };
5
6
  //# sourceMappingURL=index.d.mts.map
@@ -1,11 +1,14 @@
1
1
  import { sbDepsVitePlugin } from "./vitePlugin.mjs";
2
+ import { createRequire } from "module";
2
3
 
3
4
  //#region src/addon/index.ts
5
+ const require = createRequire(import.meta.url);
6
+ const managerEntries = [require.resolve("../manager.mjs")];
4
7
  async function viteFinal(config) {
5
8
  config.plugins = [...config.plugins ?? [], sbDepsVitePlugin()];
6
9
  return config;
7
10
  }
8
11
 
9
12
  //#endregion
10
- export { viteFinal };
13
+ export { managerEntries, viteFinal };
11
14
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../../src/addon/index.ts"],"sourcesContent":["import { sbDepsVitePlugin } from './vitePlugin'\n\nexport async function viteFinal(config: any) {\n\tconfig.plugins = [...(config.plugins ?? []), sbDepsVitePlugin()]\n\treturn config\n}\n"],"mappings":";;;AAEA,eAAsB,UAAU,QAAa;AAC5C,QAAO,UAAU,CAAC,GAAI,OAAO,WAAW,EAAE,EAAG,kBAAkB,CAAC;AAChE,QAAO"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../src/addon/index.ts"],"sourcesContent":["import { createRequire } from 'module'\nimport { sbDepsVitePlugin } from './vitePlugin'\n\nconst require = createRequire(import.meta.url)\n\nexport const managerEntries = [require.resolve('../manager.mjs')]\n\nexport async function viteFinal(config: any) {\n\tconfig.plugins = [...(config.plugins ?? []), sbDepsVitePlugin()]\n\treturn config\n}\n"],"mappings":";;;;AAGA,MAAM,UAAU,cAAc,OAAO,KAAK,IAAI;AAE9C,MAAa,iBAAiB,CAAC,QAAQ,QAAQ,iBAAiB,CAAC;AAEjE,eAAsB,UAAU,QAAa;AAC5C,QAAO,UAAU,CAAC,GAAI,OAAO,WAAW,EAAE,EAAG,kBAAkB,CAAC;AAChE,QAAO"}
@@ -1,7 +1,7 @@
1
- import * as react_jsx_runtime0 from "react/jsx-runtime";
1
+ import * as react_jsx_runtime1 from "react/jsx-runtime";
2
2
 
3
3
  //#region src/blocks/DependencyPreviews.d.ts
4
- declare function DependencyPreviews(): react_jsx_runtime0.JSX.Element;
4
+ declare function DependencyPreviews(): react_jsx_runtime1.JSX.Element;
5
5
  //#endregion
6
6
  export { DependencyPreviews };
7
7
  //# sourceMappingURL=DependencyPreviews.d.cts.map
@@ -1,7 +1,7 @@
1
- import * as react_jsx_runtime0 from "react/jsx-runtime";
1
+ import * as react_jsx_runtime1 from "react/jsx-runtime";
2
2
 
3
3
  //#region src/blocks/DependencyPreviews.d.ts
4
- declare function DependencyPreviews(): react_jsx_runtime0.JSX.Element;
4
+ declare function DependencyPreviews(): react_jsx_runtime1.JSX.Element;
5
5
  //#endregion
6
6
  export { DependencyPreviews };
7
7
  //# sourceMappingURL=DependencyPreviews.d.mts.map