1 | const path = require('path');
|
2 |
|
3 | const { generate } = require('../scripts/generate');
|
4 | const { WebSocketServer } = require('ws');
|
5 |
|
6 | module.exports = (
|
7 | config,
|
8 | { configPath, enabled = true, websockets, useJs = false, onDisabledRemoveStorybook = false } = {
|
9 | enabled: true,
|
10 | useJs: false,
|
11 | onDisabledRemoveStorybook: false,
|
12 | }
|
13 | ) => {
|
14 | if (!enabled) {
|
15 | if (onDisabledRemoveStorybook) {
|
16 | return {
|
17 | ...config,
|
18 | resolver: {
|
19 | ...config.resolver,
|
20 | resolveRequest: (context, moduleName, platform) => {
|
21 | const resolveFunction = config?.resolver?.resolveRequest
|
22 | ? config?.resolver?.resolveRequest
|
23 | : context.resolveRequest;
|
24 |
|
25 | if (moduleName.startsWith('storybook') || moduleName.startsWith('@storybook')) {
|
26 | return {
|
27 | type: 'empty',
|
28 | };
|
29 | }
|
30 |
|
31 | return resolveFunction(context, moduleName, platform);
|
32 | },
|
33 | },
|
34 | };
|
35 | }
|
36 |
|
37 | return config;
|
38 | }
|
39 |
|
40 | if (websockets) {
|
41 | const port = websockets.port ?? 7007;
|
42 |
|
43 | const host = websockets.host ?? 'localhost';
|
44 |
|
45 | const wss = new WebSocketServer({ port, host });
|
46 |
|
47 | wss.on('connection', function connection(ws) {
|
48 | console.log('websocket connection established');
|
49 |
|
50 | ws.on('error', console.error);
|
51 |
|
52 | ws.on('message', function message(data) {
|
53 | try {
|
54 | const json = JSON.parse(data.toString());
|
55 |
|
56 | wss.clients.forEach((wsClient) => wsClient.send(JSON.stringify(json)));
|
57 | } catch (error) {
|
58 | console.error(error);
|
59 | }
|
60 | });
|
61 | });
|
62 | }
|
63 |
|
64 | generate({
|
65 | configPath: configPath ?? path.resolve(process.cwd(), './.storybook'),
|
66 | useJs,
|
67 | });
|
68 |
|
69 | return {
|
70 | ...config,
|
71 | transformer: {
|
72 | ...config.transformer,
|
73 | unstable_allowRequireContext: true,
|
74 | },
|
75 | resolver: {
|
76 | ...config.resolver,
|
77 | resolveRequest: (context, moduleName, platform) => {
|
78 | const resolveFunction = config?.resolver?.resolveRequest
|
79 | ? config?.resolver?.resolveRequest
|
80 | : context.resolveRequest;
|
81 |
|
82 | const isStorybookModule =
|
83 | moduleName.startsWith('storybook') || moduleName.startsWith('@storybook');
|
84 |
|
85 | const theContext = isStorybookModule
|
86 | ? {
|
87 | ...context,
|
88 | unstable_enablePackageExports: true,
|
89 | unstable_conditionNames: ['import'],
|
90 | }
|
91 | : context;
|
92 |
|
93 | const resolveResult = resolveFunction(theContext, moduleName, platform);
|
94 |
|
95 |
|
96 | if (resolveResult?.filePath?.includes?.('@storybook/react/template/cli')) {
|
97 | return {
|
98 | type: 'empty',
|
99 | };
|
100 | }
|
101 |
|
102 | return resolveResult;
|
103 | },
|
104 | },
|
105 | };
|
106 | };
|