@modern-js/plugin-state 1.1.4 → 1.2.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/src/cli/index.ts DELETED
@@ -1,144 +0,0 @@
1
- import path from 'path';
2
- import {
3
- getEntryOptions,
4
- createRuntimeExportsUtils,
5
- PLUGIN_SCHEMAS,
6
- } from '@modern-js/utils';
7
- import {
8
- createPlugin,
9
- useAppContext,
10
- useResolvedConfigContext,
11
- } from '@modern-js/core';
12
- import {} from '../types';
13
-
14
- const PLUGIN_IDENTIFIER = 'state';
15
-
16
- const index = createPlugin(
17
- (() => {
18
- const stateConfigMap = new Map<string, any>();
19
-
20
- let pluginsExportsUtils: any;
21
- const stateModulePath = path.resolve(__dirname, '../../../../');
22
-
23
- return {
24
- config() {
25
- // eslint-disable-next-line react-hooks/rules-of-hooks
26
- const appContext = useAppContext();
27
-
28
- pluginsExportsUtils = createRuntimeExportsUtils(
29
- appContext.internalDirectory,
30
- 'plugins',
31
- );
32
-
33
- return {
34
- source: {
35
- alias: {
36
- '@modern-js/runtime/plugins': pluginsExportsUtils.getPath(),
37
- },
38
- },
39
- };
40
- },
41
- modifyEntryImports({ entrypoint, imports }: any) {
42
- const { entryName } = entrypoint;
43
- // eslint-disable-next-line react-hooks/rules-of-hooks
44
- const userConfig = useResolvedConfigContext();
45
- // eslint-disable-next-line react-hooks/rules-of-hooks
46
- const { packageName } = useAppContext();
47
-
48
- const stateConfig = getEntryOptions(
49
- entryName,
50
- userConfig.runtime,
51
- userConfig.runtimeByEntries,
52
- packageName,
53
- )?.state;
54
-
55
- stateConfigMap.set(entryName, stateConfig);
56
-
57
- const getEnabledPlugins = () => {
58
- const internalPlugins = [
59
- 'immer',
60
- 'effects',
61
- 'autoActions',
62
- 'devtools',
63
- ];
64
-
65
- return internalPlugins.filter(name => stateConfig[name] !== false);
66
- };
67
-
68
- if (stateConfig) {
69
- imports.push({
70
- value: '@modern-js/runtime/plugins',
71
- specifiers: [
72
- {
73
- imported: PLUGIN_IDENTIFIER,
74
- },
75
- ],
76
- });
77
- imports.push({
78
- value: '@modern-js/runtime/model',
79
- specifiers: getEnabledPlugins().map(imported => ({ imported })),
80
- initialize: `
81
- const createStatePlugins = (config) => {
82
- const plugins = [];
83
-
84
- ${getEnabledPlugins()
85
- .map(
86
- name => `
87
- plugins.push(${name}(config['${name}']));
88
- `,
89
- )
90
- .join('\n')}
91
-
92
- return plugins;
93
- }
94
- `,
95
- });
96
- }
97
-
98
- return {
99
- entrypoint,
100
- imports,
101
- };
102
- },
103
-
104
- modifyEntryRuntimePlugins({ entrypoint, plugins }: any) {
105
- const stateOptions = stateConfigMap.get(entrypoint.entryName);
106
-
107
- if (stateOptions) {
108
- const isBoolean = typeof stateOptions === 'boolean';
109
-
110
- let options = isBoolean ? '{}' : JSON.stringify(stateOptions);
111
-
112
- options = `${options.substr(0, options.length - 1)}${
113
- isBoolean ? '' : ','
114
- }plugins: createStatePlugins(${JSON.stringify(
115
- stateConfigMap.get(entrypoint.entryName),
116
- )})}`;
117
-
118
- plugins.push({
119
- name: PLUGIN_IDENTIFIER,
120
- options,
121
- });
122
- }
123
- return {
124
- entrypoint,
125
- plugins,
126
- };
127
- },
128
- validateSchema() {
129
- return PLUGIN_SCHEMAS['@modern-js/plugin-state'];
130
- },
131
- addRuntimeExports() {
132
- pluginsExportsUtils.addExport(
133
- `export { default as state } from '${stateModulePath}'`,
134
- );
135
- },
136
- };
137
- }) as any,
138
- {
139
- name: '@modern-js/plugin-state',
140
- required: ['@modern-js/runtime'],
141
- },
142
- );
143
-
144
- export default index;
package/src/plugins.ts DELETED
@@ -1,9 +0,0 @@
1
- import { plugin as effectsPlugin } from '@modern-js-reduck/plugin-effects';
2
- import autoActionsPlugin from '@modern-js-reduck/plugin-auto-actions';
3
- import immerPlugin from '@modern-js-reduck/plugin-immutable';
4
-
5
- export { default as devtools } from '@modern-js-reduck/plugin-devtools';
6
-
7
- export const effects = () => effectsPlugin;
8
- export const immer = () => immerPlugin;
9
- export const autoActions = () => autoActionsPlugin;
@@ -1,9 +0,0 @@
1
- // eslint-disable-next-line filenames/match-exported
2
- import statePlugin from './plugin';
3
-
4
- export * from '@modern-js-reduck/react';
5
- export { model, createStore } from '@modern-js-reduck/store';
6
-
7
- export * from './plugin';
8
-
9
- export default statePlugin;
@@ -1,73 +0,0 @@
1
- // eslint-disable-next-line filenames/match-exported
2
- import { useContext } from 'react';
3
- import { createPlugin, RuntimeReactContext } from '@modern-js/runtime-core';
4
- import { createStore, Store } from '@modern-js-reduck/store';
5
- import { Provider } from '@modern-js-reduck/react';
6
- import hoistNonReactStatics from 'hoist-non-react-statics';
7
-
8
- declare module '@modern-js/runtime-core' {
9
- interface RuntimeContext {
10
- store: Store;
11
- }
12
-
13
- interface TRuntimeContext {
14
- store: Store;
15
- }
16
-
17
- interface SSRData {
18
- storeState: any;
19
- }
20
- }
21
-
22
- type PluginProps = Parameters<typeof createStore>[0];
23
-
24
- const state = (config: PluginProps) =>
25
- createPlugin(
26
- () => ({
27
- hoc({ App }, next) {
28
- const getStateApp = (props: any) => {
29
- // eslint-disable-next-line react-hooks/rules-of-hooks
30
- const context = useContext(RuntimeReactContext);
31
-
32
- return (
33
- <Provider store={context.store} config={config}>
34
- <App {...props} />
35
- </Provider>
36
- );
37
- };
38
- return next({
39
- App: hoistNonReactStatics(getStateApp, App),
40
- });
41
- },
42
- init({ context }, next) {
43
- const storeConfig = config || {};
44
-
45
- if (typeof window !== 'undefined') {
46
- storeConfig.initialState =
47
- storeConfig.initialState ||
48
- window?._SSR_DATA?.data?.storeState ||
49
- {};
50
- }
51
-
52
- context.store = createStore(storeConfig);
53
-
54
- next({ context });
55
- },
56
- pickContext({ context, pickedContext }, next) {
57
- return next({
58
- context,
59
- pickedContext: {
60
- ...pickedContext,
61
- store: context.store,
62
- },
63
- });
64
- },
65
- }),
66
- {
67
- name: '@modern-js/plugin-state',
68
- },
69
- );
70
-
71
- export default state;
72
-
73
- export * from '../plugins';
package/src/types.ts DELETED
@@ -1,19 +0,0 @@
1
- import { createStore } from '@modern-js-reduck/store';
2
- import { ReduxLoggerOptions } from 'redux-logger';
3
-
4
- export type PluginOptions = Parameters<typeof createStore>[0] & {
5
- /**
6
- * If it's false, the logger will be disabled.
7
- * If it's a object, it means options will pass to createLogger function
8
- */
9
- logger?: false | ReduxLoggerOptions;
10
- // todo: achive effects params
11
- effects?: any;
12
- /**
13
- * Default: false
14
- * When it's true, will remove immer plugin
15
- */
16
- disableImmer?: boolean;
17
- // todo: achive devtools params
18
- devtools?: any;
19
- };