UNPKG

12.5 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.array.concat.js");
4
5require("core-js/modules/es.array.for-each.js");
6
7require("core-js/modules/es.object.assign.js");
8
9require("core-js/modules/es.object.get-prototype-of.js");
10
11require("core-js/modules/es.object.keys.js");
12
13require("core-js/modules/es.object.to-string.js");
14
15require("core-js/modules/es.regexp.to-string.js");
16
17require("core-js/modules/web.dom-collections.for-each.js");
18
19Object.defineProperty(exports, "__esModule", {
20 value: true
21});
22exports.default = exports.addArgTypesEnhancer = exports.addLoader = exports.addParameters = exports.addDecorator = void 0;
23
24var _utilDeprecate = _interopRequireDefault(require("util-deprecate"));
25
26var _tsDedent = _interopRequireDefault(require("ts-dedent"));
27
28var _clientLogger = require("@storybook/client-logger");
29
30var _csf = require("@storybook/csf");
31
32var _hooks = require("./hooks");
33
34var _decorators = require("./decorators");
35
36var _templateObject, _templateObject2;
37
38function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
40function _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; }
41
42function _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; }
43
44function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
45
46function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
47
48function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
49
50// ClientApi (and StoreStore) are really singletons. However they are not created until the
51// relevant framework instanciates them via `start.js`. The good news is this happens right away.
52var singleton;
53var addDecoratorDeprecationWarning = (0, _utilDeprecate.default)(function () {}, "`addDecorator` is deprecated, and will be removed in Storybook 7.0.\nInstead, use `export const decorators = [];` in your `preview.js`.\nRead more at https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-addparameters-and-adddecorator).");
54
55var addDecorator = function addDecorator(decorator) {
56 var deprecationWarning = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
57 if (!singleton) throw new Error("Singleton client API not yet initialized, cannot call addDecorator");
58 if (deprecationWarning) addDecoratorDeprecationWarning();
59 singleton.addDecorator(decorator);
60};
61
62exports.addDecorator = addDecorator;
63var addParametersDeprecationWarning = (0, _utilDeprecate.default)(function () {}, "`addParameters` is deprecated, and will be removed in Storybook 7.0.\nInstead, use `export const parameters = {};` in your `preview.js`.\nRead more at https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-addparameters-and-adddecorator).");
64
65var addParameters = function addParameters(parameters) {
66 var deprecationWarning = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
67 if (!singleton) throw new Error("Singleton client API not yet initialized, cannot call addParameters");
68 if (deprecationWarning) addParametersDeprecationWarning();
69 singleton.addParameters(parameters);
70};
71
72exports.addParameters = addParameters;
73var addLoaderDeprecationWarning = (0, _utilDeprecate.default)(function () {}, "`addLoader` is deprecated, and will be removed in Storybook 7.0.\nInstead, use `export const loaders = [];` in your `preview.js`.\nRead more at https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-addparameters-and-adddecorator).");
74
75var addLoader = function addLoader(loader) {
76 var deprecationWarning = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
77 if (!singleton) throw new Error("Singleton client API not yet initialized, cannot call addParameters");
78 if (deprecationWarning) addLoaderDeprecationWarning();
79 singleton.addLoader(loader);
80};
81
82exports.addLoader = addLoader;
83
84var addArgTypesEnhancer = function addArgTypesEnhancer(enhancer) {
85 if (!singleton) throw new Error("Singleton client API not yet initialized, cannot call addArgTypesEnhancer");
86 singleton.addArgTypesEnhancer(enhancer);
87};
88
89exports.addArgTypesEnhancer = addArgTypesEnhancer;
90
91var ClientApi = // React Native Fast refresh doesn't allow multiple dispose calls
92function ClientApi(_ref) {
93 var _this = this;
94
95 var storyStore = _ref.storyStore,
96 _ref$decorateStory = _ref.decorateStory,
97 decorateStory = _ref$decorateStory === void 0 ? _decorators.defaultDecorateStory : _ref$decorateStory,
98 noStoryModuleAddMethodHotDispose = _ref.noStoryModuleAddMethodHotDispose;
99
100 _classCallCheck(this, ClientApi);
101
102 this._storyStore = void 0;
103 this._addons = void 0;
104 this._decorateStory = void 0;
105 this._noStoryModuleAddMethodHotDispose = void 0;
106 this.setAddon = (0, _utilDeprecate.default)(function (addon) {
107 _this._addons = Object.assign({}, _this._addons, addon);
108 }, (0, _tsDedent.default)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n `setAddon` is deprecated and will be removed in Storybook 7.0.\n\n https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-setaddon\n "], ["\n \\`setAddon\\` is deprecated and will be removed in Storybook 7.0.\n\n https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-setaddon\n "]))));
109
110 this.addDecorator = function (decorator) {
111 _this._storyStore.addGlobalMetadata({
112 decorators: [decorator]
113 });
114 };
115
116 this.clearDecorators = (0, _utilDeprecate.default)(function () {
117 _this._storyStore.clearGlobalDecorators();
118 }, (0, _tsDedent.default)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n `clearDecorators` is deprecated and will be removed in Storybook 7.0.\n\n https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-cleardecorators\n "], ["\n \\`clearDecorators\\` is deprecated and will be removed in Storybook 7.0.\n\n https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-cleardecorators\n "]))));
119
120 this.addParameters = function (parameters) {
121 _this._storyStore.addGlobalMetadata({
122 parameters: parameters
123 });
124 };
125
126 this.addLoader = function (loader) {
127 _this._storyStore.addGlobalMetadata({
128 loaders: [loader]
129 });
130 };
131
132 this.addArgTypesEnhancer = function (enhancer) {
133 _this._storyStore.addArgTypesEnhancer(enhancer);
134 };
135
136 this.storiesOf = function (kind, m) {
137 if (!kind && typeof kind !== 'string') {
138 throw new Error('Invalid or missing kind provided for stories, should be a string');
139 }
140
141 if (!m) {
142 _clientLogger.logger.warn("Missing 'module' parameter for story with a kind of '".concat(kind, "'. It will break your HMR"));
143 }
144
145 if (m) {
146 var proto = Object.getPrototypeOf(m);
147
148 if (proto.exports && proto.exports.default) {
149 // FIXME: throw an error in SB6.0
150 _clientLogger.logger.error("Illegal mix of CSF default export and storiesOf calls in a single file: ".concat(proto.i));
151 }
152 }
153
154 if (m && m.hot && m.hot.dispose) {
155 m.hot.dispose(function () {
156 var _storyStore = _this._storyStore; // If HMR dispose happens in a story file, we know that HMR will pass up to the configuration file (preview.js)
157 // and be handled by the HMR.allow in config_api, leading to a re-run of configuration.
158 // So configuration is about to happen--we can skip the safety check.
159
160 _storyStore.removeStoryKind(kind, {
161 allowUnsafe: true
162 });
163 });
164 }
165
166 var hasAdded = false;
167 var api = {
168 kind: kind.toString(),
169 add: function add() {
170 return api;
171 },
172 addDecorator: function addDecorator() {
173 return api;
174 },
175 addLoader: function addLoader() {
176 return api;
177 },
178 addParameters: function addParameters() {
179 return api;
180 }
181 }; // apply addons
182
183 Object.keys(_this._addons).forEach(function (name) {
184 var addon = _this._addons[name];
185
186 api[name] = function () {
187 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
188 args[_key] = arguments[_key];
189 }
190
191 addon.apply(api, args);
192 return api;
193 };
194 });
195
196 api.add = function (storyName, storyFn) {
197 var parameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
198 hasAdded = true;
199 var id = parameters.__id || (0, _csf.toId)(kind, storyName);
200
201 if (typeof storyName !== 'string') {
202 throw new Error("Invalid or missing storyName provided for a \"".concat(kind, "\" story."));
203 }
204
205 if (typeof storyFn !== 'function') {
206 throw new Error("Cannot load story \"".concat(storyName, "\" in \"").concat(kind, "\" due to invalid format. Storybook expected a function but received ").concat(_typeof(storyFn), " instead."));
207 }
208
209 if (!_this._noStoryModuleAddMethodHotDispose && m && m.hot && m.hot.dispose) {
210 m.hot.dispose(function () {
211 var _storyStore = _this._storyStore; // See note about allowUnsafe above
212
213 _storyStore.remove(id, {
214 allowUnsafe: true
215 });
216 });
217 }
218
219 var fileName = m && m.id ? "".concat(m.id) : undefined;
220
221 var decorators = parameters.decorators,
222 loaders = parameters.loaders,
223 storyParameters = _objectWithoutProperties(parameters, ["decorators", "loaders"]);
224
225 _this._storyStore.addStory({
226 id: id,
227 kind: kind,
228 name: storyName,
229 storyFn: storyFn,
230 parameters: Object.assign({
231 fileName: fileName
232 }, storyParameters),
233 decorators: decorators,
234 loaders: loaders
235 }, {
236 applyDecorators: (0, _hooks.applyHooks)(_this._decorateStory)
237 });
238
239 return api;
240 };
241
242 api.addDecorator = function (decorator) {
243 if (hasAdded) throw new Error("You cannot add a decorator after the first story for a kind.\nRead more here: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md#can-no-longer-add-decoratorsparameters-after-stories");
244
245 _this._storyStore.addKindMetadata(kind, {
246 decorators: [decorator]
247 });
248
249 return api;
250 };
251
252 api.addLoader = function (loader) {
253 if (hasAdded) throw new Error("You cannot add a loader after the first story for a kind.");
254
255 _this._storyStore.addKindMetadata(kind, {
256 loaders: [loader]
257 });
258
259 return api;
260 };
261
262 api.addParameters = function (parameters) {
263 if (hasAdded) throw new Error("You cannot add parameters after the first story for a kind.\nRead more here: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md#can-no-longer-add-decoratorsparameters-after-stories");
264
265 _this._storyStore.addKindMetadata(kind, {
266 parameters: parameters
267 });
268
269 return api;
270 };
271
272 return api;
273 };
274
275 this.getStorybook = function () {
276 return _this._storyStore.getStorybook();
277 };
278
279 this.raw = function () {
280 return _this._storyStore.raw();
281 };
282
283 this.store = function () {
284 return _this._storyStore;
285 };
286
287 this._storyStore = storyStore;
288 this._addons = {};
289 this._noStoryModuleAddMethodHotDispose = noStoryModuleAddMethodHotDispose || false;
290 this._decorateStory = decorateStory;
291 if (!storyStore) throw new Error('storyStore is required');
292 singleton = this;
293};
294
295exports.default = ClientApi;
\No newline at end of file