@modern-js/plugin-testing 1.3.0 → 1.3.3

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 (39) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/js/modern/cli/plugins/modern.js +29 -48
  3. package/dist/js/modern/cli/plugins/modern.test.js +1 -13
  4. package/dist/js/modern/cli/resolver.js +1 -1
  5. package/dist/js/modern/cli/test.js +25 -9
  6. package/dist/js/modern/runtime-testing/base.js +3 -0
  7. package/dist/js/modern/runtime-testing/index.js +2 -4
  8. package/dist/js/node/cli/plugins/modern.js +29 -56
  9. package/dist/js/node/cli/plugins/modern.test.js +0 -12
  10. package/dist/js/node/cli/resolver.js +1 -1
  11. package/dist/js/node/cli/test.js +25 -9
  12. package/dist/js/node/runtime-testing/base.js +41 -0
  13. package/dist/js/node/runtime-testing/index.js +5 -29
  14. package/dist/js/treeshaking/cli/plugins/modern.js +4 -27
  15. package/dist/js/treeshaking/cli/plugins/modern.test.js +1 -13
  16. package/dist/js/treeshaking/cli/resolver.js +1 -1
  17. package/dist/js/treeshaking/cli/test.js +35 -15
  18. package/dist/js/treeshaking/runtime-testing/base.js +3 -0
  19. package/dist/js/treeshaking/runtime-testing/index.js +2 -4
  20. package/dist/types/cli/plugins/modern.d.ts +0 -1
  21. package/{src/runtime-testing/index.ts → dist/types/runtime-testing/base.d.ts} +1 -2
  22. package/dist/types/runtime-testing/index.d.ts +2 -4
  23. package/package.json +20 -12
  24. package/tests/.eslintrc.js +6 -0
  25. package/tests/resolver.test.ts +30 -0
  26. package/tests/tsconfig.json +13 -0
  27. package/type.d.ts +5 -1
  28. package/src/.eslintrc.json +0 -3
  29. package/src/cli/index.ts +0 -48
  30. package/src/cli/plugins/modern.test.ts +0 -68
  31. package/src/cli/plugins/modern.ts +0 -83
  32. package/src/cli/resolver.ts +0 -16
  33. package/src/cli/test.ts +0 -77
  34. package/src/constant.ts +0 -1
  35. package/src/index.ts +0 -4
  36. package/src/runtime-testing/app.ts +0 -48
  37. package/src/runtime-testing/customRender.ts +0 -11
  38. package/src/runtime-testing/reduck.ts +0 -38
  39. package/src/runtime-testing/resolvePlugins.ts +0 -23
@@ -22,11 +22,12 @@ import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
22
22
  import { runTest } from '@modern-js/testing';
23
23
  import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
24
24
  import testingBffPlugin from '@modern-js/testing-plugin-bff';
25
+ import { fs } from '@modern-js/utils';
25
26
  import modernTestPlugin from "./plugins/modern";
26
27
 
27
28
  var test = /*#__PURE__*/function () {
28
29
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
29
- var userConfig, config, webpackConfigs, jest, runtimeExportsPath;
30
+ var userConfig, config, srcDirectory, existSrc, jest, webpackConfigs, runtimeExportsPath;
30
31
  return _regeneratorRuntime.wrap(function _callee$(_context) {
31
32
  while (1) {
32
33
  switch (_context.prev = _context.next) {
@@ -34,9 +35,13 @@ var test = /*#__PURE__*/function () {
34
35
  // eslint-disable-next-line react-hooks/rules-of-hooks
35
36
  userConfig = useResolvedConfigContext(); // eslint-disable-next-line react-hooks/rules-of-hooks
36
37
 
37
- config = useAppContext(); // todo: consider lib-tools ...
38
+ config = useAppContext();
39
+ srcDirectory = config.srcDirectory;
40
+ _context.next = 5;
41
+ return fs.pathExists(srcDirectory);
38
42
 
39
- webpackConfigs = getWebpackConfig(WebpackConfigTarget.CLIENT);
43
+ case 5:
44
+ existSrc = _context.sent;
40
45
  userConfig.testing = userConfig.testing || {};
41
46
  jest = userConfig.testing.jest || userConfig.tools.jest;
42
47
 
@@ -45,16 +50,31 @@ var test = /*#__PURE__*/function () {
45
50
  }
46
51
 
47
52
  userConfig.testing.jest = userConfig.testing.jest || userConfig.tools.jest;
48
- userConfig.testing.plugins = [].concat(_toConsumableArray(userConfig.testing.plugins || []), [modernTestPlugin(webpackConfigs, userConfig, config.appDirectory), testingBffPlugin({
49
- pwd: config.appDirectory,
50
- userConfig: userConfig,
51
- plugins: config.plugins.map(function (p) {
52
- return p.server;
53
- }).filter(Boolean),
54
- routes: config.serverRoutes
55
- })]);
53
+
54
+ if (existSrc) {
55
+ // todo: consider lib-tools ...
56
+ webpackConfigs = getWebpackConfig(WebpackConfigTarget.CLIENT);
57
+ userConfig.testing.plugins = [].concat(_toConsumableArray(userConfig.testing.plugins || []), [modernTestPlugin(webpackConfigs, userConfig, config.appDirectory), testingBffPlugin({
58
+ pwd: config.appDirectory,
59
+ userConfig: userConfig,
60
+ plugins: config.plugins.map(function (p) {
61
+ return p.server;
62
+ }).filter(Boolean),
63
+ routes: config.serverRoutes
64
+ })]);
65
+ } else {
66
+ userConfig.testing.plugins = [].concat(_toConsumableArray(userConfig.testing.plugins || []), [testingBffPlugin({
67
+ pwd: config.appDirectory,
68
+ userConfig: userConfig,
69
+ plugins: config.plugins.map(function (p) {
70
+ return p.server;
71
+ }).filter(Boolean),
72
+ routes: config.serverRoutes
73
+ })]);
74
+ }
75
+
56
76
  runtimeExportsPath = path.join(config.internalDirectory, '.runtime-exports');
57
- _context.next = 11;
77
+ _context.next = 14;
58
78
  return compiler({
59
79
  sourceDir: runtimeExportsPath,
60
80
  rootDir: runtimeExportsPath,
@@ -66,11 +86,11 @@ var test = /*#__PURE__*/function () {
66
86
  }]]
67
87
  });
68
88
 
69
- case 11:
70
- _context.next = 13;
89
+ case 14:
90
+ _context.next = 16;
71
91
  return runTest(userConfig.testing);
72
92
 
73
- case 13:
93
+ case 16:
74
94
  case "end":
75
95
  return _context.stop();
76
96
  }
@@ -0,0 +1,3 @@
1
+ export { default as renderApp } from "./customRender";
2
+ export * from '@testing-library/react';
3
+ export { testBff } from '@modern-js/testing-plugin-bff';
@@ -1,4 +1,2 @@
1
- export { default as renderApp } from "./customRender";
2
- export * from '@testing-library/react';
3
- export * from "./reduck";
4
- export { testBff } from '@modern-js/testing-plugin-bff';
1
+ export * from "./base";
2
+ export * from "./reduck";
@@ -1,5 +1,4 @@
1
1
  import type { NormalizedConfig } from '@modern-js/core';
2
- export declare const getModuleNameMapper: (config: any) => any;
3
2
  export declare const mergeUserJestConfig: (testUtils: any) => Promise<void>;
4
3
 
5
4
  declare const _default: (webpackConfig: any, userConfig: NormalizedConfig, pwd: string) => any;
@@ -1,4 +1,3 @@
1
1
  export { default as renderApp } from './customRender';
2
2
  export * from '@testing-library/react';
3
- export * from './reduck';
4
- export { testBff } from '@modern-js/testing-plugin-bff';
3
+ export { testBff } from '@modern-js/testing-plugin-bff';
@@ -1,4 +1,2 @@
1
- export { default as renderApp } from './customRender';
2
- export * from '@testing-library/react';
3
- export * from './reduck';
4
- export { testBff } from '@modern-js/testing-plugin-bff';
1
+ export * from './base';
2
+ export * from './reduck';
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.3.0",
14
+ "version": "1.3.3",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -40,6 +40,15 @@
40
40
  },
41
41
  "default": "./dist/js/treeshaking/runtime-testing/index.js"
42
42
  },
43
+ "./runtime-base": {
44
+ "jsnext:source": "./src/runtime-testing/base.ts",
45
+ "node": {
46
+ "import": "./dist/js/modern/runtime-testing/base.js",
47
+ "require": "./dist/js/node/runtime-testing/base.js",
48
+ "types": "./types/base.d.ts"
49
+ },
50
+ "default": "./dist/js/treeshaking/runtime-testing/base.js"
51
+ },
43
52
  "./cli": {
44
53
  "jsnext:source": "./src/cli/index.ts",
45
54
  "default": "./dist/js/node/cli/index.js"
@@ -64,22 +73,22 @@
64
73
  "dependencies": {
65
74
  "@babel/preset-env": "^7.15.6",
66
75
  "@babel/runtime": "^7",
67
- "@modern-js/babel-compiler": "^1.2.0",
68
- "@modern-js/testing": "^1.3.0",
69
- "@modern-js/utils": "^1.2.0",
70
- "@modern-js/webpack": "^1.2.0",
76
+ "@modern-js/babel-compiler": "^1.2.1",
77
+ "@modern-js/testing": "^1.3.2",
78
+ "@modern-js/utils": "^1.3.1",
79
+ "@modern-js/webpack": "^1.3.1",
71
80
  "@testing-library/jest-dom": "^5.14.1",
72
81
  "@testing-library/react": "^12.0.0",
73
82
  "enhanced-resolve": "^5.8.3",
74
- "@modern-js/testing-plugin-bff": "^1.2.0"
83
+ "@modern-js/testing-plugin-bff": "^1.2.2"
75
84
  },
76
85
  "peerDependencies": {
77
86
  "@modern-js-reduck/plugin-auto-actions": "^1.0.0",
78
87
  "@modern-js-reduck/plugin-effects": "^1.0.0",
79
88
  "@modern-js-reduck/plugin-immutable": "^1.0.0",
80
89
  "@modern-js-reduck/store": "^1.0.0",
81
- "@modern-js/core": "^1.3.0",
82
- "@modern-js/runtime-core": "^1.2.0"
90
+ "@modern-js/core": "^1.4.1",
91
+ "@modern-js/runtime-core": "^1.2.1"
83
92
  },
84
93
  "devDependencies": {
85
94
  "@modern-js-reduck/plugin-auto-actions": "^1.0.0",
@@ -87,8 +96,8 @@
87
96
  "@modern-js-reduck/plugin-immutable": "^1.0.0",
88
97
  "@modern-js-reduck/store": "^1.0.0",
89
98
  "@scripts/build": "0.0.0",
90
- "@modern-js/core": "^1.3.0",
91
- "@modern-js/runtime-core": "^1.2.0",
99
+ "@modern-js/core": "^1.4.1",
100
+ "@modern-js/runtime-core": "^1.2.1",
92
101
  "@types/jest": "^26",
93
102
  "@types/node": "^14",
94
103
  "@types/react": "^17",
@@ -103,8 +112,7 @@
103
112
  "modernConfig": {},
104
113
  "publishConfig": {
105
114
  "registry": "https://registry.npmjs.org/",
106
- "access": "public",
107
- "types": "./dist/types/index.d.ts"
115
+ "access": "public"
108
116
  },
109
117
  "scripts": {
110
118
  "new": "modern new",
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ extends: ['@modern-js'],
3
+ parserOptions: {
4
+ project: require.resolve('./tsconfig.json'),
5
+ },
6
+ };
@@ -0,0 +1,30 @@
1
+ import path from 'path';
2
+ import { fs } from '@modern-js/utils';
3
+
4
+ const resolver = require('../src/cli/resolver');
5
+
6
+ describe('plugin-testing', () => {
7
+ it('resolver', () => {
8
+ const moduleDir = path.join(
9
+ __dirname,
10
+ 'node_modules/@modern-js/runtime/module',
11
+ );
12
+ fs.ensureDirSync(moduleDir);
13
+ fs.writeJSONSync(
14
+ path.join(moduleDir, 'package.json'),
15
+ {
16
+ name: '@modern-js/runtime/module',
17
+ main: './index.js',
18
+ },
19
+ 'utf-8',
20
+ );
21
+ fs.writeFileSync(
22
+ path.join(moduleDir, 'index.js'),
23
+ 'module.exports = "@modern-js/runtime/module"',
24
+ 'utf-8',
25
+ );
26
+ expect(
27
+ resolver('@modern-js/runtime/module', { basedir: __dirname }),
28
+ ).toContain('module');
29
+ });
30
+ });
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "@modern-js/tsconfig/base",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "jsx": "preserve",
6
+ "baseUrl": "./",
7
+ "outDir": "./out",
8
+ "emitDeclarationOnly": true,
9
+ "isolatedModules": true,
10
+ "paths": {},
11
+ "types": ["node", "jest"]
12
+ }
13
+ }
package/type.d.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  import '@modern-js/core';
2
+ import "@testing-library/react"
3
+ import "@testing-library/jest-dom"
4
+ import "./dist/types/runtime-testing"
5
+
2
6
  declare module '@modern-js/runtime/testing' {
3
7
  export * from '@testing-library/react';
4
- export { renderApp, createStore } from '@modern-js/plugin-testing';
8
+ export { renderApp, createStore, testBff } from './dist/types/runtime-testing';
5
9
  }
6
10
 
7
11
  declare module '@modern-js/core' {
@@ -1,3 +0,0 @@
1
- {
2
- "extends": ["@modern-js"]
3
- }
package/src/cli/index.ts DELETED
@@ -1,48 +0,0 @@
1
- import path from 'path';
2
- import { createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
3
- import { createPlugin, useAppContext } from '@modern-js/core';
4
- import test from './test';
5
-
6
- export default createPlugin(
7
- () => {
8
- let testingExportsUtils: ReturnType<typeof createRuntimeExportsUtils>;
9
-
10
- return {
11
- commands: ({ program }: any) => {
12
- program
13
- .command('test')
14
- .allowUnknownOption()
15
- .usage('[options]')
16
- .action(async () => {
17
- await test();
18
- });
19
- },
20
- validateSchema() {
21
- return PLUGIN_SCHEMAS['@modern-js/plugin-testing'];
22
- },
23
- config() {
24
- // eslint-disable-next-line react-hooks/rules-of-hooks
25
- const appContext = useAppContext();
26
-
27
- testingExportsUtils = createRuntimeExportsUtils(
28
- appContext.internalDirectory,
29
- 'testing',
30
- );
31
-
32
- return {
33
- source: {
34
- alias: {
35
- '@modern-js/runtime/testing': testingExportsUtils.getPath(),
36
- },
37
- },
38
- };
39
- },
40
- addRuntimeExports() {
41
- const testingPath = path.resolve(__dirname, '../');
42
-
43
- testingExportsUtils.addExport(`export * from '${testingPath}'`);
44
- },
45
- };
46
- },
47
- { name: '@modern-js/plugin-testing' },
48
- ) as any;
@@ -1,68 +0,0 @@
1
- import { mergeUserJestConfig, getModuleNameMapper } from './modern';
2
-
3
- describe('modern plugin', () => {
4
- test('mergeUserJestConfig support object', () => {
5
- const testUtils = {
6
- _jestConfig: {
7
- a: 1,
8
- },
9
- get jestConfig() {
10
- return this._jestConfig;
11
- },
12
- testConfig: {
13
- jest: {
14
- b: 1,
15
- },
16
- },
17
- mergeJestConfig(config: Record<string, string>) {
18
- Object.assign(this._jestConfig, config);
19
- },
20
- };
21
-
22
- mergeUserJestConfig(testUtils);
23
-
24
- expect(testUtils.jestConfig).toEqual({
25
- a: 1,
26
- b: 1,
27
- });
28
- });
29
-
30
- test('mergeUserJestConfig support function', () => {
31
- const testUtils = {
32
- _jestConfig: {
33
- a: 1,
34
- },
35
- get jestConfig() {
36
- return this._jestConfig;
37
- },
38
- testConfig: {
39
- jest: (jestConfig: Record<string, number>) => {
40
- jestConfig.b = 1;
41
- },
42
- },
43
- mergeJestConfig(config: Record<string, string>) {
44
- Object.assign(this._jestConfig, config);
45
- },
46
- };
47
-
48
- mergeUserJestConfig(testUtils);
49
- expect(testUtils.jestConfig).toEqual({
50
- a: 1,
51
- b: 1,
52
- });
53
- });
54
-
55
- // TODO: 临时测试代码,待补充
56
- test('getModuleNameMapper', () => {
57
- const mockConfig = {
58
- resolve: {
59
- alias: {
60
- '@modern-js/runtime/core': '/xxx',
61
- },
62
- },
63
- };
64
-
65
- const alias = getModuleNameMapper(mockConfig);
66
- expect(alias).toBeDefined();
67
- });
68
- });
@@ -1,83 +0,0 @@
1
- import path from 'path';
2
- import { createPlugin } from '@modern-js/testing';
3
- import type { NormalizedConfig } from '@modern-js/core';
4
- import { modernjs_config_key } from '../../constant';
5
-
6
- export const getModuleNameMapper = (config: any) => {
7
- const {
8
- resolve: { alias = {} },
9
- } = config;
10
-
11
- return Object.keys(alias).reduce((memo, cur) => {
12
- const aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
13
-
14
- const isFile = aliasValue.some((s: string) => s.endsWith('.js'));
15
-
16
- // It's special for if using @modern-js/runtime alias other module @modern-js/runtime/model would not work.
17
- if (cur === '@modern-js/runtime$') {
18
- memo[`.+${cur}`] = aliasValue[0];
19
-
20
- return memo;
21
- }
22
-
23
- if (isFile) {
24
- memo[cur] = aliasValue[0];
25
- }
26
-
27
- const key = `^${cur}/(.*)$`;
28
- const value = path.normalize(`${aliasValue}/$1`);
29
- memo[key] = value;
30
- return memo;
31
- }, {} as any);
32
- };
33
-
34
- export const mergeUserJestConfig = async (testUtils: any) => {
35
- const resolveJestConfig = testUtils.testConfig.jest;
36
-
37
- if (resolveJestConfig && typeof resolveJestConfig !== 'function') {
38
- testUtils.mergeJestConfig(resolveJestConfig);
39
- }
40
-
41
- if (typeof resolveJestConfig === 'function') {
42
- await resolveJestConfig(testUtils.jestConfig);
43
- }
44
- };
45
-
46
- export default (
47
- webpackConfig: any,
48
- userConfig: NormalizedConfig,
49
- pwd: string,
50
- ) =>
51
- createPlugin(
52
- () => ({
53
- jestConfig: (utils: any, next: any) => {
54
- utils.mergeJestConfig({
55
- globals: {
56
- [modernjs_config_key]: userConfig,
57
- },
58
- moduleNameMapper: getModuleNameMapper(webpackConfig),
59
- testEnvironment: 'jsdom',
60
- resolver: require.resolve('../resolver'),
61
- });
62
-
63
- utils.setJestConfig({
64
- rootDir: pwd || process.cwd(),
65
- // todo: diffrent test root for diffrent solutions
66
- // testMatch: [`<rootDir>/(src|tests|electron)/**/*.test.[jt]s?(x)`],
67
- // testMatch bug on windows, issue: https://github.com/facebook/jest/issues/7914
68
- testMatch: [
69
- `<rootDir>/src/**/*.test.[jt]s?(x)`,
70
- `<rootDir>/tests/**/*.test.[jt]s?(x)`,
71
- `<rootDir>/electron/**/*.test.[jt]s?(x)`,
72
- ],
73
- });
74
-
75
- mergeUserJestConfig(utils);
76
-
77
- return next(utils);
78
- },
79
- }),
80
- {
81
- name: '@modern-js/testing-plugin-modern',
82
- },
83
- ) as any;
@@ -1,16 +0,0 @@
1
- import enhanceResolve from 'enhanced-resolve';
2
-
3
- const resolver = enhanceResolve.create.sync({
4
- conditionNames: ['jsnext:source', 'require', 'node', 'default'],
5
- extensions: ['.js', '.json', '.node', '.ts', '.tsx'],
6
- });
7
-
8
- const shouldResolveByEnhance = (url: string) => /^@[^/]+\/[^/]+\/.*/.test(url);
9
-
10
- module.exports = function (request: string, options: any) {
11
- if (shouldResolveByEnhance(request)) {
12
- return resolver(options.basedir, request);
13
- }
14
-
15
- return options.defaultResolver(request, options);
16
- };
package/src/cli/test.ts DELETED
@@ -1,77 +0,0 @@
1
- import path from 'path';
2
- import { compiler } from '@modern-js/babel-compiler';
3
- import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
4
- import { runTest } from '@modern-js/testing';
5
- import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
6
- import testingBffPlugin from '@modern-js/testing-plugin-bff';
7
- import modernTestPlugin from './plugins/modern';
8
-
9
- const test = async () => {
10
- // eslint-disable-next-line react-hooks/rules-of-hooks
11
- const userConfig = useResolvedConfigContext();
12
- // eslint-disable-next-line react-hooks/rules-of-hooks
13
- const config = useAppContext();
14
-
15
- // todo: consider lib-tools ...
16
- const webpackConfigs = getWebpackConfig(WebpackConfigTarget.CLIENT);
17
-
18
- userConfig.testing = userConfig.testing || {};
19
-
20
- const jest = userConfig.testing.jest || (userConfig.tools as any).jest;
21
-
22
- if (Array.isArray(jest)) {
23
- userConfig.testing.jest = jest[0];
24
- }
25
-
26
- userConfig.testing.jest =
27
- userConfig.testing.jest || (userConfig.tools as any).jest;
28
-
29
- userConfig.testing.plugins = [
30
- ...(userConfig.testing.plugins || []),
31
- modernTestPlugin(webpackConfigs, userConfig, config.appDirectory),
32
- testingBffPlugin({
33
- pwd: config.appDirectory,
34
- userConfig,
35
- plugins: config.plugins.map(p => p.server).filter(Boolean),
36
- routes: (config as any).serverRoutes,
37
- }),
38
- ];
39
-
40
- const runtimeExportsPath = path.join(
41
- config.internalDirectory,
42
- '.runtime-exports',
43
- );
44
-
45
- await compiler(
46
- {
47
- sourceDir: runtimeExportsPath,
48
- rootDir: runtimeExportsPath,
49
- distDir: runtimeExportsPath,
50
- quiet: true,
51
- },
52
- {
53
- presets: [
54
- [
55
- require.resolve('@babel/preset-env'),
56
- {
57
- modules: 'cjs',
58
- },
59
- ],
60
- ],
61
- },
62
- );
63
-
64
- await runTest(userConfig.testing);
65
- };
66
-
67
- export default test;
68
-
69
- declare module '@modern-js/core' {
70
- interface UserConfig {
71
- testing?: import('@modern-js/testing').TestConfig;
72
- }
73
-
74
- interface ToolsConfig {
75
- jest?: import('@modern-js/testing').TestConfig['jest'];
76
- }
77
- }
package/src/constant.ts DELETED
@@ -1 +0,0 @@
1
- export const modernjs_config_key = '__modernjs_config__';
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- import '@testing-library/jest-dom/extend-expect';
2
- import '@testing-library/jest-dom';
3
-
4
- export * from './runtime-testing';
@@ -1,48 +0,0 @@
1
- import React from 'react';
2
- import { createApp } from '@modern-js/runtime-core';
3
- import { UserConfig } from '@modern-js/core';
4
- import { modernjs_config_key } from '../constant';
5
- import resolvePlugins from './resolvePlugins';
6
-
7
- interface CreateAppProps {
8
- entry?: string;
9
- children?: React.ReactElement;
10
- }
11
-
12
- class ModernRuntime {
13
- private options: UserConfig;
14
-
15
- constructor(options: UserConfig) {
16
- this.options = options;
17
- }
18
-
19
- public init(options: UserConfig) {
20
- this.options = options;
21
- }
22
-
23
- public createApp(props?: CreateAppProps) {
24
- const { entry, children } = props || {};
25
- let runtimeFeatures = this.options?.runtime;
26
-
27
- if (entry) {
28
- runtimeFeatures = {
29
- ...(runtimeFeatures || {}),
30
- ...this.options.runtimeByEntries?.[entry],
31
- };
32
- }
33
-
34
- const Component = (): React.ReactElement | null => {
35
- if (!children) {
36
- return null;
37
- }
38
-
39
- return children;
40
- };
41
-
42
- return createApp({
43
- plugins: resolvePlugins(runtimeFeatures || {}),
44
- })(Component);
45
- }
46
- }
47
-
48
- export default new ModernRuntime((global as any)[modernjs_config_key] || {});
@@ -1,11 +0,0 @@
1
- import React from 'react';
2
- import { render } from '@testing-library/react';
3
- import app from './app';
4
-
5
- const WrapModernProviders = (props: any) =>
6
- React.createElement(app.createApp(props));
7
-
8
- const customRender = (ui: React.ReactElement, options: any) =>
9
- render(ui, { wrapper: WrapModernProviders, ...options });
10
-
11
- export default customRender as typeof render;
@@ -1,38 +0,0 @@
1
- import { createStore as originCreateStore } from '@modern-js-reduck/store';
2
- import effectsPlugin from '@modern-js-reduck/plugin-effects';
3
- import autoActionsPlugin from '@modern-js-reduck/plugin-auto-actions';
4
- import immerPlugin from '@modern-js-reduck/plugin-immutable';
5
- import { modernjs_config_key } from '../constant';
6
-
7
- export const effects = () => effectsPlugin;
8
- export const immer = () => immerPlugin;
9
- export const autoActions = () => autoActionsPlugin;
10
-
11
- export const createStore: typeof originCreateStore = props => {
12
- const createStatePlugins = () => {
13
- const modernConfig = (global as any)[modernjs_config_key];
14
- const stateConfig = modernConfig?.runtime?.state;
15
- const plugins = [];
16
-
17
- if (stateConfig?.effects !== false) {
18
- plugins.push(effects());
19
- }
20
-
21
- if (stateConfig?.autoActions !== false) {
22
- plugins.push(autoActions());
23
- }
24
-
25
- if (stateConfig?.immer !== false) {
26
- plugins.push(immer());
27
- }
28
-
29
- return (props?.plugins || []).concat(plugins);
30
- };
31
-
32
- const config = {
33
- ...(props || {}),
34
- plugins: createStatePlugins(),
35
- };
36
-
37
- return originCreateStore(config);
38
- };