react-cosmos 5.7.2 → 6.0.0-alpha.5

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 (246) hide show
  1. package/bin/cosmos-export.js +4 -3
  2. package/bin/cosmos-native.js +4 -3
  3. package/bin/cosmos.js +5 -4
  4. package/config.schema.json +14 -4
  5. package/dist/index.d.ts +0 -6
  6. package/dist/index.js +0 -70
  7. package/dist/jest.d.ts +7 -0
  8. package/dist/jest.js +7 -0
  9. package/dist/{config → server/cosmosConfig}/createCosmosConfig.d.ts +1 -1
  10. package/dist/server/cosmosConfig/createCosmosConfig.js +84 -0
  11. package/dist/server/cosmosConfig/detectCosmosConfig.d.ts +2 -0
  12. package/dist/server/cosmosConfig/detectCosmosConfig.js +38 -0
  13. package/dist/{config → server/cosmosConfig}/getCosmosConfigAtPath.d.ts +1 -1
  14. package/dist/server/cosmosConfig/getCosmosConfigAtPath.js +12 -0
  15. package/dist/server/cosmosConfig/shared.d.ts +2 -0
  16. package/dist/server/cosmosConfig/shared.js +7 -0
  17. package/dist/{config/shared.d.ts → server/cosmosConfig/types.d.ts} +5 -5
  18. package/dist/server/cosmosPlugin/findCosmosPluginConfigs.d.ts +8 -0
  19. package/dist/server/cosmosPlugin/findCosmosPluginConfigs.js +14 -0
  20. package/dist/server/cosmosPlugin/pluginConfigs.d.ts +8 -0
  21. package/dist/server/cosmosPlugin/pluginConfigs.js +16 -0
  22. package/dist/server/cosmosPlugin/readCosmosPluginConfig.d.ts +8 -0
  23. package/dist/server/cosmosPlugin/readCosmosPluginConfig.js +31 -0
  24. package/dist/server/cosmosPlugin/types.d.ts +21 -0
  25. package/dist/server/devServer/corePlugins/httpProxy.d.ts +2 -0
  26. package/dist/server/devServer/corePlugins/httpProxy.js +18 -0
  27. package/dist/server/devServer/corePlugins/openFile.d.ts +2 -0
  28. package/dist/server/devServer/corePlugins/openFile.js +53 -0
  29. package/dist/server/devServer/corePlugins/pluginEndpoint.d.ts +2 -0
  30. package/dist/server/devServer/corePlugins/pluginEndpoint.js +17 -0
  31. package/dist/server/devServer/corePlugins/userDepsFile.d.ts +2 -0
  32. package/dist/server/devServer/corePlugins/userDepsFile.js +37 -0
  33. package/dist/server/devServer/expressApp.d.ts +5 -0
  34. package/dist/server/devServer/expressApp.js +21 -0
  35. package/dist/server/devServer/httpServer.d.ts +12 -0
  36. package/dist/server/devServer/httpServer.js +42 -0
  37. package/dist/{devServer → server/devServer}/messageHandler.d.ts +2 -2
  38. package/dist/server/devServer/messageHandler.js +29 -0
  39. package/dist/server/devServer/startDevServer.d.ts +2 -0
  40. package/dist/server/devServer/startDevServer.js +91 -0
  41. package/dist/server/export/generateExport.d.ts +1 -0
  42. package/dist/server/export/generateExport.js +97 -0
  43. package/dist/server/getFixtures/getFixtures.d.ts +14 -0
  44. package/dist/server/getFixtures/getFixtures.js +63 -0
  45. package/dist/server/shared/logPluginInfo.d.ts +2 -0
  46. package/dist/server/shared/logPluginInfo.js +8 -0
  47. package/dist/server/shared/playgroundHtml.d.ts +6 -0
  48. package/dist/server/shared/playgroundHtml.js +85 -0
  49. package/dist/server/shared/playgroundUrl.d.ts +2 -0
  50. package/dist/server/shared/playgroundUrl.js +6 -0
  51. package/dist/server/shared/requirePluginModule.d.ts +2 -0
  52. package/dist/server/shared/requirePluginModule.js +10 -0
  53. package/dist/server/shared/resolveUrlPath.d.ts +1 -0
  54. package/dist/server/shared/resolveUrlPath.js +12 -0
  55. package/dist/server/shared/staticPath.d.ts +1 -0
  56. package/dist/server/shared/staticPath.js +4 -0
  57. package/dist/{shared/static.d.ts → server/shared/staticServer.d.ts} +0 -1
  58. package/dist/server/shared/staticServer.js +12 -0
  59. package/dist/{shared → server}/static/index.html +1 -1
  60. package/dist/server/testHelpers/cwd.d.ts +1 -0
  61. package/dist/server/testHelpers/cwd.js +4 -0
  62. package/dist/server/testHelpers/mockConsole.d.ts +5 -0
  63. package/dist/server/testHelpers/mockConsole.js +11 -0
  64. package/dist/server/testHelpers/mockEsmRequire.js +13 -0
  65. package/dist/server/testHelpers/mockEsmResolve.d.ts +1 -0
  66. package/dist/server/testHelpers/mockEsmResolve.js +13 -0
  67. package/dist/server/testHelpers/mockEsmStaticPath.d.ts +1 -0
  68. package/dist/server/testHelpers/mockEsmStaticPath.js +7 -0
  69. package/dist/server/testHelpers/mockFetch.d.ts +2 -0
  70. package/dist/server/testHelpers/mockFetch.js +7 -0
  71. package/dist/server/testHelpers/mockFs.d.ts +5 -0
  72. package/dist/server/testHelpers/mockFs.js +64 -0
  73. package/dist/server/testHelpers/mockYargs.d.ts +2 -0
  74. package/dist/server/testHelpers/mockYargs.js +21 -0
  75. package/dist/{userDeps → server/userDeps}/findUserModulePaths.d.ts +2 -2
  76. package/dist/server/userDeps/findUserModulePaths.js +20 -0
  77. package/dist/server/userDeps/fixtureWatcher.d.ts +3 -0
  78. package/dist/server/userDeps/fixtureWatcher.js +20 -0
  79. package/dist/{userDeps → server/userDeps}/generateUserDepsModule.d.ts +3 -3
  80. package/dist/server/userDeps/generateUserDepsModule.js +18 -0
  81. package/dist/{userDeps → server/userDeps}/getUserModules.d.ts +3 -3
  82. package/dist/server/userDeps/getUserModules.js +25 -0
  83. package/dist/{userDeps → server/userDeps}/shared.d.ts +1 -1
  84. package/dist/server/userDeps/shared.js +20 -0
  85. package/dist/{userDeps → server/userDeps}/userDepsTemplate.d.ts +2 -2
  86. package/dist/server/userDeps/userDepsTemplate.js +49 -0
  87. package/dist/server/utils/cli.js +4 -0
  88. package/dist/server/utils/fs.d.ts +4 -0
  89. package/dist/server/utils/fs.js +24 -0
  90. package/dist/server/utils/isNodeError.d.ts +2 -0
  91. package/dist/server/utils/isNodeError.js +3 -0
  92. package/dist/server/utils/requireModule.d.ts +2 -0
  93. package/dist/server/utils/requireModule.js +13 -0
  94. package/dist/server/utils/requireSilent.d.ts +1 -0
  95. package/dist/server/utils/requireSilent.js +11 -0
  96. package/dist/server/utils/resolve.d.ts +2 -0
  97. package/dist/server/utils/resolve.js +14 -0
  98. package/dist/server/utils/resolveLoose.d.ts +1 -0
  99. package/dist/server/utils/resolveLoose.js +12 -0
  100. package/dist/server/utils/resolveSilent.d.ts +2 -0
  101. package/dist/server/utils/resolveSilent.js +22 -0
  102. package/dist/server/utils/slash.d.ts +1 -0
  103. package/dist/server/utils/slash.js +5 -0
  104. package/dist/server.d.ts +20 -0
  105. package/dist/server.js +20 -0
  106. package/index.d.ts +1 -1
  107. package/index.js +1 -2
  108. package/jest.d.ts +1 -0
  109. package/jest.js +1 -0
  110. package/package.json +17 -28
  111. package/server.d.ts +1 -0
  112. package/server.js +1 -0
  113. package/dist/config/createCosmosConfig.js +0 -133
  114. package/dist/config/detectCosmosConfig.d.ts +0 -2
  115. package/dist/config/detectCosmosConfig.js +0 -57
  116. package/dist/config/getCosmosConfigAtPath.js +0 -28
  117. package/dist/config/resolveModule.d.ts +0 -1
  118. package/dist/config/resolveModule.js +0 -20
  119. package/dist/config/shared.js +0 -17
  120. package/dist/devServer/app.d.ts +0 -5
  121. package/dist/devServer/app.js +0 -51
  122. package/dist/devServer/httpServer.d.ts +0 -11
  123. package/dist/devServer/httpServer.js +0 -183
  124. package/dist/devServer/messageHandler.js +0 -42
  125. package/dist/devServer/startDevServer.d.ts +0 -18
  126. package/dist/devServer/startDevServer.js +0 -209
  127. package/dist/dom/DomFixtureLoader.d.ts +0 -9
  128. package/dist/dom/DomFixtureLoader.js +0 -38
  129. package/dist/dom/ErrorCatch.d.ts +0 -15
  130. package/dist/dom/ErrorCatch.js +0 -117
  131. package/dist/dom/container.d.ts +0 -1
  132. package/dist/dom/container.js +0 -32
  133. package/dist/dom/globalErrorHandler.js +0 -14
  134. package/dist/dom/index.d.ts +0 -13
  135. package/dist/dom/index.js +0 -47
  136. package/dist/dom/renderMessage.d.ts +0 -4
  137. package/dist/dom/renderMessage.js +0 -30
  138. package/dist/dom/rendererConnect.d.ts +0 -1
  139. package/dist/dom/rendererConnect.js +0 -18
  140. package/dist/dom/rendererId.d.ts +0 -1
  141. package/dist/dom/rendererId.js +0 -26
  142. package/dist/dom/selectedFixtureId.d.ts +0 -2
  143. package/dist/dom/selectedFixtureId.js +0 -41
  144. package/dist/dom/shared.d.ts +0 -1
  145. package/dist/dom/shared.js +0 -14
  146. package/dist/export/generateExport.d.ts +0 -6
  147. package/dist/export/generateExport.js +0 -137
  148. package/dist/fixture.d.ts +0 -1
  149. package/dist/fixture.js +0 -31
  150. package/dist/generateWebExport.d.ts +0 -1
  151. package/dist/generateWebExport.js +0 -37
  152. package/dist/getFixtureUrls.d.ts +0 -8
  153. package/dist/getFixtureUrls.js +0 -97
  154. package/dist/getFixtures.d.ts +0 -13
  155. package/dist/getFixtures.js +0 -89
  156. package/dist/getFixtures2.d.ts +0 -14
  157. package/dist/getFixtures2.js +0 -117
  158. package/dist/native/NativeFixtureLoader.d.ts +0 -12
  159. package/dist/native/NativeFixtureLoader.js +0 -67
  160. package/dist/native/index.d.ts +0 -1
  161. package/dist/native/index.js +0 -25
  162. package/dist/plugins/httpProxy.d.ts +0 -2
  163. package/dist/plugins/httpProxy.js +0 -31
  164. package/dist/plugins/openFile.d.ts +0 -2
  165. package/dist/plugins/openFile.js +0 -87
  166. package/dist/plugins/userDepsFile.d.ts +0 -2
  167. package/dist/plugins/userDepsFile.js +0 -166
  168. package/dist/plugins/webpack/client/errorOverlay/index.d.ts +0 -1
  169. package/dist/plugins/webpack/client/errorOverlay/index.js +0 -16
  170. package/dist/plugins/webpack/client/errorOverlay/reactErrorOverlay.d.ts +0 -2
  171. package/dist/plugins/webpack/client/errorOverlay/reactErrorOverlay.js +0 -63
  172. package/dist/plugins/webpack/client/hmrErrorHandler.js +0 -16
  173. package/dist/plugins/webpack/client/index.d.ts +0 -6
  174. package/dist/plugins/webpack/client/index.js +0 -43
  175. package/dist/plugins/webpack/client/reactDevtoolsHook.d.ts +0 -0
  176. package/dist/plugins/webpack/client/reactDevtoolsHook.js +0 -19
  177. package/dist/plugins/webpack/client/userDeps.d.ts +0 -5
  178. package/dist/plugins/webpack/client/userDeps.js +0 -15
  179. package/dist/plugins/webpack/cosmosConfig/dom.d.ts +0 -6
  180. package/dist/plugins/webpack/cosmosConfig/dom.js +0 -19
  181. package/dist/plugins/webpack/cosmosConfig/webpack.d.ts +0 -10
  182. package/dist/plugins/webpack/cosmosConfig/webpack.js +0 -95
  183. package/dist/plugins/webpack/devServer.d.ts +0 -2
  184. package/dist/plugins/webpack/devServer.js +0 -168
  185. package/dist/plugins/webpack/export.d.ts +0 -2
  186. package/dist/plugins/webpack/export.js +0 -139
  187. package/dist/plugins/webpack/index.d.ts +0 -2
  188. package/dist/plugins/webpack/index.js +0 -21
  189. package/dist/plugins/webpack/shared.d.ts +0 -2
  190. package/dist/plugins/webpack/shared.js +0 -22
  191. package/dist/plugins/webpack/webpackConfig/getDefaultWebpackConfig.d.ts +0 -2
  192. package/dist/plugins/webpack/webpackConfig/getDefaultWebpackConfig.js +0 -152
  193. package/dist/plugins/webpack/webpackConfig/getDevWebpackConfig.d.ts +0 -3
  194. package/dist/plugins/webpack/webpackConfig/getDevWebpackConfig.js +0 -146
  195. package/dist/plugins/webpack/webpackConfig/getExportWebpackConfig.d.ts +0 -3
  196. package/dist/plugins/webpack/webpackConfig/getExportWebpackConfig.js +0 -111
  197. package/dist/plugins/webpack/webpackConfig/getUserWebpackConfig.d.ts +0 -3
  198. package/dist/plugins/webpack/webpackConfig/getUserWebpackConfig.js +0 -136
  199. package/dist/plugins/webpack/webpackConfig/getWebpackConfigModule.d.ts +0 -2
  200. package/dist/plugins/webpack/webpackConfig/getWebpackConfigModule.js +0 -45
  201. package/dist/plugins/webpack/webpackConfig/getWebpackConfigResolve.d.ts +0 -4
  202. package/dist/plugins/webpack/webpackConfig/getWebpackConfigResolve.js +0 -107
  203. package/dist/plugins/webpack/webpackConfig/getWebpackNodeEnv.d.ts +0 -1
  204. package/dist/plugins/webpack/webpackConfig/getWebpackNodeEnv.js +0 -12
  205. package/dist/plugins/webpack/webpackConfig/htmlPlugin.d.ts +0 -16
  206. package/dist/plugins/webpack/webpackConfig/htmlPlugin.js +0 -80
  207. package/dist/plugins/webpack/webpackConfig/plugins.d.ts +0 -5
  208. package/dist/plugins/webpack/webpackConfig/plugins.js +0 -37
  209. package/dist/plugins/webpack/webpackConfig/resolveWebpackClientPath.d.ts +0 -1
  210. package/dist/plugins/webpack/webpackConfig/resolveWebpackClientPath.js +0 -10
  211. package/dist/plugins/webpack/webpackConfig/userDepsLoader.js +0 -36
  212. package/dist/shared/cli.js +0 -14
  213. package/dist/shared/fs.d.ts +0 -4
  214. package/dist/shared/fs.js +0 -37
  215. package/dist/shared/playgroundHtml.d.ts +0 -6
  216. package/dist/shared/playgroundHtml.js +0 -116
  217. package/dist/shared/pluginConfigs.d.ts +0 -2
  218. package/dist/shared/pluginConfigs.js +0 -15
  219. package/dist/shared/rendererConfig.d.ts +0 -6
  220. package/dist/shared/rendererConfig.js +0 -5
  221. package/dist/shared/static.js +0 -30
  222. package/dist/shared/types.d.ts +0 -1
  223. package/dist/shared/types.js +0 -5
  224. package/dist/shared/utils.d.ts +0 -5
  225. package/dist/shared/utils.js +0 -22
  226. package/dist/startNativeServer.d.ts +0 -1
  227. package/dist/startNativeServer.js +0 -39
  228. package/dist/startWebServer.d.ts +0 -1
  229. package/dist/startWebServer.js +0 -43
  230. package/dist/userDeps/findUserModulePaths.js +0 -45
  231. package/dist/userDeps/generateUserDepsModule.js +0 -37
  232. package/dist/userDeps/getUserModules.js +0 -45
  233. package/dist/userDeps/shared.js +0 -32
  234. package/dist/userDeps/userDepsTemplate.js +0 -52
  235. package/dom.d.ts +0 -1
  236. package/dom.js +0 -2
  237. package/fixture.d.ts +0 -1
  238. package/fixture.js +0 -2
  239. package/native.d.ts +0 -1
  240. package/native.js +0 -2
  241. package/yarn-error.log +0 -13450
  242. /package/dist/{dom/globalErrorHandler.d.ts → server/cosmosConfig/types.js} +0 -0
  243. /package/dist/{plugins/webpack/client/hmrErrorHandler.d.ts → server/cosmosPlugin/types.js} +0 -0
  244. /package/dist/{shared → server}/static/favicon.ico +0 -0
  245. /package/dist/{plugins/webpack/webpackConfig/userDepsLoader.d.ts → server/testHelpers/mockEsmRequire.d.ts} +0 -0
  246. /package/dist/{shared → server/utils}/cli.d.ts +0 -0
@@ -0,0 +1,11 @@
1
+ export async function mockConsole(cb) {
2
+ const origConsoleLog = console.log;
3
+ console.log = jest.fn();
4
+ const expectedLogs = [];
5
+ const ret = await cb({
6
+ expectLog: (msg) => expectedLogs.push(msg),
7
+ });
8
+ expectedLogs.forEach(msg => expect(console.log).toBeCalledWith(msg));
9
+ console.log = origConsoleLog;
10
+ return ret;
11
+ }
@@ -0,0 +1,13 @@
1
+ jest.mock('../utils/requireModule.js', () => {
2
+ function requireModule(moduleId) {
3
+ return require(moduleId);
4
+ }
5
+ function requireFrom(fromDirectory, moduleId) {
6
+ return require(require.resolve(moduleId, { paths: [fromDirectory] }));
7
+ }
8
+ return {
9
+ requireModule,
10
+ requireFrom,
11
+ };
12
+ });
13
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ jest.mock('../utils/resolve.js', () => {
2
+ function resolve(moduleId) {
3
+ return require.resolve(moduleId);
4
+ }
5
+ function resolveFrom(fromDirectory, moduleId) {
6
+ return require.resolve(moduleId, { paths: [fromDirectory] });
7
+ }
8
+ return {
9
+ resolve,
10
+ resolveFrom,
11
+ };
12
+ });
13
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ jest.mock('../shared/staticPath.js', () => {
2
+ function getStaticPath(relPath) {
3
+ return require.resolve(`../static/${relPath}`);
4
+ }
5
+ return { getStaticPath };
6
+ });
7
+ export {};
@@ -0,0 +1,2 @@
1
+ /// <reference types="jest" />
2
+ export declare function mockFetch(httpStatus: number, cb: (fetchMock: jest.Mock) => Promise<unknown>): Promise<void>;
@@ -0,0 +1,7 @@
1
+ export async function mockFetch(httpStatus, cb) {
2
+ const w = window;
3
+ const origFetch = window.fetch;
4
+ w.fetch = jest.fn(async () => ({ status: httpStatus }));
5
+ await cb(w.fetch);
6
+ w.fetch = origFetch;
7
+ }
@@ -0,0 +1,5 @@
1
+ import { CosmosConfig } from '../cosmosConfig/types.js';
2
+ 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;
5
+ export declare function resetFsMock(): void;
@@ -0,0 +1,64 @@
1
+ import path from 'path';
2
+ import { getCwdPath } from './cwd.js';
3
+ jest.mock('../utils/fs', () => {
4
+ const actual = jest.requireActual('../utils/fs');
5
+ let mocked = false;
6
+ let fileMocks = {};
7
+ let dirMocks = [];
8
+ async function importModule(moduleId) {
9
+ if (!mocked)
10
+ return actual.importModule(moduleId);
11
+ return fileMocks[moduleId] || fileMocks[`${moduleId}.js`];
12
+ }
13
+ function moduleExists(moduleId) {
14
+ if (!mocked)
15
+ return actual.moduleExists(moduleId);
16
+ return (fileMocks.hasOwnProperty(moduleId) ||
17
+ fileMocks.hasOwnProperty(`${moduleId}.js`));
18
+ }
19
+ function fileExists(filePath) {
20
+ if (!mocked)
21
+ return actual.fileExists(filePath);
22
+ return fileMocks.hasOwnProperty(filePath);
23
+ }
24
+ function dirExists(dirPath) {
25
+ if (!mocked)
26
+ return actual.dirExists(dirPath);
27
+ return dirMocks.indexOf(dirPath) !== -1;
28
+ }
29
+ return {
30
+ importModule,
31
+ moduleExists,
32
+ fileExists,
33
+ dirExists,
34
+ __mockFile(filePath, fileMock) {
35
+ mocked = true;
36
+ fileMocks = { ...fileMocks, [filePath]: fileMock };
37
+ },
38
+ __mockDir(dirPath) {
39
+ mocked = true;
40
+ dirMocks = [...dirMocks, dirPath];
41
+ },
42
+ __resetMock() {
43
+ fileMocks = {};
44
+ dirMocks = [];
45
+ },
46
+ };
47
+ });
48
+ export function mockCosmosConfig(cosmosConfigPath, cosmosConfig) {
49
+ mockFile(cosmosConfigPath, cosmosConfig);
50
+ }
51
+ export function mockFile(filePath, fileContent) {
52
+ 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));
58
+ }
59
+ export function resetFsMock() {
60
+ requireMocked().__resetMock();
61
+ }
62
+ function requireMocked() {
63
+ return require('../utils/fs.js');
64
+ }
@@ -0,0 +1,2 @@
1
+ export declare function mockCliArgs(cliArgs: {}): void;
2
+ export declare function unmockCliArgs(): void;
@@ -0,0 +1,21 @@
1
+ // Secondary role of mocking yargs: Prevent Cosmos from intercepting the
2
+ // --config arg passed to Jest
3
+ jest.mock('yargs/yargs', () => {
4
+ let argv = {};
5
+ const yargs = () => ({
6
+ parseSync: () => argv,
7
+ });
8
+ yargs.__mockArgsv = (newArgv) => {
9
+ argv = newArgv;
10
+ };
11
+ return yargs;
12
+ });
13
+ export function mockCliArgs(cliArgs) {
14
+ requireMocked().__mockArgsv(cliArgs);
15
+ }
16
+ export function unmockCliArgs() {
17
+ requireMocked().__mockArgsv({});
18
+ }
19
+ function requireMocked() {
20
+ return require('yargs/yargs');
21
+ }
@@ -1,9 +1,9 @@
1
- declare type FindUserModulePathsArgs = {
1
+ type FindUserModulePathsArgs = {
2
2
  rootDir: string;
3
3
  fixturesDir: string;
4
4
  fixtureFileSuffix: string;
5
5
  };
6
- declare type UserModulePaths = {
6
+ type UserModulePaths = {
7
7
  fixturePaths: string[];
8
8
  decoratorPaths: string[];
9
9
  };
@@ -0,0 +1,20 @@
1
+ import glob from 'glob';
2
+ import micromatch from 'micromatch';
3
+ import { getDecoratorPatterns, getFixturePatterns, getIgnorePatterns, } from './shared.js';
4
+ export function findUserModulePaths({ rootDir, fixturesDir, fixtureFileSuffix, }) {
5
+ const paths = glob.sync('**/*', {
6
+ cwd: rootDir,
7
+ absolute: true,
8
+ ignore: getIgnorePatterns(),
9
+ });
10
+ const fixturePatterns = getFixturePatterns(fixturesDir, fixtureFileSuffix);
11
+ const fixturePaths = getMatchingPaths(paths, fixturePatterns);
12
+ const decoratorPaths = getMatchingPaths(paths, getDecoratorPatterns());
13
+ // Omit decorators from fixture paths, which happens when decorators are
14
+ // placed inside fixture dirs.
15
+ const nonDecoratorFixturePaths = fixturePaths.filter(fixturePath => decoratorPaths.indexOf(fixturePath) === -1);
16
+ return { fixturePaths: nonDecoratorFixturePaths, decoratorPaths };
17
+ }
18
+ function getMatchingPaths(paths, patterns) {
19
+ return micromatch(paths, patterns, { dot: true });
20
+ }
@@ -0,0 +1,3 @@
1
+ import { FSWatcher } from 'chokidar';
2
+ import { CosmosConfig } from '../cosmosConfig/types.js';
3
+ export declare function startFixtureWatcher(cosmosConfig: CosmosConfig, event: 'all' | 'add', callback: () => unknown): Promise<FSWatcher>;
@@ -0,0 +1,20 @@
1
+ import { watch } from 'chokidar';
2
+ import { debounce } from 'lodash-es';
3
+ import { getDecoratorPatterns, getFixturePatterns, getIgnorePatterns, } from './shared.js';
4
+ const DEBOUNCE_INTERVAL = 50;
5
+ export async function startFixtureWatcher(cosmosConfig, event, callback) {
6
+ const { fixturesDir, fixtureFileSuffix } = cosmosConfig;
7
+ const FILE_PATTERNS = [
8
+ ...getFixturePatterns(fixturesDir, fixtureFileSuffix),
9
+ ...getDecoratorPatterns(),
10
+ ];
11
+ return new Promise(resolve => {
12
+ const watcher = watch(FILE_PATTERNS, {
13
+ ignored: getIgnorePatterns(),
14
+ ignoreInitial: true,
15
+ cwd: cosmosConfig.rootDir,
16
+ })
17
+ .on('ready', () => resolve(watcher))
18
+ .on(event, debounce(() => callback(), DEBOUNCE_INTERVAL));
19
+ });
20
+ }
@@ -1,6 +1,6 @@
1
- import { CosmosConfig } from '../config/shared';
2
- import { Json } from './shared';
3
- declare type Args = {
1
+ import { CosmosConfig } from '../cosmosConfig/types.js';
2
+ import { Json } from './shared.js';
3
+ type Args = {
4
4
  cosmosConfig: CosmosConfig;
5
5
  rendererConfig: Json;
6
6
  relativeToDir: string | null;
@@ -0,0 +1,18 @@
1
+ import { findUserModulePaths } from './findUserModulePaths.js';
2
+ import { userDepsTemplate } from './userDepsTemplate.js';
3
+ export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeToDir, }) {
4
+ const { rootDir, fixturesDir, fixtureFileSuffix, globalImports } = cosmosConfig;
5
+ const { fixturePaths, decoratorPaths } = findUserModulePaths({
6
+ rootDir,
7
+ fixturesDir,
8
+ fixtureFileSuffix,
9
+ });
10
+ return userDepsTemplate({
11
+ globalImports,
12
+ fixturePaths,
13
+ decoratorPaths,
14
+ rendererConfig,
15
+ rootDir,
16
+ relativeToDir,
17
+ });
18
+ }
@@ -1,6 +1,6 @@
1
- import { ReactDecorators, ReactFixtureExports } from 'react-cosmos-shared2/react';
2
- import { CosmosConfig } from '../config/shared';
3
- declare type UserModules = {
1
+ import { ReactDecorators, ReactFixtureExports } from 'react-cosmos-core';
2
+ import { CosmosConfig } from '../cosmosConfig/types.js';
3
+ type UserModules = {
4
4
  fixtures: ReactFixtureExports;
5
5
  decorators: ReactDecorators;
6
6
  };
@@ -0,0 +1,25 @@
1
+ import path from 'path';
2
+ import { slash } from '../utils/slash.js';
3
+ import { findUserModulePaths } from './findUserModulePaths.js';
4
+ export function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, }) {
5
+ const { fixturePaths, decoratorPaths } = findUserModulePaths({
6
+ rootDir,
7
+ fixturesDir,
8
+ fixtureFileSuffix,
9
+ });
10
+ return {
11
+ fixtures: getDefaultExports(fixturePaths, rootDir),
12
+ decorators: getDefaultExports(decoratorPaths, rootDir),
13
+ };
14
+ }
15
+ function getDefaultExports(paths, rootDir) {
16
+ const exportsByPath = {};
17
+ paths.forEach(p => {
18
+ // Converting to forward slashes on Windows is important because the
19
+ // slashes are used for generating a sorted list of fixtures and
20
+ // decorators.
21
+ const relPath = slash(path.relative(rootDir, p));
22
+ exportsByPath[relPath] = require(p).default;
23
+ });
24
+ return exportsByPath;
25
+ }
@@ -1,4 +1,4 @@
1
- export declare type Json = string | number | boolean | null | {
1
+ export type Json = string | number | boolean | null | {
2
2
  [property: string]: Json;
3
3
  } | Json[];
4
4
  export declare function getFixturePatterns(fixturesDir: string, fixtureFileSuffix: string): string[];
@@ -0,0 +1,20 @@
1
+ import { replaceKeys } from 'react-cosmos-core';
2
+ // NODE: These can be made configurable if a proper need arises
3
+ const FIXTURE_PATTERNS = [
4
+ '**/<fixturesDir>/**/*.{js,jsx,ts,tsx}',
5
+ '**/*.<fixtureFileSuffix>.{js,jsx,ts,tsx}',
6
+ ];
7
+ const DECORATOR_PATTERNS = ['**/cosmos.decorator.{js,jsx,ts,tsx}'];
8
+ const IGNORE_PATTERNS = ['**/node_modules/**'];
9
+ export function getFixturePatterns(fixturesDir, fixtureFileSuffix) {
10
+ return FIXTURE_PATTERNS.map(pattern => replaceKeys(pattern, {
11
+ '<fixturesDir>': fixturesDir,
12
+ '<fixtureFileSuffix>': fixtureFileSuffix,
13
+ }));
14
+ }
15
+ export function getDecoratorPatterns() {
16
+ return DECORATOR_PATTERNS;
17
+ }
18
+ export function getIgnorePatterns() {
19
+ return IGNORE_PATTERNS;
20
+ }
@@ -1,5 +1,5 @@
1
- import { Json } from './shared';
2
- declare type Args = {
1
+ import { Json } from './shared.js';
2
+ type Args = {
3
3
  globalImports: string[];
4
4
  fixturePaths: string[];
5
5
  decoratorPaths: string[];
@@ -0,0 +1,49 @@
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
+ }
@@ -0,0 +1,4 @@
1
+ import yargs from 'yargs/yargs';
2
+ export function getCliArgs() {
3
+ return yargs(process.argv.slice(2)).parseSync();
4
+ }
@@ -0,0 +1,4 @@
1
+ export declare function importModule<T>(moduleId: string): Promise<T>;
2
+ export declare function moduleExists(moduleId: string): boolean | "";
3
+ export declare function fileExists(filePath: string): boolean;
4
+ export declare function dirExists(dirPath: string): boolean;
@@ -0,0 +1,24 @@
1
+ // NOTE: This API has been extracted to easily mock the file system in tests
2
+ import fs from 'fs';
3
+ import { requireModule } from './requireModule.js';
4
+ import { resolve } from './resolve.js';
5
+ export async function importModule(moduleId) {
6
+ return moduleId.endsWith('.json')
7
+ ? requireModule(moduleId)
8
+ : import(moduleId);
9
+ }
10
+ // Better than fs.exists because it works for module paths without an extension
11
+ export function moduleExists(moduleId) {
12
+ try {
13
+ return resolve(moduleId) && true;
14
+ }
15
+ catch (err) {
16
+ return false;
17
+ }
18
+ }
19
+ export function fileExists(filePath) {
20
+ return fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
21
+ }
22
+ export function dirExists(dirPath) {
23
+ return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
24
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference types="node" />
2
+ export declare function isNodeError(err: any): err is NodeJS.ErrnoException;
@@ -0,0 +1,3 @@
1
+ export function isNodeError(err) {
2
+ return err && err.stack && err.message;
3
+ }
@@ -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,13 @@
1
+ import { createRequire } from 'node:module';
2
+ import path from 'path';
3
+ // NOTE: This is ESM code that can't run in Jest yet, which means that this
4
+ // module always needs to be mocked in tests until Jest adds ESM support.
5
+ export function requireModule(moduleId) {
6
+ const require = createRequire(import.meta.url);
7
+ return require(moduleId);
8
+ }
9
+ export function requireFrom(fromDirectory, moduleId) {
10
+ // Inspired by https://github.com/sindresorhus/import-from
11
+ const require = createRequire(path.resolve(fromDirectory, 'noop.js'));
12
+ return require(moduleId);
13
+ }
@@ -0,0 +1 @@
1
+ export declare function requireFromSilent(fromDirectory: string, moduleId: string): any;
@@ -0,0 +1,11 @@
1
+ import { isNodeError } from './isNodeError.js';
2
+ import { requireFrom } from './requireModule.js';
3
+ export function requireFromSilent(fromDirectory, moduleId) {
4
+ try {
5
+ return requireFrom(fromDirectory, moduleId);
6
+ }
7
+ catch (err) {
8
+ if (!isNodeError(err) || err.code !== 'MODULE_NOT_FOUND')
9
+ console.log(err);
10
+ }
11
+ }
@@ -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,14 @@
1
+ import { createRequire } from 'node:module';
2
+ // NOTE: This is ESM code that can't run in Jest yet, which means that this
3
+ // module always needs to be mocked in tests until Jest adds ESM support.
4
+ // One day this code will be replaced by import.meta.resolve
5
+ // https://nodejs.org/api/esm.html#importmetaresolvespecifier-parent
6
+ // Still under --experimental-import-meta-resolve flag in Node v19
7
+ export function resolve(moduleId) {
8
+ const require = createRequire(import.meta.url);
9
+ return require.resolve(moduleId);
10
+ }
11
+ export function resolveFrom(fromDirectory, moduleId) {
12
+ const require = createRequire(import.meta.url);
13
+ return require.resolve(moduleId, { paths: [fromDirectory] });
14
+ }
@@ -0,0 +1 @@
1
+ export declare function resolveLoose(fromDirectory: string, moduleId: string): string;
@@ -0,0 +1,12 @@
1
+ import path from 'path';
2
+ import { resolveFromSilent } from './resolveSilent.js';
3
+ export function resolveLoose(fromDirectory, moduleId) {
4
+ // Use to deal with file paths and module names interchangeably.
5
+ return path.isAbsolute(moduleId)
6
+ ? moduleId
7
+ : resolveFromSilent(fromDirectory, moduleId) ||
8
+ // Final attempt: Resolve relative paths that don't either
9
+ // 1. Don't start with ./
10
+ // 2. Don't point to an existing file
11
+ path.join(fromDirectory, moduleId);
12
+ }
@@ -0,0 +1,2 @@
1
+ export declare function resolveSilent(moduleId: string): string | null;
2
+ export declare function resolveFromSilent(fromDirectory: string, moduleId: string): string | null;
@@ -0,0 +1,22 @@
1
+ import { isNodeError } from './isNodeError.js';
2
+ import { resolve, resolveFrom } from './resolve.js';
3
+ export function resolveSilent(moduleId) {
4
+ try {
5
+ return resolve(moduleId);
6
+ }
7
+ catch (err) {
8
+ if (!isNodeError(err) || err.code !== 'MODULE_NOT_FOUND')
9
+ console.log(err);
10
+ return null;
11
+ }
12
+ }
13
+ export function resolveFromSilent(fromDirectory, moduleId) {
14
+ try {
15
+ return resolveFrom(fromDirectory, moduleId);
16
+ }
17
+ catch (err) {
18
+ if (!isNodeError(err) || err.code !== 'MODULE_NOT_FOUND')
19
+ console.log(err);
20
+ return null;
21
+ }
22
+ }
@@ -0,0 +1 @@
1
+ export declare function slash(path: string): string;
@@ -0,0 +1,5 @@
1
+ // Copied from https://github.com/sindresorhus/slash/blob/205c404ecf35b7a7a8a687d8d3b79101aaaa9fba/index.js
2
+ export function slash(path) {
3
+ const isExtendedLengthPath = /^\\\\\?\\/.test(path);
4
+ return isExtendedLengthPath ? path : path.replace(/\\/g, '/');
5
+ }
@@ -0,0 +1,20 @@
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 ADDED
@@ -0,0 +1,20 @@
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/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './dist/index';
1
+ export * from './dist/index.js';
package/index.js CHANGED
@@ -1,2 +1 @@
1
- require('regenerator-runtime/runtime');
2
- module.exports = require('./dist/index');
1
+ export * from './dist/index.js';
package/jest.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/jest.js';
package/jest.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/jest.js');