@modern-js/plugin-testing 1.0.0-rc.2
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/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/js/modern/cli/index.js +43 -0
- package/dist/js/modern/cli/plugins/modern.js +44 -0
- package/dist/js/modern/cli/resolver.js +15 -0
- package/dist/js/modern/cli/test.js +43 -0
- package/dist/js/modern/constant.js +1 -0
- package/dist/js/modern/index.js +2 -0
- package/dist/js/modern/runtime-testing/app.js +51 -0
- package/dist/js/modern/runtime-testing/customRender.js +17 -0
- package/dist/js/modern/runtime-testing/index.js +2 -0
- package/dist/js/modern/runtime-testing/resolvePlugins.js +23 -0
- package/dist/js/node/cli/index.js +59 -0
- package/dist/js/node/cli/plugins/modern.js +59 -0
- package/dist/js/node/cli/resolver.js +20 -0
- package/dist/js/node/cli/test.js +60 -0
- package/dist/js/node/constant.js +8 -0
- package/dist/js/node/index.js +20 -0
- package/dist/js/node/runtime-testing/app.js +64 -0
- package/dist/js/node/runtime-testing/customRender.js +29 -0
- package/dist/js/node/runtime-testing/index.js +32 -0
- package/dist/js/node/runtime-testing/resolvePlugins.js +31 -0
- package/dist/js/treeshaking/cli/index.js +54 -0
- package/dist/js/treeshaking/cli/plugins/modern.js +47 -0
- package/dist/js/treeshaking/cli/resolver.js +17 -0
- package/dist/js/treeshaking/cli/test.js +78 -0
- package/dist/js/treeshaking/constant.js +1 -0
- package/dist/js/treeshaking/index.js +2 -0
- package/dist/js/treeshaking/runtime-testing/app.js +64 -0
- package/dist/js/treeshaking/runtime-testing/customRender.js +21 -0
- package/dist/js/treeshaking/runtime-testing/index.js +2 -0
- package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +23 -0
- package/dist/types/cli/index.d.ts +3 -0
- package/dist/types/cli/plugins/modern.d.ts +3 -0
- package/dist/types/cli/resolver.d.ts +1 -0
- package/dist/types/cli/test.d.ts +8 -0
- package/dist/types/constant.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/runtime-testing/app.d.ts +17 -0
- package/dist/types/runtime-testing/customRender.d.ts +5 -0
- package/dist/types/runtime-testing/index.d.ts +2 -0
- package/dist/types/runtime-testing/resolvePlugins.d.ts +2 -0
- package/modern.config.js +2 -0
- package/package.json +44 -0
- package/src/.eslintrc.json +3 -0
- package/src/cli/index.ts +48 -0
- package/src/cli/plugins/modern.ts +50 -0
- package/src/cli/resolver.ts +16 -0
- package/src/cli/test.ts +66 -0
- package/src/constant.ts +1 -0
- package/src/index.ts +3 -0
- package/src/runtime-testing/app.ts +48 -0
- package/src/runtime-testing/customRender.ts +11 -0
- package/src/runtime-testing/index.ts +3 -0
- package/src/runtime-testing/resolvePlugins.ts +23 -0
- package/tsconfig.json +12 -0
- package/type.d.ts +4 -0
package/src/index.ts
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { createApp } from '@modern-js/runtime-core';
|
3
|
+
import { UserConfig } from '@modern-js/core';
|
4
|
+
import resolvePlugins from './resolvePlugins';
|
5
|
+
import { modernjs_config_key } from '@/constant';
|
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] || {});
|
@@ -0,0 +1,11 @@
|
|
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;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { NormalizedConfig } from '@modern-js/core';
|
2
|
+
|
3
|
+
const allowedFeatures = ['router', 'state'];
|
4
|
+
|
5
|
+
export default function resolvePlugins(features: NormalizedConfig['runtime']) {
|
6
|
+
const plugins: any[] = [];
|
7
|
+
|
8
|
+
if (!features) {
|
9
|
+
return plugins;
|
10
|
+
}
|
11
|
+
|
12
|
+
Object.keys(features).forEach(feature => {
|
13
|
+
if (allowedFeatures.includes(feature)) {
|
14
|
+
const curPluginRes = require(`@modern-js/runtime/plugins`)[feature]({
|
15
|
+
...features[feature],
|
16
|
+
});
|
17
|
+
|
18
|
+
plugins.push(curPluginRes);
|
19
|
+
}
|
20
|
+
});
|
21
|
+
|
22
|
+
return plugins;
|
23
|
+
}
|
package/tsconfig.json
ADDED
package/type.d.ts
ADDED