-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathmain.js
237 lines (215 loc) · 9.61 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const { promises: fs } = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const toPath = (_path) => path.join(process.cwd(), _path);
try {
require('../.jest-test-results.json');
console.warn(`[Storybook Jest config] Found "/.jest-test-results.json". \nStories will display the latest Jest test results. \nRunning "yarn:test" in parallel of storybook will keep Storybook up-to-date with the latest test result.`);
} catch (e) {
console.warn(`[Storybook Jest config] The test results file couldn't be found in "/.jest-test-results.json". \nStories will not display Jest test results. \nRunning "yarn test:generate-output" prior to running storybook will fix this.`);
}
/**
* Fetches translations from Locize and store them in the filesystem.
* They will be loaded in preview.js, which will configure Locize so that components can display their translations.
*
* @param environment
*/
const fetchLocizeTranslation = async (environment) => {
const cacheFileName = '.sb-translations.cache.json';
const version = environment === 'development' ? 'latest' : 'production';
const languages = ['en', 'fr'];
const namespaces = ['common'];
const allI18nTranslations = {};
for (let i = 0; i < languages.length; i++) {
const lang = languages[i];
for (let j = 0; j < namespaces.length; j++) {
const namespace = namespaces[j];
const locizeAPIEndpoint = `https://api.locize.app/${process.env.NEXT_PUBLIC_LOCIZE_PROJECT_ID}/${version}/${lang}/${namespace}`;
console.log('Fetching translations from:', locizeAPIEndpoint);
const defaultI18nTranslationsResponse = await fetch(locizeAPIEndpoint);
const i18nTranslations = await defaultI18nTranslationsResponse.json();
allI18nTranslations[lang] = allI18nTranslations[lang] || {};
allI18nTranslations[lang][namespace] = i18nTranslations;
}
}
// Store translations
const translationCacheFile = path.join(__dirname, cacheFileName);
console.log('Writing translations cache to:', translationCacheFile);
await fs.writeFile(translationCacheFile, JSON.stringify(allI18nTranslations, null, 2), 'utf8');
};
module.exports = {
stories: [
'../src/**/*.stories.mdx',
'../src/**/*.stories.@(js|jsx|ts|tsx)',
],
addons: [
/**
* The Storybook Links addon can be used to create links that navigate between stories in Storybook.
*
* @see https://www.npmjs.com/package/@storybook/addon-links
*/
'@storybook/addon-links',
/**
* Present including "essential" Storybook addons, such as:
*
* - Actions - Storybook Addon Actions can be used to display data received by event handlers in Storybook.
* It's where the action you do are being logged.
*
* - Backgrounds - Storybook Addon Backgrounds can be used to change background colors inside the preview in Storybook.
*
* - Controls - Controls gives you a graphical UI to interact with a component's arguments dynamically, without needing to code.
* It creates an addon panel next to your component examples ("stories"), so you can edit them live.
*
* - Docs - Storybook Docs transforms your Storybook stories into world-class component documentation.
* - DocsPage: Out of the box, all your stories get a DocsPage. DocsPage is a zero-config aggregation
* of your component stories, text descriptions, docgen comments, props tables, and code examples into clean, readable pages.
* - MDX: If you want more control, MDX allows you to write long-form markdown documentation and stories in one file.
* You can also use it to write pure documentation pages and embed them inside your Storybook alongside your stories.
*
* - Viewport - Storybook Viewport Addon allows your stories to be displayed in different sizes and layouts in Storybook.
* This helps build responsive components inside of Storybook.
*
* - Toolbars - The Toolbars addon controls global story rendering options from Storybook's toolbar UI. It's a general purpose addon that can be used to:
* - set a theme for your components
* - set your components' internationalization (i18n) locale
* - configure just about anything in Storybook that makes use of a global variable
*
* @see https://storybook.js.org/addons/essentials
* @see https://github.com/storybookjs/storybook/tree/master/addons/essentials
* @see https://github.com/storybookjs/storybook/tree/next/addons/actions
* @see https://github.com/storybookjs/storybook/tree/next/addons/backgrounds
* @see https://github.com/storybookjs/storybook/tree/next/addons/controls
* @see https://github.com/storybookjs/storybook/tree/next/addons/docs
* @see https://github.com/storybookjs/storybook/tree/next/addons/viewport
* @see https://github.com/storybookjs/storybook/tree/next/addons/toolbars
*
* You can disable addons you don't want through configuration.
* @see https://github.com/storybookjs/storybook/tree/master/addons/essentials#disabling-addons
*/
{
name: '@storybook/addon-essentials',
options: {
actions: true,
backgrounds: true,
controls: true,
docs: true,
viewport: true,
toolbars: true,
},
},
/**
* Storybook Addon Knobs has been replaced by Controls and is being deprecated, it will be removed in v7.
*
* It is listed below for documentation purpose and help you avoid using it thinking it's still legit.
*
* @see https://github.com/storybookjs/storybook/blob/next/addons/controls/README.md#how-will-this-replace-addon-knobs
*/
// '@storybook/addon-knobs',
/**
* We use Google Analytics for tracking analytics usage.
*
* It's much easier to setup than Amplitude, because there is an official dedicated plugin for this.
* See ".storybook/manager.js" for Google Analytics configuration.
*
* @see https://github.com/storybookjs/storybook/tree/master/addons/google-analytics
*/
'@storybook/addon-google-analytics',
/**
* Shows stories source in the addon panel. (display the source code of the story in a dedicated panel)
*
* Adds an "Story" tab.
*
* XXX Disabled for now, because of https://github.com/storybookjs/storybook/issues/13657 (brings no useful information at the moment)
* Better to use the Docs panel and display source code, that's a good workaround for now.
*
* @see https://github.com/storybookjs/storybook/tree/master/addons/storysource
*/
// '@storybook/addon-storysource',
/**
* This storybook addon can be helpful to make your UI components more accessible.
*
* Adds an "Accessibility" tab.
*
* @see https://www.npmjs.com/package/@storybook/addon-a11y
*/
'@storybook/addon-a11y',
// ------------------- Non official addons below ------------------
/**
* Adds support for CSS Modules.
*
* Even though Next Right Now doesn't encourage the use of CSS Modules,
* we thought it's an interesting feature to support, which is natively supported by Next.js.
*
* @see https://www.npmjs.com/package/storybook-css-modules-preset How to configure Storybook to support CSS Modules
* @see https://nextjs.org/docs/basic-features/built-in-css-support#adding-component-level-css How to use CSS Modules with Next.js
*/
'storybook-css-modules-preset',
/**
* Brings Jest results in storybook.
*
* @see https://github.com/storybookjs/storybook/tree/master/addons/jest
*/
'@storybook/addon-jest',
/**
* A storybook addon to help better understand and debug performance for React components.
*
* Adds a "Performance" tab.
*
* @see https://github.com/atlassian-labs/storybook-addon-performance
*/
'storybook-addon-performance/register',
/**
* Offers suggestions on how you can improve the HTML, CSS and UX of your components to be more mobile-friendly.
*
* Adds a "Mobile" tab.
*
* @see https://github.com/aholachek/storybook-mobile
*/
'storybook-mobile',
/**
* A Storybook addon that embed Figma, websites, PDF or images in the addon panel.
*
* Adds a "Design" tab.
*
* @see https://github.com/pocka/storybook-addon-designs
*/
'storybook-addon-designs',
],
/**
* Customize webpack configuration for Storybook.
*
* This doesn't affect the Next.js application, only the Storybook compilation.
*
* @param config
* @see https://storybook.js.org/docs/react/configure/overview#configure-your-storybook-project
*/
webpackFinal: async (config) => {
const {
mode: environment,
plugins,
module,
} = config;
await fetchLocizeTranslation(environment);
return {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
/**
* Map Emotion 10 libraries to Emotion 11 libraries.
*
* Otherwise Storybook fails to compile with "Module not found: Error: Can't resolve '@emotion/styled/base'", etc.
* It wasn't necessary to do this until we imported React component using "@emotion/styled".
* This issue is probably caused because Storybook uses Emotion 10 while we have Emotion 11 used by the Next.js app.
*
* @see https://github.com/storybookjs/storybook/issues/13277#issuecomment-751747964
*/
'@emotion/core': toPath('node_modules/@emotion/react'),
'@emotion/styled': toPath('node_modules/@emotion/styled'),
'emotion-theming': toPath('node_modules/@emotion/react'),
},
},
};
},
};