react-cosmos 6.0.0-alpha.8 → 6.0.0-beta.0

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 (158) hide show
  1. package/bin/cosmos-export.js +1 -3
  2. package/bin/cosmos-native.js +1 -3
  3. package/bin/cosmos.js +1 -7
  4. package/client.d.ts +1 -0
  5. package/client.js +1 -0
  6. package/config.schema.json +53 -15
  7. package/dist/cjsMocks/requireModule.js +7 -0
  8. package/dist/cjsMocks/resolve.js +7 -0
  9. package/dist/cjsMocks/staticPath.js +4 -0
  10. package/dist/client.d.ts +1 -0
  11. package/dist/client.js +1 -0
  12. package/dist/corePlugins/exposeImports.d.ts +2 -0
  13. package/dist/corePlugins/exposeImports.js +53 -0
  14. package/dist/corePlugins/httpProxy.d.ts +2 -0
  15. package/dist/corePlugins/httpProxy.js +21 -0
  16. package/dist/corePlugins/index.d.ts +2 -0
  17. package/dist/corePlugins/index.js +12 -0
  18. package/dist/corePlugins/openFile.d.ts +2 -0
  19. package/dist/corePlugins/openFile.js +59 -0
  20. package/dist/corePlugins/pluginEndpoint.d.ts +2 -0
  21. package/dist/corePlugins/pluginEndpoint.js +22 -0
  22. package/dist/corePlugins/portRetry.d.ts +2 -0
  23. package/dist/corePlugins/portRetry.js +14 -0
  24. package/dist/{server/cosmosConfig → cosmosConfig}/createCosmosConfig.js +37 -11
  25. package/dist/{server/cosmosConfig → cosmosConfig}/shared.js +2 -2
  26. package/dist/{server/cosmosConfig → cosmosConfig}/types.d.ts +12 -2
  27. package/dist/{server/cosmosPlugin → cosmosPlugin}/findCosmosPluginConfigs.js +2 -2
  28. package/dist/{server/cosmosPlugin → cosmosPlugin}/readCosmosPluginConfig.js +4 -7
  29. package/dist/{server/cosmosPlugin → cosmosPlugin}/types.d.ts +15 -3
  30. package/dist/devServer/expressApp.d.ts +5 -0
  31. package/dist/{server/devServer → devServer}/expressApp.js +2 -2
  32. package/dist/{server/devServer → devServer}/messageHandler.d.ts +1 -1
  33. package/dist/{server/devServer → devServer}/messageHandler.js +2 -2
  34. package/dist/devServer/startDevServer.d.ts +2 -0
  35. package/dist/{server/devServer → devServer}/startDevServer.js +23 -29
  36. package/dist/{server/export → export}/generateExport.js +21 -18
  37. package/dist/{server/getFixtures → getFixtures}/getFixtures.d.ts +6 -2
  38. package/dist/getFixtures/getFixtures.js +53 -0
  39. package/dist/getFixtures/importUserModules.d.ts +8 -0
  40. package/dist/getFixtures/importUserModules.js +28 -0
  41. package/dist/index.d.ts +20 -0
  42. package/dist/index.js +20 -1
  43. package/dist/jest.d.ts +4 -7
  44. package/dist/jest.js +4 -7
  45. package/dist/shared/applyServerConfigPlugins.d.ts +11 -0
  46. package/dist/shared/applyServerConfigPlugins.js +14 -0
  47. package/dist/shared/findNextAvailablePort.d.ts +1 -0
  48. package/dist/shared/findNextAvailablePort.js +29 -0
  49. package/dist/shared/getServerPlugins.d.ts +2 -0
  50. package/dist/shared/getServerPlugins.js +6 -0
  51. package/dist/shared/importServerPlugins.d.ts +3 -0
  52. package/dist/shared/importServerPlugins.js +16 -0
  53. package/dist/shared/playgroundHtml.d.ts +5 -0
  54. package/dist/{server/shared → shared}/playgroundHtml.js +25 -33
  55. package/dist/shared/staticPath.d.ts +1 -0
  56. package/dist/{server/shared → shared}/staticServer.js +3 -1
  57. package/dist/{server/testHelpers → testHelpers}/mockConsole.js +8 -4
  58. package/dist/{server/testHelpers → testHelpers}/mockFs.d.ts +2 -2
  59. package/dist/{server/testHelpers → testHelpers}/mockFs.js +26 -8
  60. package/dist/{server/testHelpers → testHelpers}/mockYargs.js +1 -0
  61. package/dist/{server/userDeps → userModules}/findUserModulePaths.js +2 -2
  62. package/dist/userModules/generateUserImports.d.ts +10 -0
  63. package/dist/{server/userDeps/generateUserDepsModule.js → userModules/generateUserImports.js} +8 -3
  64. package/dist/userModules/shared.d.ts +15 -0
  65. package/dist/{server/userDeps → userModules}/shared.js +16 -0
  66. package/dist/userModules/userImportsLazyTemplate.d.ts +2 -0
  67. package/dist/userModules/userImportsLazyTemplate.js +54 -0
  68. package/dist/userModules/userImportsTemplate.d.ts +2 -0
  69. package/dist/userModules/userImportsTemplate.js +56 -0
  70. package/dist/{server/utils → utils}/cli.d.ts +1 -0
  71. package/dist/utils/cli.js +4 -0
  72. package/dist/utils/fs.d.ts +5 -0
  73. package/dist/{server/utils → utils}/fs.js +10 -6
  74. package/dist/utils/json.d.ts +3 -0
  75. package/dist/utils/requireModule.d.ts +2 -0
  76. package/dist/utils/resolve.d.ts +2 -0
  77. package/package.json +10 -9
  78. package/dist/server/devServer/corePlugins/httpProxy.d.ts +0 -2
  79. package/dist/server/devServer/corePlugins/httpProxy.js +0 -18
  80. package/dist/server/devServer/corePlugins/openFile.d.ts +0 -2
  81. package/dist/server/devServer/corePlugins/openFile.js +0 -53
  82. package/dist/server/devServer/corePlugins/pluginEndpoint.d.ts +0 -2
  83. package/dist/server/devServer/corePlugins/pluginEndpoint.js +0 -17
  84. package/dist/server/devServer/corePlugins/userDepsFile.d.ts +0 -2
  85. package/dist/server/devServer/corePlugins/userDepsFile.js +0 -37
  86. package/dist/server/devServer/expressApp.d.ts +0 -5
  87. package/dist/server/devServer/startDevServer.d.ts +0 -2
  88. package/dist/server/getFixtures/getFixtures.js +0 -63
  89. package/dist/server/shared/playgroundHtml.d.ts +0 -6
  90. package/dist/server/shared/requirePluginModule.d.ts +0 -2
  91. package/dist/server/shared/requirePluginModule.js +0 -10
  92. package/dist/server/shared/resolveRendererUrl.d.ts +0 -1
  93. package/dist/server/shared/resolveRendererUrl.js +0 -7
  94. package/dist/server/testHelpers/mockEsmRequire.js +0 -13
  95. package/dist/server/testHelpers/mockEsmResolve.d.ts +0 -1
  96. package/dist/server/testHelpers/mockEsmResolve.js +0 -13
  97. package/dist/server/testHelpers/mockEsmStaticPath.d.ts +0 -1
  98. package/dist/server/testHelpers/mockEsmStaticPath.js +0 -7
  99. package/dist/server/userDeps/generateUserDepsModule.d.ts +0 -9
  100. package/dist/server/userDeps/getUserModules.d.ts +0 -8
  101. package/dist/server/userDeps/getUserModules.js +0 -26
  102. package/dist/server/userDeps/shared.d.ts +0 -5
  103. package/dist/server/userDeps/userDepsTemplate.d.ts +0 -11
  104. package/dist/server/userDeps/userDepsTemplate.js +0 -49
  105. package/dist/server/utils/cli.js +0 -4
  106. package/dist/server/utils/fs.d.ts +0 -4
  107. package/dist/server.d.ts +0 -20
  108. package/dist/server.js +0 -20
  109. package/server.d.ts +0 -1
  110. package/server.js +0 -1
  111. /package/dist/{server/utils → cjsMocks}/requireModule.d.ts +0 -0
  112. /package/dist/{server/utils → cjsMocks}/resolve.d.ts +0 -0
  113. /package/dist/{server/shared → cjsMocks}/staticPath.d.ts +0 -0
  114. /package/dist/{server/cosmosConfig → cosmosConfig}/createCosmosConfig.d.ts +0 -0
  115. /package/dist/{server/cosmosConfig → cosmosConfig}/detectCosmosConfig.d.ts +0 -0
  116. /package/dist/{server/cosmosConfig → cosmosConfig}/detectCosmosConfig.js +0 -0
  117. /package/dist/{server/cosmosConfig → cosmosConfig}/getCosmosConfigAtPath.d.ts +0 -0
  118. /package/dist/{server/cosmosConfig → cosmosConfig}/getCosmosConfigAtPath.js +0 -0
  119. /package/dist/{server/cosmosConfig → cosmosConfig}/shared.d.ts +0 -0
  120. /package/dist/{server/cosmosConfig → cosmosConfig}/types.js +0 -0
  121. /package/dist/{server/cosmosPlugin → cosmosPlugin}/findCosmosPluginConfigs.d.ts +0 -0
  122. /package/dist/{server/cosmosPlugin → cosmosPlugin}/pluginConfigs.d.ts +0 -0
  123. /package/dist/{server/cosmosPlugin → cosmosPlugin}/pluginConfigs.js +0 -0
  124. /package/dist/{server/cosmosPlugin → cosmosPlugin}/readCosmosPluginConfig.d.ts +0 -0
  125. /package/dist/{server/cosmosPlugin → cosmosPlugin}/types.js +0 -0
  126. /package/dist/{server/devServer → devServer}/httpServer.d.ts +0 -0
  127. /package/dist/{server/devServer → devServer}/httpServer.js +0 -0
  128. /package/dist/{server/export → export}/generateExport.d.ts +0 -0
  129. /package/dist/{server/shared → shared}/logPluginInfo.d.ts +0 -0
  130. /package/dist/{server/shared → shared}/logPluginInfo.js +0 -0
  131. /package/dist/{server/shared → shared}/playgroundUrl.d.ts +0 -0
  132. /package/dist/{server/shared → shared}/playgroundUrl.js +0 -0
  133. /package/dist/{server/shared → shared}/staticPath.js +0 -0
  134. /package/dist/{server/shared → shared}/staticServer.d.ts +0 -0
  135. /package/dist/{server/static → static}/favicon.ico +0 -0
  136. /package/dist/{server/static → static}/index.html +0 -0
  137. /package/dist/{server/testHelpers → testHelpers}/cwd.d.ts +0 -0
  138. /package/dist/{server/testHelpers → testHelpers}/cwd.js +0 -0
  139. /package/dist/{server/testHelpers → testHelpers}/mockConsole.d.ts +0 -0
  140. /package/dist/{server/testHelpers → testHelpers}/mockFetch.d.ts +0 -0
  141. /package/dist/{server/testHelpers → testHelpers}/mockFetch.js +0 -0
  142. /package/dist/{server/testHelpers → testHelpers}/mockYargs.d.ts +0 -0
  143. /package/dist/{server/userDeps → userModules}/findUserModulePaths.d.ts +0 -0
  144. /package/dist/{server/userDeps → userModules}/fixtureWatcher.d.ts +0 -0
  145. /package/dist/{server/userDeps → userModules}/fixtureWatcher.js +0 -0
  146. /package/dist/{server/utils → utils}/isNodeError.d.ts +0 -0
  147. /package/dist/{server/utils → utils}/isNodeError.js +0 -0
  148. /package/dist/{server/testHelpers/mockEsmRequire.d.ts → utils/json.js} +0 -0
  149. /package/dist/{server/utils → utils}/requireModule.js +0 -0
  150. /package/dist/{server/utils → utils}/requireSilent.d.ts +0 -0
  151. /package/dist/{server/utils → utils}/requireSilent.js +0 -0
  152. /package/dist/{server/utils → utils}/resolve.js +0 -0
  153. /package/dist/{server/utils → utils}/resolveLoose.d.ts +0 -0
  154. /package/dist/{server/utils → utils}/resolveLoose.js +0 -0
  155. /package/dist/{server/utils → utils}/resolveSilent.d.ts +0 -0
  156. /package/dist/{server/utils → utils}/resolveSilent.js +0 -0
  157. /package/dist/{server/utils → utils}/slash.d.ts +0 -0
  158. /package/dist/{server/utils → utils}/slash.js +0 -0
@@ -2,9 +2,9 @@ import express from 'express';
2
2
  import { getDevPlaygroundHtml } from '../shared/playgroundHtml.js';
3
3
  import { getStaticPath } from '../shared/staticPath.js';
4
4
  import { resolve } from '../utils/resolve.js';
5
- export async function createExpressApp(platformType, cosmosConfig, pluginConfigs) {
5
+ export async function createExpressApp(platform, cosmosConfig, pluginConfigs) {
6
6
  const app = express();
7
- const playgroundHtml = await getDevPlaygroundHtml(platformType, cosmosConfig, pluginConfigs);
7
+ const playgroundHtml = await getDevPlaygroundHtml(platform, cosmosConfig, pluginConfigs);
8
8
  app.get('/', (req, res) => {
9
9
  res.send(playgroundHtml);
10
10
  });
@@ -3,5 +3,5 @@ import http from 'http';
3
3
  import { ServerMessage } from 'react-cosmos-core';
4
4
  export declare function createMessageHandler(httpServer: http.Server): {
5
5
  sendMessage: (msg: ServerMessage) => void;
6
- cleanUp: () => void;
6
+ close: () => void;
7
7
  };
@@ -21,9 +21,9 @@ export function createMessageHandler(httpServer) {
21
21
  }
22
22
  });
23
23
  }
24
- function cleanUp() {
24
+ function close() {
25
25
  wss.clients.forEach(client => client.close());
26
26
  wss.close();
27
27
  }
28
- return { sendMessage, cleanUp };
28
+ return { sendMessage, close };
29
29
  }
@@ -0,0 +1,2 @@
1
+ import { CosmosPlatform } from '../cosmosPlugin/types.js';
2
+ export declare function startDevServer(platform: CosmosPlatform): Promise<() => Promise<void>>;
@@ -1,24 +1,15 @@
1
1
  import path from 'path';
2
2
  import { detectCosmosConfig, detectCosmosConfigPath, } from '../cosmosConfig/detectCosmosConfig.js';
3
3
  import { getPluginConfigs } from '../cosmosPlugin/pluginConfigs.js';
4
+ import { applyServerConfigPlugins } from '../shared/applyServerConfigPlugins.js';
5
+ import { getServerPlugins } from '../shared/getServerPlugins.js';
4
6
  import { logPluginInfo } from '../shared/logPluginInfo.js';
5
- import { requirePluginModule } from '../shared/requirePluginModule.js';
6
7
  import { serveStaticDir } from '../shared/staticServer.js';
7
- import { httpProxyDevServerPlugin } from './corePlugins/httpProxy.js';
8
- import openFileDevServerPlugin from './corePlugins/openFile.js';
9
- import pluginEndpointDevServerPlugin from './corePlugins/pluginEndpoint.js';
10
- import { userDepsFileDevServerPlugin } from './corePlugins/userDepsFile.js';
11
8
  import { createExpressApp } from './expressApp.js';
12
9
  import { createHttpServer } from './httpServer.js';
13
10
  import { createMessageHandler } from './messageHandler.js';
14
- const builtInPlugins = [
15
- userDepsFileDevServerPlugin,
16
- httpProxyDevServerPlugin,
17
- openFileDevServerPlugin,
18
- pluginEndpointDevServerPlugin,
19
- ];
20
- export async function startDevServer(platformType) {
21
- const cosmosConfig = await detectCosmosConfig();
11
+ export async function startDevServer(platform) {
12
+ let cosmosConfig = await detectCosmosConfig();
22
13
  logCosmosConfigInfo();
23
14
  const pluginConfigs = await getPluginConfigs({
24
15
  cosmosConfig,
@@ -28,24 +19,35 @@ export async function startDevServer(platformType) {
28
19
  relativePaths: false,
29
20
  });
30
21
  logPluginInfo(pluginConfigs);
31
- const app = await createExpressApp(platformType, cosmosConfig, pluginConfigs);
22
+ const serverPlugins = await getServerPlugins(pluginConfigs, cosmosConfig.rootDir);
23
+ cosmosConfig = await applyServerConfigPlugins({
24
+ cosmosConfig,
25
+ serverPlugins,
26
+ command: 'dev',
27
+ platform,
28
+ });
29
+ const app = await createExpressApp(platform, cosmosConfig, pluginConfigs);
32
30
  const httpServer = await createHttpServer(cosmosConfig, app);
33
31
  const msgHandler = createMessageHandler(httpServer.server);
32
+ if (cosmosConfig.staticPath) {
33
+ serveStaticDir(app, cosmosConfig.staticPath, cosmosConfig.publicUrl);
34
+ }
34
35
  // Make Playground available as soon as possible and show a loading screen
35
36
  // while plugins are initializing
36
37
  await httpServer.start();
37
38
  const pluginCleanupCallbacks = [];
38
39
  async function cleanUp() {
39
40
  await Promise.all(pluginCleanupCallbacks.map(cleanup => cleanup()));
41
+ msgHandler.close();
40
42
  await httpServer.stop();
41
- msgHandler.cleanUp();
42
43
  }
43
- const userPlugins = await getDevServerPlugins(pluginConfigs, cosmosConfig.rootDir);
44
- for (const plugin of [...builtInPlugins, ...userPlugins]) {
44
+ for (const plugin of serverPlugins) {
45
45
  try {
46
- const pluginReturn = await plugin({
46
+ if (!plugin.devServer)
47
+ continue;
48
+ const pluginReturn = await plugin.devServer({
47
49
  cosmosConfig,
48
- platformType,
50
+ platform,
49
51
  httpServer: httpServer.server,
50
52
  expressApp: app,
51
53
  sendMessage: msgHandler.sendMessage,
@@ -58,7 +60,7 @@ export async function startDevServer(platformType) {
58
60
  catch (err) {
59
61
  // Log when a plugin fails to clean up, but continue to attempt
60
62
  // to clean up the remaining plugins
61
- console.log(`[Cosmos][${plugin.name}] Dev server plugin cleanup failed`);
63
+ console.log(`[Cosmos][plugin:${plugin.name}] Dev server cleanup failed`);
62
64
  console.log(err);
63
65
  }
64
66
  });
@@ -67,14 +69,11 @@ export async function startDevServer(platformType) {
67
69
  catch (err) {
68
70
  // Abort starting server if a plugin init fails and attempt cleanup of all
69
71
  // plugins that have already initialized
70
- console.log(`[Cosmos][${plugin.name}] Dev server plugin init failed`);
72
+ console.log(`[Cosmos][plugin:${plugin.name}] Dev server init failed`);
71
73
  cleanUp();
72
74
  throw err;
73
75
  }
74
76
  }
75
- if (cosmosConfig.staticPath) {
76
- serveStaticDir(app, cosmosConfig.staticPath, cosmosConfig.publicUrl);
77
- }
78
77
  return cleanUp;
79
78
  }
80
79
  function logCosmosConfigInfo() {
@@ -86,8 +85,3 @@ function logCosmosConfigInfo() {
86
85
  const relConfigPath = path.relative(process.cwd(), cosmosConfigPath);
87
86
  console.log(`[Cosmos] Using cosmos config found at ${relConfigPath}`);
88
87
  }
89
- async function getDevServerPlugins(pluginConfigs, rootDir) {
90
- return Promise.all(pluginConfigs
91
- .filter(pluginConfig => pluginConfig.devServer)
92
- .map(pluginConfig => requirePluginModule(rootDir, pluginConfig, 'devServer')));
93
- }
@@ -1,16 +1,15 @@
1
1
  import fs from 'fs/promises';
2
2
  import path from 'path';
3
- import { removeLeadingSlash, } from 'react-cosmos-core';
4
3
  import { detectCosmosConfig } from '../cosmosConfig/detectCosmosConfig.js';
5
4
  import { getPluginConfigs } from '../cosmosPlugin/pluginConfigs.js';
5
+ import { applyServerConfigPlugins } from '../shared/applyServerConfigPlugins.js';
6
+ import { getServerPlugins } from '../shared/getServerPlugins.js';
6
7
  import { logPluginInfo } from '../shared/logPluginInfo.js';
7
8
  import { getExportPlaygroundHtml } from '../shared/playgroundHtml.js';
8
- import { requirePluginModule } from '../shared/requirePluginModule.js';
9
9
  import { getStaticPath } from '../shared/staticPath.js';
10
10
  import { resolve } from '../utils/resolve.js';
11
- const builtInPlugins = [];
12
11
  export async function generateExport() {
13
- const cosmosConfig = await detectCosmosConfig();
12
+ let cosmosConfig = await detectCosmosConfig();
14
13
  const pluginConfigs = await getPluginConfigs({
15
14
  cosmosConfig,
16
15
  // Relative paths work in exports because all plugin modules are copied
@@ -18,19 +17,28 @@ export async function generateExport() {
18
17
  relativePaths: true,
19
18
  });
20
19
  logPluginInfo(pluginConfigs);
20
+ const serverPlugins = await getServerPlugins(pluginConfigs, cosmosConfig.rootDir);
21
+ cosmosConfig = await applyServerConfigPlugins({
22
+ cosmosConfig,
23
+ serverPlugins,
24
+ command: 'export',
25
+ platform: 'web',
26
+ });
21
27
  // Clear previous export (or other files at export path)
22
28
  const { exportPath } = cosmosConfig;
23
29
  await fs.rm(exportPath, { recursive: true, force: true });
30
+ await fs.mkdir(exportPath, { recursive: true });
24
31
  // Copy static assets first, so that the built index.html overrides the its
25
32
  // template file (in case the static assets are served from the root path)
26
33
  await copyStaticAssets(cosmosConfig);
27
- const userPlugins = await getExportPlugins(pluginConfigs, cosmosConfig.rootDir);
28
- for (const plugin of [...builtInPlugins, ...userPlugins]) {
34
+ for (const plugin of serverPlugins) {
35
+ if (!plugin.export)
36
+ continue;
29
37
  try {
30
- await plugin({ cosmosConfig });
38
+ await plugin.export({ cosmosConfig });
31
39
  }
32
40
  catch (err) {
33
- console.log(`[Cosmos][${plugin.name}] Export plugin failed`);
41
+ console.log(`[Cosmos][plugin:${plugin.name}] Export failed`);
34
42
  throw err;
35
43
  }
36
44
  }
@@ -56,25 +64,20 @@ async function copyStaticAssets(cosmosConfig) {
56
64
  return;
57
65
  }
58
66
  const { publicUrl } = cosmosConfig;
59
- const exportStaticPath = path.resolve(exportPath, removeLeadingSlash(publicUrl));
67
+ const exportStaticPath = path.join(exportPath, publicUrl);
60
68
  fs.cp(staticPath, exportStaticPath, { recursive: true });
61
69
  }
62
- async function getExportPlugins(pluginConfigs, rootDir) {
63
- return Promise.all(pluginConfigs
64
- .filter(pluginConfig => pluginConfig.export)
65
- .map(pluginConfig => requirePluginModule(rootDir, pluginConfig, 'export')));
66
- }
67
70
  async function exportPlaygroundFiles(cosmosConfig, pluginConfigs) {
68
71
  const { exportPath } = cosmosConfig;
69
72
  // Copy UI plugins to export path and embed them in the playground HTML
70
73
  // with updated paths
71
74
  const uiPlugins = pluginConfigs.filter(isUiPlugin);
72
75
  const copiedUiPlugins = await Promise.all(uiPlugins.map(p => exportUiPlugin(cosmosConfig, p)));
73
- await fs.copyFile(resolve('react-cosmos-ui/dist/playground.bundle.js'), path.resolve(exportPath, 'playground.bundle.js'));
74
- await fs.copyFile(resolve('react-cosmos-ui/dist/playground.bundle.js.map'), path.resolve(exportPath, 'playground.bundle.js.map'));
75
- await fs.copyFile(getStaticPath('favicon.ico'), path.resolve(exportPath, '_cosmos.ico'));
76
+ await fs.copyFile(resolve('react-cosmos-ui/dist/playground.bundle.js'), path.join(exportPath, 'playground.bundle.js'));
77
+ await fs.copyFile(resolve('react-cosmos-ui/dist/playground.bundle.js.map'), path.join(exportPath, 'playground.bundle.js.map'));
78
+ await fs.copyFile(getStaticPath('favicon.ico'), path.join(exportPath, '_cosmos.ico'));
76
79
  const playgroundHtml = await getExportPlaygroundHtml(cosmosConfig, copiedUiPlugins);
77
- await fs.writeFile(path.resolve(exportPath, 'index.html'), playgroundHtml);
80
+ await fs.writeFile(path.join(exportPath, 'index.html'), playgroundHtml);
78
81
  }
79
82
  async function exportUiPlugin(cosmosConfig, pluginConfig) {
80
83
  const { rootDir, exportPath } = cosmosConfig;
@@ -8,7 +8,11 @@ export type FixtureApi = {
8
8
  parents: string[];
9
9
  playgroundUrl: string;
10
10
  relativeFilePath: string;
11
- rendererUrl: string;
11
+ rendererUrl: string | null;
12
12
  treePath: string[];
13
13
  };
14
- export declare function getFixtures(cosmosConfig: CosmosConfig): FixtureApi[];
14
+ type Options = {
15
+ rendererUrl?: string;
16
+ };
17
+ export declare function getFixtures(cosmosConfig: CosmosConfig, options?: Options): FixtureApi[];
18
+ export {};
@@ -0,0 +1,53 @@
1
+ import { mapValues } from 'lodash-es';
2
+ import path from 'path';
3
+ import { buildPlaygroundQueryString, createFixtureTree, createRendererUrl, flattenFixtureTree, getFixtureFromExport, getFixtureListFromExports, getSortedDecoratorsForFixturePath, } from 'react-cosmos-core';
4
+ import { createFixtureNode, decorateFixture } from 'react-cosmos-renderer';
5
+ import { getPlaygroundUrl } from '../shared/playgroundUrl.js';
6
+ import { importUserModules } from './importUserModules.js';
7
+ export function getFixtures(cosmosConfig, options = {}) {
8
+ const { fixtures, decorators } = importUserModules(cosmosConfig);
9
+ const fixtureExports = mapValues(fixtures, f => f.default);
10
+ const decoratorExports = mapValues(decorators, f => f.default);
11
+ const result = [];
12
+ getFlatFixtureTree(cosmosConfig, fixtureExports).forEach(({ fileName, fixtureId, name, parents }) => {
13
+ const fixtureExport = fixtures[fixtureId.path].default;
14
+ const fixture = getFixtureFromExport(fixtureExport, fixtureId.name);
15
+ if (!fixture) {
16
+ throw new Error(`Could not read fixture: ${JSON.stringify(fixtureId)}`);
17
+ }
18
+ const treePath = [...parents, fileName];
19
+ if (name)
20
+ treePath.push(name);
21
+ result.push({
22
+ absoluteFilePath: path.join(cosmosConfig.rootDir, fixtureId.path),
23
+ fileName,
24
+ getElement: createFixtureElementGetter(fixture, fixtureId.path, decoratorExports),
25
+ name,
26
+ parents,
27
+ playgroundUrl: getPlaygroundFixtureUrl(cosmosConfig, fixtureId),
28
+ relativeFilePath: fixtureId.path,
29
+ rendererUrl: options.rendererUrl
30
+ ? createRendererUrl(options.rendererUrl, fixtureId)
31
+ : null,
32
+ treePath,
33
+ });
34
+ });
35
+ return result;
36
+ }
37
+ function getFlatFixtureTree(cosmosConfig, fixtures) {
38
+ const { fixturesDir, fixtureFileSuffix } = cosmosConfig;
39
+ return flattenFixtureTree(createFixtureTree({
40
+ fixtures: getFixtureListFromExports(fixtures),
41
+ fixturesDir,
42
+ fixtureFileSuffix,
43
+ }));
44
+ }
45
+ function getPlaygroundFixtureUrl(cosmosConfig, fixtureId) {
46
+ const baseUrl = getPlaygroundUrl(cosmosConfig);
47
+ const query = buildPlaygroundQueryString({ fixtureId });
48
+ return `${baseUrl}/${query}`;
49
+ }
50
+ function createFixtureElementGetter(fixture, fixturePath, decoratorsByPath) {
51
+ const decorators = getSortedDecoratorsForFixturePath(fixturePath, decoratorsByPath);
52
+ return () => decorateFixture(createFixtureNode(fixture), decorators);
53
+ }
@@ -0,0 +1,8 @@
1
+ import { ByPath, ReactDecoratorModule, ReactFixtureModule } from 'react-cosmos-core';
2
+ import { CosmosConfig } from '../cosmosConfig/types.js';
3
+ type UserModules = {
4
+ fixtures: ByPath<ReactFixtureModule>;
5
+ decorators: ByPath<ReactDecoratorModule>;
6
+ };
7
+ export declare function importUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }: CosmosConfig): UserModules;
8
+ export {};
@@ -0,0 +1,28 @@
1
+ import path from 'path';
2
+ import { findUserModulePaths } from '../userModules/findUserModulePaths.js';
3
+ import { slash } from '../utils/slash.js';
4
+ export function importUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }) {
5
+ const { fixturePaths, decoratorPaths } = findUserModulePaths({
6
+ rootDir,
7
+ fixturesDir,
8
+ fixtureFileSuffix,
9
+ ignore,
10
+ });
11
+ return {
12
+ fixtures: importModules(fixturePaths, rootDir),
13
+ decorators: importModules(decoratorPaths, rootDir),
14
+ };
15
+ }
16
+ function importModules(paths, rootDir) {
17
+ const modules = paths.map(p => {
18
+ // Converting to forward slashes on Windows is important because the
19
+ // slashes are used for generating a sorted list of fixtures and
20
+ // decorators.
21
+ const relPath = slash(path.relative(rootDir, p));
22
+ return { relPath, module: require(p) };
23
+ });
24
+ return modules.reduce((acc, { relPath, module }) => ({
25
+ ...acc,
26
+ [relPath]: module,
27
+ }), {});
28
+ }
package/dist/index.d.ts CHANGED
@@ -0,0 +1,20 @@
1
+ export * from './cosmosConfig/createCosmosConfig.js';
2
+ export * from './cosmosConfig/detectCosmosConfig.js';
3
+ export * from './cosmosConfig/getCosmosConfigAtPath.js';
4
+ export * from './cosmosConfig/types.js';
5
+ export * from './cosmosPlugin/findCosmosPluginConfigs.js';
6
+ export * from './cosmosPlugin/pluginConfigs.js';
7
+ export * from './cosmosPlugin/types.js';
8
+ export * from './getFixtures/getFixtures.js';
9
+ export * from './shared/findNextAvailablePort.js';
10
+ export * from './shared/playgroundUrl.js';
11
+ export * from './shared/staticServer.js';
12
+ export * from './testHelpers/cwd.js';
13
+ export * from './userModules/fixtureWatcher.js';
14
+ export * from './userModules/generateUserImports.js';
15
+ export * from './utils/cli.js';
16
+ export * from './utils/fs.js';
17
+ export * from './utils/requireSilent.js';
18
+ export * from './utils/resolve.js';
19
+ export * from './utils/resolveLoose.js';
20
+ export * from './utils/resolveSilent.js';
package/dist/index.js CHANGED
@@ -1 +1,20 @@
1
- "use strict";
1
+ export * from './cosmosConfig/createCosmosConfig.js';
2
+ export * from './cosmosConfig/detectCosmosConfig.js';
3
+ export * from './cosmosConfig/getCosmosConfigAtPath.js';
4
+ export * from './cosmosConfig/types.js';
5
+ export * from './cosmosPlugin/findCosmosPluginConfigs.js';
6
+ export * from './cosmosPlugin/pluginConfigs.js';
7
+ export * from './cosmosPlugin/types.js';
8
+ export * from './getFixtures/getFixtures.js';
9
+ export * from './shared/findNextAvailablePort.js';
10
+ export * from './shared/playgroundUrl.js';
11
+ export * from './shared/staticServer.js';
12
+ export * from './testHelpers/cwd.js';
13
+ export * from './userModules/fixtureWatcher.js';
14
+ export * from './userModules/generateUserImports.js';
15
+ export * from './utils/cli.js';
16
+ export * from './utils/fs.js';
17
+ export * from './utils/requireSilent.js';
18
+ export * from './utils/resolve.js';
19
+ export * from './utils/resolveLoose.js';
20
+ export * from './utils/resolveSilent.js';
package/dist/jest.d.ts CHANGED
@@ -1,7 +1,4 @@
1
- export * from './server/testHelpers/mockConsole.js';
2
- export * from './server/testHelpers/mockEsmRequire.js';
3
- export * from './server/testHelpers/mockEsmResolve.js';
4
- export * from './server/testHelpers/mockEsmStaticPath.js';
5
- export * from './server/testHelpers/mockFetch.js';
6
- export * from './server/testHelpers/mockFs.js';
7
- export * from './server/testHelpers/mockYargs.js';
1
+ export * from './testHelpers/mockConsole.js';
2
+ export * from './testHelpers/mockFetch.js';
3
+ export * from './testHelpers/mockFs.js';
4
+ export * from './testHelpers/mockYargs.js';
package/dist/jest.js CHANGED
@@ -1,7 +1,4 @@
1
- export * from './server/testHelpers/mockConsole.js';
2
- export * from './server/testHelpers/mockEsmRequire.js';
3
- export * from './server/testHelpers/mockEsmResolve.js';
4
- export * from './server/testHelpers/mockEsmStaticPath.js';
5
- export * from './server/testHelpers/mockFetch.js';
6
- export * from './server/testHelpers/mockFs.js';
7
- export * from './server/testHelpers/mockYargs.js';
1
+ export * from './testHelpers/mockConsole.js';
2
+ export * from './testHelpers/mockFetch.js';
3
+ export * from './testHelpers/mockFs.js';
4
+ export * from './testHelpers/mockYargs.js';
@@ -0,0 +1,11 @@
1
+ import { CosmosCommand } from 'react-cosmos-core';
2
+ import { CosmosConfig } from '../cosmosConfig/types.js';
3
+ import { CosmosPlatform, CosmosServerPlugin } from '../cosmosPlugin/types.js';
4
+ type Args = {
5
+ cosmosConfig: CosmosConfig;
6
+ serverPlugins: CosmosServerPlugin[];
7
+ command: CosmosCommand;
8
+ platform: CosmosPlatform;
9
+ };
10
+ export declare function applyServerConfigPlugins({ cosmosConfig, serverPlugins, command, platform, }: Args): Promise<CosmosConfig>;
11
+ export {};
@@ -0,0 +1,14 @@
1
+ export async function applyServerConfigPlugins({ cosmosConfig, serverPlugins, command, platform, }) {
2
+ for (const plugin of serverPlugins) {
3
+ if (plugin.config) {
4
+ try {
5
+ cosmosConfig = await plugin.config({ cosmosConfig, command, platform });
6
+ }
7
+ catch (err) {
8
+ console.log(`[Cosmos][plugin:${plugin.name}] Config hook failed`);
9
+ throw err;
10
+ }
11
+ }
12
+ }
13
+ return cosmosConfig;
14
+ }
@@ -0,0 +1 @@
1
+ export declare function findNextAvailablePort(port: number, retries: number, retriesLeft?: number): Promise<number>;
@@ -0,0 +1,29 @@
1
+ import net from 'net';
2
+ // Inspired by https://stackoverflow.com/q/19129570
3
+ export async function findNextAvailablePort(port, retries, retriesLeft = retries) {
4
+ return new Promise((resolve, reject) => {
5
+ const socket = new net.Socket();
6
+ socket.on('connect', () => {
7
+ console.log(`[Cosmos] Port ${port} already in use, trying next...`);
8
+ if (retriesLeft > 0) {
9
+ socket.destroy();
10
+ resolve(findNextAvailablePort(port + 1, retries, retriesLeft - 1));
11
+ }
12
+ else {
13
+ reject(`No available port found after ${retries} retries.`);
14
+ }
15
+ });
16
+ socket.on('error', (error) => {
17
+ if (error.code === 'ECONNREFUSED')
18
+ resolve(port);
19
+ else
20
+ reject(error);
21
+ });
22
+ socket.setTimeout(400);
23
+ socket.on('timeout', () => {
24
+ socket.destroy();
25
+ resolve(port);
26
+ });
27
+ socket.connect(port, '0.0.0.0');
28
+ });
29
+ }
@@ -0,0 +1,2 @@
1
+ import { CosmosPluginConfig } from 'react-cosmos-core';
2
+ export declare function getServerPlugins(pluginConfigs: CosmosPluginConfig[], rootDir: string): Promise<import("../index.js").CosmosServerPlugin[]>;
@@ -0,0 +1,6 @@
1
+ import { coreServerPlugins } from '../corePlugins/index.js';
2
+ import { importServerPlugins } from './importServerPlugins.js';
3
+ export async function getServerPlugins(pluginConfigs, rootDir) {
4
+ const userPlugins = await importServerPlugins(pluginConfigs, rootDir);
5
+ return [...coreServerPlugins, ...userPlugins];
6
+ }
@@ -0,0 +1,3 @@
1
+ import { CosmosPluginConfig } from 'react-cosmos-core';
2
+ import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
3
+ export declare function importServerPlugins(pluginConfigs: CosmosPluginConfig[], rootDir: string): Promise<CosmosServerPlugin[]>;
@@ -0,0 +1,16 @@
1
+ import path from 'node:path';
2
+ import { importModule } from '../utils/fs.js';
3
+ export async function importServerPlugins(pluginConfigs, rootDir) {
4
+ return Promise.all(pluginConfigs
5
+ .filter(pluginConfig => pluginConfig.server)
6
+ .map(pluginConfig => importServerModule(pluginConfig, rootDir)));
7
+ }
8
+ async function importServerModule(pluginConfig, rootDir) {
9
+ const modulePath = pluginConfig.server;
10
+ if (!modulePath) {
11
+ throw new Error(`Server module missing in plugin "${pluginConfig.name}"`);
12
+ }
13
+ const absPath = path.resolve(rootDir, modulePath);
14
+ const module = await importModule(absPath);
15
+ return module.default;
16
+ }
@@ -0,0 +1,5 @@
1
+ import { CosmosPluginConfig } from 'react-cosmos-core';
2
+ import { CosmosConfig } from '../cosmosConfig/types.js';
3
+ import { CosmosPlatform } from '../cosmosPlugin/types.js';
4
+ export declare function getDevPlaygroundHtml(platform: CosmosPlatform, cosmosConfig: CosmosConfig, pluginConfigs: CosmosPluginConfig[]): Promise<string>;
5
+ export declare function getExportPlaygroundHtml(cosmosConfig: CosmosConfig, pluginConfigs: CosmosPluginConfig[]): Promise<string>;
@@ -2,16 +2,22 @@ import fs from 'fs';
2
2
  import { readFile } from 'fs/promises';
3
3
  import path from 'path';
4
4
  import { pkgUpSync } from 'pkg-up';
5
- import { replaceKeys } from 'react-cosmos-core';
6
- import { resolveRendererUrl } from './resolveRendererUrl.js';
5
+ import { pickRendererUrl, replaceKeys, } from 'react-cosmos-core';
6
+ import { findUserModulePaths } from '../userModules/findUserModulePaths.js';
7
+ import { importKeyPath } from '../userModules/shared.js';
7
8
  import { getStaticPath } from './staticPath.js';
8
- export const RENDERER_FILENAME = '_renderer.html';
9
- export async function getDevPlaygroundHtml(platformType, cosmosConfig, pluginConfigs) {
9
+ export async function getDevPlaygroundHtml(platform, cosmosConfig, pluginConfigs) {
10
10
  const { ui } = cosmosConfig;
11
11
  return getPlaygroundHtml({
12
12
  playgroundConfig: {
13
13
  ...ui,
14
- core: await getDevCoreConfig(platformType, cosmosConfig),
14
+ core: await getCoreConfig(cosmosConfig, true),
15
+ rendererCore: {
16
+ fixtures: getFixtureList(cosmosConfig),
17
+ rendererUrl: platform === 'web'
18
+ ? pickRendererUrl(cosmosConfig.rendererUrl, 'dev')
19
+ : null,
20
+ },
15
21
  },
16
22
  pluginConfigs,
17
23
  });
@@ -21,43 +27,22 @@ export async function getExportPlaygroundHtml(cosmosConfig, pluginConfigs) {
21
27
  return getPlaygroundHtml({
22
28
  playgroundConfig: {
23
29
  ...ui,
24
- core: await getExportCoreConfig(cosmosConfig),
30
+ core: await getCoreConfig(cosmosConfig, false),
31
+ rendererCore: {
32
+ fixtures: getFixtureList(cosmosConfig),
33
+ rendererUrl: pickRendererUrl(cosmosConfig.rendererUrl, 'export'),
34
+ },
25
35
  },
26
36
  pluginConfigs,
27
37
  });
28
38
  }
29
- async function getDevCoreConfig(platformType, cosmosConfig) {
30
- switch (platformType) {
31
- case 'native':
32
- return {
33
- ...(await getSharedCoreConfig(cosmosConfig)),
34
- devServerOn: true,
35
- webRendererUrl: null,
36
- };
37
- case 'web':
38
- return {
39
- ...(await getSharedCoreConfig(cosmosConfig)),
40
- devServerOn: true,
41
- webRendererUrl: cosmosConfig.rendererUrl ||
42
- resolveRendererUrl(cosmosConfig.publicUrl, RENDERER_FILENAME),
43
- };
44
- default:
45
- throw new Error(`Invalid platform type: ${platformType}`);
46
- }
47
- }
48
- async function getExportCoreConfig(cosmosConfig) {
49
- return {
50
- ...(await getSharedCoreConfig(cosmosConfig)),
51
- devServerOn: false,
52
- webRendererUrl: resolveRendererUrl(cosmosConfig.publicUrl, RENDERER_FILENAME),
53
- };
54
- }
55
- async function getSharedCoreConfig(cosmosConfig) {
39
+ async function getCoreConfig(cosmosConfig, devServerOn) {
56
40
  const { rootDir, fixturesDir, fixtureFileSuffix } = cosmosConfig;
57
41
  return {
58
42
  projectId: await getProjectId(rootDir),
59
43
  fixturesDir,
60
44
  fixtureFileSuffix,
45
+ devServerOn,
61
46
  };
62
47
  }
63
48
  async function getProjectId(rootDir) {
@@ -83,3 +68,10 @@ function getPlaygroundHtml(playgroundArgs) {
83
68
  function getPlaygroundHtmlTemplate() {
84
69
  return fs.readFileSync(getStaticPath('index.html'), 'utf8');
85
70
  }
71
+ function getFixtureList(cosmosConfig) {
72
+ const { fixturePaths } = findUserModulePaths(cosmosConfig);
73
+ return fixturePaths.reduce((acc, fixturePath) => ({
74
+ ...acc,
75
+ [importKeyPath(fixturePath, cosmosConfig.rootDir)]: { type: 'single' },
76
+ }), {});
77
+ }
@@ -0,0 +1 @@
1
+ export declare function getStaticPath(relPath: string): string;
@@ -1,6 +1,5 @@
1
1
  import express from 'express';
2
2
  import path from 'path';
3
- import { removeLeadingDot } from 'react-cosmos-core';
4
3
  export function serveStaticDir(app, staticPath, publicUrl) {
5
4
  const relStaticPath = path.relative(process.cwd(), staticPath);
6
5
  console.log(`[Cosmos] Serving static files from ${relStaticPath}`);
@@ -10,3 +9,6 @@ export function serveStaticDir(app, staticPath, publicUrl) {
10
9
  index: false,
11
10
  }));
12
11
  }
12
+ function removeLeadingDot(fromPath) {
13
+ return fromPath.indexOf('.') === 0 ? fromPath.slice(1) : fromPath;
14
+ }
@@ -1,11 +1,15 @@
1
1
  export async function mockConsole(cb) {
2
- const origConsoleLog = console.log;
3
- console.log = jest.fn();
4
2
  const expectedLogs = [];
5
- const ret = await cb({
3
+ const origConsoleLog = console.log;
4
+ console.log = jest.fn((...args) => {
5
+ if (typeof args[0] !== 'string' || !expectedLogs.includes(args[0])) {
6
+ origConsoleLog(...args);
7
+ }
8
+ });
9
+ const cbReturn = await cb({
6
10
  expectLog: (msg) => expectedLogs.push(msg),
7
11
  });
8
12
  expectedLogs.forEach(msg => expect(console.log).toBeCalledWith(msg));
9
13
  console.log = origConsoleLog;
10
- return ret;
14
+ return cbReturn;
11
15
  }
@@ -1,5 +1,5 @@
1
1
  import { CosmosConfig } from '../cosmosConfig/types.js';
2
+ export declare function mockFile(filePath: string, fileMock: {}): void;
2
3
  export declare function mockCosmosConfig(cosmosConfigPath: string, cosmosConfig: Partial<CosmosConfig>): void;
3
- export declare function mockFile(filePath: string, fileContent: {}): void;
4
- export declare function mockDir(dirPath: string): void;
4
+ export declare function mockCwdModuleDefault(filePath: string, fileMock: {}): void;
5
5
  export declare function resetFsMock(): void;