react-cosmos 5.7.2 → 6.0.0-alpha.0
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.
- package/bin/cosmos-export.js +4 -3
- package/bin/cosmos-native.js +4 -3
- package/bin/cosmos.js +5 -4
- package/config.schema.json +14 -4
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -70
- package/dist/jest.d.ts +7 -0
- package/dist/jest.js +7 -0
- package/dist/{config → server/cosmosConfig}/createCosmosConfig.d.ts +1 -1
- package/dist/server/cosmosConfig/createCosmosConfig.js +84 -0
- package/dist/server/cosmosConfig/detectCosmosConfig.d.ts +2 -0
- package/dist/server/cosmosConfig/detectCosmosConfig.js +38 -0
- package/dist/{config → server/cosmosConfig}/getCosmosConfigAtPath.d.ts +1 -1
- package/dist/server/cosmosConfig/getCosmosConfigAtPath.js +12 -0
- package/dist/server/cosmosConfig/shared.d.ts +2 -0
- package/dist/server/cosmosConfig/shared.js +7 -0
- package/dist/{config/shared.d.ts → server/cosmosConfig/types.d.ts} +5 -5
- package/dist/server/cosmosPlugin/findCosmosPluginConfigs.d.ts +8 -0
- package/dist/server/cosmosPlugin/findCosmosPluginConfigs.js +14 -0
- package/dist/server/cosmosPlugin/pluginConfigs.d.ts +8 -0
- package/dist/server/cosmosPlugin/pluginConfigs.js +16 -0
- package/dist/server/cosmosPlugin/readCosmosPluginConfig.d.ts +8 -0
- package/dist/server/cosmosPlugin/readCosmosPluginConfig.js +31 -0
- package/dist/server/cosmosPlugin/types.d.ts +21 -0
- package/dist/server/devServer/corePlugins/httpProxy.d.ts +2 -0
- package/dist/server/devServer/corePlugins/httpProxy.js +18 -0
- package/dist/server/devServer/corePlugins/openFile.d.ts +2 -0
- package/dist/server/devServer/corePlugins/openFile.js +53 -0
- package/dist/server/devServer/corePlugins/pluginEndpoint.d.ts +2 -0
- package/dist/server/devServer/corePlugins/pluginEndpoint.js +17 -0
- package/dist/server/devServer/corePlugins/userDepsFile.d.ts +2 -0
- package/dist/server/devServer/corePlugins/userDepsFile.js +37 -0
- package/dist/server/devServer/expressApp.d.ts +5 -0
- package/dist/server/devServer/expressApp.js +21 -0
- package/dist/server/devServer/httpServer.d.ts +12 -0
- package/dist/server/devServer/httpServer.js +42 -0
- package/dist/{devServer → server/devServer}/messageHandler.d.ts +2 -2
- package/dist/server/devServer/messageHandler.js +29 -0
- package/dist/server/devServer/startDevServer.d.ts +2 -0
- package/dist/server/devServer/startDevServer.js +91 -0
- package/dist/server/export/generateExport.d.ts +1 -0
- package/dist/server/export/generateExport.js +97 -0
- package/dist/server/getFixtures/getFixtures.d.ts +14 -0
- package/dist/server/getFixtures/getFixtures.js +63 -0
- package/dist/server/shared/logPluginInfo.d.ts +2 -0
- package/dist/server/shared/logPluginInfo.js +8 -0
- package/dist/server/shared/playgroundHtml.d.ts +6 -0
- package/dist/server/shared/playgroundHtml.js +85 -0
- package/dist/server/shared/playgroundUrl.d.ts +2 -0
- package/dist/server/shared/playgroundUrl.js +6 -0
- package/dist/server/shared/requirePluginModule.d.ts +2 -0
- package/dist/server/shared/requirePluginModule.js +10 -0
- package/dist/server/shared/resolveUrlPath.d.ts +1 -0
- package/dist/server/shared/resolveUrlPath.js +12 -0
- package/dist/server/shared/staticPath.d.ts +1 -0
- package/dist/server/shared/staticPath.js +4 -0
- package/dist/{shared/static.d.ts → server/shared/staticServer.d.ts} +0 -1
- package/dist/server/shared/staticServer.js +12 -0
- package/dist/{shared → server}/static/index.html +1 -1
- package/dist/server/testHelpers/cwd.d.ts +1 -0
- package/dist/server/testHelpers/cwd.js +4 -0
- package/dist/server/testHelpers/mockConsole.d.ts +5 -0
- package/dist/server/testHelpers/mockConsole.js +11 -0
- package/dist/server/testHelpers/mockEsmRequire.js +13 -0
- package/dist/server/testHelpers/mockEsmResolve.d.ts +1 -0
- package/dist/server/testHelpers/mockEsmResolve.js +13 -0
- package/dist/server/testHelpers/mockEsmStaticPath.d.ts +1 -0
- package/dist/server/testHelpers/mockEsmStaticPath.js +7 -0
- package/dist/server/testHelpers/mockFetch.d.ts +2 -0
- package/dist/server/testHelpers/mockFetch.js +7 -0
- package/dist/server/testHelpers/mockFs.d.ts +5 -0
- package/dist/server/testHelpers/mockFs.js +64 -0
- package/dist/server/testHelpers/mockYargs.d.ts +2 -0
- package/dist/server/testHelpers/mockYargs.js +21 -0
- package/dist/{userDeps → server/userDeps}/findUserModulePaths.d.ts +2 -2
- package/dist/server/userDeps/findUserModulePaths.js +20 -0
- package/dist/server/userDeps/fixtureWatcher.d.ts +3 -0
- package/dist/server/userDeps/fixtureWatcher.js +20 -0
- package/dist/{userDeps → server/userDeps}/generateUserDepsModule.d.ts +3 -3
- package/dist/server/userDeps/generateUserDepsModule.js +18 -0
- package/dist/{userDeps → server/userDeps}/getUserModules.d.ts +3 -3
- package/dist/server/userDeps/getUserModules.js +25 -0
- package/dist/{userDeps → server/userDeps}/shared.d.ts +1 -1
- package/dist/server/userDeps/shared.js +20 -0
- package/dist/{userDeps → server/userDeps}/userDepsTemplate.d.ts +2 -2
- package/dist/server/userDeps/userDepsTemplate.js +49 -0
- package/dist/server/utils/cli.js +4 -0
- package/dist/server/utils/fs.d.ts +4 -0
- package/dist/server/utils/fs.js +24 -0
- package/dist/server/utils/isNodeError.d.ts +2 -0
- package/dist/server/utils/isNodeError.js +3 -0
- package/dist/server/utils/requireModule.d.ts +2 -0
- package/dist/server/utils/requireModule.js +13 -0
- package/dist/server/utils/requireSilent.d.ts +1 -0
- package/dist/server/utils/requireSilent.js +11 -0
- package/dist/server/utils/resolve.d.ts +2 -0
- package/dist/server/utils/resolve.js +14 -0
- package/dist/server/utils/resolveLoose.d.ts +1 -0
- package/dist/server/utils/resolveLoose.js +12 -0
- package/dist/server/utils/resolveSilent.d.ts +2 -0
- package/dist/server/utils/resolveSilent.js +22 -0
- package/dist/server/utils/slash.d.ts +1 -0
- package/dist/server/utils/slash.js +5 -0
- package/dist/server.d.ts +20 -0
- package/dist/server.js +20 -0
- package/index.d.ts +1 -1
- package/index.js +1 -2
- package/jest.d.ts +1 -0
- package/jest.js +1 -0
- package/package.json +17 -28
- package/server.d.ts +1 -0
- package/server.js +1 -0
- package/dist/config/createCosmosConfig.js +0 -133
- package/dist/config/detectCosmosConfig.d.ts +0 -2
- package/dist/config/detectCosmosConfig.js +0 -57
- package/dist/config/getCosmosConfigAtPath.js +0 -28
- package/dist/config/resolveModule.d.ts +0 -1
- package/dist/config/resolveModule.js +0 -20
- package/dist/config/shared.js +0 -17
- package/dist/devServer/app.d.ts +0 -5
- package/dist/devServer/app.js +0 -51
- package/dist/devServer/httpServer.d.ts +0 -11
- package/dist/devServer/httpServer.js +0 -183
- package/dist/devServer/messageHandler.js +0 -42
- package/dist/devServer/startDevServer.d.ts +0 -18
- package/dist/devServer/startDevServer.js +0 -209
- package/dist/dom/DomFixtureLoader.d.ts +0 -9
- package/dist/dom/DomFixtureLoader.js +0 -38
- package/dist/dom/ErrorCatch.d.ts +0 -15
- package/dist/dom/ErrorCatch.js +0 -117
- package/dist/dom/container.d.ts +0 -1
- package/dist/dom/container.js +0 -32
- package/dist/dom/globalErrorHandler.js +0 -14
- package/dist/dom/index.d.ts +0 -13
- package/dist/dom/index.js +0 -47
- package/dist/dom/renderMessage.d.ts +0 -4
- package/dist/dom/renderMessage.js +0 -30
- package/dist/dom/rendererConnect.d.ts +0 -1
- package/dist/dom/rendererConnect.js +0 -18
- package/dist/dom/rendererId.d.ts +0 -1
- package/dist/dom/rendererId.js +0 -26
- package/dist/dom/selectedFixtureId.d.ts +0 -2
- package/dist/dom/selectedFixtureId.js +0 -41
- package/dist/dom/shared.d.ts +0 -1
- package/dist/dom/shared.js +0 -14
- package/dist/export/generateExport.d.ts +0 -6
- package/dist/export/generateExport.js +0 -137
- package/dist/fixture.d.ts +0 -1
- package/dist/fixture.js +0 -31
- package/dist/generateWebExport.d.ts +0 -1
- package/dist/generateWebExport.js +0 -37
- package/dist/getFixtureUrls.d.ts +0 -8
- package/dist/getFixtureUrls.js +0 -97
- package/dist/getFixtures.d.ts +0 -13
- package/dist/getFixtures.js +0 -89
- package/dist/getFixtures2.d.ts +0 -14
- package/dist/getFixtures2.js +0 -117
- package/dist/native/NativeFixtureLoader.d.ts +0 -12
- package/dist/native/NativeFixtureLoader.js +0 -67
- package/dist/native/index.d.ts +0 -1
- package/dist/native/index.js +0 -25
- package/dist/plugins/httpProxy.d.ts +0 -2
- package/dist/plugins/httpProxy.js +0 -31
- package/dist/plugins/openFile.d.ts +0 -2
- package/dist/plugins/openFile.js +0 -87
- package/dist/plugins/userDepsFile.d.ts +0 -2
- package/dist/plugins/userDepsFile.js +0 -166
- package/dist/plugins/webpack/client/errorOverlay/index.d.ts +0 -1
- package/dist/plugins/webpack/client/errorOverlay/index.js +0 -16
- package/dist/plugins/webpack/client/errorOverlay/reactErrorOverlay.d.ts +0 -2
- package/dist/plugins/webpack/client/errorOverlay/reactErrorOverlay.js +0 -63
- package/dist/plugins/webpack/client/hmrErrorHandler.js +0 -16
- package/dist/plugins/webpack/client/index.d.ts +0 -6
- package/dist/plugins/webpack/client/index.js +0 -43
- package/dist/plugins/webpack/client/reactDevtoolsHook.d.ts +0 -0
- package/dist/plugins/webpack/client/reactDevtoolsHook.js +0 -19
- package/dist/plugins/webpack/client/userDeps.d.ts +0 -5
- package/dist/plugins/webpack/client/userDeps.js +0 -15
- package/dist/plugins/webpack/cosmosConfig/dom.d.ts +0 -6
- package/dist/plugins/webpack/cosmosConfig/dom.js +0 -19
- package/dist/plugins/webpack/cosmosConfig/webpack.d.ts +0 -10
- package/dist/plugins/webpack/cosmosConfig/webpack.js +0 -95
- package/dist/plugins/webpack/devServer.d.ts +0 -2
- package/dist/plugins/webpack/devServer.js +0 -168
- package/dist/plugins/webpack/export.d.ts +0 -2
- package/dist/plugins/webpack/export.js +0 -139
- package/dist/plugins/webpack/index.d.ts +0 -2
- package/dist/plugins/webpack/index.js +0 -21
- package/dist/plugins/webpack/shared.d.ts +0 -2
- package/dist/plugins/webpack/shared.js +0 -22
- package/dist/plugins/webpack/webpackConfig/getDefaultWebpackConfig.d.ts +0 -2
- package/dist/plugins/webpack/webpackConfig/getDefaultWebpackConfig.js +0 -152
- package/dist/plugins/webpack/webpackConfig/getDevWebpackConfig.d.ts +0 -3
- package/dist/plugins/webpack/webpackConfig/getDevWebpackConfig.js +0 -146
- package/dist/plugins/webpack/webpackConfig/getExportWebpackConfig.d.ts +0 -3
- package/dist/plugins/webpack/webpackConfig/getExportWebpackConfig.js +0 -111
- package/dist/plugins/webpack/webpackConfig/getUserWebpackConfig.d.ts +0 -3
- package/dist/plugins/webpack/webpackConfig/getUserWebpackConfig.js +0 -136
- package/dist/plugins/webpack/webpackConfig/getWebpackConfigModule.d.ts +0 -2
- package/dist/plugins/webpack/webpackConfig/getWebpackConfigModule.js +0 -45
- package/dist/plugins/webpack/webpackConfig/getWebpackConfigResolve.d.ts +0 -4
- package/dist/plugins/webpack/webpackConfig/getWebpackConfigResolve.js +0 -107
- package/dist/plugins/webpack/webpackConfig/getWebpackNodeEnv.d.ts +0 -1
- package/dist/plugins/webpack/webpackConfig/getWebpackNodeEnv.js +0 -12
- package/dist/plugins/webpack/webpackConfig/htmlPlugin.d.ts +0 -16
- package/dist/plugins/webpack/webpackConfig/htmlPlugin.js +0 -80
- package/dist/plugins/webpack/webpackConfig/plugins.d.ts +0 -5
- package/dist/plugins/webpack/webpackConfig/plugins.js +0 -37
- package/dist/plugins/webpack/webpackConfig/resolveWebpackClientPath.d.ts +0 -1
- package/dist/plugins/webpack/webpackConfig/resolveWebpackClientPath.js +0 -10
- package/dist/plugins/webpack/webpackConfig/userDepsLoader.js +0 -36
- package/dist/shared/cli.js +0 -14
- package/dist/shared/fs.d.ts +0 -4
- package/dist/shared/fs.js +0 -37
- package/dist/shared/playgroundHtml.d.ts +0 -6
- package/dist/shared/playgroundHtml.js +0 -116
- package/dist/shared/pluginConfigs.d.ts +0 -2
- package/dist/shared/pluginConfigs.js +0 -15
- package/dist/shared/rendererConfig.d.ts +0 -6
- package/dist/shared/rendererConfig.js +0 -5
- package/dist/shared/static.js +0 -30
- package/dist/shared/types.d.ts +0 -1
- package/dist/shared/types.js +0 -5
- package/dist/shared/utils.d.ts +0 -5
- package/dist/shared/utils.js +0 -22
- package/dist/startNativeServer.d.ts +0 -1
- package/dist/startNativeServer.js +0 -39
- package/dist/startWebServer.d.ts +0 -1
- package/dist/startWebServer.js +0 -43
- package/dist/userDeps/findUserModulePaths.js +0 -45
- package/dist/userDeps/generateUserDepsModule.js +0 -37
- package/dist/userDeps/getUserModules.js +0 -45
- package/dist/userDeps/shared.js +0 -32
- package/dist/userDeps/userDepsTemplate.js +0 -52
- package/dom.d.ts +0 -1
- package/dom.js +0 -2
- package/fixture.d.ts +0 -1
- package/fixture.js +0 -2
- package/native.d.ts +0 -1
- package/native.js +0 -2
- package/yarn-error.log +0 -13450
- /package/dist/{dom/globalErrorHandler.d.ts → server/cosmosConfig/types.js} +0 -0
- /package/dist/{plugins/webpack/client/hmrErrorHandler.d.ts → server/cosmosPlugin/types.js} +0 -0
- /package/dist/{shared → server}/static/favicon.ico +0 -0
- /package/dist/{plugins/webpack/webpackConfig/userDepsLoader.d.ts → server/testHelpers/mockEsmRequire.d.ts} +0 -0
- /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,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,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
|
-
|
|
1
|
+
type FindUserModulePathsArgs = {
|
|
2
2
|
rootDir: string;
|
|
3
3
|
fixturesDir: string;
|
|
4
4
|
fixtureFileSuffix: string;
|
|
5
5
|
};
|
|
6
|
-
|
|
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,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 '../
|
|
2
|
-
import { Json } from './shared';
|
|
3
|
-
|
|
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-
|
|
2
|
-
import { CosmosConfig } from '../
|
|
3
|
-
|
|
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
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -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,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,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,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,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;
|
package/dist/server.d.ts
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/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
|
-
|
|
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');
|