@storybook/react-native 6.5.5-alpha.1 → 6.5.5

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.
@@ -24,7 +24,10 @@ function executeLoadable(loadable) {
24
24
  req.keys().forEach(function (filename) {
25
25
  try {
26
26
  var fileExports = req(filename);
27
- exportsMap.set(typeof req.resolve === 'function' ? req.resolve(filename) : filename, fileExports);
27
+ exportsMap.set(
28
+ // NOTE context.resolve is not yet implemented for metro
29
+ // typeof req.resolve === 'function' ? req.resolve(filename) : filename,
30
+ filename, fileExports);
28
31
  }
29
32
  catch (error) {
30
33
  var errorString = error.message && error.stack ? "".concat(error.message, "\n ").concat(error.stack) : error.toString();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/react-native",
3
- "version": "6.5.5-alpha.1",
3
+ "version": "6.5.5",
4
4
  "description": "A better way to develop React Native Components for your app",
5
5
  "keywords": [
6
6
  "react",
@@ -63,7 +63,7 @@
63
63
  "@storybook/core-events": "^6.5.14",
64
64
  "@storybook/csf": "0.0.2--canary.4566f4d.1",
65
65
  "@storybook/preview-web": "^6.5.14",
66
- "@storybook/react-native-theming": "^6.5.5-alpha.1",
66
+ "@storybook/react-native-theming": "^6.5.5",
67
67
  "chokidar": "^3.5.1",
68
68
  "commander": "^8.2.0",
69
69
  "deepmerge": "^4.3.0",
@@ -78,7 +78,8 @@
78
78
  "@types/react": "~18.2.14",
79
79
  "babel-jest": "^29.4.3",
80
80
  "jest": "^29.4.3",
81
- "react-test-renderer": "18.2.0"
81
+ "react-test-renderer": "18.2.0",
82
+ "typescript": "^5.1.3"
82
83
  },
83
84
  "peerDependencies": {
84
85
  "@react-native-async-storage/async-storage": ">=1",
@@ -92,5 +93,5 @@
92
93
  "publishConfig": {
93
94
  "access": "public"
94
95
  },
95
- "gitHead": "3a03b09c728691717dee406e91439227d3483a7b"
96
+ "gitHead": "ad15a9640397fbaf3536c1b8469c7b13b70676d8"
96
97
  }
@@ -41,11 +41,14 @@ import "@storybook/addon-ondevice-actions/register";
41
41
  } catch{}
42
42
 
43
43
 
44
+
44
45
  const getStories=() => {
45
- return {"./scripts/mocks/file-extensions/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
46
+ return {"./scripts/mocks/file-extensions/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
46
47
  }
47
48
 
48
49
  configure(getStories, module, false)
50
+
51
+
49
52
  "
50
53
  `;
51
54
 
@@ -90,11 +93,14 @@ import "@storybook/addon-ondevice-actions/register";
90
93
  } catch{}
91
94
 
92
95
 
96
+
93
97
  const getStories=() => {
94
- return {"./scripts/mocks/configuration-objects/components/FakeStory.stories.tsx": require("./components/FakeStory.stories.tsx")};
98
+ return {"./scripts/mocks/configuration-objects/components/FakeStory.stories.tsx": require("./components/FakeStory.stories.tsx")};
95
99
  }
96
100
 
97
101
  configure(getStories, module, false)
102
+
103
+
98
104
  "
99
105
  `;
100
106
 
@@ -139,11 +145,14 @@ import "@storybook/addon-ondevice-actions/register";
139
145
  } catch{}
140
146
 
141
147
 
148
+
142
149
  const getStories=() => {
143
- return {"./scripts/mocks/exclude-config-files/include-components/FakeStory.stories.tsx": require("./include-components/FakeStory.stories.tsx")};
150
+ return {"./scripts/mocks/exclude-config-files/include-components/FakeStory.stories.tsx": require("./include-components/FakeStory.stories.tsx")};
144
151
  }
145
152
 
146
153
  configure(getStories, module, false)
154
+
155
+
147
156
  "
148
157
  `;
149
158
 
@@ -188,11 +197,14 @@ import "@storybook/addon-ondevice-actions/register";
188
197
  } catch{}
189
198
 
190
199
 
200
+
191
201
  const getStories=() => {
192
- return {"./scripts/mocks/all-config-files/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
202
+ return {"./scripts/mocks/all-config-files/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
193
203
  }
194
204
 
195
205
  configure(getStories, module, false)
206
+
207
+
196
208
  "
197
209
  `;
198
210
 
@@ -219,11 +231,14 @@ import "@storybook/addon-ondevice-actions/register";
219
231
  } catch{}
220
232
 
221
233
 
234
+
222
235
  const getStories=() => {
223
- return {"./scripts/mocks/no-preview/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
236
+ return {"./scripts/mocks/no-preview/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
224
237
  }
225
238
 
226
239
  configure(getStories, module, false)
240
+
241
+
227
242
  "
228
243
  `;
229
244
 
@@ -243,10 +258,13 @@ exports[`loader writeRequires when there is no story glob or addons writes no st
243
258
 
244
259
 
245
260
 
261
+
246
262
  const getStories=() => {
247
- return {};
263
+ return {};
248
264
  }
249
265
 
250
266
  configure(getStories, module, false)
267
+
268
+
251
269
  "
252
270
  `;
@@ -8,7 +8,11 @@ function getArguments() {
8
8
  'The path to your config folder relative to your project-dir',
9
9
  './.storybook'
10
10
  )
11
- .option('-a, --absolute', 'Use absolute paths for story imports');
11
+ .option('-a, --absolute', 'Use absolute paths for story imports')
12
+ .option(
13
+ '-unstable_useRequireContext, --unstable_useRequireContext',
14
+ 'Use experimental require.context to load stories'
15
+ );
12
16
 
13
17
  program.parse();
14
18
 
package/scripts/loader.js CHANGED
@@ -2,7 +2,7 @@ const path = require('path');
2
2
  const fs = require('fs');
3
3
  const glob = require('glob');
4
4
  const prettier = require('prettier');
5
- const normalizeStories = require('@storybook/core-common').normalizeStories;
5
+ const { normalizeStories, toRequireContext } = require('@storybook/core-common');
6
6
 
7
7
  const cwd = process.cwd();
8
8
  const supportedExtensions = ['js', 'jsx', 'ts', 'tsx', 'cjs', 'mjs'];
@@ -45,14 +45,17 @@ function normalizeExcludePaths(paths) {
45
45
 
46
46
  function requireUncached(module) {
47
47
  delete require.cache[require.resolve(module)];
48
+
48
49
  return require(module);
49
50
  }
50
51
 
51
52
  function getMain({ configPath }) {
52
53
  const fileExtension = getFilePathExtension({ configPath }, 'main');
54
+
53
55
  if (fileExtension === null) {
54
56
  throw new Error('main config file not found');
55
57
  }
58
+
56
59
  const mainPath = path.resolve(cwd, configPath, `main.${fileExtension}`);
57
60
 
58
61
  return requireUncached(mainPath);
@@ -61,10 +64,12 @@ function getMain({ configPath }) {
61
64
  function getFilePathExtension({ configPath }, fileName) {
62
65
  for (const ext of supportedExtensions) {
63
66
  const filePath = path.resolve(cwd, configPath, `${fileName}.${ext}`);
67
+
64
68
  if (fs.existsSync(filePath)) {
65
69
  return ext;
66
70
  }
67
71
  }
72
+
68
73
  return null;
69
74
  }
70
75
 
@@ -76,13 +81,17 @@ function ensureRelativePathHasDot(relativePath) {
76
81
  return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
77
82
  }
78
83
 
79
- function writeRequires({ configPath, absolute = false }) {
84
+ function writeRequires({ configPath, absolute = false, unstable_useRequireContext = false }) {
80
85
  const storybookRequiresLocation = path.resolve(cwd, configPath, 'storybook.requires.js');
81
86
 
82
87
  const mainImport = getMain({ configPath });
88
+
83
89
  const main = mainImport.default ?? mainImport;
90
+
84
91
  const reactNativeOptions = main.reactNativeOptions;
92
+
85
93
  const excludePaths = reactNativeOptions && reactNativeOptions.excludePaths;
94
+
86
95
  const normalizedExcludePaths = normalizeExcludePaths(excludePaths);
87
96
 
88
97
  const storiesSpecifiers = normalizeStories(main.stories, {
@@ -90,32 +99,61 @@ function writeRequires({ configPath, absolute = false }) {
90
99
  workingDir: cwd,
91
100
  });
92
101
 
93
- const storyRequires = storiesSpecifiers.reduce((acc, specifier) => {
94
- const paths = glob
95
- .sync(specifier.files, {
96
- cwd: path.resolve(cwd, specifier.directory),
97
- absolute,
98
- // default to always ignore (exclude) anything in node_modules
99
- ignore: normalizedExcludePaths !== undefined ? normalizedExcludePaths : ['**/node_modules'],
100
- })
101
- .map((storyPath) => {
102
- const pathWithDirectory = path.join(specifier.directory, storyPath);
103
- const requirePath = absolute
104
- ? storyPath
105
- : ensureRelativePathHasDot(path.relative(configPath, pathWithDirectory));
106
-
107
- const absolutePath = absolute ? requirePath : path.resolve(configPath, requirePath);
108
- const pathRelativeToCwd = path.relative(cwd, absolutePath);
109
-
110
- const normalizePathForWindows = (str) =>
111
- path.sep === '\\' ? str.replace(/\\/g, '/') : str;
112
-
113
- return `"./${normalizePathForWindows(
114
- pathRelativeToCwd
115
- )}": require("${normalizePathForWindows(requirePath)}")`;
116
- });
117
- return [...acc, ...paths];
118
- }, []);
102
+ let configure = '';
103
+
104
+ if (unstable_useRequireContext) {
105
+ const contexts = storiesSpecifiers.map((specifier) => {
106
+ const { path: p, recursive: r, match: m } = toRequireContext(specifier);
107
+ // TODO remove this dot 👇 and find actual solution
108
+ return `require.context('.${p}', ${r}, ${m})`;
109
+ });
110
+
111
+ configure = `
112
+ const stories = [${contexts.join(',')}];
113
+
114
+ configure(stories, module, false)
115
+ `;
116
+ } else {
117
+ const storyRequires = storiesSpecifiers.reduce((acc, specifier) => {
118
+ const paths = glob
119
+ .sync(specifier.files, {
120
+ cwd: path.resolve(cwd, specifier.directory),
121
+ absolute,
122
+ // default to always ignore (exclude) anything in node_modules
123
+ ignore:
124
+ normalizedExcludePaths !== undefined ? normalizedExcludePaths : ['**/node_modules'],
125
+ })
126
+ .map((storyPath) => {
127
+ const pathWithDirectory = path.join(specifier.directory, storyPath);
128
+
129
+ const requirePath = absolute
130
+ ? storyPath
131
+ : ensureRelativePathHasDot(path.relative(configPath, pathWithDirectory));
132
+
133
+ const absolutePath = absolute ? requirePath : path.resolve(configPath, requirePath);
134
+
135
+ const pathRelativeToCwd = path.relative(cwd, absolutePath);
136
+
137
+ const normalizePathForWindows = (str) =>
138
+ path.sep === '\\' ? str.replace(/\\/g, '/') : str;
139
+
140
+ return `"./${normalizePathForWindows(
141
+ pathRelativeToCwd
142
+ )}": require("${normalizePathForWindows(requirePath)}")`;
143
+ });
144
+ return [...acc, ...paths];
145
+ }, []);
146
+
147
+ const path_obj_str = `{${storyRequires.join(',')}}`;
148
+
149
+ configure = `
150
+ const getStories=() => {
151
+ return ${path_obj_str};
152
+ }
153
+
154
+ configure(getStories, module, false)
155
+ `;
156
+ }
119
157
 
120
158
  fs.writeFileSync(storybookRequiresLocation, '');
121
159
 
@@ -123,14 +161,14 @@ function writeRequires({ configPath, absolute = false }) {
123
161
 
124
162
  let previewJs = previewExists ? previewImports : '';
125
163
 
126
- const path_obj_str = `{${storyRequires.join(',')}}`;
127
-
128
164
  const registerAddons = main.addons?.map((addon) => `import "${addon}/register";`).join('\n');
165
+
129
166
  let enhancersImport = '';
167
+
130
168
  let enhancers = '';
131
169
 
132
170
  // TODO: implement presets or something similar
133
- if (main.addons.includes('@storybook/addon-ondevice-actions')) {
171
+ if (main.addons?.includes('@storybook/addon-ondevice-actions')) {
134
172
  enhancersImport =
135
173
  'import { argsEnhancers } from "@storybook/addon-actions/dist/modern/preset/addArgs"';
136
174
 
@@ -167,11 +205,8 @@ function writeRequires({ configPath, absolute = false }) {
167
205
 
168
206
  ${enhancers}
169
207
 
170
- const getStories=() => {
171
- return ${path_obj_str};
172
- }
173
-
174
- configure(getStories, module, false)
208
+ ${configure}
209
+
175
210
  `;
176
211
 
177
212
  const formattedFileContent = prettier.format(fileContent, { parser: 'babel' });
@@ -0,0 +1,54 @@
1
+ /* do not change this file, it is auto generated by storybook. */
2
+
3
+ import {
4
+ configure,
5
+ addDecorator,
6
+ addParameters,
7
+ addArgsEnhancer,
8
+ clearDecorators,
9
+ } from '@storybook/react-native';
10
+
11
+ global.STORIES = [
12
+ {
13
+ titlePrefix: '',
14
+ directory: './.storybook/stories',
15
+ files: '**/*.stories.?(ts|tsx|js|jsx)',
16
+ importPathMatcher:
17
+ '^\\.[\\\\/](?:\\.storybook\\/stories(?:\\/(?!\\.)(?:(?:(?!(?:^|\\/)\\.).)*?)\\/|\\/|$)(?!\\.)(?=.)[^/]*?\\.stories\\.(?:ts|tsx|js|jsx)?)$',
18
+ },
19
+ ];
20
+
21
+ import '@storybook/addon-ondevice-controls/register';
22
+ import '@storybook/addon-ondevice-actions/register';
23
+
24
+ import { argsEnhancers } from '@storybook/addon-actions/dist/modern/preset/addArgs';
25
+
26
+ import { decorators, parameters } from './preview';
27
+
28
+ if (decorators) {
29
+ if (__DEV__) {
30
+ // stops the warning from showing on every HMR
31
+ require('react-native').LogBox.ignoreLogs([
32
+ '`clearDecorators` is deprecated and will be removed in Storybook 7.0',
33
+ ]);
34
+ }
35
+ // workaround for global decorators getting infinitely applied on HMR, see https://github.com/storybookjs/react-native/issues/185
36
+ clearDecorators();
37
+ decorators.forEach((decorator) => addDecorator(decorator));
38
+ }
39
+
40
+ if (parameters) {
41
+ addParameters(parameters);
42
+ }
43
+
44
+ try {
45
+ argsEnhancers.forEach((enhancer) => addArgsEnhancer(enhancer));
46
+ } catch {}
47
+
48
+ const getStories = () => {
49
+ return {
50
+ './.storybook/stories/Button/Button.stories.js': require('./stories/Button/Button.stories.js'),
51
+ };
52
+ };
53
+
54
+ configure(getStories, module, false);