@storybook/react 10.1.0-alpha.6 → 10.1.0-alpha.7

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.
package/dist/index.d.ts CHANGED
@@ -531,4 +531,4 @@ interface ReactStory<T extends ReactTypes, TInput extends StoryAnnotations<T, T[
531
531
  Component: ComponentType<Partial<T['args']>>;
532
532
  }
533
533
 
534
- export { type AddMocks, type Decorator, INTERNAL_DEFAULT_PROJECT_ANNOTATIONS, type Loader, type Meta, type Preview, type ReactPreview, type ReactRenderer, type ReactStory, type ReactTypes, type StoryContext, type StoryFn, type StoryObj, __definePreview, composeStories, composeStory, setProjectAnnotations };
534
+ export { type AddMocks, type Decorator, INTERNAL_DEFAULT_PROJECT_ANNOTATIONS, type Loader, type Meta, type Preview, type ReactMeta, type ReactPreview, type ReactRenderer, type ReactStory, type ReactTypes, type StoryContext, type StoryFn, type StoryObj, __definePreview, composeStories, composeStory, setProjectAnnotations };
package/dist/preset.js CHANGED
@@ -1,10 +1,10 @@
1
- import CJS_COMPAT_NODE_URL_2o033vi7i5u from 'node:url';
2
- import CJS_COMPAT_NODE_PATH_2o033vi7i5u from 'node:path';
3
- import CJS_COMPAT_NODE_MODULE_2o033vi7i5u from "node:module";
1
+ import CJS_COMPAT_NODE_URL_121w8xeb4kr from 'node:url';
2
+ import CJS_COMPAT_NODE_PATH_121w8xeb4kr from 'node:path';
3
+ import CJS_COMPAT_NODE_MODULE_121w8xeb4kr from "node:module";
4
4
 
5
- var __filename = CJS_COMPAT_NODE_URL_2o033vi7i5u.fileURLToPath(import.meta.url);
6
- var __dirname = CJS_COMPAT_NODE_PATH_2o033vi7i5u.dirname(__filename);
7
- var require = CJS_COMPAT_NODE_MODULE_2o033vi7i5u.createRequire(import.meta.url);
5
+ var __filename = CJS_COMPAT_NODE_URL_121w8xeb4kr.fileURLToPath(import.meta.url);
6
+ var __dirname = CJS_COMPAT_NODE_PATH_121w8xeb4kr.dirname(__filename);
7
+ var require = CJS_COMPAT_NODE_MODULE_121w8xeb4kr.createRequire(import.meta.url);
8
8
 
9
9
  // ------------------------------------------------------------
10
10
  // end of CJS compatibility banner, injected by Storybook's esbuild configuration
@@ -42,6 +42,56 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
42
42
  mod
43
43
  ));
44
44
 
45
+ // ../../node_modules/ts-dedent/dist/index.js
46
+ var require_dist = __commonJS({
47
+ "../../node_modules/ts-dedent/dist/index.js"(exports) {
48
+ "use strict";
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.dedent = void 0;
51
+ function dedent2(templ) {
52
+ var values = [];
53
+ for (var _i = 1; _i < arguments.length; _i++) {
54
+ values[_i - 1] = arguments[_i];
55
+ }
56
+ var strings = Array.from(typeof templ === "string" ? [templ] : templ);
57
+ strings[strings.length - 1] = strings[strings.length - 1].replace(/\r?\n([\t ]*)$/, "");
58
+ var indentLengths = strings.reduce(function(arr, str) {
59
+ var matches = str.match(/\n([\t ]+|(?!\s).)/g);
60
+ if (matches) {
61
+ return arr.concat(matches.map(function(match) {
62
+ var _a, _b;
63
+ return (_b = (_a = match.match(/[\t ]/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
64
+ }));
65
+ }
66
+ return arr;
67
+ }, []);
68
+ if (indentLengths.length) {
69
+ var pattern_1 = new RegExp("\n[ ]{" + Math.min.apply(Math, indentLengths) + "}", "g");
70
+ strings = strings.map(function(str) {
71
+ return str.replace(pattern_1, "\n");
72
+ });
73
+ }
74
+ strings[0] = strings[0].replace(/^\r?\n/, "");
75
+ var string = strings[0];
76
+ values.forEach(function(value, i) {
77
+ var endentations = string.match(/(?:^|\n)( *)$/);
78
+ var endentation = endentations ? endentations[1] : "";
79
+ var indentedValue = value;
80
+ if (typeof value === "string" && value.includes("\n")) {
81
+ indentedValue = String(value).split("\n").map(function(str, i2) {
82
+ return i2 === 0 ? str : "" + endentation + str;
83
+ }).join("\n");
84
+ }
85
+ string += indentedValue + strings[i + 1];
86
+ });
87
+ return string;
88
+ }
89
+ __name(dedent2, "dedent");
90
+ exports.dedent = dedent2;
91
+ exports.default = dedent2;
92
+ }
93
+ });
94
+
45
95
  // ../../node_modules/tsconfig-paths/lib/filesystem.js
46
96
  var require_filesystem = __commonJS({
47
97
  "../../node_modules/tsconfig-paths/lib/filesystem.js"(exports) {
@@ -3965,7 +4015,7 @@ var resolvePackageDir = /* @__PURE__ */ __name((pkg, parent) => {
3965
4015
  import { recast as recast2 } from "storybook/internal/babel";
3966
4016
  import { combineTags } from "storybook/internal/csf";
3967
4017
  import { extractDescription, loadCsf } from "storybook/internal/csf-tools";
3968
- import { logger as logger3 } from "storybook/internal/node-logger";
4018
+ import { logger as logger4 } from "storybook/internal/node-logger";
3969
4019
 
3970
4020
  // src/componentManifest/generateCodeSnippet.ts
3971
4021
  import { types as t } from "storybook/internal/babel";
@@ -4138,11 +4188,20 @@ function getCodeSnippet(csf, storyName, componentName) {
4138
4188
  const storyProps = normalizedPath.isObjectExpression() ? normalizedPath.get("properties").filter((p) => p.isObjectProperty()) : [];
4139
4189
  const metaPath = pathForNode(csf._file.path, metaObj);
4140
4190
  const metaProps = metaPath?.isObjectExpression() ? metaPath.get("properties").filter((p) => p.isObjectProperty()) : [];
4141
- const getRenderPath = /* @__PURE__ */ __name((object) => object.filter((p) => keyOf(p.node) === "render").map((p) => p.get("value")).find(
4142
- (v) => v.isArrowFunctionExpression() || v.isFunctionExpression()
4143
- ), "getRenderPath");
4144
- const renderPath = getRenderPath(storyProps);
4191
+ const getRenderPath = /* @__PURE__ */ __name((object) => {
4192
+ const renderPath2 = object.find((p) => keyOf(p.node) === "render")?.get("value");
4193
+ if (renderPath2?.isIdentifier()) {
4194
+ componentName = renderPath2.node.name;
4195
+ }
4196
+ if (renderPath2 && !(renderPath2.isArrowFunctionExpression() || renderPath2.isFunctionExpression())) {
4197
+ throw renderPath2.buildCodeFrameError(
4198
+ "Expected render to be an arrow function or function expression"
4199
+ );
4200
+ }
4201
+ return renderPath2;
4202
+ }, "getRenderPath");
4145
4203
  const metaRenderPath = getRenderPath(metaProps);
4204
+ const renderPath = getRenderPath(storyProps);
4146
4205
  storyFn ??= renderPath ?? metaRenderPath;
4147
4206
  const metaArgs = metaArgsRecord(metaObj ?? null);
4148
4207
  const storyArgsPath = storyProps.filter((p) => keyOf(p.node) === "args").map((p) => p.get("value")).find((v) => v.isObjectExpression());
@@ -4461,13 +4520,15 @@ __name(pathForNode, "pathForNode");
4461
4520
  // src/componentManifest/getComponentImports.ts
4462
4521
  import { dirname as dirname4 } from "node:path";
4463
4522
  import { babelParse as babelParse2, recast, types as t3 } from "storybook/internal/babel";
4464
- import { logger as logger2 } from "storybook/internal/node-logger";
4523
+ import { logger as logger3 } from "storybook/internal/node-logger";
4465
4524
 
4466
4525
  // src/componentManifest/reactDocgen.ts
4467
4526
  import { existsSync as existsSync2 } from "node:fs";
4468
4527
  import { dirname as dirname3, sep as sep2 } from "node:path";
4469
4528
  import { babelParse, types as t2 } from "storybook/internal/babel";
4470
4529
  import { getProjectRoot, supportedExtensions as supportedExtensions2 } from "storybook/internal/common";
4530
+ import { logger as logger2 } from "storybook/internal/node-logger";
4531
+ var import_ts_dedent = __toESM(require_dist(), 1);
4471
4532
  var TsconfigPaths = __toESM(require_lib2(), 1);
4472
4533
  import {
4473
4534
  builtinHandlers as docgenHandlers,
@@ -5061,6 +5122,9 @@ var defaultHandlers = Object.values(docgenHandlers).map((handler) => handler);
5061
5122
  var defaultResolver = new docgenResolver.FindExportedDefinitionsResolver();
5062
5123
  var handlers = [...defaultHandlers, actualNameHandler_default, exportNameHandler_default];
5063
5124
  function getMatchingDocgen(docgens, component) {
5125
+ if (docgens.length === 0) {
5126
+ return;
5127
+ }
5064
5128
  if (docgens.length === 1) {
5065
5129
  return docgens[0];
5066
5130
  }
@@ -5110,93 +5174,143 @@ var parseWithReactDocgen = cached(
5110
5174
  );
5111
5175
  var getExportPaths = cached(
5112
5176
  (code, filePath) => {
5113
- const ast = (() => {
5114
- try {
5115
- return babelParse(code);
5116
- } catch (_) {
5117
- return void 0;
5118
- }
5119
- })();
5120
- if (!ast) {
5177
+ let ast;
5178
+ try {
5179
+ ast = babelParse(code);
5180
+ } catch (_) {
5121
5181
  return [];
5122
5182
  }
5123
5183
  const basedir = dirname3(filePath);
5124
5184
  const body = ast.program.body;
5125
5185
  return body.flatMap(
5126
- (n) => t2.isExportAllDeclaration(n) ? [n.source.value] : t2.isExportNamedDeclaration(n) && !!n.source && !n.declaration ? [n.source.value] : []
5127
- ).map((s) => matchPath(s, basedir)).map((s) => {
5186
+ (statement) => t2.isExportAllDeclaration(statement) ? [statement.source.value] : t2.isExportNamedDeclaration(statement) && !!statement.source && !statement.declaration ? [statement.source.value] : []
5187
+ ).map((id) => matchPath(id, basedir)).flatMap((id) => {
5128
5188
  try {
5129
- return cachedResolveImport(s, { basedir });
5130
- } catch {
5131
- return void 0;
5189
+ return [cachedResolveImport(id, { basedir })];
5190
+ } catch (e) {
5191
+ logger2.debug(e);
5192
+ return [];
5132
5193
  }
5133
- }).filter((p) => !!p && !p.includes("node_modules"));
5194
+ });
5134
5195
  },
5135
5196
  { name: "getExportPaths" }
5136
5197
  );
5137
5198
  var gatherDocgensForPath = cached(
5138
- (filePath, depth) => {
5139
- if (depth > 5 || filePath.includes("node_modules")) {
5140
- return { docgens: [], analyzed: [] };
5199
+ (path2, depth) => {
5200
+ if (path2.includes("node_modules")) {
5201
+ return {
5202
+ docgens: [],
5203
+ errors: [
5204
+ {
5205
+ path: path2,
5206
+ code: "/* File in node_modules */",
5207
+ name: "Component file in node_modules",
5208
+ message: import_ts_dedent.dedent`
5209
+ Component files in node_modules are not supported.
5210
+ The distributed files in node_modules usually don't contain the necessary comments or types needed to analyze component information.
5211
+ Configure TypeScript path aliases to map your package name to the source file instead.
5212
+
5213
+ Example (tsconfig.json):
5214
+ {
5215
+ "compilerOptions": {
5216
+ "baseUrl": ".",
5217
+ "paths": {
5218
+ "@design-system/button": ["src/components/Button.tsx"],
5219
+ "@design-system/*": ["src/components/*"]
5220
+ }
5221
+ }
5222
+ }
5223
+
5224
+ Then import using:
5225
+ import { Button } from '@design-system/button'
5226
+
5227
+ Storybook resolves tsconfig paths automatically.
5228
+ `
5229
+ }
5230
+ ]
5231
+ };
5141
5232
  }
5142
5233
  let code;
5143
5234
  try {
5144
- code = cachedReadFileSync(filePath, "utf-8");
5235
+ code = cachedReadFileSync(path2, "utf-8");
5145
5236
  } catch {
5237
+ return {
5238
+ docgens: [],
5239
+ errors: [
5240
+ {
5241
+ path: path2,
5242
+ code: "/* File not found or unreadable */",
5243
+ name: "Component file could not be read",
5244
+ message: `Could not read the component file located at "${path2}".
5245
+ Prefer relative imports if possible.`
5246
+ }
5247
+ ]
5248
+ };
5146
5249
  }
5147
- if (!code) {
5148
- return { docgens: [], analyzed: [{ path: filePath, code: "/* File not found */" }] };
5250
+ if (depth > 5) {
5251
+ return {
5252
+ docgens: [],
5253
+ errors: [
5254
+ {
5255
+ path: path2,
5256
+ code,
5257
+ name: "Max re-export depth exceeded",
5258
+ message: import_ts_dedent.dedent`
5259
+ Traversal stopped after 5 steps while following re-exports starting from this file.
5260
+ This usually indicates a deep or circular re-export chain. Try one of the following:
5261
+ - Import the component file directly (e.g., src/components/Button.tsx),
5262
+ - Reduce the number of re-export hops.
5263
+ `
5264
+ }
5265
+ ]
5266
+ };
5149
5267
  }
5150
- const reexportResults = getExportPaths(code, filePath).map(
5151
- (p) => gatherDocgensForPath(p, depth + 1)
5152
- );
5153
- const fromReexports = reexportResults.flatMap((r) => r.docgens);
5154
- const analyzedChildren = reexportResults.flatMap((r) => r.analyzed);
5155
- let locals;
5268
+ const exportPaths = getExportPaths(code, path2).map((p) => gatherDocgensForPath(p, depth + 1));
5269
+ const docgens = exportPaths.flatMap((r) => r.docgens);
5270
+ const errors = exportPaths.flatMap((r) => r.errors);
5156
5271
  try {
5157
- locals = parseWithReactDocgen(code, filePath);
5158
- } catch {
5159
- locals = [];
5272
+ return {
5273
+ docgens: [...parseWithReactDocgen(code, path2), ...docgens],
5274
+ errors
5275
+ };
5276
+ } catch (e) {
5277
+ const message = e instanceof Error ? e.message : String(e);
5278
+ return {
5279
+ docgens,
5280
+ errors: [
5281
+ {
5282
+ path: path2,
5283
+ code,
5284
+ name: "No component definition found",
5285
+ message: import_ts_dedent.dedent`
5286
+ ${message}
5287
+ You can debug your component file in this playground: https://react-docgen.dev/playground
5288
+ `
5289
+ },
5290
+ ...errors
5291
+ ]
5292
+ };
5160
5293
  }
5161
- return {
5162
- docgens: [...locals, ...fromReexports],
5163
- analyzed: [{ path: filePath, code }, ...analyzedChildren]
5164
- };
5165
5294
  },
5166
5295
  { name: "gatherDocgensWithTrace", key: /* @__PURE__ */ __name((filePath) => filePath, "key") }
5167
5296
  );
5168
5297
  var getReactDocgen = cached(
5169
5298
  (path2, component) => {
5170
- if (path2.includes("node_modules")) {
5171
- return {
5172
- type: "error",
5173
- error: {
5174
- name: "Component file in node_modules",
5175
- message: `Component files in node_modules are not supported. Please import your component file directly.`
5176
- }
5177
- };
5178
- }
5179
- const docgenWithInfo = gatherDocgensForPath(path2, 0);
5180
- const docgens = docgenWithInfo.docgens;
5181
- const noCompDefError = {
5182
- type: "error",
5183
- error: {
5184
- name: "No component definition found",
5185
- message: `Could not find a component definition.
5186
- Prefer relative imports if possible.
5187
- Avoid pointing to transpiled files.
5188
- You can debug your component file in this playground: https://react-docgen.dev/playground
5189
-
5190
- ` + docgenWithInfo.analyzed.map(({ path: path3, code }) => `File: ${path3}
5191
- ${code}`).join("\n")
5192
- }
5193
- };
5194
- if (!docgens || docgens.length === 0) {
5195
- return noCompDefError;
5196
- }
5299
+ const { docgens, errors } = gatherDocgensForPath(path2, 0);
5197
5300
  const docgen = getMatchingDocgen(docgens, component);
5198
5301
  if (!docgen) {
5199
- return noCompDefError;
5302
+ const error = {
5303
+ name: errors.at(-1)?.name ?? "No component definition found",
5304
+ message: errors.map(
5305
+ (e) => import_ts_dedent.dedent`
5306
+ File: ${e.path}
5307
+ Error:
5308
+ ${e.message}
5309
+ Code:
5310
+ ${e.code}`
5311
+ ).join("\n\n")
5312
+ };
5313
+ return { type: "error", error };
5200
5314
  }
5201
5315
  return { type: "success", data: docgen };
5202
5316
  },
@@ -5348,7 +5462,7 @@ var getComponents = /* @__PURE__ */ __name(({
5348
5462
  });
5349
5463
  }
5350
5464
  } catch (e) {
5351
- logger2.error(e);
5465
+ logger3.debug(e);
5352
5466
  }
5353
5467
  if (path2) {
5354
5468
  const reactDocgen = getReactDocgen(path2, component);
@@ -5565,7 +5679,7 @@ var componentManifestGenerator = /* @__PURE__ */ __name(async () => {
5565
5679
  invalidateCache();
5566
5680
  const startIndex = performance.now();
5567
5681
  const index = await storyIndexGenerator.getIndex();
5568
- logger3.verbose(`Story index generation took ${performance.now() - startIndex}ms`);
5682
+ logger4.verbose(`Story index generation took ${performance.now() - startIndex}ms`);
5569
5683
  const startPerformance = performance.now();
5570
5684
  const groupByComponentId = groupBy(
5571
5685
  Object.values(index.entries).filter((entry) => entry.type === "story").filter((entry) => entry.subtype === "story"),
@@ -5638,18 +5752,21 @@ var componentManifestGenerator = /* @__PURE__ */ __name(async () => {
5638
5752
  jsDocTags: {}
5639
5753
  };
5640
5754
  if (!component?.reactDocgen) {
5641
- const error2 = !component ? {
5642
- name: "No meta.component specified",
5643
- message: "Specify meta.component for the component to be included in the manifest."
5755
+ const error2 = !csf._meta?.component ? {
5756
+ name: "No component found",
5757
+ message: "We could not detect the component from your story file. Specify meta.component."
5644
5758
  } : {
5645
5759
  name: "No component import found",
5646
- message: `No component file found for the "${component.componentName}" component.`
5760
+ message: `No component file found for the "${csf.meta.component}" component.`
5647
5761
  };
5648
5762
  return {
5649
5763
  ...base,
5650
5764
  error: {
5651
5765
  name: error2.name,
5652
- message: csf._metaStatementPath?.buildCodeFrameError(error2.message).message ?? error2.message
5766
+ message: (csf._metaStatementPath?.buildCodeFrameError(error2.message).message ?? error2.message) + `
5767
+
5768
+ ${entry.importPath}:
5769
+ ${storyFile}`
5653
5770
  }
5654
5771
  };
5655
5772
  }
@@ -5668,7 +5785,7 @@ var componentManifestGenerator = /* @__PURE__ */ __name(async () => {
5668
5785
  error
5669
5786
  };
5670
5787
  });
5671
- logger3.verbose(`Component manifest generation took ${performance.now() - startPerformance}ms`);
5788
+ logger4.verbose(`Component manifest generation took ${performance.now() - startPerformance}ms`);
5672
5789
  return {
5673
5790
  v: 0,
5674
5791
  components: Object.fromEntries(
package/dist/preview.d.ts CHANGED
@@ -422,4 +422,4 @@ interface ReactStory<T extends ReactTypes, TInput extends StoryAnnotations<T, T[
422
422
  Component: ComponentType<Partial<T['args']>>;
423
423
  }
424
424
 
425
- export { type ReactPreview, type ReactStory, __definePreview };
425
+ export { type ReactMeta, type ReactPreview, type ReactStory, __definePreview };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/react",
3
- "version": "10.1.0-alpha.6",
3
+ "version": "10.1.0-alpha.7",
4
4
  "description": "Storybook React renderer",
5
5
  "keywords": [
6
6
  "storybook"
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@storybook/global": "^5.0.0",
57
- "@storybook/react-dom-shim": "10.1.0-alpha.6",
57
+ "@storybook/react-dom-shim": "10.1.0-alpha.7",
58
58
  "react-docgen": "^8.0.2"
59
59
  },
60
60
  "devDependencies": {
@@ -81,7 +81,7 @@
81
81
  "peerDependencies": {
82
82
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
83
83
  "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
84
- "storybook": "^10.1.0-alpha.6",
84
+ "storybook": "^10.1.0-alpha.7",
85
85
  "typescript": ">= 4.9.x"
86
86
  },
87
87
  "peerDependenciesMeta": {