react-cosmos 6.0.0-alpha.16 → 6.0.0-alpha.17

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 (37) hide show
  1. package/config.schema.json +3 -4
  2. package/dist/corePlugins/exposeImports.d.ts +2 -0
  3. package/dist/corePlugins/exposeImports.js +51 -0
  4. package/dist/corePlugins/index.js +2 -2
  5. package/dist/cosmosConfig/createCosmosConfig.js +16 -7
  6. package/dist/cosmosConfig/types.d.ts +1 -1
  7. package/dist/getFixtures/getFixtures.js +2 -2
  8. package/dist/index.d.ts +2 -2
  9. package/dist/index.js +2 -2
  10. package/dist/shared/playgroundHtml.js +3 -3
  11. package/dist/userModules/generateUserImports.d.ts +10 -0
  12. package/dist/{userDeps/generateUserDepsModule.js → userModules/generateUserImports.js} +7 -4
  13. package/dist/{userDeps/getUserModules.d.ts → userModules/importUserModules.d.ts} +1 -1
  14. package/dist/{userDeps/getUserModules.js → userModules/importUserModules.js} +1 -1
  15. package/dist/userModules/shared.d.ts +15 -0
  16. package/dist/{userDeps → userModules}/shared.js +16 -0
  17. package/dist/userModules/userImportsLazyTemplate.d.ts +2 -0
  18. package/dist/userModules/userImportsLazyTemplate.js +54 -0
  19. package/dist/userModules/userImportsTemplate.d.ts +2 -0
  20. package/dist/userModules/userImportsTemplate.js +56 -0
  21. package/dist/utils/json.d.ts +3 -0
  22. package/dist/utils/json.js +1 -0
  23. package/package.json +4 -4
  24. package/dist/corePlugins/userDepsFile.d.ts +0 -2
  25. package/dist/corePlugins/userDepsFile.js +0 -40
  26. package/dist/userDeps/generateUserDepsModule.d.ts +0 -9
  27. package/dist/userDeps/shared.d.ts +0 -5
  28. package/dist/userDeps/userDepsLazyTemplate.d.ts +0 -2
  29. package/dist/userDeps/userDepsLazyTemplate.js +0 -37
  30. package/dist/userDeps/userDepsShared.d.ts +0 -12
  31. package/dist/userDeps/userDepsShared.js +0 -16
  32. package/dist/userDeps/userDepsTemplate.d.ts +0 -2
  33. package/dist/userDeps/userDepsTemplate.js +0 -45
  34. /package/dist/{userDeps → userModules}/findUserModulePaths.d.ts +0 -0
  35. /package/dist/{userDeps → userModules}/findUserModulePaths.js +0 -0
  36. /package/dist/{userDeps → userModules}/fixtureWatcher.d.ts +0 -0
  37. /package/dist/{userDeps → userModules}/fixtureWatcher.js +0 -0
@@ -69,10 +69,9 @@
69
69
  "description": "Dynamically import fixture and decorator modules as they are loaded. When false all fixture and decorator modules are imported statically and bundled together. [default: false]",
70
70
  "type": "boolean"
71
71
  },
72
- "userDepsFilePath": {
73
- "description": "Where to generate a file that contains a map to all fixtures and other Cosmos-related user modules. Only used in setups where Cosmos can't piggyback on the user's build (eg. React Native setups). [default: \"cosmos.userdeps.js\"]",
74
- "type": "string",
75
- "minLength": 1
72
+ "exposeImports": {
73
+ "description": "Expose user imports and config required for the Cosmos renderer. Used with React Native and in custom integrations. When a path is specified it requires a file extension (eg. \"src/cosmos.imports.ts\"). [default: false]",
74
+ "anyOf": [{ "type": "string", "minLength": 1 }, { "type": "boolean" }]
76
75
  },
77
76
  "hostname": {
78
77
  "description": "Dev server hostname. Set to null to accept connections with any hostname. [default: null]",
@@ -0,0 +1,2 @@
1
+ import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
2
+ export declare const exposeImportsServerPlugin: CosmosServerPlugin;
@@ -0,0 +1,51 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { getPlaygroundUrl } from '../shared/playgroundUrl.js';
4
+ import { startFixtureWatcher } from '../userModules/fixtureWatcher.js';
5
+ import { generateUserImports } from '../userModules/generateUserImports.js';
6
+ import { moduleExists } from '../utils/fs.js';
7
+ export const exposeImportsServerPlugin = {
8
+ name: 'exposeImports',
9
+ async devServer({ cosmosConfig, platformType }) {
10
+ if (!shouldExposeImports(platformType, cosmosConfig))
11
+ return;
12
+ await generateImportsFile(cosmosConfig);
13
+ const watcher = await startFixtureWatcher(cosmosConfig, 'all', () => {
14
+ generateImportsFile(cosmosConfig);
15
+ });
16
+ return () => {
17
+ watcher.close();
18
+ };
19
+ },
20
+ };
21
+ function shouldExposeImports(platformType, cosmosConfig) {
22
+ return platformType === 'native' || Boolean(cosmosConfig.exposeImports);
23
+ }
24
+ async function generateImportsFile(cosmosConfig) {
25
+ const { exposeImports } = cosmosConfig;
26
+ const filePath = typeof exposeImports === 'string'
27
+ ? exposeImports
28
+ : getDefaultFilePath(cosmosConfig.rootDir);
29
+ const typeScript = /\.tsx?$/.test(filePath);
30
+ const rendererConfig = {
31
+ playgroundUrl: getPlaygroundUrl(cosmosConfig),
32
+ };
33
+ const fileSource = generateUserImports({
34
+ cosmosConfig,
35
+ rendererConfig,
36
+ relativeToDir: path.dirname(filePath),
37
+ typeScript,
38
+ });
39
+ await fs.writeFile(filePath, fileSource, 'utf8');
40
+ const relModulesPath = path.relative(process.cwd(), filePath);
41
+ if (typeScript && typeof exposeImports === 'boolean') {
42
+ console.log(`[Cosmos] Generated ${relModulesPath} (TypeScript detected)`);
43
+ }
44
+ else {
45
+ console.log(`[Cosmos] Generated ${relModulesPath}`);
46
+ }
47
+ }
48
+ function getDefaultFilePath(rootDir) {
49
+ const ext = moduleExists('typescript') ? 'ts' : 'js';
50
+ return path.resolve(rootDir, `cosmos.imports.${ext}`);
51
+ }
@@ -1,11 +1,11 @@
1
+ import { exposeImportsServerPlugin } from './exposeImports.js';
1
2
  import { httpProxyServerPlugin } from './httpProxy.js';
2
3
  import { openFileServerPlugin } from './openFile.js';
3
4
  import { pluginEndpointServerPlugin } from './pluginEndpoint.js';
4
5
  import { portRetryServerPlugin } from './portRetry.js';
5
- import { userDepsFileServerPlugin } from './userDepsFile.js';
6
6
  export const coreServerPlugins = [
7
7
  portRetryServerPlugin,
8
- userDepsFileServerPlugin,
8
+ exposeImportsServerPlugin,
9
9
  httpProxyServerPlugin,
10
10
  openFileServerPlugin,
11
11
  pluginEndpointServerPlugin,
@@ -5,9 +5,10 @@ export function createCosmosConfig(rootDir, cosmosConfigInput = {}) {
5
5
  return {
6
6
  ...cosmosConfigInput,
7
7
  rootDir,
8
- exportPath: getExportPath(cosmosConfigInput, rootDir),
9
8
  detectLocalPlugins: cosmosConfigInput.detectLocalPlugins ?? true,
10
9
  disablePlugins: cosmosConfigInput.disablePlugins ?? false,
10
+ exposeImports: getExposeImports(cosmosConfigInput, rootDir),
11
+ exportPath: getExportPath(cosmosConfigInput, rootDir),
11
12
  fixtureFileSuffix: getFixtureFileSuffix(cosmosConfigInput),
12
13
  fixturesDir: getFixturesDir(cosmosConfigInput),
13
14
  globalImports: getGlobalImports(cosmosConfigInput, rootDir),
@@ -16,14 +17,13 @@ export function createCosmosConfig(rootDir, cosmosConfigInput = {}) {
16
17
  httpsOptions: getHttpsOptions(cosmosConfigInput, rootDir),
17
18
  ignore: getIgnore(cosmosConfigInput),
18
19
  lazy: getLazy(cosmosConfigInput),
20
+ plugins: getPlugins(cosmosConfigInput, rootDir),
19
21
  port: getPort(cosmosConfigInput),
20
22
  portRetries: getPortRetries(cosmosConfigInput),
21
- plugins: getPlugins(cosmosConfigInput, rootDir),
22
23
  publicUrl: getPublicUrl(cosmosConfigInput),
24
+ rendererUrl: cosmosConfigInput.rendererUrl ?? null,
23
25
  staticPath: getStaticPath(cosmosConfigInput, rootDir),
24
- userDepsFilePath: getUserDepsFilePath(cosmosConfigInput, rootDir),
25
26
  watchDirs: getWatchDirs(cosmosConfigInput, rootDir),
26
- rendererUrl: cosmosConfigInput.rendererUrl ?? null,
27
27
  dom: getDomConfig(cosmosConfigInput.dom || {}),
28
28
  ui: cosmosConfigInput.ui || {},
29
29
  };
@@ -66,9 +66,18 @@ function getWatchDirs(cosmosConfigInput, rootDir) {
66
66
  const { watchDirs = ['.'] } = cosmosConfigInput;
67
67
  return watchDirs.map(watchDir => path.resolve(rootDir, watchDir));
68
68
  }
69
- function getUserDepsFilePath(cosmosConfigInput, rootDir) {
70
- const { userDepsFilePath = 'cosmos.userdeps.js' } = cosmosConfigInput;
71
- return path.resolve(rootDir, userDepsFilePath);
69
+ function getExposeImports(cosmosConfigInput, rootDir) {
70
+ const cliArgs = getCliArgs();
71
+ if (typeof cliArgs.exposeImports === 'boolean') {
72
+ return cliArgs.exposeImports;
73
+ }
74
+ else if (typeof cliArgs.exposeImports === 'string') {
75
+ return path.resolve(rootDir, cliArgs.exposeImports);
76
+ }
77
+ const { exposeImports = false } = cosmosConfigInput;
78
+ return typeof exposeImports === 'string'
79
+ ? path.resolve(rootDir, exposeImports)
80
+ : exposeImports;
72
81
  }
73
82
  function getHostname({ hostname = null }) {
74
83
  return hostname;
@@ -10,6 +10,7 @@ export type CosmosConfig = {
10
10
  detectLocalPlugins: boolean;
11
11
  disablePlugins: boolean;
12
12
  dom: CosmosDomConfig;
13
+ exposeImports: boolean | string;
13
14
  fixtureFileSuffix: string;
14
15
  fixturesDir: string;
15
16
  globalImports: string[];
@@ -25,7 +26,6 @@ export type CosmosConfig = {
25
26
  rendererUrl: string | null;
26
27
  rootDir: string;
27
28
  staticPath: null | string;
28
- userDepsFilePath: string;
29
29
  watchDirs: string[];
30
30
  [option: string]: unknown;
31
31
  ui: {
@@ -2,11 +2,11 @@ import path from 'path';
2
2
  import { createFixtureTree, flattenFixtureTree, getDecoratedFixtureElement, getFixtureListFromExports, getSortedDecoratorsForFixturePath, stringifyPlaygroundUrlQuery, stringifyRendererUrlQuery, } from 'react-cosmos-core';
3
3
  import { RENDERER_FILENAME } from '../shared/playgroundHtml.js';
4
4
  import { resolveRendererUrl } from '../shared/resolveRendererUrl.js';
5
- import { getUserModules } from '../userDeps/getUserModules.js';
5
+ import { importUserModules } from '../userModules/importUserModules.js';
6
6
  export function getFixtures(cosmosConfig) {
7
7
  const { fixturesDir, fixtureFileSuffix, rootDir } = cosmosConfig;
8
8
  const fixtureInfo = [];
9
- const { fixtures, decorators } = getUserModules(cosmosConfig);
9
+ const { fixtures, decorators } = importUserModules(cosmosConfig);
10
10
  const fixtureList = getFixtureListFromExports(fixtures);
11
11
  const fixtureTree = createFixtureTree({
12
12
  fixtures: fixtureList,
package/dist/index.d.ts CHANGED
@@ -11,8 +11,8 @@ export { RENDERER_FILENAME } from './shared/playgroundHtml.js';
11
11
  export * from './shared/playgroundUrl.js';
12
12
  export * from './shared/staticServer.js';
13
13
  export * from './testHelpers/cwd.js';
14
- export * from './userDeps/fixtureWatcher.js';
15
- export * from './userDeps/generateUserDepsModule.js';
14
+ export * from './userModules/fixtureWatcher.js';
15
+ export * from './userModules/generateUserImports.js';
16
16
  export * from './utils/cli.js';
17
17
  export * from './utils/fs.js';
18
18
  export * from './utils/requireSilent.js';
package/dist/index.js CHANGED
@@ -11,8 +11,8 @@ export { RENDERER_FILENAME } from './shared/playgroundHtml.js';
11
11
  export * from './shared/playgroundUrl.js';
12
12
  export * from './shared/staticServer.js';
13
13
  export * from './testHelpers/cwd.js';
14
- export * from './userDeps/fixtureWatcher.js';
15
- export * from './userDeps/generateUserDepsModule.js';
14
+ export * from './userModules/fixtureWatcher.js';
15
+ export * from './userModules/generateUserImports.js';
16
16
  export * from './utils/cli.js';
17
17
  export * from './utils/fs.js';
18
18
  export * from './utils/requireSilent.js';
@@ -3,8 +3,8 @@ 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 { findUserModulePaths } from '../userDeps/findUserModulePaths.js';
7
- import { userDepsKeyPath } from '../userDeps/userDepsShared.js';
6
+ import { findUserModulePaths } from '../userModules/findUserModulePaths.js';
7
+ import { importKeyPath } from '../userModules/shared.js';
8
8
  import { resolveRendererUrl } from './resolveRendererUrl.js';
9
9
  import { getStaticPath } from './staticPath.js';
10
10
  export const RENDERER_FILENAME = '_renderer.html';
@@ -91,6 +91,6 @@ function getFixtureList(cosmosConfig) {
91
91
  const { fixturePaths } = findUserModulePaths(cosmosConfig);
92
92
  return fixturePaths.reduce((acc, fixturePath) => ({
93
93
  ...acc,
94
- [userDepsKeyPath(fixturePath, cosmosConfig.rootDir)]: { type: 'single' },
94
+ [importKeyPath(fixturePath, cosmosConfig.rootDir)]: { type: 'single' },
95
95
  }), {});
96
96
  }
@@ -0,0 +1,10 @@
1
+ import { CosmosConfig } from '../cosmosConfig/types.js';
2
+ import { Json } from '../utils/json.js';
3
+ type Args = {
4
+ cosmosConfig: CosmosConfig;
5
+ rendererConfig: Json;
6
+ relativeToDir: string | null;
7
+ typeScript: boolean;
8
+ };
9
+ export declare function generateUserImports({ cosmosConfig, rendererConfig, relativeToDir, typeScript, }: Args): string;
10
+ export {};
@@ -1,7 +1,7 @@
1
1
  import { findUserModulePaths } from './findUserModulePaths.js';
2
- import { userDepsLazyTemplate } from './userDepsLazyTemplate.js';
3
- import { userDepsTemplate } from './userDepsTemplate.js';
4
- export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeToDir, }) {
2
+ import { userImportsLazyTemplate } from './userImportsLazyTemplate.js';
3
+ import { userImportsTemplate } from './userImportsTemplate.js';
4
+ export function generateUserImports({ cosmosConfig, rendererConfig, relativeToDir, typeScript, }) {
5
5
  const { rootDir, fixturesDir, fixtureFileSuffix, globalImports, ignore } = cosmosConfig;
6
6
  const { fixturePaths, decoratorPaths } = findUserModulePaths({
7
7
  rootDir,
@@ -9,7 +9,9 @@ export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeT
9
9
  fixtureFileSuffix,
10
10
  ignore,
11
11
  });
12
- const template = cosmosConfig.lazy ? userDepsLazyTemplate : userDepsTemplate;
12
+ const template = cosmosConfig.lazy
13
+ ? userImportsLazyTemplate
14
+ : userImportsTemplate;
13
15
  return template({
14
16
  globalImports,
15
17
  fixturePaths,
@@ -17,5 +19,6 @@ export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeT
17
19
  rendererConfig,
18
20
  rootDir,
19
21
  relativeToDir,
22
+ typeScript,
20
23
  });
21
24
  }
@@ -4,5 +4,5 @@ type UserModules = {
4
4
  fixtures: ByPath<ReactFixtureExport>;
5
5
  decorators: ByPath<ReactDecorator>;
6
6
  };
7
- export declare function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }: CosmosConfig): UserModules;
7
+ export declare function importUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }: CosmosConfig): UserModules;
8
8
  export {};
@@ -1,7 +1,7 @@
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, ignore, }) {
4
+ export function importUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }) {
5
5
  const { fixturePaths, decoratorPaths } = findUserModulePaths({
6
6
  rootDir,
7
7
  fixturesDir,
@@ -0,0 +1,15 @@
1
+ import { Json } from '../utils/json.js';
2
+ export type UserImportsTemplateArgs = {
3
+ globalImports: string[];
4
+ fixturePaths: string[];
5
+ decoratorPaths: string[];
6
+ rendererConfig: Json;
7
+ rootDir: string;
8
+ relativeToDir: string | null;
9
+ typeScript: boolean;
10
+ };
11
+ export declare function getFixturePatterns(fixturesDir: string, fixtureFileSuffix: string): string[];
12
+ export declare function getDecoratorPatterns(): string[];
13
+ export declare function createImportMap(paths: string[], rootDir: string, relativeToDir: string | null): Record<string, string>;
14
+ export declare function importKeyPath(filePath: string, rootDir: string): string;
15
+ export declare function importPath(filePath: string, relativeToDir: string | null): string;
@@ -1,4 +1,6 @@
1
+ import path from 'path';
1
2
  import { replaceKeys } from 'react-cosmos-core';
3
+ import { slash } from '../utils/slash.js';
2
4
  // NODE: These can be made configurable if a proper need arises
3
5
  const FIXTURE_PATTERNS = [
4
6
  '**/<fixturesDir>/**/*.{js,jsx,ts,tsx}',
@@ -14,3 +16,17 @@ export function getFixturePatterns(fixturesDir, fixtureFileSuffix) {
14
16
  export function getDecoratorPatterns() {
15
17
  return DECORATOR_PATTERNS;
16
18
  }
19
+ export function createImportMap(paths, rootDir, relativeToDir) {
20
+ return paths.reduce((acc, p) => ({
21
+ ...acc,
22
+ [importKeyPath(p, rootDir)]: importPath(p, relativeToDir),
23
+ }), {});
24
+ }
25
+ export function importKeyPath(filePath, rootDir) {
26
+ return slash(path.relative(rootDir, filePath));
27
+ }
28
+ export function importPath(filePath, relativeToDir) {
29
+ return slash(relativeToDir
30
+ ? `.${path.sep}${path.relative(relativeToDir, filePath)}`
31
+ : filePath);
32
+ }
@@ -0,0 +1,2 @@
1
+ import { UserImportsTemplateArgs } from './shared.js';
2
+ export declare function userImportsLazyTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, typeScript, }: UserImportsTemplateArgs): string;
@@ -0,0 +1,54 @@
1
+ import { flatten } from 'lodash-es';
2
+ import { createImportMap, importPath, } from './shared.js';
3
+ export function userImportsLazyTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, typeScript, }) {
4
+ function ext(filePath) {
5
+ // Add compat with projects where allowImportingTsExtensions isn't enabled
6
+ return typeScript ? filePath.replace(/\.tsx?$/, '') : filePath;
7
+ }
8
+ function ts(annotation) {
9
+ return typeScript ? annotation : '';
10
+ }
11
+ const globalImportItems = globalImports.map(p => `import '${ext(importPath(p, relativeToDir))}';`);
12
+ const fixtures = createImportMap(fixturePaths, rootDir, relativeToDir);
13
+ const fixtureKeys = Object.keys(fixtures);
14
+ const fixtureItems = fixtureKeys.map(k => ` '${k}': { getModule: () => import('${ext(fixtures[k])}') }`);
15
+ const decorators = createImportMap(decoratorPaths, rootDir, relativeToDir);
16
+ const decoratorKeys = Object.keys(decorators);
17
+ const decoratorItems = decoratorKeys.map(k => ` '${k}': { getModule: () => import('${ext(decorators[k])}') }`);
18
+ const rendererConfigStr = JSON.stringify(rendererConfig, null, 2);
19
+ let importGroups = [globalImportItems];
20
+ if (typeScript) {
21
+ importGroups.unshift([
22
+ `import { RendererConfig, UserModuleWrappers } from 'react-cosmos-core';`,
23
+ ]);
24
+ }
25
+ return `
26
+ // This file is automatically generated by Cosmos. Add it to .gitignore and
27
+ // only edit if you know what you're doing.
28
+ ${importsStr(importGroups)}
29
+ export const rendererConfig${ts(': RendererConfig')} = ${rendererConfigStr};
30
+
31
+ const fixtures = {${objBody(fixtureItems)}};
32
+
33
+ const decorators = {${objBody(decoratorItems)}};
34
+
35
+ export const moduleWrappers${ts(': UserModuleWrappers')} = {
36
+ lazy: true,
37
+ fixtures,
38
+ decorators
39
+ };
40
+ `.trimStart();
41
+ }
42
+ function importsStr(items) {
43
+ if (flatten(items).length === 0) {
44
+ return '';
45
+ }
46
+ const str = items
47
+ .filter(rows => rows.length > 0)
48
+ .map(rows => rows.join('\n'))
49
+ .join('\n\n');
50
+ return `\n${str}\n`;
51
+ }
52
+ function objBody(items) {
53
+ return items.length > 0 ? `\n${items.join(`,\n`)}\n` : '';
54
+ }
@@ -0,0 +1,2 @@
1
+ import { UserImportsTemplateArgs } from './shared.js';
2
+ export declare function userImportsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, typeScript, }: UserImportsTemplateArgs): string;
@@ -0,0 +1,56 @@
1
+ import { flatten } from 'lodash-es';
2
+ import { createImportMap, importPath, } from './shared.js';
3
+ export function userImportsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, typeScript, }) {
4
+ function ext(filePath) {
5
+ // Add compat with projects where allowImportingTsExtensions isn't enabled
6
+ return typeScript ? filePath.replace(/\.tsx?$/, '') : filePath;
7
+ }
8
+ function ts(annotation) {
9
+ return typeScript ? annotation : '';
10
+ }
11
+ const globalImportItems = globalImports.map(p => `import '${ext(importPath(p, relativeToDir))}';`);
12
+ const fixtures = createImportMap(fixturePaths, rootDir, relativeToDir);
13
+ const fixtureKeys = Object.keys(fixtures);
14
+ const fixtureImports = fixtureKeys.map((k, i) => `import * as fixture${i} from '${ext(fixtures[k])}';`);
15
+ const fixtureItems = fixtureKeys.map((k, i) => ` '${k}': { module: fixture${i} }`);
16
+ const decorators = createImportMap(decoratorPaths, rootDir, relativeToDir);
17
+ const decoratorKeys = Object.keys(decorators);
18
+ const decoratorImports = decoratorKeys.map((k, i) => `import * as decorator${i} from '${ext(decorators[k])}';`);
19
+ const decoratorItems = decoratorKeys.map((k, i) => ` '${k}': { module: decorator${i} }`);
20
+ const rendererConfigStr = JSON.stringify(rendererConfig, null, 2);
21
+ let importGroups = [globalImportItems, fixtureImports, decoratorImports];
22
+ if (typeScript) {
23
+ importGroups.unshift([
24
+ `import { RendererConfig, UserModuleWrappers } from 'react-cosmos-core';`,
25
+ ]);
26
+ }
27
+ return `
28
+ // This file is automatically generated by Cosmos. Add it to .gitignore and
29
+ // only edit if you know what you're doing.
30
+ ${importsStr(importGroups)}
31
+ export const rendererConfig${ts(': RendererConfig')} = ${rendererConfigStr};
32
+
33
+ const fixtures = {${objBody(fixtureItems)}};
34
+
35
+ const decorators = {${objBody(decoratorItems)}};
36
+
37
+ export const moduleWrappers${ts(': UserModuleWrappers')} = {
38
+ lazy: false,
39
+ fixtures,
40
+ decorators
41
+ };
42
+ `.trimStart();
43
+ }
44
+ function importsStr(items) {
45
+ if (flatten(items).length === 0) {
46
+ return '';
47
+ }
48
+ const str = items
49
+ .filter(rows => rows.length > 0)
50
+ .map(rows => rows.join('\n'))
51
+ .join('\n\n');
52
+ return `\n${str}\n`;
53
+ }
54
+ function objBody(items) {
55
+ return items.length > 0 ? `\n${items.join(`,\n`)}\n` : '';
56
+ }
@@ -0,0 +1,3 @@
1
+ export type Json = string | number | boolean | null | {
2
+ [property: string]: Json;
3
+ } | Json[];
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-cosmos",
3
- "version": "6.0.0-alpha.16",
3
+ "version": "6.0.0-alpha.17",
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.7",
19
19
  "pkg-up": "^4.0.0",
20
- "react-cosmos-core": "6.0.0-alpha.16",
21
- "react-cosmos-ui": "6.0.0-alpha.16",
20
+ "react-cosmos-core": "6.0.0-alpha.17",
21
+ "react-cosmos-ui": "6.0.0-alpha.17",
22
22
  "ws": "^8.13.0",
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": "2a4ec3107303a4a47e09d63e557cee5f8cceb972"
30
+ "gitHead": "2369058dc27280b59ef854a9246086d99e94990b"
31
31
  }
@@ -1,2 +0,0 @@
1
- import { CosmosServerPlugin } from '../cosmosPlugin/types.js';
2
- export declare const userDepsFileServerPlugin: CosmosServerPlugin;
@@ -1,40 +0,0 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import { getPlaygroundUrl } from '../shared/playgroundUrl.js';
4
- import { startFixtureWatcher } from '../userDeps/fixtureWatcher.js';
5
- import { generateUserDepsModule } from '../userDeps/generateUserDepsModule.js';
6
- import { getCliArgs } from '../utils/cli.js';
7
- export const userDepsFileServerPlugin = {
8
- name: 'userDepsFile',
9
- async devServer(args) {
10
- if (!shouldGenerateUserDepsFile(args.platformType))
11
- return;
12
- const { cosmosConfig } = args;
13
- await generateUserDepsFile(cosmosConfig);
14
- const watcher = await startFixtureWatcher(cosmosConfig, 'all', () => {
15
- generateUserDepsFile(cosmosConfig);
16
- });
17
- return () => {
18
- watcher.close();
19
- };
20
- },
21
- };
22
- function shouldGenerateUserDepsFile(platformType) {
23
- return (platformType === 'native' ||
24
- // CLI support for --external-userdeps flag (useful with react-native-web)
25
- Boolean(getCliArgs().externalUserdeps));
26
- }
27
- async function generateUserDepsFile(cosmosConfig) {
28
- const { userDepsFilePath } = cosmosConfig;
29
- const rendererConfig = {
30
- playgroundUrl: getPlaygroundUrl(cosmosConfig),
31
- };
32
- const userDepsModule = generateUserDepsModule({
33
- cosmosConfig,
34
- rendererConfig,
35
- relativeToDir: path.dirname(userDepsFilePath),
36
- });
37
- await fs.writeFile(userDepsFilePath, userDepsModule, 'utf8');
38
- const relUserDepsFilePath = path.relative(process.cwd(), userDepsFilePath);
39
- console.log(`[Cosmos] Generated ${relUserDepsFilePath}`);
40
- }
@@ -1,9 +0,0 @@
1
- import { CosmosConfig } from '../cosmosConfig/types.js';
2
- import { Json } from './shared.js';
3
- type Args = {
4
- cosmosConfig: CosmosConfig;
5
- rendererConfig: Json;
6
- relativeToDir: string | null;
7
- };
8
- export declare function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeToDir, }: Args): string;
9
- export {};
@@ -1,5 +0,0 @@
1
- export type Json = string | number | boolean | null | {
2
- [property: string]: Json;
3
- } | Json[];
4
- export declare function getFixturePatterns(fixturesDir: string, fixtureFileSuffix: string): string[];
5
- export declare function getDecoratorPatterns(): string[];
@@ -1,2 +0,0 @@
1
- import { UserDepsTemplateArgs } from './userDepsShared.js';
2
- export declare function userDepsLazyTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: UserDepsTemplateArgs): string;
@@ -1,37 +0,0 @@
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
- }
@@ -1,12 +0,0 @@
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 userDepsKeyPath(filePath: string, rootDir: string): string;
12
- export declare function userDepsImportPath(filePath: string, relativeToDir: string | null): string;
@@ -1,16 +0,0 @@
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
- [userDepsKeyPath(p, rootDir)]: userDepsImportPath(p, relativeToDir),
7
- }), {});
8
- }
9
- export function userDepsKeyPath(filePath, rootDir) {
10
- return slash(path.relative(rootDir, filePath));
11
- }
12
- export function userDepsImportPath(filePath, relativeToDir) {
13
- return slash(relativeToDir
14
- ? `.${path.sep}${path.relative(relativeToDir, filePath)}`
15
- : filePath);
16
- }
@@ -1,2 +0,0 @@
1
- import { UserDepsTemplateArgs } from './userDepsShared.js';
2
- export declare function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: UserDepsTemplateArgs): string;
@@ -1,45 +0,0 @@
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
- }