forked from storybookjs/storybook
/
preview.js
100 lines (94 loc) · 2.26 KB
/
preview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import addons from '@storybook/addons';
import { logger } from '@storybook/node-logger';
import { STORY_EVENT_ID } from './events';
const getLocation = (context, locationsMap) => locationsMap[context.id];
function sendEvent(
context,
source,
locationsMap,
mainFileLocation,
dependencies,
localDependencies,
prefix,
idsToFrameworks
) {
if (!context || !context.id || !context.kind || !context.story) {
logger.warn(
'@storybook/source-loader was applied to a file which does not contain a story. Please check your webpack configuration and make sure to apply @storybook/source-loader only to files containg stories. Related file:'
);
logger.warn(source);
return;
}
const channel = addons.getChannel();
const currentLocation = getLocation(context, locationsMap);
channel.emit(STORY_EVENT_ID, {
edition: {
source,
mainFileLocation,
dependencies,
localDependencies,
prefix,
idsToFrameworks,
},
story: {
kind: context.kind,
story: context.story,
},
location: {
currentLocation,
locationsMap,
},
});
}
export function addSource(storyFn, sourceContext) {
const {
__STORY__: source,
__ADDS_MAP__: locationsMap = {},
__MAIN_FILE_LOCATION__: mainFileLocation = '/index.js',
__MODULE_DEPENDENCIES__: dependencies = [],
__LOCAL_DEPENDENCIES__: localDependencies = {},
__SOURCE_PREFIX__: prefix,
__IDS_TO_FRAMEWORKS__: idsToFrameworks,
} = sourceContext;
const decorated = (context = {}) => {
sendEvent(
context,
source,
locationsMap,
mainFileLocation,
dependencies,
localDependencies,
prefix,
idsToFrameworks
);
if (typeof storyFn === 'function') {
return storyFn(context);
}
return storyFn;
};
decorated.story = (storyFn || {}).story;
return decorated;
}
export function withSource(
source,
locationsMap = {},
mainFileLocation = '/index.js',
dependencies = [],
localDependencies = {},
prefix,
idsToFrameworks
) {
return (storyFn, context) => {
sendEvent(
context,
source,
locationsMap,
mainFileLocation,
dependencies,
localDependencies,
prefix,
idsToFrameworks
);
return storyFn(context);
};
}