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

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.
@@ -65,6 +65,10 @@
65
65
  },
66
66
  "uniqueItems": true
67
67
  },
68
+ "lazy": {
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
+ "type": "boolean"
71
+ },
68
72
  "userDepsFilePath": {
69
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\"]",
70
74
  "type": "string",
@@ -83,11 +87,11 @@
83
87
  "type": "number"
84
88
  },
85
89
  "https": {
86
- "description": "Server will be served over HTTPS",
90
+ "description": "Server will be served over HTTPS.",
87
91
  "type": "boolean"
88
92
  },
89
93
  "httpsOptions": {
90
- "description": "Additional options for HTTPS server",
94
+ "description": "Additional options for HTTPS server.",
91
95
  "type": "object",
92
96
  "additionalProperties": false,
93
97
  "properties": {
@@ -15,6 +15,7 @@ export function createCosmosConfig(rootDir, cosmosConfigInput = {}) {
15
15
  https: getHttps(cosmosConfigInput),
16
16
  httpsOptions: getHttpsOptions(cosmosConfigInput, rootDir),
17
17
  ignore: getIgnore(cosmosConfigInput),
18
+ lazy: getLazy(cosmosConfigInput),
18
19
  port: getPort(cosmosConfigInput),
19
20
  portRetries: getPortRetries(cosmosConfigInput),
20
21
  plugins: getPlugins(cosmosConfigInput, rootDir),
@@ -97,3 +98,11 @@ function getDomConfig(cosmosConfigInput) {
97
98
  containerQuerySelector,
98
99
  };
99
100
  }
101
+ function getLazy(cosmosConfigInput) {
102
+ const cliArgs = getCliArgs();
103
+ if (typeof cliArgs.lazy === 'boolean') {
104
+ return cliArgs.lazy;
105
+ }
106
+ const { lazy = false } = cosmosConfigInput;
107
+ return lazy;
108
+ }
@@ -17,6 +17,7 @@ export type CosmosConfig = {
17
17
  https: boolean;
18
18
  httpsOptions: null | HttpsOptions;
19
19
  ignore: string[];
20
+ lazy: boolean;
20
21
  port: number;
21
22
  portRetries: number;
22
23
  plugins: string[];
@@ -3,6 +3,7 @@
3
3
  jest.mock('yargs/yargs', () => {
4
4
  let argv = {};
5
5
  const yargs = () => ({
6
+ boolean: () => yargs(),
6
7
  parseSync: () => argv,
7
8
  });
8
9
  yargs.__mockArgsv = (newArgv) => {
@@ -1,4 +1,5 @@
1
1
  import { findUserModulePaths } from './findUserModulePaths.js';
2
+ import { userDepsLazyTemplate } from './userDepsLazyTemplate.js';
2
3
  import { userDepsTemplate } from './userDepsTemplate.js';
3
4
  export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeToDir, }) {
4
5
  const { rootDir, fixturesDir, fixtureFileSuffix, globalImports, ignore } = cosmosConfig;
@@ -8,7 +9,8 @@ export function generateUserDepsModule({ cosmosConfig, rendererConfig, relativeT
8
9
  fixtureFileSuffix,
9
10
  ignore,
10
11
  });
11
- return userDepsTemplate({
12
+ const template = cosmosConfig.lazy ? userDepsLazyTemplate : userDepsTemplate;
13
+ return template({
12
14
  globalImports,
13
15
  fixturePaths,
14
16
  decoratorPaths,
@@ -1,8 +1,8 @@
1
- import { ReactDecorators, ReactFixtureExports } from 'react-cosmos-core';
1
+ import { ByPath, ReactDecorator, ReactFixtureExport } from 'react-cosmos-core';
2
2
  import { CosmosConfig } from '../cosmosConfig/types.js';
3
3
  type UserModules = {
4
- fixtures: ReactFixtureExports;
5
- decorators: ReactDecorators;
4
+ fixtures: ByPath<ReactFixtureExport>;
5
+ decorators: ByPath<ReactDecorator>;
6
6
  };
7
7
  export declare function getUserModules({ rootDir, fixturesDir, fixtureFileSuffix, ignore, }: CosmosConfig): UserModules;
8
8
  export {};
@@ -0,0 +1,2 @@
1
+ import { UserDepsTemplateArgs } from './userDepsShared.js';
2
+ export declare function userDepsLazyTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: UserDepsTemplateArgs): string;
@@ -0,0 +1,37 @@
1
+ import { userDepsImportMap, userDepsImportPath, } from './userDepsShared.js';
2
+ export function userDepsLazyTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }) {
3
+ const fixtures = userDepsImportMap(fixturePaths, rootDir, relativeToDir);
4
+ const fixtureKeys = Object.keys(fixtures);
5
+ const decorators = userDepsImportMap(decoratorPaths, rootDir, relativeToDir);
6
+ const decoratorKeys = Object.keys(decorators);
7
+ return `
8
+ // This file is automatically generated by Cosmos. Add it to .gitignore and
9
+ // only edit if you know what you're doing.
10
+
11
+ // Keeping global imports here is superior to making them bundle entry points
12
+ // because this way they become hot-reloadable.
13
+ ${globalImports
14
+ .map(p => `import '${userDepsImportPath(p, relativeToDir)}';`)
15
+ .join(`\n`)}
16
+
17
+ export const rendererConfig = ${JSON.stringify(rendererConfig, null, 2)};
18
+
19
+ const fixtures = {
20
+ ${fixtureKeys
21
+ .map(k => ` '${k}': { getModule: () => import('${fixtures[k]}') }`)
22
+ .join(`,\n`)}
23
+ };
24
+
25
+ const decorators = {
26
+ ${decoratorKeys
27
+ .map(k => ` '${k}': { getModule: () => import('${decorators[k]}') }`)
28
+ .join(`,\n`)}
29
+ };
30
+
31
+ export const moduleWrappers = {
32
+ lazy: true,
33
+ fixtures,
34
+ decorators,
35
+ };
36
+ `.trimStart();
37
+ }
@@ -0,0 +1,11 @@
1
+ import { Json } from './shared.js';
2
+ export type UserDepsTemplateArgs = {
3
+ globalImports: string[];
4
+ fixturePaths: string[];
5
+ decoratorPaths: string[];
6
+ rendererConfig: Json;
7
+ rootDir: string;
8
+ relativeToDir: string | null;
9
+ };
10
+ export declare function userDepsImportMap(paths: string[], rootDir: string, relativeToDir: string | null): Record<string, string>;
11
+ export declare function userDepsImportPath(filePath: string, relativeToDir: string | null): string;
@@ -0,0 +1,13 @@
1
+ import path from 'path';
2
+ import { slash } from '../utils/slash.js';
3
+ export function userDepsImportMap(paths, rootDir, relativeToDir) {
4
+ return paths.reduce((acc, p) => ({
5
+ ...acc,
6
+ [slash(path.relative(rootDir, p))]: userDepsImportPath(p, relativeToDir),
7
+ }), {});
8
+ }
9
+ export function userDepsImportPath(filePath, relativeToDir) {
10
+ return slash(relativeToDir
11
+ ? `.${path.sep}${path.relative(relativeToDir, filePath)}`
12
+ : filePath);
13
+ }
@@ -1,11 +1,2 @@
1
- import { Json } from './shared.js';
2
- type Args = {
3
- globalImports: string[];
4
- fixturePaths: string[];
5
- decoratorPaths: string[];
6
- rendererConfig: Json;
7
- rootDir: string;
8
- relativeToDir: string | null;
9
- };
10
- export declare function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: Args): string;
11
- export {};
1
+ import { UserDepsTemplateArgs } from './userDepsShared.js';
2
+ export declare function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }: UserDepsTemplateArgs): string;
@@ -1,9 +1,8 @@
1
- import path from 'path';
2
- import { slash } from '../utils/slash.js';
1
+ import { userDepsImportMap, userDepsImportPath, } from './userDepsShared.js';
3
2
  export function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths, rendererConfig, rootDir, relativeToDir, }) {
4
- const fixtures = createImportMap(fixturePaths, rootDir, relativeToDir);
3
+ const fixtures = userDepsImportMap(fixturePaths, rootDir, relativeToDir);
5
4
  const fixtureKeys = Object.keys(fixtures);
6
- const decorators = createImportMap(decoratorPaths, rootDir, relativeToDir);
5
+ const decorators = userDepsImportMap(decoratorPaths, rootDir, relativeToDir);
7
6
  const decoratorKeys = Object.keys(decorators);
8
7
  return `
9
8
  // This file is automatically generated by Cosmos. Add it to .gitignore and
@@ -12,7 +11,7 @@ export function userDepsTemplate({ globalImports, fixturePaths, decoratorPaths,
12
11
  // Keeping global imports here is superior to making them bundle entry points
13
12
  // because this way they become hot-reloadable.
14
13
  ${globalImports
15
- .map(p => `import '${resolveImportPath(p, relativeToDir)}';`)
14
+ .map(p => `import '${userDepsImportPath(p, relativeToDir)}';`)
16
15
  .join(`\n`)}
17
16
 
18
17
  ${fixtureKeys
@@ -25,25 +24,22 @@ ${decoratorKeys
25
24
 
26
25
  export const rendererConfig = ${JSON.stringify(rendererConfig, null, 2)};
27
26
 
28
- export const fixtures = {
27
+ const fixtures = {
29
28
  ${fixtureKeys
30
- .map((k, i) => ` '${k}': { module: { default: fixture${i} } }`)
29
+ .map((k, i) => ` '${k}': { module: { default: fixture${i} } }`)
31
30
  .join(`,\n`)}
32
31
  };
33
32
 
34
- export const decorators = {
35
- ${decoratorKeys.map((k, i) => ` '${k}': decorator${i}`).join(`,\n`)}
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,
36
43
  };
37
44
  `.trimStart();
38
45
  }
39
- function createImportMap(paths, rootDir, relativeToDir) {
40
- return paths.reduce((acc, p) => ({
41
- ...acc,
42
- [slash(path.relative(rootDir, p))]: resolveImportPath(p, relativeToDir),
43
- }), {});
44
- }
45
- function resolveImportPath(filePath, relativeToDir) {
46
- return slash(relativeToDir
47
- ? `.${path.sep}${path.relative(relativeToDir, filePath)}`
48
- : filePath);
49
- }
@@ -1,5 +1,6 @@
1
1
  export declare function getCliArgs(): {
2
2
  [x: string]: unknown;
3
+ lazy: boolean | undefined;
3
4
  _: (string | number)[];
4
5
  $0: string;
5
6
  };
package/dist/utils/cli.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import yargs from 'yargs/yargs';
2
2
  export function getCliArgs() {
3
- return yargs(process.argv.slice(2)).parseSync();
3
+ return yargs(process.argv.slice(2)).boolean('lazy').parseSync();
4
4
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-cosmos",
3
- "version": "6.0.0-alpha.11",
3
+ "version": "6.0.0-alpha.13",
4
4
  "description": "Sandbox for developing and testing UI components in isolation",
5
5
  "repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos",
6
6
  "license": "MIT",
@@ -17,8 +17,8 @@
17
17
  "open": "^8.4.2",
18
18
  "pem": "^1.14.6",
19
19
  "pkg-up": "^4.0.0",
20
- "react-cosmos-core": "^6.0.0-alpha.11",
21
- "react-cosmos-ui": "^6.0.0-alpha.11",
20
+ "react-cosmos-core": "6.0.0-alpha.13",
21
+ "react-cosmos-ui": "6.0.0-alpha.13",
22
22
  "ws": "^8.12.1",
23
23
  "yargs": "^17.7.1"
24
24
  },
@@ -27,5 +27,5 @@
27
27
  "cosmos-export": "./bin/cosmos-export.js",
28
28
  "cosmos-native": "./bin/cosmos-native.js"
29
29
  },
30
- "gitHead": "8e9d1e52749388f471f6ec848a8c904ec8b89f67"
30
+ "gitHead": "cf95e67ce942f6a242f023ecebe45d6916d5b753"
31
31
  }