react-cosmos 6.0.0-canary.fe0c7d1.0 → 6.0.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 (92) hide show
  1. package/README.md +37 -3
  2. package/bin/cosmos.js +0 -4
  3. package/client.d.ts +1 -0
  4. package/client.js +1 -0
  5. package/config.schema.json +31 -20
  6. package/dist/cjsMocks/requireModule.d.ts +2 -0
  7. package/dist/cjsMocks/requireModule.js +7 -0
  8. package/dist/cjsMocks/resolve.d.ts +2 -0
  9. package/dist/cjsMocks/resolve.js +7 -0
  10. package/dist/cjsMocks/staticPath.d.ts +1 -0
  11. package/dist/cjsMocks/staticPath.js +4 -0
  12. package/dist/client.d.ts +1 -0
  13. package/dist/client.js +1 -0
  14. package/dist/corePlugins/{openFile.d.ts → fixtureWatcherPlugin.d.ts} +1 -1
  15. package/dist/corePlugins/fixtureWatcherPlugin.js +63 -0
  16. package/dist/corePlugins/httpProxyPlugin.d.ts +2 -0
  17. package/dist/corePlugins/{httpProxy.js → httpProxyPlugin.js} +3 -3
  18. package/dist/corePlugins/index.js +13 -10
  19. package/dist/corePlugins/openFilePlugin.d.ts +2 -0
  20. package/dist/corePlugins/{openFile.js → openFilePlugin.js} +1 -1
  21. package/dist/corePlugins/pluginEndpointPlugin.d.ts +2 -0
  22. package/dist/corePlugins/{pluginEndpoint.js → pluginEndpointPlugin.js} +1 -1
  23. package/dist/corePlugins/portRetryPlugin.d.ts +2 -0
  24. package/dist/corePlugins/{portRetry.js → portRetryPlugin.js} +5 -2
  25. package/dist/cosmosConfig/createCosmosConfig.js +17 -11
  26. package/dist/cosmosConfig/types.d.ts +5 -2
  27. package/dist/cosmosPlugin/types.d.ts +5 -4
  28. package/dist/devServer/expressApp.d.ts +2 -2
  29. package/dist/devServer/expressApp.js +3 -4
  30. package/dist/devServer/startDevServer.d.ts +2 -2
  31. package/dist/devServer/startDevServer.js +15 -21
  32. package/dist/export/generateExport.js +15 -23
  33. package/dist/getFixtures/getFixtures.d.ts +6 -2
  34. package/dist/getFixtures/getFixtures.js +36 -46
  35. package/dist/getFixtures/importUserModules.d.ts +8 -0
  36. package/dist/getFixtures/importUserModules.js +28 -0
  37. package/dist/index.d.ts +4 -3
  38. package/dist/index.js +4 -3
  39. package/dist/jest.d.ts +0 -3
  40. package/dist/jest.js +0 -3
  41. package/dist/shared/applyServerConfigPlugins.d.ts +11 -0
  42. package/dist/shared/applyServerConfigPlugins.js +14 -0
  43. package/dist/shared/getServerPlugins.d.ts +2 -0
  44. package/dist/shared/getServerPlugins.js +6 -0
  45. package/dist/shared/playgroundHtml.d.ts +2 -3
  46. package/dist/shared/playgroundHtml.js +19 -46
  47. package/dist/shared/serverFixtureList.d.ts +4 -0
  48. package/dist/shared/serverFixtureList.js +19 -0
  49. package/dist/shared/staticServer.js +3 -1
  50. package/dist/static/favicon.ico +0 -0
  51. package/dist/{userDeps → userModules}/findUserModulePaths.d.ts +1 -4
  52. package/dist/{userDeps → userModules}/findUserModulePaths.js +1 -1
  53. package/dist/userModules/generateUserImports.d.ts +12 -0
  54. package/dist/userModules/generateUserImports.js +16 -0
  55. package/dist/userModules/shared.d.ts +18 -0
  56. package/dist/userModules/shared.js +32 -0
  57. package/dist/userModules/userImportsLazyTemplate.d.ts +2 -0
  58. package/dist/userModules/userImportsLazyTemplate.js +54 -0
  59. package/dist/userModules/userImportsTemplate.d.ts +2 -0
  60. package/dist/userModules/userImportsTemplate.js +56 -0
  61. package/dist/utils/findUp.d.ts +1 -0
  62. package/dist/utils/findUp.js +19 -0
  63. package/dist/utils/json.d.ts +3 -0
  64. package/jest.js +1 -1
  65. package/package.json +17 -17
  66. package/dist/corePlugins/httpProxy.d.ts +0 -2
  67. package/dist/corePlugins/pluginEndpoint.d.ts +0 -2
  68. package/dist/corePlugins/portRetry.d.ts +0 -2
  69. package/dist/corePlugins/userDepsFile.d.ts +0 -2
  70. package/dist/corePlugins/userDepsFile.js +0 -40
  71. package/dist/shared/resolveRendererUrl.d.ts +0 -1
  72. package/dist/shared/resolveRendererUrl.js +0 -7
  73. package/dist/testHelpers/mockEsmRequire.js +0 -13
  74. package/dist/testHelpers/mockEsmResolve.d.ts +0 -1
  75. package/dist/testHelpers/mockEsmResolve.js +0 -13
  76. package/dist/testHelpers/mockEsmStaticPath.d.ts +0 -1
  77. package/dist/testHelpers/mockEsmStaticPath.js +0 -7
  78. package/dist/userDeps/generateUserDepsModule.d.ts +0 -9
  79. package/dist/userDeps/generateUserDepsModule.js +0 -21
  80. package/dist/userDeps/getUserModules.d.ts +0 -8
  81. package/dist/userDeps/getUserModules.js +0 -26
  82. package/dist/userDeps/shared.d.ts +0 -5
  83. package/dist/userDeps/shared.js +0 -16
  84. package/dist/userDeps/userDepsLazyTemplate.d.ts +0 -2
  85. package/dist/userDeps/userDepsLazyTemplate.js +0 -37
  86. package/dist/userDeps/userDepsShared.d.ts +0 -12
  87. package/dist/userDeps/userDepsShared.js +0 -16
  88. package/dist/userDeps/userDepsTemplate.d.ts +0 -2
  89. package/dist/userDeps/userDepsTemplate.js +0 -45
  90. /package/dist/{userDeps → userModules}/fixtureWatcher.d.ts +0 -0
  91. /package/dist/{userDeps → userModules}/fixtureWatcher.js +0 -0
  92. /package/dist/{testHelpers/mockEsmRequire.d.ts → utils/json.js} +0 -0
package/README.md CHANGED
@@ -1,7 +1,41 @@
1
1
  <p align="center">
2
- <a href="https://reactcosmos.org"><img alt="Cosmos" width="150" height="150" src="https://raw.githubusercontent.com/react-cosmos/react-cosmos/main/cosmos.png"></a>
2
+ <a href="https://reactcosmos.org"><img alt="Cosmos" width="150" height="150" src="cosmos.png"></a>
3
+ <h1 align="center">React Cosmos</h1>
3
4
  </p>
4
5
 
5
- # React Cosmos
6
+ <p align="center">
7
+ <a aria-label="npm version" href="https://www.npmjs.com/package/react-cosmos"><img src="https://img.shields.io/npm/v/react-cosmos/next.svg?style=flat&colorA=000000&colorB=000000" alt="" /></a>
8
+ <a aria-label="CI Status" href="https://github.com/react-cosmos/react-cosmos/actions/workflows/test.yml"><img src="https://img.shields.io/github/actions/workflow/status/react-cosmos/react-cosmos/test.yml?branch=main&style=flat&colorA=000000&colorB=000000" alt="" /></a>
9
+ <a aria-label="Codecov" href="https://app.codecov.io/gh/react-cosmos/react-cosmos"><img src="https://img.shields.io/codecov/c/github/react-cosmos/react-cosmos?colorA=000000&colorB=000000" alt="" /></a>
10
+ <a aria-label="Twitter" href="https://twitter.com/ReactCosmos"><img src="https://img.shields.io/badge/twitter-follow-%2300acee?colorA=000000&colorB=000000" alt="" /></a>
11
+ <a aria-label="Discord" href="https://discord.gg/3X95VgfnW5"><img src="https://img.shields.io/discord/620737684859781150?label=discord&logo=discord&logoColor=ffffff&colorA=000000&colorB=000000" alt="" /></a>
12
+ <a aria-label="PRs Welcome" href="https://github.com/react-cosmos/react-cosmos/blob/main/CONTRIBUTING.md#how-to-contribute"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?colorA=000000&colorB=000000" alt="" /></a>
13
+ </p>
14
+
15
+ ## Getting Started
16
+
17
+ Choose one of the [Getting Started](https://reactcosmos.org/docs/getting-started) guides to dive into React Cosmos.
18
+
19
+ > Check out the [Next.js integration](https://reactcosmos.org/docs/getting-started/next) to get started with React Server Components.
20
+
21
+ Visit [reactcosmos.org/docs](https://reactcosmos.org/docs) to view the full documentation.
22
+
23
+ ## Community
24
+
25
+ To chat with other community members you can join the [React Cosmos Discord](https://discord.gg/3X95VgfnW5).
26
+
27
+ You can also ask questions, voice ideas, and share your projects on [GitHub Discussions](https://github.com/react-cosmos/react-cosmos/discussions).
28
+
29
+ Our [Code of Conduct](/CODE_OF_CONDUCT.md) applies to all React Cosmos community channels.
30
+
31
+ ## Contributing
32
+
33
+ Please see our [CONTRIBUTING.md](/CONTRIBUTING.md).
34
+
35
+ [Become a Sponsor](https://github.com/users/skidding/sponsorship) to support the ongoing development of React Cosmos.
36
+
37
+ ## Live Demo
38
+
39
+ Visit [reactcosmos.org/demo/](https://reactcosmos.org/demo/) for a live demo of React Cosmos.
6
40
 
7
- Sandbox for developing and testing UI components in isolation.
41
+ [![React Cosmos](/docs/public/demo.png)](https://reactcosmos.org/demo/)
package/bin/cosmos.js CHANGED
@@ -5,10 +5,6 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development';
5
5
 
6
6
  const { startDevServer } = await import('../dist/devServer/startDevServer.js');
7
7
 
8
- // Auto disable Fast Refresh in CRA 4 until integration is implemented
9
- // https://github.com/react-cosmos/react-cosmos/issues/1272
10
- process.env.FAST_REFRESH = process.env.FAST_REFRESH || false;
11
-
12
8
  startDevServer('web').catch(err => {
13
9
  console.log('[Cosmos] Server crashed...');
14
10
  console.log(`\n (╯°□°)╯︵ ┻━┻\n`);
package/client.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/client.js';
package/client.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/client.js';
@@ -1,7 +1,5 @@
1
1
  {
2
- "$schema": "http://json-schema.org/draft-04/schema",
3
- "title": "JSON schema for React Cosmos configs",
4
- "type": "object",
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
5
3
  "additionalProperties": false,
6
4
  "properties": {
7
5
  "$schema": {
@@ -13,16 +11,16 @@
13
11
  "minLength": 1
14
12
  },
15
13
  "exportPath": {
16
- "description": "Where to generate static exports on disk. [default: \"cosmos-export\"]",
14
+ "description": "Output directory for static exports. [default: \"cosmos-export\"]",
17
15
  "type": "string",
18
16
  "minLength": 1
19
17
  },
20
18
  "detectLocalPlugins": {
21
- "description": "Automatically detect and load local plugins",
19
+ "description": "Automatically detect and load local plugins. [default: true]",
22
20
  "type": "boolean"
23
21
  },
24
22
  "disablePlugins": {
25
- "description": "Disable all plugins 3rd party plugins",
23
+ "description": "Disable plugins. [default: false]",
26
24
  "type": "boolean"
27
25
  },
28
26
  "staticPath": {
@@ -37,11 +35,11 @@
37
35
  "uniqueItems": true
38
36
  },
39
37
  "publicUrl": {
40
- "description": "Base URL for static assets. [default: \"/\"]",
38
+ "description": "Base URL for static assets. [default: \"\"]",
41
39
  "type": "string"
42
40
  },
43
41
  "fixturesDir": {
44
- "description": "Name for directories that contain fixtures (eg. __fixtures__/example.jsx). [default: \"__fixtures__\"]",
42
+ "description": "Name for directories that contain fixture files (eg. __fixtures__/example.jsx). [default: \"__fixtures__\"]",
45
43
  "type": "string",
46
44
  "minLength": 1
47
45
  },
@@ -51,7 +49,7 @@
51
49
  "minLength": 1
52
50
  },
53
51
  "ignore": {
54
- "description": "Patterns for ignoring fixture and decorator files (eg. [\"**/dist/**\"]]).",
52
+ "description": "Patterns for ignoring fixture and decorator files (eg. [\"**/dist/**\"]).",
55
53
  "type": "array",
56
54
  "items": { "type": "string", "minLength": 1 },
57
55
  "uniqueItems": true
@@ -69,10 +67,9 @@
69
67
  "description": "Dynamically import fixture and decorator modules as they are loaded. When false all fixture and decorator modules are imported statically and bundled together. [default: false]",
70
68
  "type": "boolean"
71
69
  },
72
- "userDepsFilePath": {
73
- "description": "Where to generate a file that contains a map to all fixtures and other Cosmos-related user modules. Only used in setups where Cosmos can't piggyback on the user's build (eg. React Native setups). [default: \"cosmos.userdeps.js\"]",
74
- "type": "string",
75
- "minLength": 1
70
+ "exposeImports": {
71
+ "description": "Expose user imports and config required for the Cosmos renderer. Used with React Native and in custom integrations. When a path is specified it requires a file extension (eg. \"src/cosmos.imports.ts\"). [default: false]",
72
+ "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "boolean" }]
76
73
  },
77
74
  "hostname": {
78
75
  "description": "Dev server hostname. Set to null to accept connections with any hostname. [default: null]",
@@ -96,17 +93,17 @@
96
93
  "additionalProperties": false,
97
94
  "properties": {
98
95
  "certPath": {
99
- "description": "Path of a certificate file",
96
+ "description": "Path of a certificate file.",
100
97
  "type": "string"
101
98
  },
102
99
  "keyPath": {
103
- "description": "Path of a certificate's key file",
100
+ "description": "Path of a certificate's key file.",
104
101
  "type": "string"
105
102
  }
106
103
  }
107
104
  },
108
105
  "httpProxy": {
109
- "description": "Proxy some URLs to a different HTTP server (eg. an API backend dev server). Similar to devServer.proxy in webpack config.",
106
+ "description": "Proxy some URLs to a different HTTP server (eg. an API backend dev server).",
110
107
  "type": "object",
111
108
  "additionalProperties": true,
112
109
  "patternProperties": {
@@ -143,7 +140,19 @@
143
140
  },
144
141
  "rendererUrl": {
145
142
  "description": "Required when renderer doesn't run on the same port (or host) as the Playground. Useful for integrations with build tools. [default: null]",
146
- "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }]
143
+ "anyOf": [
144
+ { "type": "string", "minLength": 1 },
145
+ {
146
+ "type": "object",
147
+ "additionalProperties": false,
148
+ "required": ["dev", "export"],
149
+ "properties": {
150
+ "dev": { "type": "string", "minLength": 1 },
151
+ "export": { "type": "string", "minLength": 1 }
152
+ }
153
+ },
154
+ { "type": "null" }
155
+ ]
147
156
  },
148
157
  "dom": {
149
158
  "description": "DOM-related options",
@@ -162,11 +171,11 @@
162
171
  "additionalProperties": false,
163
172
  "properties": {
164
173
  "configPath": {
165
- "description": "Path to Vite config. Set to false to disable reading it from the default path. [default: \"vite.config.js\"]",
174
+ "description": "Path to Vite config. Set to false to disable reading it from the default path. [default: \"vite.config.js\" or \"vite.config.ts\"]",
166
175
  "anyOf": [{ "type": "string", "minLength": 1 }, { "enum": [false] }]
167
176
  },
168
177
  "indexPath": {
169
- "description": "Path to index module (eg. \"src/my-index.tsx\"). [default: matches common index/main module paths]",
178
+ "description": "Path to index module (eg. \"src/my-index.tsx\"). [default: detects common index/main module paths]",
170
179
  "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }]
171
180
  },
172
181
  "port": {
@@ -229,5 +238,7 @@
229
238
  }
230
239
  }
231
240
  }
232
- }
241
+ },
242
+ "title": "JSON schema for React Cosmos configs",
243
+ "type": "object"
233
244
  }
@@ -0,0 +1,2 @@
1
+ export declare function requireModule(moduleId: string): any;
2
+ export declare function requireFrom(fromDirectory: string, moduleId: string): any;
@@ -0,0 +1,7 @@
1
+ // CJS mock for packages/react-cosmos/src/utils/requireModule.ts
2
+ export function requireModule(moduleId) {
3
+ return require(moduleId);
4
+ }
5
+ export function requireFrom(fromDirectory, moduleId) {
6
+ return require(require.resolve(moduleId, { paths: [fromDirectory] }));
7
+ }
@@ -0,0 +1,2 @@
1
+ export declare function resolve(moduleId: string): string;
2
+ export declare function resolveFrom(fromDirectory: string, moduleId: string): string;
@@ -0,0 +1,7 @@
1
+ // CJS mock for packages/react-cosmos/src/utils/resolve.ts
2
+ export function resolve(moduleId) {
3
+ return require.resolve(moduleId);
4
+ }
5
+ export function resolveFrom(fromDirectory, moduleId) {
6
+ return require.resolve(moduleId, { paths: [fromDirectory] });
7
+ }
@@ -0,0 +1 @@
1
+ export declare function getStaticPath(relPath: string): string;
@@ -0,0 +1,4 @@
1
+ // CJS mock for packages/react-cosmos/src/shared/staticPath.ts
2
+ export function getStaticPath(relPath) {
3
+ return require.resolve(`../static/${relPath}`);
4
+ }
@@ -0,0 +1 @@
1
+ export { FixtureContext, Viewport, useFixtureState, useSelect, useValue, } from 'react-cosmos-renderer/client';
package/dist/client.js ADDED
@@ -0,0 +1 @@
1
+ export { FixtureContext, Viewport, useFixtureState, useSelect, useValue, } from 'react-cosmos-renderer/client';
@@ -1,2 +1,2 @@
1
1
  import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
2
- export declare const openFileServerPlugin: CosmosServerPlugin;
2
+ export declare const fixtureWatcherPlugin: CosmosServerPlugin;
@@ -0,0 +1,63 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { pickRendererUrl, } from 'react-cosmos-core';
4
+ import { getPlaygroundUrl } from '../shared/playgroundUrl.js';
5
+ import { updateFixtureListCache } from '../shared/serverFixtureList.js';
6
+ import { findUserModulePaths } from '../userModules/findUserModulePaths.js';
7
+ import { startFixtureWatcher } from '../userModules/fixtureWatcher.js';
8
+ import { generateUserImports } from '../userModules/generateUserImports.js';
9
+ import { moduleExists } from '../utils/fs.js';
10
+ export const fixtureWatcherPlugin = {
11
+ name: 'fixtureWatcher',
12
+ async devServer({ cosmosConfig, platform }) {
13
+ const exposeImports = shouldExposeImports(platform, cosmosConfig);
14
+ if (exposeImports) {
15
+ const modulePaths = findUserModulePaths(cosmosConfig);
16
+ await generateImportsFile(cosmosConfig, 'dev', modulePaths);
17
+ }
18
+ const watcher = await startFixtureWatcher(cosmosConfig, 'all', () => {
19
+ const modulePaths = findUserModulePaths(cosmosConfig);
20
+ updateFixtureListCache(cosmosConfig.rootDir, modulePaths.fixturePaths);
21
+ if (exposeImports) {
22
+ generateImportsFile(cosmosConfig, 'dev', modulePaths);
23
+ }
24
+ });
25
+ return async () => {
26
+ await watcher.close();
27
+ };
28
+ },
29
+ async export({ cosmosConfig }) {
30
+ if (shouldExposeImports('web', cosmosConfig)) {
31
+ const modulePaths = findUserModulePaths(cosmosConfig);
32
+ await generateImportsFile(cosmosConfig, 'export', modulePaths);
33
+ }
34
+ },
35
+ };
36
+ function shouldExposeImports(platform, cosmosConfig) {
37
+ return platform === 'native' || Boolean(cosmosConfig.exposeImports);
38
+ }
39
+ async function generateImportsFile(cosmosConfig, command, modulePaths) {
40
+ const { exposeImports } = cosmosConfig;
41
+ const filePath = typeof exposeImports === 'string'
42
+ ? exposeImports
43
+ : getDefaultFilePath(cosmosConfig.rootDir);
44
+ const typeScript = /\.tsx?$/.test(filePath);
45
+ const rendererConfig = {
46
+ playgroundUrl: getPlaygroundUrl(cosmosConfig),
47
+ rendererUrl: pickRendererUrl(cosmosConfig.rendererUrl, command),
48
+ };
49
+ const fileSource = generateUserImports({
50
+ cosmosConfig,
51
+ modulePaths,
52
+ rendererConfig,
53
+ relativeToDir: path.dirname(filePath),
54
+ typeScript,
55
+ });
56
+ await fs.writeFile(filePath, fileSource, 'utf8');
57
+ const relModulesPath = path.relative(process.cwd(), filePath);
58
+ console.log(`[Cosmos] Generated ${relModulesPath}`);
59
+ }
60
+ function getDefaultFilePath(rootDir) {
61
+ const ext = moduleExists('typescript') ? 'ts' : 'js';
62
+ return path.join(rootDir, `cosmos.imports.${ext}`);
63
+ }
@@ -0,0 +1,2 @@
1
+ import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
2
+ export declare const httpProxyPlugin: CosmosServerPlugin;
@@ -1,8 +1,8 @@
1
1
  import { createProxyMiddleware } from 'http-proxy-middleware';
2
- export const httpProxyServerPlugin = {
2
+ export const httpProxyPlugin = {
3
3
  name: 'httpProxy',
4
- devServer({ platformType, cosmosConfig, expressApp }) {
5
- if (platformType !== 'web')
4
+ devServer({ cosmosConfig, platform, expressApp }) {
5
+ if (platform !== 'web')
6
6
  return;
7
7
  const httpProxyConfig = getHttpProxyCosmosConfig(cosmosConfig);
8
8
  Object.keys(httpProxyConfig).forEach(context => {
@@ -1,12 +1,15 @@
1
- import { httpProxyServerPlugin } from './httpProxy.js';
2
- import { openFileServerPlugin } from './openFile.js';
3
- import { pluginEndpointServerPlugin } from './pluginEndpoint.js';
4
- import { portRetryServerPlugin } from './portRetry.js';
5
- import { userDepsFileServerPlugin } from './userDepsFile.js';
1
+ import { fixtureWatcherPlugin } from './fixtureWatcherPlugin.js';
2
+ import { httpProxyPlugin } from './httpProxyPlugin.js';
3
+ import { openFilePlugin } from './openFilePlugin.js';
4
+ import { pluginEndpointPlugin } from './pluginEndpointPlugin.js';
5
+ import { portRetryPlugin } from './portRetryPlugin.js';
6
6
  export const coreServerPlugins = [
7
- portRetryServerPlugin,
8
- userDepsFileServerPlugin,
9
- httpProxyServerPlugin,
10
- openFileServerPlugin,
11
- pluginEndpointServerPlugin,
7
+ portRetryPlugin,
8
+ httpProxyPlugin,
9
+ openFilePlugin,
10
+ pluginEndpointPlugin,
12
11
  ];
12
+ // Omit starting chokidar in Jest tests for performance reasons
13
+ if (process.env.JEST_WORKER_ID === undefined) {
14
+ coreServerPlugins.push(fixtureWatcherPlugin);
15
+ }
@@ -0,0 +1,2 @@
1
+ import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
2
+ export declare const openFilePlugin: CosmosServerPlugin;
@@ -2,7 +2,7 @@ import launchEditor from '@skidding/launch-editor';
2
2
  import fs from 'fs';
3
3
  import open from 'open';
4
4
  import path from 'path';
5
- export const openFileServerPlugin = {
5
+ export const openFilePlugin = {
6
6
  name: 'openFile',
7
7
  devServer({ cosmosConfig, expressApp }) {
8
8
  expressApp.get('/_open', (req, res) => {
@@ -0,0 +1,2 @@
1
+ import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
2
+ export declare const pluginEndpointPlugin: CosmosServerPlugin;
@@ -1,6 +1,6 @@
1
1
  import path from 'node:path';
2
2
  import { resolveSilent } from '../utils/resolveSilent.js';
3
- export const pluginEndpointServerPlugin = {
3
+ export const pluginEndpointPlugin = {
4
4
  name: 'pluginEndpoint',
5
5
  devServer({ expressApp }) {
6
6
  expressApp.get('/_plugin/*.js', (req, res) => {
@@ -0,0 +1,2 @@
1
+ import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
2
+ export declare const portRetryPlugin: CosmosServerPlugin;
@@ -1,7 +1,10 @@
1
1
  import { findNextAvailablePort } from '../shared/findNextAvailablePort.js';
2
- export const portRetryServerPlugin = {
2
+ export const portRetryPlugin = {
3
3
  name: 'portRetry',
4
- async config({ cosmosConfig }) {
4
+ async config({ cosmosConfig, command }) {
5
+ if (command === 'export') {
6
+ return cosmosConfig;
7
+ }
5
8
  const { port, portRetries } = cosmosConfig;
6
9
  return {
7
10
  ...cosmosConfig,
@@ -5,9 +5,10 @@ export function createCosmosConfig(rootDir, cosmosConfigInput = {}) {
5
5
  return {
6
6
  ...cosmosConfigInput,
7
7
  rootDir,
8
- exportPath: getExportPath(cosmosConfigInput, rootDir),
9
8
  detectLocalPlugins: cosmosConfigInput.detectLocalPlugins ?? true,
10
9
  disablePlugins: cosmosConfigInput.disablePlugins ?? false,
10
+ exposeImports: getExposeImports(cosmosConfigInput, rootDir),
11
+ exportPath: getExportPath(cosmosConfigInput, rootDir),
11
12
  fixtureFileSuffix: getFixtureFileSuffix(cosmosConfigInput),
12
13
  fixturesDir: getFixturesDir(cosmosConfigInput),
13
14
  globalImports: getGlobalImports(cosmosConfigInput, rootDir),
@@ -16,14 +17,13 @@ export function createCosmosConfig(rootDir, cosmosConfigInput = {}) {
16
17
  httpsOptions: getHttpsOptions(cosmosConfigInput, rootDir),
17
18
  ignore: getIgnore(cosmosConfigInput),
18
19
  lazy: getLazy(cosmosConfigInput),
20
+ plugins: getPlugins(cosmosConfigInput, rootDir),
19
21
  port: getPort(cosmosConfigInput),
20
22
  portRetries: getPortRetries(cosmosConfigInput),
21
- plugins: getPlugins(cosmosConfigInput, rootDir),
22
- publicUrl: getPublicUrl(cosmosConfigInput),
23
+ publicUrl: cosmosConfigInput.publicUrl ?? '',
24
+ rendererUrl: cosmosConfigInput.rendererUrl ?? null,
23
25
  staticPath: getStaticPath(cosmosConfigInput, rootDir),
24
- userDepsFilePath: getUserDepsFilePath(cosmosConfigInput, rootDir),
25
26
  watchDirs: getWatchDirs(cosmosConfigInput, rootDir),
26
- rendererUrl: cosmosConfigInput.rendererUrl ?? null,
27
27
  dom: getDomConfig(cosmosConfigInput.dom || {}),
28
28
  ui: cosmosConfigInput.ui || {},
29
29
  };
@@ -53,9 +53,6 @@ function getHttpsOptions(cosmosConfigInput, rootDir) {
53
53
  function getIgnore({ ignore = [] }) {
54
54
  return ['**/node_modules/**', ...ignore];
55
55
  }
56
- function getPublicUrl({ publicUrl = '/' }) {
57
- return publicUrl;
58
- }
59
56
  function getFixturesDir({ fixturesDir = '__fixtures__' }) {
60
57
  return fixturesDir;
61
58
  }
@@ -66,9 +63,18 @@ function getWatchDirs(cosmosConfigInput, rootDir) {
66
63
  const { watchDirs = ['.'] } = cosmosConfigInput;
67
64
  return watchDirs.map(watchDir => path.resolve(rootDir, watchDir));
68
65
  }
69
- function getUserDepsFilePath(cosmosConfigInput, rootDir) {
70
- const { userDepsFilePath = 'cosmos.userdeps.js' } = cosmosConfigInput;
71
- return path.resolve(rootDir, userDepsFilePath);
66
+ function getExposeImports(cosmosConfigInput, rootDir) {
67
+ const cliArgs = getCliArgs();
68
+ if (typeof cliArgs.exposeImports === 'boolean') {
69
+ return cliArgs.exposeImports;
70
+ }
71
+ else if (typeof cliArgs.exposeImports === 'string') {
72
+ return path.resolve(rootDir, cliArgs.exposeImports);
73
+ }
74
+ const { exposeImports = false } = cosmosConfigInput;
75
+ return typeof exposeImports === 'string'
76
+ ? path.resolve(rootDir, exposeImports)
77
+ : exposeImports;
72
78
  }
73
79
  function getHostname({ hostname = null }) {
74
80
  return hostname;
@@ -10,6 +10,7 @@ export type CosmosConfig = {
10
10
  detectLocalPlugins: boolean;
11
11
  disablePlugins: boolean;
12
12
  dom: CosmosDomConfig;
13
+ exposeImports: boolean | string;
13
14
  fixtureFileSuffix: string;
14
15
  fixturesDir: string;
15
16
  globalImports: string[];
@@ -22,10 +23,12 @@ export type CosmosConfig = {
22
23
  portRetries: number;
23
24
  plugins: string[];
24
25
  publicUrl: string;
25
- rendererUrl: string | null;
26
+ rendererUrl: null | string | {
27
+ dev: string;
28
+ export: string;
29
+ };
26
30
  rootDir: string;
27
31
  staticPath: null | string;
28
- userDepsFilePath: string;
29
32
  watchDirs: string[];
30
33
  [option: string]: unknown;
31
34
  ui: {
@@ -1,17 +1,18 @@
1
1
  /// <reference types="node" />
2
2
  import express from 'express';
3
3
  import http from 'http';
4
- import { MessageType } from 'react-cosmos-core';
4
+ import { CosmosCommand, MessageType } from 'react-cosmos-core';
5
5
  import { CosmosConfig } from '../cosmosConfig/types.js';
6
- export type PlatformType = 'web' | 'native';
6
+ export type CosmosPlatform = 'web' | 'native';
7
7
  export type CosmosConfigPluginArgs = {
8
8
  cosmosConfig: CosmosConfig;
9
- platformType: PlatformType;
9
+ command: CosmosCommand;
10
+ platform: CosmosPlatform;
10
11
  };
11
12
  export type CosmosConfigPlugin = (args: CosmosConfigPluginArgs) => Promise<CosmosConfig> | CosmosConfig;
12
13
  export type DevServerPluginArgs = {
13
14
  cosmosConfig: CosmosConfig;
14
- platformType: PlatformType;
15
+ platform: CosmosPlatform;
15
16
  httpServer: http.Server;
16
17
  expressApp: express.Express;
17
18
  sendMessage(msg: MessageType): unknown;
@@ -1,5 +1,5 @@
1
1
  import express from 'express';
2
2
  import { CosmosPluginConfig } from 'react-cosmos-core';
3
3
  import { CosmosConfig } from '../cosmosConfig/types.js';
4
- import { PlatformType } from '../cosmosPlugin/types.js';
5
- export declare function createExpressApp(platformType: PlatformType, cosmosConfig: CosmosConfig, pluginConfigs: CosmosPluginConfig[]): Promise<express.Express>;
4
+ import { CosmosPlatform } from '../cosmosPlugin/types.js';
5
+ export declare function createExpressApp(platform: CosmosPlatform, cosmosConfig: CosmosConfig, pluginConfigs: CosmosPluginConfig[]): Promise<express.Express>;
@@ -2,11 +2,10 @@ 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);
8
- app.get('/', (req, res) => {
9
- res.send(playgroundHtml);
7
+ app.get('/', async (req, res) => {
8
+ res.send(await getDevPlaygroundHtml(platform, cosmosConfig, pluginConfigs));
10
9
  });
11
10
  app.get('/playground.bundle.js', (req, res) => {
12
11
  res.sendFile(resolve('react-cosmos-ui/dist/playground.bundle.js'));
@@ -1,2 +1,2 @@
1
- import { PlatformType } from '../cosmosPlugin/types.js';
2
- export declare function startDevServer(platformType: PlatformType): Promise<() => Promise<void>>;
1
+ import { CosmosPlatform } from '../cosmosPlugin/types.js';
2
+ export declare function startDevServer(platform: CosmosPlatform): Promise<() => Promise<void>>;
@@ -1,14 +1,14 @@
1
1
  import path from 'path';
2
- import { coreServerPlugins } from '../corePlugins/index.js';
3
2
  import { detectCosmosConfig, detectCosmosConfigPath, } from '../cosmosConfig/detectCosmosConfig.js';
4
3
  import { getPluginConfigs } from '../cosmosPlugin/pluginConfigs.js';
5
- import { importServerPlugins } from '../shared/importServerPlugins.js';
4
+ import { applyServerConfigPlugins } from '../shared/applyServerConfigPlugins.js';
5
+ import { getServerPlugins } from '../shared/getServerPlugins.js';
6
6
  import { logPluginInfo } from '../shared/logPluginInfo.js';
7
7
  import { serveStaticDir } from '../shared/staticServer.js';
8
8
  import { createExpressApp } from './expressApp.js';
9
9
  import { createHttpServer } from './httpServer.js';
10
10
  import { createMessageHandler } from './messageHandler.js';
11
- export async function startDevServer(platformType) {
11
+ export async function startDevServer(platform) {
12
12
  let cosmosConfig = await detectCosmosConfig();
13
13
  logCosmosConfigInfo();
14
14
  const pluginConfigs = await getPluginConfigs({
@@ -19,20 +19,14 @@ export async function startDevServer(platformType) {
19
19
  relativePaths: false,
20
20
  });
21
21
  logPluginInfo(pluginConfigs);
22
- const userPlugins = await importServerPlugins(pluginConfigs, cosmosConfig.rootDir);
23
- const plugins = [...coreServerPlugins, ...userPlugins];
24
- for (const plugin of plugins) {
25
- if (plugin.config) {
26
- try {
27
- cosmosConfig = await plugin.config({ cosmosConfig, platformType });
28
- }
29
- catch (err) {
30
- console.log(`[Cosmos][plugin:${plugin.name}] Config hook failed`);
31
- throw err;
32
- }
33
- }
34
- }
35
- 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);
36
30
  const httpServer = await createHttpServer(cosmosConfig, app);
37
31
  const msgHandler = createMessageHandler(httpServer.server);
38
32
  if (cosmosConfig.staticPath) {
@@ -47,21 +41,21 @@ export async function startDevServer(platformType) {
47
41
  msgHandler.close();
48
42
  await httpServer.stop();
49
43
  }
50
- for (const plugin of plugins) {
44
+ for (const plugin of serverPlugins) {
51
45
  try {
52
46
  if (!plugin.devServer)
53
47
  continue;
54
48
  const pluginReturn = await plugin.devServer({
55
49
  cosmosConfig,
56
- platformType,
50
+ platform,
57
51
  httpServer: httpServer.server,
58
52
  expressApp: app,
59
53
  sendMessage: msgHandler.sendMessage,
60
54
  });
61
55
  if (typeof pluginReturn === 'function') {
62
- pluginCleanupCallbacks.push(() => {
56
+ pluginCleanupCallbacks.push(async () => {
63
57
  try {
64
- pluginReturn();
58
+ await pluginReturn();
65
59
  }
66
60
  catch (err) {
67
61
  // Log when a plugin fails to clean up, but continue to attempt