react-cosmos 6.0.0-alpha.0 → 6.0.0-alpha.11

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 (130) 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 -0
  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 +15 -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}/pluginConfigs.js +1 -1
  21. package/dist/{server/cosmosPlugin → cosmosPlugin}/readCosmosPluginConfig.js +4 -7
  22. package/dist/{server/cosmosPlugin → cosmosPlugin}/types.d.ts +11 -0
  23. package/dist/{server/devServer → devServer}/messageHandler.d.ts +1 -1
  24. package/dist/{server/devServer → devServer}/messageHandler.js +2 -2
  25. package/dist/{server/devServer → devServer}/startDevServer.js +26 -24
  26. package/dist/{server/export → export}/generateExport.js +23 -12
  27. package/dist/{server/getFixtures → getFixtures}/getFixtures.js +3 -3
  28. package/dist/index.d.ts +21 -0
  29. package/dist/index.js +21 -1
  30. package/dist/jest.d.ts +7 -7
  31. package/dist/jest.js +7 -7
  32. package/dist/shared/findNextAvailablePort.d.ts +1 -0
  33. package/dist/shared/findNextAvailablePort.js +29 -0
  34. package/dist/shared/importServerPlugins.d.ts +3 -0
  35. package/dist/shared/importServerPlugins.js +16 -0
  36. package/dist/{server/shared → shared}/playgroundHtml.js +3 -3
  37. package/dist/shared/resolveRendererUrl.d.ts +1 -0
  38. package/dist/shared/resolveRendererUrl.js +7 -0
  39. package/dist/{server/testHelpers → testHelpers}/mockConsole.js +8 -4
  40. package/dist/{server/testHelpers → testHelpers}/mockFs.d.ts +2 -2
  41. package/dist/{server/testHelpers → testHelpers}/mockFs.js +26 -8
  42. package/dist/{server/userDeps → userDeps}/findUserModulePaths.d.ts +2 -1
  43. package/dist/{server/userDeps → userDeps}/findUserModulePaths.js +3 -3
  44. package/dist/{server/userDeps → userDeps}/fixtureWatcher.js +3 -3
  45. package/dist/{server/userDeps → userDeps}/generateUserDepsModule.js +2 -1
  46. package/dist/{server/userDeps → userDeps}/getUserModules.d.ts +1 -1
  47. package/dist/{server/userDeps → userDeps}/getUserModules.js +2 -1
  48. package/dist/{server/userDeps → userDeps}/shared.d.ts +0 -1
  49. package/dist/{server/userDeps → userDeps}/shared.js +0 -4
  50. package/dist/{server/utils → utils}/fs.d.ts +2 -1
  51. package/dist/{server/utils → utils}/fs.js +8 -5
  52. package/package.json +5 -5
  53. package/dist/server/devServer/corePlugins/httpProxy.d.ts +0 -2
  54. package/dist/server/devServer/corePlugins/httpProxy.js +0 -18
  55. package/dist/server/devServer/corePlugins/openFile.d.ts +0 -2
  56. package/dist/server/devServer/corePlugins/openFile.js +0 -53
  57. package/dist/server/devServer/corePlugins/pluginEndpoint.d.ts +0 -2
  58. package/dist/server/devServer/corePlugins/pluginEndpoint.js +0 -17
  59. package/dist/server/devServer/corePlugins/userDepsFile.d.ts +0 -2
  60. package/dist/server/shared/requirePluginModule.d.ts +0 -2
  61. package/dist/server/shared/requirePluginModule.js +0 -10
  62. package/dist/server/shared/resolveUrlPath.d.ts +0 -1
  63. package/dist/server/shared/resolveUrlPath.js +0 -12
  64. package/dist/server.d.ts +0 -20
  65. package/dist/server.js +0 -20
  66. package/server.d.ts +0 -1
  67. package/server.js +0 -1
  68. /package/dist/{server/cosmosConfig → cosmosConfig}/createCosmosConfig.d.ts +0 -0
  69. /package/dist/{server/cosmosConfig → cosmosConfig}/detectCosmosConfig.d.ts +0 -0
  70. /package/dist/{server/cosmosConfig → cosmosConfig}/detectCosmosConfig.js +0 -0
  71. /package/dist/{server/cosmosConfig → cosmosConfig}/getCosmosConfigAtPath.d.ts +0 -0
  72. /package/dist/{server/cosmosConfig → cosmosConfig}/getCosmosConfigAtPath.js +0 -0
  73. /package/dist/{server/cosmosConfig → cosmosConfig}/shared.d.ts +0 -0
  74. /package/dist/{server/cosmosConfig → cosmosConfig}/types.js +0 -0
  75. /package/dist/{server/cosmosPlugin → cosmosPlugin}/findCosmosPluginConfigs.d.ts +0 -0
  76. /package/dist/{server/cosmosPlugin → cosmosPlugin}/findCosmosPluginConfigs.js +0 -0
  77. /package/dist/{server/cosmosPlugin → cosmosPlugin}/pluginConfigs.d.ts +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/shared → shared}/logPluginInfo.d.ts +0 -0
  88. /package/dist/{server/shared → shared}/logPluginInfo.js +0 -0
  89. /package/dist/{server/shared → shared}/playgroundHtml.d.ts +0 -0
  90. /package/dist/{server/shared → shared}/playgroundUrl.d.ts +0 -0
  91. /package/dist/{server/shared → shared}/playgroundUrl.js +0 -0
  92. /package/dist/{server/shared → shared}/staticPath.d.ts +0 -0
  93. /package/dist/{server/shared → shared}/staticPath.js +0 -0
  94. /package/dist/{server/shared → shared}/staticServer.d.ts +0 -0
  95. /package/dist/{server/shared → shared}/staticServer.js +0 -0
  96. /package/dist/{server/static → static}/favicon.ico +0 -0
  97. /package/dist/{server/static → static}/index.html +0 -0
  98. /package/dist/{server/testHelpers → testHelpers}/cwd.d.ts +0 -0
  99. /package/dist/{server/testHelpers → testHelpers}/cwd.js +0 -0
  100. /package/dist/{server/testHelpers → testHelpers}/mockConsole.d.ts +0 -0
  101. /package/dist/{server/testHelpers → testHelpers}/mockEsmRequire.d.ts +0 -0
  102. /package/dist/{server/testHelpers → testHelpers}/mockEsmRequire.js +0 -0
  103. /package/dist/{server/testHelpers → testHelpers}/mockEsmResolve.d.ts +0 -0
  104. /package/dist/{server/testHelpers → testHelpers}/mockEsmResolve.js +0 -0
  105. /package/dist/{server/testHelpers → testHelpers}/mockEsmStaticPath.d.ts +0 -0
  106. /package/dist/{server/testHelpers → testHelpers}/mockEsmStaticPath.js +0 -0
  107. /package/dist/{server/testHelpers → testHelpers}/mockFetch.d.ts +0 -0
  108. /package/dist/{server/testHelpers → testHelpers}/mockFetch.js +0 -0
  109. /package/dist/{server/testHelpers → testHelpers}/mockYargs.d.ts +0 -0
  110. /package/dist/{server/testHelpers → testHelpers}/mockYargs.js +0 -0
  111. /package/dist/{server/userDeps → userDeps}/fixtureWatcher.d.ts +0 -0
  112. /package/dist/{server/userDeps → userDeps}/generateUserDepsModule.d.ts +0 -0
  113. /package/dist/{server/userDeps → userDeps}/userDepsTemplate.d.ts +0 -0
  114. /package/dist/{server/userDeps → userDeps}/userDepsTemplate.js +0 -0
  115. /package/dist/{server/utils → utils}/cli.d.ts +0 -0
  116. /package/dist/{server/utils → utils}/cli.js +0 -0
  117. /package/dist/{server/utils → utils}/isNodeError.d.ts +0 -0
  118. /package/dist/{server/utils → utils}/isNodeError.js +0 -0
  119. /package/dist/{server/utils → utils}/requireModule.d.ts +0 -0
  120. /package/dist/{server/utils → utils}/requireModule.js +0 -0
  121. /package/dist/{server/utils → utils}/requireSilent.d.ts +0 -0
  122. /package/dist/{server/utils → utils}/requireSilent.js +0 -0
  123. /package/dist/{server/utils → utils}/resolve.d.ts +0 -0
  124. /package/dist/{server/utils → utils}/resolve.js +0 -0
  125. /package/dist/{server/utils → utils}/resolveLoose.d.ts +0 -0
  126. /package/dist/{server/utils → utils}/resolveLoose.js +0 -0
  127. /package/dist/{server/utils → utils}/resolveSilent.d.ts +0 -0
  128. /package/dist/{server/utils → utils}/resolveSilent.js +0 -0
  129. /package/dist/{server/utils → utils}/slash.d.ts +0 -0
  130. /package/dist/{server/utils → utils}/slash.js +0 -0
package/dist/index.js CHANGED
@@ -1 +1,21 @@
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 { RENDERER_FILENAME } from './shared/playgroundHtml.js';
11
+ export * from './shared/playgroundUrl.js';
12
+ export * from './shared/staticServer.js';
13
+ export * from './testHelpers/cwd.js';
14
+ export * from './userDeps/fixtureWatcher.js';
15
+ export * from './userDeps/generateUserDepsModule.js';
16
+ export * from './utils/cli.js';
17
+ export * from './utils/fs.js';
18
+ export * from './utils/requireSilent.js';
19
+ export * from './utils/resolve.js';
20
+ export * from './utils/resolveLoose.js';
21
+ export * from './utils/resolveSilent.js';
package/dist/jest.d.ts 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';
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
+ }
@@ -3,7 +3,7 @@ import { readFile } from 'fs/promises';
3
3
  import path from 'path';
4
4
  import { pkgUpSync } from 'pkg-up';
5
5
  import { replaceKeys } from 'react-cosmos-core';
6
- import { resolveUrlPath } from './resolveUrlPath.js';
6
+ import { resolveRendererUrl } from './resolveRendererUrl.js';
7
7
  import { getStaticPath } from './staticPath.js';
8
8
  export const RENDERER_FILENAME = '_renderer.html';
9
9
  export async function getDevPlaygroundHtml(platformType, cosmosConfig, pluginConfigs) {
@@ -39,7 +39,7 @@ async function getDevCoreConfig(platformType, cosmosConfig) {
39
39
  ...(await getSharedCoreConfig(cosmosConfig)),
40
40
  devServerOn: true,
41
41
  webRendererUrl: cosmosConfig.rendererUrl ||
42
- resolveUrlPath(cosmosConfig.publicUrl, RENDERER_FILENAME),
42
+ resolveRendererUrl(cosmosConfig.publicUrl, RENDERER_FILENAME),
43
43
  };
44
44
  default:
45
45
  throw new Error(`Invalid platform type: ${platformType}`);
@@ -49,7 +49,7 @@ async function getExportCoreConfig(cosmosConfig) {
49
49
  return {
50
50
  ...(await getSharedCoreConfig(cosmosConfig)),
51
51
  devServerOn: false,
52
- webRendererUrl: resolveUrlPath(cosmosConfig.publicUrl, RENDERER_FILENAME),
52
+ webRendererUrl: resolveRendererUrl(cosmosConfig.publicUrl, RENDERER_FILENAME),
53
53
  };
54
54
  }
55
55
  async function getSharedCoreConfig(cosmosConfig) {
@@ -0,0 +1 @@
1
+ export declare function resolveRendererUrl(publicUrl: string, filename: string): string;
@@ -0,0 +1,7 @@
1
+ export function resolveRendererUrl(publicUrl, filename) {
2
+ if (publicUrl === './')
3
+ return filename;
4
+ if (publicUrl.endsWith('/'))
5
+ return publicUrl + filename;
6
+ return `${publicUrl}/${filename}`;
7
+ }
@@ -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();
@@ -2,10 +2,11 @@ type FindUserModulePathsArgs = {
2
2
  rootDir: string;
3
3
  fixturesDir: string;
4
4
  fixtureFileSuffix: string;
5
+ ignore: string[];
5
6
  };
6
7
  type UserModulePaths = {
7
8
  fixturePaths: string[];
8
9
  decoratorPaths: string[];
9
10
  };
10
- export declare function findUserModulePaths({ rootDir, fixturesDir, fixtureFileSuffix, }: FindUserModulePathsArgs): UserModulePaths;
11
+ export declare function findUserModulePaths({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }: FindUserModulePathsArgs): UserModulePaths;
11
12
  export {};
@@ -1,11 +1,11 @@
1
1
  import glob from 'glob';
2
2
  import micromatch from 'micromatch';
3
- import { getDecoratorPatterns, getFixturePatterns, getIgnorePatterns, } from './shared.js';
4
- export function findUserModulePaths({ rootDir, fixturesDir, fixtureFileSuffix, }) {
3
+ import { getDecoratorPatterns, getFixturePatterns } from './shared.js';
4
+ export function findUserModulePaths({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }) {
5
5
  const paths = glob.sync('**/*', {
6
6
  cwd: rootDir,
7
7
  absolute: true,
8
- ignore: getIgnorePatterns(),
8
+ ignore,
9
9
  });
10
10
  const fixturePatterns = getFixturePatterns(fixturesDir, fixtureFileSuffix);
11
11
  const fixturePaths = getMatchingPaths(paths, fixturePatterns);
@@ -1,16 +1,16 @@
1
1
  import { watch } from 'chokidar';
2
2
  import { debounce } from 'lodash-es';
3
- import { getDecoratorPatterns, getFixturePatterns, getIgnorePatterns, } from './shared.js';
3
+ import { getDecoratorPatterns, getFixturePatterns } from './shared.js';
4
4
  const DEBOUNCE_INTERVAL = 50;
5
5
  export async function startFixtureWatcher(cosmosConfig, event, callback) {
6
- const { fixturesDir, fixtureFileSuffix } = cosmosConfig;
6
+ const { fixturesDir, fixtureFileSuffix, ignore } = cosmosConfig;
7
7
  const FILE_PATTERNS = [
8
8
  ...getFixturePatterns(fixturesDir, fixtureFileSuffix),
9
9
  ...getDecoratorPatterns(),
10
10
  ];
11
11
  return new Promise(resolve => {
12
12
  const watcher = watch(FILE_PATTERNS, {
13
- ignored: getIgnorePatterns(),
13
+ ignored: ignore,
14
14
  ignoreInitial: true,
15
15
  cwd: cosmosConfig.rootDir,
16
16
  })
@@ -1,11 +1,12 @@
1
1
  import { findUserModulePaths } from './findUserModulePaths.js';
2
2
  import { userDepsTemplate } from './userDepsTemplate.js';
3
3
  export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeToDir, }) {
4
- const { rootDir, fixturesDir, fixtureFileSuffix, globalImports } = cosmosConfig;
4
+ const { rootDir, fixturesDir, fixtureFileSuffix, globalImports, ignore } = cosmosConfig;
5
5
  const { fixturePaths, decoratorPaths } = findUserModulePaths({
6
6
  rootDir,
7
7
  fixturesDir,
8
8
  fixtureFileSuffix,
9
+ ignore,
9
10
  });
10
11
  return userDepsTemplate({
11
12
  globalImports,
@@ -4,5 +4,5 @@ type UserModules = {
4
4
  fixtures: ReactFixtureExports;
5
5
  decorators: ReactDecorators;
6
6
  };
7
- export declare function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, }: CosmosConfig): UserModules;
7
+ export declare function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }: CosmosConfig): UserModules;
8
8
  export {};
@@ -1,11 +1,12 @@
1
1
  import path from 'path';
2
2
  import { slash } from '../utils/slash.js';
3
3
  import { findUserModulePaths } from './findUserModulePaths.js';
4
- export function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, }) {
4
+ export function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }) {
5
5
  const { fixturePaths, decoratorPaths } = findUserModulePaths({
6
6
  rootDir,
7
7
  fixturesDir,
8
8
  fixtureFileSuffix,
9
+ ignore,
9
10
  });
10
11
  return {
11
12
  fixtures: getDefaultExports(fixturePaths, rootDir),
@@ -3,4 +3,3 @@ export type Json = string | number | boolean | null | {
3
3
  } | Json[];
4
4
  export declare function getFixturePatterns(fixturesDir: string, fixtureFileSuffix: string): string[];
5
5
  export declare function getDecoratorPatterns(): string[];
6
- export declare function getIgnorePatterns(): string[];
@@ -5,7 +5,6 @@ const FIXTURE_PATTERNS = [
5
5
  '**/*.<fixtureFileSuffix>.{js,jsx,ts,tsx}',
6
6
  ];
7
7
  const DECORATOR_PATTERNS = ['**/cosmos.decorator.{js,jsx,ts,tsx}'];
8
- const IGNORE_PATTERNS = ['**/node_modules/**'];
9
8
  export function getFixturePatterns(fixturesDir, fixtureFileSuffix) {
10
9
  return FIXTURE_PATTERNS.map(pattern => replaceKeys(pattern, {
11
10
  '<fixturesDir>': fixturesDir,
@@ -15,6 +14,3 @@ export function getFixturePatterns(fixturesDir, fixtureFileSuffix) {
15
14
  export function getDecoratorPatterns() {
16
15
  return DECORATOR_PATTERNS;
17
16
  }
18
- export function getIgnorePatterns() {
19
- return IGNORE_PATTERNS;
20
- }
@@ -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.0",
3
+ "version": "6.0.0-alpha.11",
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,15 +17,15 @@
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.0",
21
- "react-cosmos-ui": "^6.0.0-alpha.0",
20
+ "react-cosmos-core": "^6.0.0-alpha.11",
21
+ "react-cosmos-ui": "^6.0.0-alpha.11",
22
22
  "ws": "^8.12.1",
23
- "yargs": "^17.7.0"
23
+ "yargs": "^17.7.1"
24
24
  },
25
25
  "bin": {
26
26
  "cosmos": "./bin/cosmos.js",
27
27
  "cosmos-export": "./bin/cosmos-export.js",
28
28
  "cosmos-native": "./bin/cosmos-native.js"
29
29
  },
30
- "gitHead": "b021231584639f3028fe08deacd418f0ec4cc002"
30
+ "gitHead": "8e9d1e52749388f471f6ec848a8c904ec8b89f67"
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 +0,0 @@
1
- export declare function resolveUrlPath(base: string, urlPath: string): string;
@@ -1,12 +0,0 @@
1
- // resolveUrlPath('/', '_renderer.html') => '/_renderer.html'
2
- // resolveUrlPath('./', '_renderer.html') => '/_renderer.html'
3
- // resolveUrlPath('http://localhost:5000', '_renderer.html') => 'http://localhost:5000/_renderer.html'
4
- export function resolveUrlPath(base, urlPath) {
5
- // The 'resolve://' protocol is only added when base is a relative URL.
6
- const resolvedUrl = new URL(urlPath, new URL(base, 'resolve://'));
7
- if (resolvedUrl.protocol === 'resolve:') {
8
- const { pathname, search, hash } = resolvedUrl;
9
- return pathname + search + hash;
10
- }
11
- return resolvedUrl.toString();
12
- }
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';