@storybook/core-server 6.4.0-beta.23 → 6.4.0-beta.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/cjs/build-dev.js +1 -0
  2. package/dist/cjs/build-static.js +22 -1
  3. package/dist/cjs/cli/utils.js +21 -3
  4. package/dist/cjs/dev-server.js +4 -1
  5. package/dist/cjs/utils/copy-all-static-files.js +33 -0
  6. package/dist/cjs/utils/get-server-channel.js +47 -0
  7. package/dist/cjs/utils/server-address.js +10 -2
  8. package/dist/cjs/utils/server-statics.js +35 -4
  9. package/dist/cjs/utils/stories-json.js +5 -11
  10. package/dist/esm/build-dev.js +2 -1
  11. package/dist/esm/build-static.js +22 -2
  12. package/dist/esm/cli/utils.js +22 -3
  13. package/dist/esm/dev-server.js +3 -1
  14. package/dist/esm/utils/copy-all-static-files.js +27 -0
  15. package/dist/esm/utils/get-server-channel.js +30 -0
  16. package/dist/esm/utils/server-address.js +5 -0
  17. package/dist/esm/utils/server-statics.js +33 -4
  18. package/dist/esm/utils/stories-json.js +4 -9
  19. package/dist/modern/build-dev.js +2 -1
  20. package/dist/modern/build-static.js +22 -2
  21. package/dist/modern/cli/utils.js +22 -3
  22. package/dist/modern/dev-server.js +3 -1
  23. package/dist/modern/utils/copy-all-static-files.js +27 -0
  24. package/dist/modern/utils/get-server-channel.js +30 -0
  25. package/dist/modern/utils/server-address.js +5 -0
  26. package/dist/modern/utils/server-statics.js +33 -4
  27. package/dist/modern/utils/stories-json.js +4 -9
  28. package/dist/ts3.4/cli/prod.d.ts +2 -15
  29. package/dist/ts3.4/cli/utils.d.ts +3 -5
  30. package/dist/ts3.4/utils/copy-all-static-files.d.ts +1 -0
  31. package/dist/ts3.4/utils/get-server-channel.d.ts +9 -0
  32. package/dist/ts3.4/utils/server-address.d.ts +3 -0
  33. package/dist/ts3.4/utils/server-statics.d.ts +2 -3
  34. package/dist/ts3.4/utils/stories-json.d.ts +3 -2
  35. package/dist/ts3.9/cli/prod.d.ts +2 -15
  36. package/dist/ts3.9/cli/utils.d.ts +3 -5
  37. package/dist/ts3.9/utils/copy-all-static-files.d.ts +1 -0
  38. package/dist/ts3.9/utils/get-server-channel.d.ts +9 -0
  39. package/dist/ts3.9/utils/server-address.d.ts +3 -0
  40. package/dist/ts3.9/utils/server-statics.d.ts +2 -3
  41. package/dist/ts3.9/utils/stories-json.d.ts +3 -2
  42. package/package.json +17 -13
  43. package/dist/cjs/utils/use-events-as-sse.js +0 -65
  44. package/dist/esm/utils/use-events-as-sse.js +0 -58
  45. package/dist/modern/utils/use-events-as-sse.js +0 -58
  46. package/dist/ts3.4/utils/use-events-as-sse.d.ts +0 -4
  47. package/dist/ts3.9/utils/use-events-as-sse.d.ts +0 -4
@@ -96,6 +96,7 @@ async function buildDevStandalone(options) {
96
96
  options.configType = 'DEVELOPMENT';
97
97
  options.configDir = _path.default.resolve(options.configDir);
98
98
  options.outputDir = options.smokeTest ? (0, _coreCommon.resolvePathInStorybookCache)('public') : _path.default.resolve(options.outputDir || (0, _coreCommon.resolvePathInStorybookCache)('public'));
99
+ options.serverChannelUrl = (0, _serverAddress.getServerChannelUrl)(port, options);
99
100
  /* eslint-enable no-param-reassign */
100
101
 
101
102
  var previewBuilder = await (0, _getPreviewBuilder.getPreviewBuilder)(options.configDir);
@@ -18,6 +18,8 @@ var _fsExtra = _interopRequireDefault(require("fs-extra"));
18
18
 
19
19
  var _path = _interopRequireDefault(require("path"));
20
20
 
21
+ var _tsDedent = _interopRequireDefault(require("ts-dedent"));
22
+
21
23
  var _nodeLogger = require("@storybook/node-logger");
22
24
 
23
25
  var _coreCommon = require("@storybook/core-common");
@@ -90,13 +92,32 @@ async function buildStaticStandalone(options) {
90
92
 
91
93
  await _fsExtra.default.emptyDir(options.outputDir);
92
94
  await (0, _cpy.default)(defaultFavIcon, options.outputDir);
93
- await (0, _copyAllStaticFiles.copyAllStaticFiles)(options.staticDir, options.outputDir);
94
95
  var previewBuilder = await (0, _getPreviewBuilder.getPreviewBuilder)(options.configDir);
95
96
  var managerBuilder = await (0, _getManagerBuilder.getManagerBuilder)(options.configDir);
96
97
  var presets = (0, _coreCommon.loadAllPresets)(_objectSpread({
97
98
  corePresets: [require.resolve('./presets/common-preset'), ...managerBuilder.corePresets, ...previewBuilder.corePresets, require.resolve('./presets/babel-cache-preset')],
98
99
  overridePresets: previewBuilder.overridePresets
99
100
  }, options));
101
+ var staticDirs = await presets.apply('staticDirs');
102
+
103
+ if (staticDirs && options.staticDir) {
104
+ throw new Error((0, _tsDedent.default)`
105
+ Conflict when trying to read staticDirs:
106
+ * Storybook's configuration option: 'staticDirs'
107
+ * Storybook's CLI flag: '--staticDir' or '-s'
108
+
109
+ Choose one of them, but not both.
110
+ `);
111
+ }
112
+
113
+ if (staticDirs) {
114
+ await (0, _copyAllStaticFiles.copyAllStaticFilesRelativeToMain)(staticDirs, options.outputDir, options.configDir);
115
+ }
116
+
117
+ if (options.staticDir) {
118
+ await (0, _copyAllStaticFiles.copyAllStaticFiles)(options.staticDir, options.outputDir);
119
+ }
120
+
100
121
  var features = await presets.apply('features');
101
122
 
102
123
  if (features !== null && features !== void 0 && features.buildStoriesJson || features !== null && features !== void 0 && features.storyStoreV7) {
@@ -32,14 +32,32 @@ function getEnvConfig(program, configEnv) {
32
32
  });
33
33
  }
34
34
 
35
- var warnDLLsDeprecated = (0, _utilDeprecate.default)(function () {}, (0, _tsDedent.default)`
35
+ var warnDeprecatedFlag = function (message) {
36
+ return (0, _utilDeprecate.default)(function () {}, (0, _tsDedent.default)(message));
37
+ };
38
+
39
+ var warnDLLsDeprecated = warnDeprecatedFlag(`
36
40
  DLL-related CLI flags are deprecated, see:
37
41
 
38
42
  https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-dll-flags
39
43
  `);
44
+ var warnStaticDirDeprecated = warnDeprecatedFlag(`
45
+ --static-dir CLI flag is deprecated, see:
46
+
47
+ https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-static-dir-flag
48
+ `);
40
49
 
41
- function checkDeprecatedFlags(options) {
42
- if (!options.dll || options.uiDll || options.docsDll) {
50
+ function checkDeprecatedFlags({
51
+ dll: dll,
52
+ uiDll: uiDll,
53
+ docsDll: docsDll,
54
+ staticDir: staticDir
55
+ }) {
56
+ if (!dll || uiDll || docsDll) {
43
57
  warnDLLsDeprecated();
44
58
  }
59
+
60
+ if (staticDir) {
61
+ warnStaticDirDeprecated();
62
+ }
45
63
  }
@@ -26,6 +26,8 @@ var _serverStatics = require("./utils/server-statics");
26
26
 
27
27
  var _storiesJson = require("./utils/stories-json");
28
28
 
29
+ var _getServerChannel = require("./utils/get-server-channel");
30
+
29
31
  var _openInBrowser = require("./utils/open-in-browser");
30
32
 
31
33
  var _getPreviewBuilder = require("./utils/get-preview-builder");
@@ -58,6 +60,7 @@ async function storybookDevServer(options) {
58
60
  var startTime = process.hrtime();
59
61
  var app = (0, _express.default)();
60
62
  var server = await (0, _serverInit.getServer)(app, options);
63
+ var serverChannel = (0, _getServerChannel.getServerChannel)(server);
61
64
  app.use((0, _compression.default)({
62
65
  level: 1
63
66
  }));
@@ -76,7 +79,7 @@ async function storybookDevServer(options) {
76
79
  var features = await options.presets.apply('features');
77
80
 
78
81
  if (features !== null && features !== void 0 && features.buildStoriesJson || features !== null && features !== void 0 && features.storyStoreV7) {
79
- await (0, _storiesJson.useStoriesJson)(router, options);
82
+ await (0, _storiesJson.useStoriesJson)(router, serverChannel, options);
80
83
  }
81
84
 
82
85
  (0, _middleware.getMiddleware)(options.configDir)(router);
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.copyAllStaticFiles = copyAllStaticFiles;
7
+ exports.copyAllStaticFilesRelativeToMain = copyAllStaticFilesRelativeToMain;
7
8
 
8
9
  require("core-js/modules/es.promise.js");
9
10
 
@@ -15,6 +16,8 @@ var _path = _interopRequireDefault(require("path"));
15
16
 
16
17
  var _nodeLogger = require("@storybook/node-logger");
17
18
 
19
+ var _coreCommon = require("@storybook/core-common");
20
+
18
21
  var _serverStatics = require("./server-statics");
19
22
 
20
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -50,4 +53,34 @@ async function copyAllStaticFiles(staticDirs, outputDir) {
50
53
  }
51
54
  }));
52
55
  }
56
+ }
57
+
58
+ async function copyAllStaticFilesRelativeToMain(staticDirs, outputDir, configDir) {
59
+ staticDirs.forEach(async function (dir) {
60
+ var staticDirAndTarget = typeof dir === 'string' ? dir : `${dir.from}:${dir.to}`;
61
+
62
+ var _await$parseStaticDir2 = await (0, _serverStatics.parseStaticDir)((0, _coreCommon.getDirectoryFromWorkingDir)({
63
+ configDir: configDir,
64
+ workingDir: process.cwd(),
65
+ directory: staticDirAndTarget
66
+ })),
67
+ from = _await$parseStaticDir2.staticPath,
68
+ to = _await$parseStaticDir2.targetEndpoint;
69
+
70
+ var targetPath = _path.default.join(outputDir, to);
71
+
72
+ var skipPaths = ['index.html', 'iframe.html'].map(function (f) {
73
+ return _path.default.join(targetPath, f);
74
+ });
75
+
76
+ _nodeLogger.logger.info((0, _chalk.default)`=> Copying static files: {cyan ${from}} at {cyan ${targetPath}}`);
77
+
78
+ await _fsExtra.default.copy(from, targetPath, {
79
+ dereference: true,
80
+ preserveTimestamps: true,
81
+ filter: function (_, dest) {
82
+ return !skipPaths.includes(dest);
83
+ }
84
+ });
85
+ });
53
86
  }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getServerChannel = getServerChannel;
7
+ exports.ServerChannel = void 0;
8
+
9
+ var _ws = _interopRequireWildcard(require("ws"));
10
+
11
+ var _telejson = require("telejson");
12
+
13
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
14
+
15
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
16
+
17
+ class ServerChannel {
18
+ constructor(server) {
19
+ this.webSocketServer = void 0;
20
+ this.webSocketServer = new _ws.WebSocketServer({
21
+ server: server
22
+ });
23
+ }
24
+
25
+ emit(type, args = []) {
26
+ var event = {
27
+ type: type,
28
+ args: args
29
+ };
30
+ var data = (0, _telejson.stringify)(event, {
31
+ maxDepth: 15,
32
+ allowFunction: true
33
+ });
34
+ Array.from(this.webSocketServer.clients).filter(function (c) {
35
+ return c.readyState === _ws.default.OPEN;
36
+ }).forEach(function (client) {
37
+ return client.send(data);
38
+ });
39
+ }
40
+
41
+ }
42
+
43
+ exports.ServerChannel = ServerChannel;
44
+
45
+ function getServerChannel(server) {
46
+ return new ServerChannel(server);
47
+ }
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getServerAddresses = getServerAddresses;
7
- exports.getServerPort = void 0;
7
+ exports.getServerChannelUrl = exports.getServerPort = void 0;
8
8
 
9
9
  var _ip = _interopRequireDefault(require("ip"));
10
10
 
@@ -29,4 +29,12 @@ var getServerPort = function (port) {
29
29
  });
30
30
  };
31
31
 
32
- exports.getServerPort = getServerPort;
32
+ exports.getServerPort = getServerPort;
33
+
34
+ var getServerChannelUrl = function (port, {
35
+ https: https
36
+ }) {
37
+ return `${https ? 'wss' : 'ws'}://localhost:${port}/`;
38
+ };
39
+
40
+ exports.getServerChannelUrl = getServerChannelUrl;
@@ -12,6 +12,8 @@ require("core-js/modules/es.promise.js");
12
12
 
13
13
  var _nodeLogger = require("@storybook/node-logger");
14
14
 
15
+ var _coreCommon = require("@storybook/core-common");
16
+
15
17
  var _chalk = _interopRequireDefault(require("chalk"));
16
18
 
17
19
  var _express = _interopRequireDefault(require("express"));
@@ -42,14 +44,43 @@ var defaultFavIcon = require.resolve('../public/favicon.ico');
42
44
 
43
45
  async function useStatics(router, options) {
44
46
  var hasCustomFavicon = false;
47
+ var staticDirs = await options.presets.apply('staticDirs', []);
48
+
49
+ if (staticDirs && options.staticDir) {
50
+ throw new Error((0, _tsDedent.default)`
51
+ Conflict when trying to read staticDirs:
52
+ * Storybook's configuration option: 'staticDirs'
53
+ * Storybook's CLI flag: '--staticDir' or '-s'
54
+
55
+ Choose one of them, but not both.
56
+ `);
57
+ }
58
+
59
+ staticDirs.forEach(async function (dir) {
60
+ var staticDirAndTarget = typeof dir === 'string' ? dir : `${dir.from}:${dir.to}`;
61
+
62
+ var _await$parseStaticDir = await parseStaticDir((0, _coreCommon.getDirectoryFromWorkingDir)({
63
+ configDir: options.configDir,
64
+ workingDir: process.cwd(),
65
+ directory: staticDirAndTarget
66
+ })),
67
+ from = _await$parseStaticDir.staticPath,
68
+ to = _await$parseStaticDir.targetEndpoint;
69
+
70
+ _nodeLogger.logger.info((0, _chalk.default)`=> Serving static files from {cyan ${from}} at {cyan ${to}}`);
71
+
72
+ router.use(to, _express.default.static(from, {
73
+ index: false
74
+ }));
75
+ });
45
76
 
46
77
  if (options.staticDir && options.staticDir.length > 0) {
47
78
  await Promise.all(options.staticDir.map(async function (dir) {
48
79
  try {
49
- var _await$parseStaticDir = await parseStaticDir(dir),
50
- staticDir = _await$parseStaticDir.staticDir,
51
- staticPath = _await$parseStaticDir.staticPath,
52
- targetEndpoint = _await$parseStaticDir.targetEndpoint;
80
+ var _await$parseStaticDir2 = await parseStaticDir(dir),
81
+ staticDir = _await$parseStaticDir2.staticDir,
82
+ staticPath = _await$parseStaticDir2.staticPath,
83
+ targetEndpoint = _await$parseStaticDir2.targetEndpoint;
53
84
 
54
85
  _nodeLogger.logger.info((0, _chalk.default)`=> Serving static files from {cyan ${staticDir}} at {cyan ${targetEndpoint}}`);
55
86
 
@@ -11,22 +11,19 @@ require("core-js/modules/es.promise.js");
11
11
 
12
12
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
13
13
 
14
- var _events = _interopRequireDefault(require("events"));
15
-
16
14
  var _coreCommon = require("@storybook/core-common");
17
15
 
16
+ var _coreEvents = _interopRequireDefault(require("@storybook/core-events"));
17
+
18
18
  var _debounce = _interopRequireDefault(require("lodash/debounce"));
19
19
 
20
20
  var _StoryIndexGenerator = require("./StoryIndexGenerator");
21
21
 
22
22
  var _watchStorySpecifiers = require("./watch-story-specifiers");
23
23
 
24
- var _useEventsAsSse = require("./use-events-as-sse");
25
-
26
24
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
25
 
28
- var INVALIDATE = 'INVALIDATE';
29
- var DEBOUNCE = 50;
26
+ var DEBOUNCE = 100;
30
27
  exports.DEBOUNCE = DEBOUNCE;
31
28
 
32
29
  async function extractStoriesJson(outputFile, normalizedStories, options) {
@@ -36,7 +33,7 @@ async function extractStoriesJson(outputFile, normalizedStories, options) {
36
33
  await _fsExtra.default.writeJson(outputFile, index);
37
34
  }
38
35
 
39
- async function useStoriesJson(router, options, workingDir = process.cwd()) {
36
+ async function useStoriesJson(router, serverChannel, options, workingDir = process.cwd()) {
40
37
  var normalizedStories = (0, _coreCommon.normalizeStories)(await options.presets.apply('stories'), {
41
38
  configDir: options.configDir,
42
39
  workingDir: workingDir
@@ -50,9 +47,8 @@ async function useStoriesJson(router, options, workingDir = process.cwd()) {
50
47
  // This is mainly for testing purposes.
51
48
 
52
49
  var started = false;
53
- var invalidationEmitter = new _events.default();
54
50
  var maybeInvalidate = (0, _debounce.default)(function () {
55
- return invalidationEmitter.emit(INVALIDATE);
51
+ return serverChannel.emit(_coreEvents.default.STORY_INDEX_INVALIDATED);
56
52
  }, DEBOUNCE, {
57
53
  leading: true
58
54
  });
@@ -69,10 +65,8 @@ async function useStoriesJson(router, options, workingDir = process.cwd()) {
69
65
  await generator.initialize();
70
66
  }
71
67
 
72
- var eventsAsSSE = (0, _useEventsAsSse.useEventsAsSSE)(invalidationEmitter, [INVALIDATE]);
73
68
  router.use('/stories.json', async function (req, res) {
74
69
  await ensureStarted();
75
- if (eventsAsSSE(req, res)) return;
76
70
 
77
71
  try {
78
72
  var index = await generator.getIndex();
@@ -29,7 +29,7 @@ import { getReleaseNotesData, getReleaseNotesFailedState } from './utils/release
29
29
  import { outputStats } from './utils/output-stats';
30
30
  import { outputStartupInformation } from './utils/output-startup-information';
31
31
  import { updateCheck } from './utils/update-check';
32
- import { getServerPort } from './utils/server-address';
32
+ import { getServerPort, getServerChannelUrl } from './utils/server-address';
33
33
  import { getPreviewBuilder } from './utils/get-preview-builder';
34
34
  import { getManagerBuilder } from './utils/get-manager-builder';
35
35
  export async function buildDevStandalone(options) {
@@ -70,6 +70,7 @@ export async function buildDevStandalone(options) {
70
70
  options.configType = 'DEVELOPMENT';
71
71
  options.configDir = path.resolve(options.configDir);
72
72
  options.outputDir = options.smokeTest ? resolvePathInStorybookCache('public') : path.resolve(options.outputDir || resolvePathInStorybookCache('public'));
73
+ options.serverChannelUrl = getServerChannelUrl(port, options);
73
74
  /* eslint-enable no-param-reassign */
74
75
 
75
76
  var previewBuilder = await getPreviewBuilder(options.configDir);
@@ -26,11 +26,12 @@ import chalk from 'chalk';
26
26
  import cpy from 'cpy';
27
27
  import fs from 'fs-extra';
28
28
  import path from 'path';
29
+ import dedent from 'ts-dedent';
29
30
  import { logger } from '@storybook/node-logger';
30
31
  import { loadAllPresets, cache, normalizeStories, logConfig } from '@storybook/core-common';
31
32
  import { getProdCli } from './cli';
32
33
  import { outputStats } from './utils/output-stats';
33
- import { copyAllStaticFiles } from './utils/copy-all-static-files';
34
+ import { copyAllStaticFiles, copyAllStaticFilesRelativeToMain } from './utils/copy-all-static-files';
34
35
  import { getPreviewBuilder } from './utils/get-preview-builder';
35
36
  import { getManagerBuilder } from './utils/get-manager-builder';
36
37
  import { extractStoriesJson } from './utils/stories-json';
@@ -62,13 +63,32 @@ export async function buildStaticStandalone(options) {
62
63
 
63
64
  await fs.emptyDir(options.outputDir);
64
65
  await cpy(defaultFavIcon, options.outputDir);
65
- await copyAllStaticFiles(options.staticDir, options.outputDir);
66
66
  var previewBuilder = await getPreviewBuilder(options.configDir);
67
67
  var managerBuilder = await getManagerBuilder(options.configDir);
68
68
  var presets = loadAllPresets(_objectSpread({
69
69
  corePresets: [require.resolve('./presets/common-preset'), ...managerBuilder.corePresets, ...previewBuilder.corePresets, require.resolve('./presets/babel-cache-preset')],
70
70
  overridePresets: previewBuilder.overridePresets
71
71
  }, options));
72
+ var staticDirs = await presets.apply('staticDirs');
73
+
74
+ if (staticDirs && options.staticDir) {
75
+ throw new Error(dedent`
76
+ Conflict when trying to read staticDirs:
77
+ * Storybook's configuration option: 'staticDirs'
78
+ * Storybook's CLI flag: '--staticDir' or '-s'
79
+
80
+ Choose one of them, but not both.
81
+ `);
82
+ }
83
+
84
+ if (staticDirs) {
85
+ await copyAllStaticFilesRelativeToMain(staticDirs, options.outputDir, options.configDir);
86
+ }
87
+
88
+ if (options.staticDir) {
89
+ await copyAllStaticFiles(options.staticDir, options.outputDir);
90
+ }
91
+
72
92
  var features = await presets.apply('features');
73
93
 
74
94
  if (features !== null && features !== void 0 && features.buildStoriesJson || features !== null && features !== void 0 && features.storyStoreV7) {
@@ -17,13 +17,32 @@ export function getEnvConfig(program, configEnv) {
17
17
  }
18
18
  });
19
19
  }
20
- var warnDLLsDeprecated = deprecate(function () {}, dedent`
20
+
21
+ var warnDeprecatedFlag = function (message) {
22
+ return deprecate(function () {}, dedent(message));
23
+ };
24
+
25
+ var warnDLLsDeprecated = warnDeprecatedFlag(`
21
26
  DLL-related CLI flags are deprecated, see:
22
27
 
23
28
  https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-dll-flags
24
29
  `);
25
- export function checkDeprecatedFlags(options) {
26
- if (!options.dll || options.uiDll || options.docsDll) {
30
+ var warnStaticDirDeprecated = warnDeprecatedFlag(`
31
+ --static-dir CLI flag is deprecated, see:
32
+
33
+ https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-static-dir-flag
34
+ `);
35
+ export function checkDeprecatedFlags({
36
+ dll: dll,
37
+ uiDll: uiDll,
38
+ docsDll: docsDll,
39
+ staticDir: staticDir
40
+ }) {
41
+ if (!dll || uiDll || docsDll) {
27
42
  warnDLLsDeprecated();
28
43
  }
44
+
45
+ if (staticDir) {
46
+ warnStaticDirDeprecated();
47
+ }
29
48
  }
@@ -20,6 +20,7 @@ import { getServerAddresses } from './utils/server-address';
20
20
  import { getServer } from './utils/server-init';
21
21
  import { useStatics } from './utils/server-statics';
22
22
  import { useStoriesJson } from './utils/stories-json';
23
+ import { getServerChannel } from './utils/get-server-channel';
23
24
  import { openInBrowser } from './utils/open-in-browser';
24
25
  import { getPreviewBuilder } from './utils/get-preview-builder';
25
26
  import { getManagerBuilder } from './utils/get-manager-builder'; // @ts-ignore
@@ -29,6 +30,7 @@ export async function storybookDevServer(options) {
29
30
  var startTime = process.hrtime();
30
31
  var app = express();
31
32
  var server = await getServer(app, options);
33
+ var serverChannel = getServerChannel(server);
32
34
  app.use(compression({
33
35
  level: 1
34
36
  }));
@@ -47,7 +49,7 @@ export async function storybookDevServer(options) {
47
49
  var features = await options.presets.apply('features');
48
50
 
49
51
  if (features !== null && features !== void 0 && features.buildStoriesJson || features !== null && features !== void 0 && features.storyStoreV7) {
50
- await useStoriesJson(router, options);
52
+ await useStoriesJson(router, serverChannel, options);
51
53
  }
52
54
 
53
55
  getMiddleware(options.configDir)(router);
@@ -3,6 +3,7 @@ import chalk from 'chalk';
3
3
  import fs from 'fs-extra';
4
4
  import path from 'path';
5
5
  import { logger } from '@storybook/node-logger';
6
+ import { getDirectoryFromWorkingDir } from '@storybook/core-common';
6
7
  import { parseStaticDir } from './server-statics';
7
8
  export async function copyAllStaticFiles(staticDirs, outputDir) {
8
9
  if (staticDirs && staticDirs.length > 0) {
@@ -32,4 +33,30 @@ export async function copyAllStaticFiles(staticDirs, outputDir) {
32
33
  }
33
34
  }));
34
35
  }
36
+ }
37
+ export async function copyAllStaticFilesRelativeToMain(staticDirs, outputDir, configDir) {
38
+ staticDirs.forEach(async function (dir) {
39
+ var staticDirAndTarget = typeof dir === 'string' ? dir : `${dir.from}:${dir.to}`;
40
+
41
+ var _await$parseStaticDir2 = await parseStaticDir(getDirectoryFromWorkingDir({
42
+ configDir: configDir,
43
+ workingDir: process.cwd(),
44
+ directory: staticDirAndTarget
45
+ })),
46
+ from = _await$parseStaticDir2.staticPath,
47
+ to = _await$parseStaticDir2.targetEndpoint;
48
+
49
+ var targetPath = path.join(outputDir, to);
50
+ var skipPaths = ['index.html', 'iframe.html'].map(function (f) {
51
+ return path.join(targetPath, f);
52
+ });
53
+ logger.info(chalk`=> Copying static files: {cyan ${from}} at {cyan ${targetPath}}`);
54
+ await fs.copy(from, targetPath, {
55
+ dereference: true,
56
+ preserveTimestamps: true,
57
+ filter: function (_, dest) {
58
+ return !skipPaths.includes(dest);
59
+ }
60
+ });
61
+ });
35
62
  }
@@ -0,0 +1,30 @@
1
+ import WebSocket, { WebSocketServer } from 'ws';
2
+ import { stringify } from 'telejson';
3
+ export class ServerChannel {
4
+ constructor(server) {
5
+ this.webSocketServer = void 0;
6
+ this.webSocketServer = new WebSocketServer({
7
+ server: server
8
+ });
9
+ }
10
+
11
+ emit(type, args = []) {
12
+ var event = {
13
+ type: type,
14
+ args: args
15
+ };
16
+ var data = stringify(event, {
17
+ maxDepth: 15,
18
+ allowFunction: true
19
+ });
20
+ Array.from(this.webSocketServer.clients).filter(function (c) {
21
+ return c.readyState === WebSocket.OPEN;
22
+ }).forEach(function (client) {
23
+ return client.send(data);
24
+ });
25
+ }
26
+
27
+ }
28
+ export function getServerChannel(server) {
29
+ return new ServerChannel(server);
30
+ }
@@ -12,4 +12,9 @@ export var getServerPort = function (port) {
12
12
  logger.error(error);
13
13
  process.exit(-1);
14
14
  });
15
+ };
16
+ export var getServerChannelUrl = function (port, {
17
+ https: https
18
+ }) {
19
+ return `${https ? 'wss' : 'ws'}://localhost:${port}/`;
15
20
  };
@@ -13,6 +13,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
13
  import "core-js/modules/es.promise.js";
14
14
  import "core-js/modules/es.symbol.description.js";
15
15
  import { logger } from '@storybook/node-logger';
16
+ import { getDirectoryFromWorkingDir } from '@storybook/core-common';
16
17
  import chalk from 'chalk';
17
18
  import express from 'express';
18
19
  import { pathExists } from 'fs-extra';
@@ -24,14 +25,42 @@ var defaultFavIcon = require.resolve('../public/favicon.ico');
24
25
 
25
26
  export async function useStatics(router, options) {
26
27
  var hasCustomFavicon = false;
28
+ var staticDirs = await options.presets.apply('staticDirs', []);
29
+
30
+ if (staticDirs && options.staticDir) {
31
+ throw new Error(dedent`
32
+ Conflict when trying to read staticDirs:
33
+ * Storybook's configuration option: 'staticDirs'
34
+ * Storybook's CLI flag: '--staticDir' or '-s'
35
+
36
+ Choose one of them, but not both.
37
+ `);
38
+ }
39
+
40
+ staticDirs.forEach(async function (dir) {
41
+ var staticDirAndTarget = typeof dir === 'string' ? dir : `${dir.from}:${dir.to}`;
42
+
43
+ var _await$parseStaticDir = await parseStaticDir(getDirectoryFromWorkingDir({
44
+ configDir: options.configDir,
45
+ workingDir: process.cwd(),
46
+ directory: staticDirAndTarget
47
+ })),
48
+ from = _await$parseStaticDir.staticPath,
49
+ to = _await$parseStaticDir.targetEndpoint;
50
+
51
+ logger.info(chalk`=> Serving static files from {cyan ${from}} at {cyan ${to}}`);
52
+ router.use(to, express.static(from, {
53
+ index: false
54
+ }));
55
+ });
27
56
 
28
57
  if (options.staticDir && options.staticDir.length > 0) {
29
58
  await Promise.all(options.staticDir.map(async function (dir) {
30
59
  try {
31
- var _await$parseStaticDir = await parseStaticDir(dir),
32
- staticDir = _await$parseStaticDir.staticDir,
33
- staticPath = _await$parseStaticDir.staticPath,
34
- targetEndpoint = _await$parseStaticDir.targetEndpoint;
60
+ var _await$parseStaticDir2 = await parseStaticDir(dir),
61
+ staticDir = _await$parseStaticDir2.staticDir,
62
+ staticPath = _await$parseStaticDir2.staticPath,
63
+ targetEndpoint = _await$parseStaticDir2.targetEndpoint;
35
64
 
36
65
  logger.info(chalk`=> Serving static files from {cyan ${staticDir}} at {cyan ${targetEndpoint}}`);
37
66
  router.use(targetEndpoint, express.static(staticPath, {
@@ -1,20 +1,18 @@
1
1
  import "core-js/modules/es.promise.js";
2
2
  import fs from 'fs-extra';
3
- import EventEmitter from 'events';
4
3
  import { normalizeStories } from '@storybook/core-common';
4
+ import Events from '@storybook/core-events';
5
5
  import debounce from 'lodash/debounce';
6
6
  import { StoryIndexGenerator } from './StoryIndexGenerator';
7
7
  import { watchStorySpecifiers } from './watch-story-specifiers';
8
- import { useEventsAsSSE } from './use-events-as-sse';
9
- var INVALIDATE = 'INVALIDATE';
10
- export var DEBOUNCE = 50;
8
+ export var DEBOUNCE = 100;
11
9
  export async function extractStoriesJson(outputFile, normalizedStories, options) {
12
10
  var generator = new StoryIndexGenerator(normalizedStories, options);
13
11
  await generator.initialize();
14
12
  var index = await generator.getIndex();
15
13
  await fs.writeJson(outputFile, index);
16
14
  }
17
- export async function useStoriesJson(router, options, workingDir = process.cwd()) {
15
+ export async function useStoriesJson(router, serverChannel, options, workingDir = process.cwd()) {
18
16
  var normalizedStories = normalizeStories(await options.presets.apply('stories'), {
19
17
  configDir: options.configDir,
20
18
  workingDir: workingDir
@@ -28,9 +26,8 @@ export async function useStoriesJson(router, options, workingDir = process.cwd()
28
26
  // This is mainly for testing purposes.
29
27
 
30
28
  var started = false;
31
- var invalidationEmitter = new EventEmitter();
32
29
  var maybeInvalidate = debounce(function () {
33
- return invalidationEmitter.emit(INVALIDATE);
30
+ return serverChannel.emit(Events.STORY_INDEX_INVALIDATED);
34
31
  }, DEBOUNCE, {
35
32
  leading: true
36
33
  });
@@ -47,10 +44,8 @@ export async function useStoriesJson(router, options, workingDir = process.cwd()
47
44
  await generator.initialize();
48
45
  }
49
46
 
50
- var eventsAsSSE = useEventsAsSSE(invalidationEmitter, [INVALIDATE]);
51
47
  router.use('/stories.json', async function (req, res) {
52
48
  await ensureStarted();
53
- if (eventsAsSSE(req, res)) return;
54
49
 
55
50
  try {
56
51
  var index = await generator.getIndex();