UNPKG

11.8 kBJavaScriptView Raw
1import "core-js/modules/es.symbol.js";
2import "core-js/modules/es.symbol.description.js";
3import "core-js/modules/es.symbol.iterator.js";
4import "core-js/modules/es.array.iterator.js";
5import "core-js/modules/es.string.iterator.js";
6import "core-js/modules/web.dom-collections.iterator.js";
7import "core-js/modules/es.array.slice.js";
8import "core-js/modules/es.array.from.js";
9import "core-js/modules/es.object.freeze.js";
10var _excluded = ["default", "__namedExportsOrder"];
11
12var _templateObject;
13
14function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
15
16function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
17
18function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
19
20function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
21
22function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
23
24function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
25
26function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
27
28function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
29
30function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
31
32import "core-js/modules/es.object.keys.js";
33import "core-js/modules/es.object.entries.js";
34import "core-js/modules/es.array.map.js";
35import "core-js/modules/es.object.to-string.js";
36import "core-js/modules/web.dom-collections.for-each.js";
37import "core-js/modules/es.object.assign.js";
38import "core-js/modules/es.regexp.constructor.js";
39import "core-js/modules/es.regexp.exec.js";
40import "core-js/modules/es.regexp.to-string.js";
41import "core-js/modules/es.array.concat.js";
42import "core-js/modules/es.array.filter.js";
43import "core-js/modules/es.function.name.js";
44
45function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
46
47function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
48
49function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
50
51import global from 'global';
52import dedent from 'ts-dedent';
53import { SynchronousPromise } from 'synchronous-promise';
54import { toId, isExportStory, storyNameFromExport } from '@storybook/csf';
55import { userOrAutoTitle, sortStoriesV6 } from '@storybook/store';
56import { logger } from '@storybook/client-logger';
57export var StoryStoreFacade = /*#__PURE__*/function () {
58 function StoryStoreFacade() {
59 _classCallCheck(this, StoryStoreFacade);
60
61 this.projectAnnotations = void 0;
62 this.stories = void 0;
63 this.csfExports = void 0;
64 this.projectAnnotations = {
65 loaders: [],
66 decorators: [],
67 parameters: {},
68 argsEnhancers: [],
69 argTypesEnhancers: [],
70 args: {},
71 argTypes: {}
72 };
73 this.stories = {};
74 this.csfExports = {};
75 } // This doesn't actually import anything because the client-api loads fully
76 // on startup, but this is a shim after all.
77
78
79 _createClass(StoryStoreFacade, [{
80 key: "importFn",
81 value: function importFn(path) {
82 var _this = this;
83
84 return SynchronousPromise.resolve().then(function () {
85 var moduleExports = _this.csfExports[path];
86 if (!moduleExports) throw new Error("Unknown path: ".concat(path));
87 return moduleExports;
88 });
89 }
90 }, {
91 key: "getStoryIndex",
92 value: function getStoryIndex(store) {
93 var _this$projectAnnotati,
94 _this$projectAnnotati2,
95 _this2 = this;
96
97 var fileNameOrder = Object.keys(this.csfExports);
98 var storySortParameter = (_this$projectAnnotati = this.projectAnnotations.parameters) === null || _this$projectAnnotati === void 0 ? void 0 : (_this$projectAnnotati2 = _this$projectAnnotati.options) === null || _this$projectAnnotati2 === void 0 ? void 0 : _this$projectAnnotati2.storySort;
99 var storyEntries = Object.entries(this.stories); // Add the kind parameters and global parameters to each entry
100
101 var sortableV6 = storyEntries.map(function (_ref) {
102 var _ref2 = _slicedToArray(_ref, 2),
103 storyId = _ref2[0],
104 importPath = _ref2[1].importPath;
105
106 var exports = _this2.csfExports[importPath];
107 var csfFile = store.processCSFFileWithCache(exports, importPath, exports.default.title);
108 return [storyId, store.storyFromCSFFile({
109 storyId: storyId,
110 csfFile: csfFile
111 }), csfFile.meta.parameters, _this2.projectAnnotations.parameters];
112 }); // NOTE: the sortStoriesV6 version returns the v7 data format. confusing but more convenient!
113
114 var sortedV7;
115
116 try {
117 sortedV7 = sortStoriesV6(sortableV6, storySortParameter, fileNameOrder);
118 } catch (err) {
119 if (typeof storySortParameter === 'function') {
120 throw new Error(dedent(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n Error sorting stories with sort parameter ", ":\n\n > ", "\n \n Are you using a V7-style sort function in V6 compatibility mode?\n \n More info: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#v7-style-story-sort\n "])), storySortParameter, err.message));
121 }
122
123 throw err;
124 }
125
126 var stories = sortedV7.reduce(function (acc, s) {
127 // We use the original entry we stored in `this.stories` because it is possible that the CSF file itself
128 // exports a `parameters.fileName` which can be different and mess up our `importFn`.
129 // In fact, in Storyshots there is a Jest transformer that does exactly that.
130 // NOTE: this doesn't actually change the story object, just the index.
131 acc[s.id] = _this2.stories[s.id];
132 return acc;
133 }, {});
134 return {
135 v: 3,
136 stories: stories
137 };
138 }
139 }, {
140 key: "clearFilenameExports",
141 value: function clearFilenameExports(fileName) {
142 var _this3 = this;
143
144 if (!this.csfExports[fileName]) {
145 return;
146 } // Clear this module's stories from the storyList and existing exports
147
148
149 Object.entries(this.stories).forEach(function (_ref3) {
150 var _ref4 = _slicedToArray(_ref3, 2),
151 id = _ref4[0],
152 importPath = _ref4[1].importPath;
153
154 if (importPath === fileName) {
155 delete _this3.stories[id];
156 }
157 }); // We keep this as an empty record so we can use it to maintain component order
158
159 this.csfExports[fileName] = {};
160 } // NOTE: we could potentially share some of this code with the stories.json generation
161
162 }, {
163 key: "addStoriesFromExports",
164 value: function addStoriesFromExports(fileName, fileExports) {
165 var _this4 = this;
166
167 // if the export haven't changed since last time we added them, this is a no-op
168 if (this.csfExports[fileName] === fileExports) {
169 return;
170 } // OTOH, if they have changed, let's clear them out first
171
172
173 this.clearFilenameExports(fileName);
174
175 var defaultExport = fileExports.default,
176 __namedExportsOrder = fileExports.__namedExportsOrder,
177 namedExports = _objectWithoutProperties(fileExports, _excluded); // eslint-disable-next-line prefer-const
178
179
180 var _ref5 = defaultExport || {},
181 componentId = _ref5.id,
182 title = _ref5.title;
183
184 var specifiers = (global.STORIES || []).map(function (specifier) {
185 return Object.assign({}, specifier, {
186 importPathMatcher: new RegExp(specifier.importPathMatcher)
187 });
188 });
189 title = userOrAutoTitle(fileName, specifiers, title);
190
191 if (!title) {
192 logger.info("Unexpected default export without title in '".concat(fileName, "': ").concat(JSON.stringify(fileExports.default)));
193 return;
194 }
195
196 this.csfExports[fileName] = Object.assign({}, fileExports, {
197 default: Object.assign({}, defaultExport, {
198 title: title
199 })
200 });
201 var sortedExports = namedExports; // prefer a user/loader provided `__namedExportsOrder` array if supplied
202 // we do this as es module exports are always ordered alphabetically
203 // see https://github.com/storybookjs/storybook/issues/9136
204
205 if (Array.isArray(__namedExportsOrder)) {
206 sortedExports = {};
207
208 __namedExportsOrder.forEach(function (name) {
209 var namedExport = namedExports[name];
210 if (namedExport) sortedExports[name] = namedExport;
211 });
212 }
213
214 Object.entries(sortedExports).filter(function (_ref6) {
215 var _ref7 = _slicedToArray(_ref6, 1),
216 key = _ref7[0];
217
218 return isExportStory(key, defaultExport);
219 }).forEach(function (_ref8) {
220 var _storyExport$paramete, _storyExport$story;
221
222 var _ref9 = _slicedToArray(_ref8, 2),
223 key = _ref9[0],
224 storyExport = _ref9[1];
225
226 var exportName = storyNameFromExport(key);
227 var id = ((_storyExport$paramete = storyExport.parameters) === null || _storyExport$paramete === void 0 ? void 0 : _storyExport$paramete.__id) || toId(componentId || title, exportName);
228 var name = typeof storyExport !== 'function' && storyExport.name || storyExport.storyName || ((_storyExport$story = storyExport.story) === null || _storyExport$story === void 0 ? void 0 : _storyExport$story.name) || exportName;
229 _this4.stories[id] = {
230 id: id,
231 name: name,
232 title: title,
233 importPath: fileName
234 };
235 });
236 }
237 }]);
238
239 return StoryStoreFacade;
240}();
\No newline at end of file