react-cosmos 6.0.0-alpha.10 → 6.0.0-alpha.12

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 (135) hide show
  1. package/bin/cosmos-export.js +1 -3
  2. package/bin/cosmos-native.js +1 -3
  3. package/bin/cosmos.js +1 -3
  4. package/config.schema.json +10 -2
  5. package/dist/corePlugins/httpProxy.d.ts +2 -0
  6. package/dist/corePlugins/httpProxy.js +21 -0
  7. package/dist/corePlugins/index.d.ts +2 -0
  8. package/dist/corePlugins/index.js +12 -0
  9. package/dist/corePlugins/openFile.d.ts +2 -0
  10. package/dist/corePlugins/openFile.js +56 -0
  11. package/dist/corePlugins/pluginEndpoint.d.ts +2 -0
  12. package/dist/corePlugins/pluginEndpoint.js +22 -0
  13. package/dist/corePlugins/portRetry.d.ts +2 -0
  14. package/dist/corePlugins/portRetry.js +11 -0
  15. package/dist/corePlugins/userDepsFile.d.ts +2 -0
  16. package/dist/{server/devServer/corePlugins → corePlugins}/userDepsFile.js +19 -16
  17. package/dist/{server/cosmosConfig → cosmosConfig}/createCosmosConfig.js +20 -0
  18. package/dist/{server/cosmosConfig → cosmosConfig}/shared.js +2 -2
  19. package/dist/{server/cosmosConfig → cosmosConfig}/types.d.ts +7 -0
  20. package/dist/{server/cosmosPlugin → cosmosPlugin}/readCosmosPluginConfig.js +4 -7
  21. package/dist/{server/cosmosPlugin → cosmosPlugin}/types.d.ts +11 -0
  22. package/dist/{server/devServer → devServer}/messageHandler.d.ts +1 -1
  23. package/dist/{server/devServer → devServer}/messageHandler.js +2 -2
  24. package/dist/{server/devServer → devServer}/startDevServer.js +23 -23
  25. package/dist/{server/export → export}/generateExport.js +23 -12
  26. package/dist/index.d.ts +21 -0
  27. package/dist/index.js +21 -1
  28. package/dist/jest.d.ts +7 -7
  29. package/dist/jest.js +7 -7
  30. package/dist/shared/findNextAvailablePort.d.ts +1 -0
  31. package/dist/shared/findNextAvailablePort.js +29 -0
  32. package/dist/shared/importServerPlugins.d.ts +3 -0
  33. package/dist/shared/importServerPlugins.js +16 -0
  34. package/dist/{server/testHelpers → testHelpers}/mockConsole.js +8 -4
  35. package/dist/{server/testHelpers → testHelpers}/mockFs.d.ts +2 -2
  36. package/dist/{server/testHelpers → testHelpers}/mockFs.js +26 -8
  37. package/dist/{server/testHelpers → testHelpers}/mockYargs.js +1 -0
  38. package/dist/{server/userDeps → userDeps}/generateUserDepsModule.js +3 -1
  39. package/dist/{server/userDeps → userDeps}/getUserModules.d.ts +3 -3
  40. package/dist/userDeps/userDepsLazyTemplate.d.ts +2 -0
  41. package/dist/userDeps/userDepsLazyTemplate.js +37 -0
  42. package/dist/userDeps/userDepsShared.d.ts +11 -0
  43. package/dist/userDeps/userDepsShared.js +13 -0
  44. package/dist/userDeps/userDepsTemplate.d.ts +2 -0
  45. package/dist/userDeps/userDepsTemplate.js +45 -0
  46. package/dist/{server/utils → utils}/cli.d.ts +1 -0
  47. package/dist/utils/cli.js +4 -0
  48. package/dist/{server/utils → utils}/fs.d.ts +2 -1
  49. package/dist/{server/utils → utils}/fs.js +8 -5
  50. package/package.json +4 -4
  51. package/dist/server/devServer/corePlugins/httpProxy.d.ts +0 -2
  52. package/dist/server/devServer/corePlugins/httpProxy.js +0 -18
  53. package/dist/server/devServer/corePlugins/openFile.d.ts +0 -2
  54. package/dist/server/devServer/corePlugins/openFile.js +0 -53
  55. package/dist/server/devServer/corePlugins/pluginEndpoint.d.ts +0 -2
  56. package/dist/server/devServer/corePlugins/pluginEndpoint.js +0 -17
  57. package/dist/server/devServer/corePlugins/userDepsFile.d.ts +0 -2
  58. package/dist/server/shared/requirePluginModule.d.ts +0 -2
  59. package/dist/server/shared/requirePluginModule.js +0 -10
  60. package/dist/server/userDeps/userDepsTemplate.d.ts +0 -11
  61. package/dist/server/userDeps/userDepsTemplate.js +0 -49
  62. package/dist/server/utils/cli.js +0 -4
  63. package/dist/server.d.ts +0 -20
  64. package/dist/server.js +0 -20
  65. package/server.d.ts +0 -1
  66. package/server.js +0 -1
  67. /package/dist/{server/cosmosConfig → cosmosConfig}/createCosmosConfig.d.ts +0 -0
  68. /package/dist/{server/cosmosConfig → cosmosConfig}/detectCosmosConfig.d.ts +0 -0
  69. /package/dist/{server/cosmosConfig → cosmosConfig}/detectCosmosConfig.js +0 -0
  70. /package/dist/{server/cosmosConfig → cosmosConfig}/getCosmosConfigAtPath.d.ts +0 -0
  71. /package/dist/{server/cosmosConfig → cosmosConfig}/getCosmosConfigAtPath.js +0 -0
  72. /package/dist/{server/cosmosConfig → cosmosConfig}/shared.d.ts +0 -0
  73. /package/dist/{server/cosmosConfig → cosmosConfig}/types.js +0 -0
  74. /package/dist/{server/cosmosPlugin → cosmosPlugin}/findCosmosPluginConfigs.d.ts +0 -0
  75. /package/dist/{server/cosmosPlugin → cosmosPlugin}/findCosmosPluginConfigs.js +0 -0
  76. /package/dist/{server/cosmosPlugin → cosmosPlugin}/pluginConfigs.d.ts +0 -0
  77. /package/dist/{server/cosmosPlugin → cosmosPlugin}/pluginConfigs.js +0 -0
  78. /package/dist/{server/cosmosPlugin → cosmosPlugin}/readCosmosPluginConfig.d.ts +0 -0
  79. /package/dist/{server/cosmosPlugin → cosmosPlugin}/types.js +0 -0
  80. /package/dist/{server/devServer → devServer}/expressApp.d.ts +0 -0
  81. /package/dist/{server/devServer → devServer}/expressApp.js +0 -0
  82. /package/dist/{server/devServer → devServer}/httpServer.d.ts +0 -0
  83. /package/dist/{server/devServer → devServer}/httpServer.js +0 -0
  84. /package/dist/{server/devServer → devServer}/startDevServer.d.ts +0 -0
  85. /package/dist/{server/export → export}/generateExport.d.ts +0 -0
  86. /package/dist/{server/getFixtures → getFixtures}/getFixtures.d.ts +0 -0
  87. /package/dist/{server/getFixtures → getFixtures}/getFixtures.js +0 -0
  88. /package/dist/{server/shared → shared}/logPluginInfo.d.ts +0 -0
  89. /package/dist/{server/shared → shared}/logPluginInfo.js +0 -0
  90. /package/dist/{server/shared → shared}/playgroundHtml.d.ts +0 -0
  91. /package/dist/{server/shared → shared}/playgroundHtml.js +0 -0
  92. /package/dist/{server/shared → shared}/playgroundUrl.d.ts +0 -0
  93. /package/dist/{server/shared → shared}/playgroundUrl.js +0 -0
  94. /package/dist/{server/shared → shared}/resolveRendererUrl.d.ts +0 -0
  95. /package/dist/{server/shared → shared}/resolveRendererUrl.js +0 -0
  96. /package/dist/{server/shared → shared}/staticPath.d.ts +0 -0
  97. /package/dist/{server/shared → shared}/staticPath.js +0 -0
  98. /package/dist/{server/shared → shared}/staticServer.d.ts +0 -0
  99. /package/dist/{server/shared → shared}/staticServer.js +0 -0
  100. /package/dist/{server/static → static}/favicon.ico +0 -0
  101. /package/dist/{server/static → static}/index.html +0 -0
  102. /package/dist/{server/testHelpers → testHelpers}/cwd.d.ts +0 -0
  103. /package/dist/{server/testHelpers → testHelpers}/cwd.js +0 -0
  104. /package/dist/{server/testHelpers → testHelpers}/mockConsole.d.ts +0 -0
  105. /package/dist/{server/testHelpers → testHelpers}/mockEsmRequire.d.ts +0 -0
  106. /package/dist/{server/testHelpers → testHelpers}/mockEsmRequire.js +0 -0
  107. /package/dist/{server/testHelpers → testHelpers}/mockEsmResolve.d.ts +0 -0
  108. /package/dist/{server/testHelpers → testHelpers}/mockEsmResolve.js +0 -0
  109. /package/dist/{server/testHelpers → testHelpers}/mockEsmStaticPath.d.ts +0 -0
  110. /package/dist/{server/testHelpers → testHelpers}/mockEsmStaticPath.js +0 -0
  111. /package/dist/{server/testHelpers → testHelpers}/mockFetch.d.ts +0 -0
  112. /package/dist/{server/testHelpers → testHelpers}/mockFetch.js +0 -0
  113. /package/dist/{server/testHelpers → testHelpers}/mockYargs.d.ts +0 -0
  114. /package/dist/{server/userDeps → userDeps}/findUserModulePaths.d.ts +0 -0
  115. /package/dist/{server/userDeps → userDeps}/findUserModulePaths.js +0 -0
  116. /package/dist/{server/userDeps → userDeps}/fixtureWatcher.d.ts +0 -0
  117. /package/dist/{server/userDeps → userDeps}/fixtureWatcher.js +0 -0
  118. /package/dist/{server/userDeps → userDeps}/generateUserDepsModule.d.ts +0 -0
  119. /package/dist/{server/userDeps → userDeps}/getUserModules.js +0 -0
  120. /package/dist/{server/userDeps → userDeps}/shared.d.ts +0 -0
  121. /package/dist/{server/userDeps → userDeps}/shared.js +0 -0
  122. /package/dist/{server/utils → utils}/isNodeError.d.ts +0 -0
  123. /package/dist/{server/utils → utils}/isNodeError.js +0 -0
  124. /package/dist/{server/utils → utils}/requireModule.d.ts +0 -0
  125. /package/dist/{server/utils → utils}/requireModule.js +0 -0
  126. /package/dist/{server/utils → utils}/requireSilent.d.ts +0 -0
  127. /package/dist/{server/utils → utils}/requireSilent.js +0 -0
  128. /package/dist/{server/utils → utils}/resolve.d.ts +0 -0
  129. /package/dist/{server/utils → utils}/resolve.js +0 -0
  130. /package/dist/{server/utils → utils}/resolveLoose.d.ts +0 -0
  131. /package/dist/{server/utils → utils}/resolveLoose.js +0 -0
  132. /package/dist/{server/utils → utils}/resolveSilent.d.ts +0 -0
  133. /package/dist/{server/utils → utils}/resolveSilent.js +0 -0
  134. /package/dist/{server/utils → utils}/slash.d.ts +0 -0
  135. /package/dist/{server/utils → utils}/slash.js +0 -0
package/dist/jest.js CHANGED
@@ -1,7 +1,7 @@
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/mockEsmRequire.js';
3
+ export * from './testHelpers/mockEsmResolve.js';
4
+ export * from './testHelpers/mockEsmStaticPath.js';
5
+ export * from './testHelpers/mockFetch.js';
6
+ export * from './testHelpers/mockFs.js';
7
+ export * from './testHelpers/mockYargs.js';
@@ -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,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
+ }
@@ -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;
@@ -1,4 +1,4 @@
1
- import path from 'path';
1
+ import path from 'node:path';
2
2
  import { getCwdPath } from './cwd.js';
3
3
  jest.mock('../utils/fs', () => {
4
4
  const actual = jest.requireActual('../utils/fs');
@@ -8,8 +8,20 @@ jest.mock('../utils/fs', () => {
8
8
  async function importModule(moduleId) {
9
9
  if (!mocked)
10
10
  return actual.importModule(moduleId);
11
+ if (!fileMocks.hasOwnProperty(moduleId) &&
12
+ !fileMocks.hasOwnProperty(`${moduleId}.js`)) {
13
+ throw new Error(`Cannot find module '${moduleId}'`);
14
+ }
11
15
  return fileMocks[moduleId] || fileMocks[`${moduleId}.js`];
12
16
  }
17
+ async function importJson(filePath) {
18
+ if (!mocked)
19
+ return actual.importJson(filePath);
20
+ if (!fileMocks.hasOwnProperty(filePath)) {
21
+ throw new Error(`Cannot find JSON '${filePath}'`);
22
+ }
23
+ return fileMocks[filePath];
24
+ }
13
25
  function moduleExists(moduleId) {
14
26
  if (!mocked)
15
27
  return actual.moduleExists(moduleId);
@@ -28,6 +40,7 @@ jest.mock('../utils/fs', () => {
28
40
  }
29
41
  return {
30
42
  importModule,
43
+ importJson,
31
44
  moduleExists,
32
45
  fileExists,
33
46
  dirExists,
@@ -35,6 +48,10 @@ jest.mock('../utils/fs', () => {
35
48
  mocked = true;
36
49
  fileMocks = { ...fileMocks, [filePath]: fileMock };
37
50
  },
51
+ __mockJson(filePath, jsonMock) {
52
+ mocked = true;
53
+ fileMocks = { ...fileMocks, [filePath]: jsonMock };
54
+ },
38
55
  __mockDir(dirPath) {
39
56
  mocked = true;
40
57
  dirMocks = [...dirMocks, dirPath];
@@ -45,16 +62,17 @@ jest.mock('../utils/fs', () => {
45
62
  },
46
63
  };
47
64
  });
65
+ export function mockFile(filePath, fileMock) {
66
+ requireMocked().__mockFile(filePath, fileMock);
67
+ requireMocked().__mockDir(path.dirname(filePath));
68
+ }
48
69
  export function mockCosmosConfig(cosmosConfigPath, cosmosConfig) {
49
- mockFile(cosmosConfigPath, cosmosConfig);
70
+ const absPath = getCwdPath(cosmosConfigPath);
71
+ mockFile(absPath, cosmosConfig);
50
72
  }
51
- export function mockFile(filePath, fileContent) {
73
+ export function mockCwdModuleDefault(filePath, fileMock) {
52
74
  const absPath = getCwdPath(filePath);
53
- requireMocked().__mockFile(absPath, fileContent);
54
- requireMocked().__mockDir(path.dirname(absPath));
55
- }
56
- export function mockDir(dirPath) {
57
- requireMocked().__mockDir(getCwdPath(dirPath));
75
+ mockFile(absPath, { default: fileMock });
58
76
  }
59
77
  export function resetFsMock() {
60
78
  requireMocked().__resetMock();
@@ -3,6 +3,7 @@
3
3
  jest.mock('yargs/yargs', () => {
4
4
  let argv = {};
5
5
  const yargs = () => ({
6
+ boolean: () => yargs(),
6
7
  parseSync: () => argv,
7
8
  });
8
9
  yargs.__mockArgsv = (newArgv) => {
@@ -1,4 +1,5 @@
1
1
  import { findUserModulePaths } from './findUserModulePaths.js';
2
+ import { userDepsLazyTemplate } from './userDepsLazyTemplate.js';
2
3
  import { userDepsTemplate } from './userDepsTemplate.js';
3
4
  export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeToDir, }) {
4
5
  const { rootDir, fixturesDir, fixtureFileSuffix, globalImports, ignore } = cosmosConfig;
@@ -8,7 +9,8 @@ export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeT
8
9
  fixtureFileSuffix,
9
10
  ignore,
10
11
  });
11
- return userDepsTemplate({
12
+ const template = cosmosConfig.lazy ? userDepsLazyTemplate : userDepsTemplate;
13
+ return template({
12
14
  globalImports,
13
15
  fixturePaths,
14
16
  decoratorPaths,
@@ -1,8 +1,8 @@
1
- import { ReactDecorators, ReactFixtureExports } from 'react-cosmos-core';
1
+ import { ByPath, ReactDecorator, ReactFixtureExport } from 'react-cosmos-core';
2
2
  import { CosmosConfig } from '../cosmosConfig/types.js';
3
3
  type UserModules = {
4
- fixtures: ReactFixtureExports;
5
- decorators: ReactDecorators;
4
+ fixtures: ByPath<ReactFixtureExport>;
5
+ decorators: ByPath<ReactDecorator>;
6
6
  };
7
7
  export declare function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }: CosmosConfig): UserModules;
8
8
  export {};
@@ -0,0 +1,2 @@
1
+ import { UserDepsTemplateArgs } from './userDepsShared.js';
2
+ export declare function userDepsLazyTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: UserDepsTemplateArgs): string;
@@ -0,0 +1,37 @@
1
+ import { userDepsImportMap, userDepsImportPath, } from './userDepsShared.js';
2
+ export function userDepsLazyTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }) {
3
+ const fixtures = userDepsImportMap(fixturePaths, rootDir, relativeToDir);
4
+ const fixtureKeys = Object.keys(fixtures);
5
+ const decorators = userDepsImportMap(decoratorPaths, rootDir, relativeToDir);
6
+ const decoratorKeys = Object.keys(decorators);
7
+ return `
8
+ // This file is automatically generated by Cosmos. Add it to .gitignore and
9
+ // only edit if you know what you're doing.
10
+
11
+ // Keeping global imports here is superior to making them bundle entry points
12
+ // because this way they become hot-reloadable.
13
+ ${globalImports
14
+ .map(p => `import '${userDepsImportPath(p, relativeToDir)}';`)
15
+ .join(`\n`)}
16
+
17
+ export const rendererConfig = ${JSON.stringify(rendererConfig, null, 2)};
18
+
19
+ const fixtures = {
20
+ ${fixtureKeys
21
+ .map(k => ` '${k}': { getModule: () => import('${fixtures[k]}') }`)
22
+ .join(`,\n`)}
23
+ };
24
+
25
+ const decorators = {
26
+ ${decoratorKeys
27
+ .map(k => ` '${k}': { getModule: () => import('${decorators[k]}') }`)
28
+ .join(`,\n`)}
29
+ };
30
+
31
+ export const moduleWrappers = {
32
+ lazy: true,
33
+ fixtures,
34
+ decorators,
35
+ };
36
+ `.trimStart();
37
+ }
@@ -0,0 +1,11 @@
1
+ import { Json } from './shared.js';
2
+ export type UserDepsTemplateArgs = {
3
+ globalImports: string[];
4
+ fixturePaths: string[];
5
+ decoratorPaths: string[];
6
+ rendererConfig: Json;
7
+ rootDir: string;
8
+ relativeToDir: string | null;
9
+ };
10
+ export declare function userDepsImportMap(paths: string[], rootDir: string, relativeToDir: string | null): Record<string, string>;
11
+ export declare function userDepsImportPath(filePath: string, relativeToDir: string | null): string;
@@ -0,0 +1,13 @@
1
+ import path from 'path';
2
+ import { slash } from '../utils/slash.js';
3
+ export function userDepsImportMap(paths, rootDir, relativeToDir) {
4
+ return paths.reduce((acc, p) => ({
5
+ ...acc,
6
+ [slash(path.relative(rootDir, p))]: userDepsImportPath(p, relativeToDir),
7
+ }), {});
8
+ }
9
+ export function userDepsImportPath(filePath, relativeToDir) {
10
+ return slash(relativeToDir
11
+ ? `.${path.sep}${path.relative(relativeToDir, filePath)}`
12
+ : filePath);
13
+ }
@@ -0,0 +1,2 @@
1
+ import { UserDepsTemplateArgs } from './userDepsShared.js';
2
+ export declare function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: UserDepsTemplateArgs): string;
@@ -0,0 +1,45 @@
1
+ import { userDepsImportMap, userDepsImportPath, } from './userDepsShared.js';
2
+ export function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }) {
3
+ const fixtures = userDepsImportMap(fixturePaths, rootDir, relativeToDir);
4
+ const fixtureKeys = Object.keys(fixtures);
5
+ const decorators = userDepsImportMap(decoratorPaths, rootDir, relativeToDir);
6
+ const decoratorKeys = Object.keys(decorators);
7
+ return `
8
+ // This file is automatically generated by Cosmos. Add it to .gitignore and
9
+ // only edit if you know what you're doing.
10
+
11
+ // Keeping global imports here is superior to making them bundle entry points
12
+ // because this way they become hot-reloadable.
13
+ ${globalImports
14
+ .map(p => `import '${userDepsImportPath(p, relativeToDir)}';`)
15
+ .join(`\n`)}
16
+
17
+ ${fixtureKeys
18
+ .map((k, i) => `import fixture${i} from '${fixtures[k]}';`)
19
+ .join(`\n`)}
20
+
21
+ ${decoratorKeys
22
+ .map((k, i) => `import decorator${i} from '${decorators[k]}';`)
23
+ .join(`\n`)}
24
+
25
+ export const rendererConfig = ${JSON.stringify(rendererConfig, null, 2)};
26
+
27
+ const fixtures = {
28
+ ${fixtureKeys
29
+ .map((k, i) => ` '${k}': { module: { default: fixture${i} } }`)
30
+ .join(`,\n`)}
31
+ };
32
+
33
+ const decorators = {
34
+ ${decoratorKeys
35
+ .map((k, i) => ` '${k}': { module: { default: decorator${i} } }`)
36
+ .join(`,\n`)}
37
+ };
38
+
39
+ export const moduleWrappers = {
40
+ lazy: false,
41
+ fixtures,
42
+ decorators,
43
+ };
44
+ `.trimStart();
45
+ }
@@ -1,5 +1,6 @@
1
1
  export declare function getCliArgs(): {
2
2
  [x: string]: unknown;
3
+ lazy: boolean | undefined;
3
4
  _: (string | number)[];
4
5
  $0: string;
5
6
  };
@@ -0,0 +1,4 @@
1
+ import yargs from 'yargs/yargs';
2
+ export function getCliArgs() {
3
+ return yargs(process.argv.slice(2)).boolean('lazy').parseSync();
4
+ }
@@ -1,4 +1,5 @@
1
- export declare function importModule<T>(moduleId: string): Promise<T>;
1
+ export declare function importModule<T>(filePath: string): Promise<T>;
2
+ export declare function importJson<T>(filePath: string): Promise<T>;
2
3
  export declare function moduleExists(moduleId: string): boolean | "";
3
4
  export declare function fileExists(filePath: string): boolean;
4
5
  export declare function dirExists(dirPath: string): boolean;
@@ -1,11 +1,14 @@
1
1
  // NOTE: This API has been extracted to easily mock the file system in tests
2
- import fs from 'fs';
2
+ import fs from 'node:fs';
3
+ import url from 'node:url';
3
4
  import { requireModule } from './requireModule.js';
4
5
  import { resolve } from './resolve.js';
5
- export async function importModule(moduleId) {
6
- return moduleId.endsWith('.json')
7
- ? requireModule(moduleId)
8
- : import(moduleId);
6
+ export async function importModule(filePath) {
7
+ const fileUrl = url.pathToFileURL(filePath).href;
8
+ return import(fileUrl);
9
+ }
10
+ export async function importJson(filePath) {
11
+ return requireModule(filePath);
9
12
  }
10
13
  // Better than fs.exists because it works for module paths without an extension
11
14
  export function moduleExists(moduleId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-cosmos",
3
- "version": "6.0.0-alpha.10",
3
+ "version": "6.0.0-alpha.12",
4
4
  "description": "Sandbox for developing and testing UI components in isolation",
5
5
  "repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos",
6
6
  "license": "MIT",
@@ -17,8 +17,8 @@
17
17
  "open": "^8.4.2",
18
18
  "pem": "^1.14.6",
19
19
  "pkg-up": "^4.0.0",
20
- "react-cosmos-core": "^6.0.0-alpha.9",
21
- "react-cosmos-ui": "^6.0.0-alpha.10",
20
+ "react-cosmos-core": "^6.0.0-alpha.12",
21
+ "react-cosmos-ui": "^6.0.0-alpha.12",
22
22
  "ws": "^8.12.1",
23
23
  "yargs": "^17.7.1"
24
24
  },
@@ -27,5 +27,5 @@
27
27
  "cosmos-export": "./bin/cosmos-export.js",
28
28
  "cosmos-native": "./bin/cosmos-native.js"
29
29
  },
30
- "gitHead": "26afefd047caa631daf5648fc740e2f32957f44d"
30
+ "gitHead": "5987f899563ec2ddae727d4d57431a0400c49161"
31
31
  }
@@ -1,2 +0,0 @@
1
- import { DevServerPluginArgs } from '../../cosmosPlugin/types.js';
2
- export declare function httpProxyDevServerPlugin({ platformType, cosmosConfig, expressApp, }: DevServerPluginArgs): void;
@@ -1,18 +0,0 @@
1
- import { createProxyMiddleware } from 'http-proxy-middleware';
2
- export function httpProxyDevServerPlugin({ platformType, cosmosConfig, expressApp, }) {
3
- if (platformType !== 'web')
4
- return;
5
- const httpProxyConfig = getHttpProxyCosmosConfig(cosmosConfig);
6
- Object.keys(httpProxyConfig).forEach(context => {
7
- const config = httpProxyConfig[context];
8
- if (typeof config === 'string') {
9
- expressApp.use(context, createProxyMiddleware(context, { target: config }));
10
- }
11
- else if (typeof config === 'object') {
12
- expressApp.use(context, createProxyMiddleware(context, config));
13
- }
14
- });
15
- }
16
- function getHttpProxyCosmosConfig(cosmosConfig) {
17
- return (cosmosConfig.httpProxy || {});
18
- }
@@ -1,2 +0,0 @@
1
- import { DevServerPluginArgs } from '../../cosmosPlugin/types.js';
2
- export default function openFileDevServerPlugin({ cosmosConfig, expressApp, }: DevServerPluginArgs): void;
@@ -1,53 +0,0 @@
1
- import launchEditor from '@skidding/launch-editor';
2
- import fs from 'fs';
3
- import open from 'open';
4
- import path from 'path';
5
- export default function openFileDevServerPlugin({ cosmosConfig, expressApp, }) {
6
- expressApp.get('/_open', (req, res) => {
7
- const { filePath, line, column } = getReqQuery(req);
8
- if (!filePath) {
9
- res.status(400).send(`File path missing`);
10
- return;
11
- }
12
- const absFilePath = resolveFilePath(cosmosConfig.rootDir, filePath);
13
- if (!fs.existsSync(absFilePath)) {
14
- res.status(404).send(`File not found at path: ${absFilePath}`);
15
- return;
16
- }
17
- new Promise((resolve, reject) => {
18
- const file = `${absFilePath}:${line}:${column}`;
19
- launchEditor(file, (fileName, errorMsg) => reject(errorMsg));
20
- // If launchEditor doesn't report error within 500ms we assume it worked
21
- setTimeout(resolve, 500);
22
- })
23
- // Fall back to open in case launchEditor fails. launchEditor only works
24
- // when the editor app is already open, but is favorable because it can
25
- // open a code file on a specific line & column.
26
- .catch(err => open(absFilePath))
27
- .catch(err => res.status(500).send('Failed to open file'))
28
- .then(() => res.send());
29
- });
30
- }
31
- function getReqQuery(req) {
32
- const { filePath, line, column } = req.query;
33
- if (typeof filePath !== 'string')
34
- throw new Error('filePath missing');
35
- return {
36
- filePath,
37
- line: typeof line === 'string' ? parseInt(line, 10) : 1,
38
- column: typeof column === 'string' ? parseInt(column, 10) : 1,
39
- };
40
- }
41
- function resolveFilePath(rootDir, filePath) {
42
- // This heuristic is needed because the open file endpoint is used for
43
- // multiple applications, which provide different file path types:
44
- // 1. Edit fixture button: Sends path relative to Cosmos rootDir
45
- // 2. react-error-overlay runtime error: Sends absolute path
46
- // 3. react-error-overlay compile error: Sends path relative to CWD
47
- if (path.isAbsolute(filePath)) {
48
- return filePath;
49
- }
50
- const cosmosRelPath = path.join(rootDir, filePath);
51
- const cwdRelPath = path.join(process.cwd(), filePath);
52
- return fs.existsSync(cosmosRelPath) ? cosmosRelPath : cwdRelPath;
53
- }
@@ -1,2 +0,0 @@
1
- import { DevServerPluginArgs } from '../../cosmosPlugin/types.js';
2
- export default function pluginEndpointDevServerPlugin({ cosmosConfig, expressApp, }: DevServerPluginArgs): void;
@@ -1,17 +0,0 @@
1
- import { resolveFromSilent, resolveSilent } from '../../utils/resolveSilent.js';
2
- export default function pluginEndpointDevServerPlugin({ cosmosConfig, expressApp, }) {
3
- expressApp.get('/_plugin/*.js', (req, res) => {
4
- const moduleId = req.params['0'];
5
- if (!moduleId) {
6
- res.sendStatus(404);
7
- return;
8
- }
9
- const resolvedPath = resolveSilent(`/${moduleId}`) ||
10
- resolveFromSilent(cosmosConfig.rootDir, moduleId);
11
- if (!resolvedPath) {
12
- res.sendStatus(404);
13
- return;
14
- }
15
- res.sendFile(resolvedPath);
16
- });
17
- }
@@ -1,2 +0,0 @@
1
- import { DevServerPluginArgs } from '../../cosmosPlugin/types.js';
2
- export declare function userDepsFileDevServerPlugin(args: DevServerPluginArgs): Promise<(() => void) | undefined>;
@@ -1,2 +0,0 @@
1
- import { CosmosPluginConfig } from 'react-cosmos-core';
2
- export declare function requirePluginModule<T>(rootDir: string, pluginConfig: CosmosPluginConfig, configKey: 'ui' | 'devServer' | 'export'): Promise<T>;
@@ -1,10 +0,0 @@
1
- import path from 'path';
2
- import { pathToFileURL } from 'url';
3
- export async function requirePluginModule(rootDir, pluginConfig, configKey) {
4
- const moduleId = pluginConfig[configKey];
5
- if (!moduleId) {
6
- throw new Error(`Module missing in plugin "${pluginConfig.name}": ${configKey}`);
7
- }
8
- const esModule = await import(pathToFileURL(path.resolve(rootDir, moduleId)).href);
9
- return esModule.default;
10
- }
@@ -1,11 +0,0 @@
1
- import { Json } from './shared.js';
2
- type Args = {
3
- globalImports: string[];
4
- fixturePaths: string[];
5
- decoratorPaths: string[];
6
- rendererConfig: Json;
7
- rootDir: string;
8
- relativeToDir: string | null;
9
- };
10
- export declare function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: Args): string;
11
- export {};
@@ -1,49 +0,0 @@
1
- import path from 'path';
2
- import { slash } from '../utils/slash.js';
3
- export function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }) {
4
- const fixtures = createImportMap(fixturePaths, rootDir, relativeToDir);
5
- const fixtureKeys = Object.keys(fixtures);
6
- const decorators = createImportMap(decoratorPaths, rootDir, relativeToDir);
7
- const decoratorKeys = Object.keys(decorators);
8
- return `
9
- // This file is automatically generated by Cosmos. Add it to .gitignore and
10
- // only edit if you know what you're doing.
11
-
12
- // Keeping global imports here is superior to making them bundle entry points
13
- // because this way they become hot-reloadable.
14
- ${globalImports
15
- .map(p => `import '${resolveImportPath(p, relativeToDir)}';`)
16
- .join(`\n`)}
17
-
18
- ${fixtureKeys
19
- .map((k, i) => `import fixture${i} from '${fixtures[k]}';`)
20
- .join(`\n`)}
21
-
22
- ${decoratorKeys
23
- .map((k, i) => `import decorator${i} from '${decorators[k]}';`)
24
- .join(`\n`)}
25
-
26
- export const rendererConfig = ${JSON.stringify(rendererConfig, null, 2)};
27
-
28
- export const fixtures = {
29
- ${fixtureKeys
30
- .map((k, i) => ` '${k}': { module: { default: fixture${i} } }`)
31
- .join(`,\n`)}
32
- };
33
-
34
- export const decorators = {
35
- ${decoratorKeys.map((k, i) => ` '${k}': decorator${i}`).join(`,\n`)}
36
- };
37
- `.trimStart();
38
- }
39
- function createImportMap(paths, rootDir, relativeToDir) {
40
- return paths.reduce((acc, p) => ({
41
- ...acc,
42
- [slash(path.relative(rootDir, p))]: resolveImportPath(p, relativeToDir),
43
- }), {});
44
- }
45
- function resolveImportPath(filePath, relativeToDir) {
46
- return slash(relativeToDir
47
- ? `.${path.sep}${path.relative(relativeToDir, filePath)}`
48
- : filePath);
49
- }
@@ -1,4 +0,0 @@
1
- import yargs from 'yargs/yargs';
2
- export function getCliArgs() {
3
- return yargs(process.argv.slice(2)).parseSync();
4
- }
package/dist/server.d.ts DELETED
@@ -1,20 +0,0 @@
1
- export * from './server/cosmosConfig/createCosmosConfig.js';
2
- export * from './server/cosmosConfig/detectCosmosConfig.js';
3
- export * from './server/cosmosConfig/getCosmosConfigAtPath.js';
4
- export * from './server/cosmosConfig/types.js';
5
- export * from './server/cosmosPlugin/findCosmosPluginConfigs.js';
6
- export * from './server/cosmosPlugin/pluginConfigs.js';
7
- export * from './server/cosmosPlugin/types.js';
8
- export * from './server/getFixtures/getFixtures.js';
9
- export { RENDERER_FILENAME } from './server/shared/playgroundHtml.js';
10
- export * from './server/shared/playgroundUrl.js';
11
- export * from './server/shared/staticServer.js';
12
- export * from './server/testHelpers/cwd.js';
13
- export * from './server/userDeps/fixtureWatcher.js';
14
- export * from './server/userDeps/generateUserDepsModule.js';
15
- export * from './server/utils/cli.js';
16
- export * from './server/utils/fs.js';
17
- export * from './server/utils/requireSilent.js';
18
- export * from './server/utils/resolve.js';
19
- export * from './server/utils/resolveLoose.js';
20
- export * from './server/utils/resolveSilent.js';
package/dist/server.js DELETED
@@ -1,20 +0,0 @@
1
- export * from './server/cosmosConfig/createCosmosConfig.js';
2
- export * from './server/cosmosConfig/detectCosmosConfig.js';
3
- export * from './server/cosmosConfig/getCosmosConfigAtPath.js';
4
- export * from './server/cosmosConfig/types.js';
5
- export * from './server/cosmosPlugin/findCosmosPluginConfigs.js';
6
- export * from './server/cosmosPlugin/pluginConfigs.js';
7
- export * from './server/cosmosPlugin/types.js';
8
- export * from './server/getFixtures/getFixtures.js';
9
- export { RENDERER_FILENAME } from './server/shared/playgroundHtml.js';
10
- export * from './server/shared/playgroundUrl.js';
11
- export * from './server/shared/staticServer.js';
12
- export * from './server/testHelpers/cwd.js';
13
- export * from './server/userDeps/fixtureWatcher.js';
14
- export * from './server/userDeps/generateUserDepsModule.js';
15
- export * from './server/utils/cli.js';
16
- export * from './server/utils/fs.js';
17
- export * from './server/utils/requireSilent.js';
18
- export * from './server/utils/resolve.js';
19
- export * from './server/utils/resolveLoose.js';
20
- export * from './server/utils/resolveSilent.js';
package/server.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './dist/server.js';
package/server.js DELETED
@@ -1 +0,0 @@
1
- export * from './dist/server.js';