@modern-js/plugin-garfish 1.4.2 → 1.4.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.
@@ -1,4 +1,5 @@
1
1
  import garfish, { interfaces as GarfishInterfaces } from 'garfish';
2
+ import React from 'react';
2
3
  export declare type Options = typeof garfish.options;
3
4
  export declare type ModuleInfo = GarfishInterfaces.AppInfo & {
4
5
  Component?: React.ComponentType | React.ElementType;
@@ -29,10 +30,9 @@ export declare type MicroComponentProps = {
29
30
  loadable?: LoadableConfig;
30
31
  };
31
32
  export declare type Config = Partial<Options> & ModernGarfishConfig;
32
- export declare function useModuleApps(): Record<string, import("react").ComponentType<any>>;
33
- export declare function useModuleApp(): import("react").FC<any>;
34
- export declare function useMicroApps(): {
35
- MApp: import("react").FC<any>;
36
- apps: ModulesInfo;
37
- Components: Record<string, import("react").ComponentType<any>>;
38
- };
33
+ export declare type UseModuleApps = { [index in 'apps' | string]: index extends 'apps' ? ModulesInfo : React.FC<any> } & {
34
+ readonly MApp: React.FC<any>;
35
+ readonly apps: ModulesInfo;
36
+ };
37
+ export declare function useModuleApps(): UseModuleApps;
38
+ export declare function useModuleApp(): React.FC<any>;
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  import { ModulesInfo } from '../useModuleApps';
3
3
  export interface GarfishContextType {
4
4
  MApp: React.FC<any>;
5
- apps: Record<string, React.ComponentType<any>>;
5
+ apps: Record<string, React.FC<any>>;
6
6
  appInfoList: ModulesInfo;
7
7
  }
8
8
  declare const GarfishContext: React.Context<GarfishContextType>;
@@ -1,8 +1,8 @@
1
1
  import React from 'react';
2
2
  import Garfish from 'garfish';
3
- import { Manifest, MicroComponentProps, ModulesInfo } from '../useModuleApps';
3
+ import { Manifest, ModulesInfo } from '../useModuleApps';
4
4
  export interface AppMap {
5
- [key: string]: React.ComponentType<MicroComponentProps>;
5
+ [key: string]: React.FC<any>;
6
6
  }
7
7
  export declare function generateApps(options: typeof Garfish.options, manifest?: Manifest): {
8
8
  apps: AppMap;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.2",
14
+ "version": "1.4.3",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/runtime/index.d.ts",
17
17
  "typesVersions": {
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@babel/runtime": "^7",
49
- "@modern-js/utils": "^1.3.4",
49
+ "@modern-js/utils": "^1.3.5",
50
50
  "@types/debug": "^4.1.7",
51
51
  "@types/react-loadable": "^5.5.6",
52
52
  "debug": "^4.3.2",
@@ -55,10 +55,10 @@
55
55
  "react-loadable": "^5.5.0"
56
56
  },
57
57
  "devDependencies": {
58
- "@modern-js/core": "^1.4.6",
58
+ "@modern-js/core": "^1.5.0",
59
59
  "@modern-js/plugin-router": "^1.2.5",
60
60
  "@modern-js/runtime-core": "^1.2.4",
61
- "@modern-js/types": "^1.3.4",
61
+ "@modern-js/types": "^1.3.5",
62
62
  "@scripts/build": "0.0.0",
63
63
  "@scripts/jest-config": "0.0.0",
64
64
  "@testing-library/jest-dom": "^5.16.1",
@@ -79,7 +79,6 @@
79
79
  "webpack-chain": "^6.5.1"
80
80
  },
81
81
  "peerDependencies": {
82
- "@modern-js/core": "^1.4.6",
83
82
  "@modern-js/plugin-router": "^1.2.5",
84
83
  "@modern-js/runtime-core": "^1.2.4"
85
84
  },
@@ -0,0 +1,82 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`plugin-garfish cli cli get runtime config 1`] = `
4
+ Object {
5
+ "masterApp": Object {
6
+ "basename": "/test",
7
+ },
8
+ }
9
+ `;
10
+
11
+ exports[`plugin-garfish cli cli get runtime features config 1`] = `
12
+ Object {
13
+ "masterApp": Object {
14
+ "basename": "/test2",
15
+ },
16
+ }
17
+ `;
18
+
19
+ exports[`plugin-garfish cli cli makeRender function 1`] = `
20
+ Object {
21
+ "renderByProvider": true,
22
+ "routerConfig": Object {
23
+ "basename": "/test",
24
+ "historyOptions": Object {
25
+ "basename": "/test",
26
+ },
27
+ },
28
+ }
29
+ `;
30
+
31
+ exports[`plugin-garfish cli cli set runtime config 1`] = `
32
+ Object {
33
+ "masterApp": true,
34
+ }
35
+ `;
36
+
37
+ exports[`plugin-garfish cli cli set runtime features config 1`] = `
38
+ Object {
39
+ "features": Object {
40
+ "masterApp": true,
41
+ },
42
+ }
43
+ `;
44
+
45
+ exports[`plugin-garfish cli webpack config close external and use js entry 1`] = `
46
+ Object {
47
+ "devServer": Object {
48
+ "headers": Object {
49
+ "Access-Control-Allow-Origin": "*",
50
+ },
51
+ },
52
+ "externals": Object {
53
+ "react": "react",
54
+ "react-dom": "react-dom",
55
+ },
56
+ "optimization": Object {
57
+ "runtimeChunk": false,
58
+ "splitChunks": Object {
59
+ "chunks": "async",
60
+ },
61
+ },
62
+ "output": Object {
63
+ "filename": "index.js",
64
+ "libraryTarget": "umd",
65
+ "publicPath": "//localhost:8080/",
66
+ },
67
+ }
68
+ `;
69
+
70
+ exports[`plugin-garfish cli webpack config default micro config 1`] = `
71
+ Object {
72
+ "devServer": Object {
73
+ "headers": Object {
74
+ "Access-Control-Allow-Origin": "*",
75
+ },
76
+ },
77
+ "output": Object {
78
+ "libraryTarget": "umd",
79
+ "publicPath": "//localhost:8080/",
80
+ },
81
+ }
82
+ `;
@@ -1,87 +1,41 @@
1
1
  import '@testing-library/jest-dom';
2
- import { manager, useAppContext } from '@modern-js/core';
2
+ import { manager } from '@modern-js/core';
3
3
  import WebpackChain from 'webpack-chain';
4
- import GarfishPlugin, { externals, resolvedConfig } from '../src/cli';
4
+ import GarfishPlugin, { externals } from '../src/cli';
5
5
  import { getRuntimeConfig, makeRenderFunction, setRuntimeConfig } from '../src/cli/utils';
6
6
 
7
- const mock_config_context = {
8
- context: {},
9
- get() {
10
- return this.context;
11
- },
12
- set(newContext: any) {
13
- Object.assign(this.context, newContext);
14
- },
15
- recover(newContext: any) {
16
- this.context = newContext;
17
- }
18
- };
19
-
20
- jest.mock('@modern-js/core', () => {
21
- const originalModule = jest.requireActual('@modern-js/core');
22
- return {
23
- __esModule: true,
24
- ...originalModule,
25
- useResolvedConfigContext: ()=>{
26
- return mock_config_context.get();
27
- }
28
- };
29
- });
30
-
31
-
32
7
  describe('plugin-garfish cli', () => {
33
8
  test('cli garfish basename', async () => {
34
- expect(GarfishPlugin.name).toBe('@modern-js/plugin-garfish');
35
- const basename = '/test';
36
- const resolveConfig = {
9
+ expect(GarfishPlugin().name).toBe('@modern-js/plugin-garfish');
10
+
11
+ const main = manager.clone().usePlugin(GarfishPlugin);
12
+ const runner = await main.init();
13
+ await runner.prepare();
14
+ const configHistoryOptions: any = await runner.resolvedConfig({
37
15
  resolved: {
38
16
  runtime: {
39
17
  router: {
40
- historyOptions: { basename }
18
+ historyOptions: { basename: '/test' }
41
19
  },
42
20
  masterApp: {}
43
21
  },
44
22
  }
45
- };
46
- const config = await resolvedConfig(resolveConfig as any);
47
- expect(config.resolved.runtime.masterApp.basename).toBe(basename);
48
- });
49
-
50
- test('cli makeRender function', ()=>{
51
- const code = `
52
- const router = (config)=> config;
53
- const App = {};
54
- const routerConfig = router({...App?.config?.router, ...App?.config?.features?.router});
55
- const resultConfig = {
56
- routerConfig,
57
- renderByProvider: true,
58
- };
23
+ } as any);
59
24
 
60
- if (IS_BROWSER) {
61
- resultConfig.renderByProvider = false;
62
- }
63
- return resultConfig;
64
- `;
65
- const generateNewRenderFn = new Function('appInfo', 'IS_BROWSER', '__GARFISH_EXPORTS__', makeRenderFunction(code));
66
-
67
- // render byGarfish but don't provider appInfo
68
- expect(generateNewRenderFn(undefined, true, false)).toBe(null);
25
+ expect(configHistoryOptions.resolved.runtime.masterApp.basename).toBe('/test');
69
26
 
70
- // run alone
71
- expect(generateNewRenderFn(undefined, true)).toMatchObject({
72
- renderByProvider: false
73
- });
74
-
75
- // render ByGarfish and provider appInfo
76
- expect(generateNewRenderFn({ basename: '/test' }, true, true)).toMatchObject({
77
- renderByProvider: true,
78
- routerConfig: {
79
- basename: '/test',
80
- historyOptions: {
81
- basename: '/test'
82
- }
27
+ const configHistory: any = await runner.resolvedConfig({
28
+ resolved: {
29
+ runtime: {
30
+ router: {
31
+ basename: '/test2'
32
+ },
33
+ masterApp: {}
34
+ },
83
35
  }
84
- });
36
+ } as any);
37
+
38
+ expect(configHistory.resolved.runtime.masterApp.basename).toBe('/test2');
85
39
  });
86
40
 
87
41
  test('cli get runtime config', ()=>{
@@ -92,11 +46,7 @@ describe('plugin-garfish cli', () => {
92
46
  }
93
47
  }
94
48
  });
95
- expect(runtimeConfig).toMatchObject({
96
- masterApp: {
97
- basename: '/test'
98
- }
99
- });
49
+ expect(runtimeConfig).toMatchSnapshot();
100
50
  });
101
51
 
102
52
  test('cli get runtime features config', ()=>{
@@ -113,11 +63,7 @@ describe('plugin-garfish cli', () => {
113
63
  }
114
64
  });
115
65
 
116
- expect(runtimeConfig).toMatchObject({
117
- masterApp: {
118
- basename: '/test2'
119
- }
120
- });
66
+ expect(runtimeConfig).toMatchSnapshot();
121
67
  });
122
68
 
123
69
  test('cli set runtime config', ()=>{
@@ -131,7 +77,7 @@ describe('plugin-garfish cli', () => {
131
77
 
132
78
  setRuntimeConfig(runtimeConfig, 'masterApp', true);
133
79
 
134
- expect(runtimeConfig.runtime.masterApp).toBe(true);
80
+ expect(runtimeConfig.runtime).toMatchSnapshot();
135
81
  });
136
82
 
137
83
  test('cli set runtime features config', ()=>{
@@ -147,16 +93,41 @@ describe('plugin-garfish cli', () => {
147
93
 
148
94
  setRuntimeConfig(runtimeConfig, 'masterApp', true);
149
95
 
150
- expect(runtimeConfig.runtime.features.masterApp).toBe(true);
96
+ expect(runtimeConfig.runtime).toMatchSnapshot();
151
97
  });
152
98
 
99
+ test('cli makeRender function', ()=>{
100
+ const code = `
101
+ const router = (config)=> config;
102
+ const App = {};
103
+ const routerConfig = router({...App?.config?.router, ...App?.config?.features?.router});
104
+ const resultConfig = {
105
+ routerConfig,
106
+ renderByProvider: true,
107
+ };
108
+
109
+ if (IS_BROWSER) {
110
+ resultConfig.renderByProvider = false;
111
+ }
112
+ return resultConfig;
113
+ `;
114
+ const generateNewRenderFn = new Function('appInfo', 'IS_BROWSER', '__GARFISH_EXPORTS__', makeRenderFunction(code));
115
+
116
+ // render byGarfish but don't provider appInfo
117
+ expect(generateNewRenderFn(undefined, true, false)).toBe(null);
118
+
119
+ // run alone
120
+ expect(generateNewRenderFn(undefined, true)).toMatchObject({
121
+ renderByProvider: false
122
+ });
123
+
124
+ // render ByGarfish and provider appInfo
125
+ expect(generateNewRenderFn({ basename: '/test' }, true, true)).toMatchSnapshot();
126
+ });
127
+
128
+
153
129
  test('webpack config close external and use js entry', async ()=>{
154
- const main = manager.clone().usePlugin(GarfishPlugin);
155
- const runner = await main.init();
156
- await runner.prepare();
157
- const config: any = await runner.config();
158
- const webpackConfig = new WebpackChain();
159
- mock_config_context.recover({
130
+ const resolveConfig: any = {
160
131
  deploy: {
161
132
  microFrontend: {
162
133
  externalBasicLibrary: true,
@@ -164,9 +135,18 @@ describe('plugin-garfish cli', () => {
164
135
  },
165
136
  },
166
137
  server: {
167
- port: '8080'
138
+ port: 8080
168
139
  }
169
- });
140
+ };
141
+
142
+ const main = manager.clone({
143
+ useResolvedConfigContext: ()=>resolveConfig
144
+ }).usePlugin(GarfishPlugin);
145
+
146
+ const runner = await main.init();
147
+ await runner.prepare();
148
+ const config: any = await runner.config();
149
+ const webpackConfig = new WebpackChain();
170
150
 
171
151
  config[0].tools.webpack({}, {
172
152
  chain: webpackConfig,
@@ -175,6 +155,7 @@ describe('plugin-garfish cli', () => {
175
155
  });
176
156
 
177
157
  const generateConfig = webpackConfig.toConfig();
158
+ expect(generateConfig).toMatchSnapshot();
178
159
  expect(generateConfig).toMatchObject({
179
160
  output: {
180
161
  libraryTarget: 'umd',
@@ -187,20 +168,22 @@ describe('plugin-garfish cli', () => {
187
168
  })
188
169
 
189
170
  test('webpack config default micro config', async ()=>{
190
- const main = manager.clone().usePlugin(GarfishPlugin);
191
- const runner = await main.init();
192
- await runner.prepare();
193
- const config: any = await runner.config();
194
- const webpackConfig = new WebpackChain();
195
-
196
- mock_config_context.recover({
171
+ const resolveConfig: any = {
197
172
  deploy: {
198
173
  microFrontend: true,
199
174
  },
200
175
  server: {
201
176
  port: '8080'
202
177
  }
203
- });
178
+ };
179
+
180
+ const main = manager.clone({
181
+ useResolvedConfigContext: ()=>resolveConfig
182
+ }).usePlugin(GarfishPlugin);
183
+ const runner = await main.init();
184
+ await runner.prepare();
185
+ const config: any = await runner.config();
186
+ const webpackConfig = new WebpackChain();
204
187
 
205
188
  config[0].tools.webpack({}, {
206
189
  chain: webpackConfig,
@@ -209,6 +192,7 @@ describe('plugin-garfish cli', () => {
209
192
  });
210
193
 
211
194
  const generateConfig = webpackConfig.toConfig();
195
+ expect(generateConfig).toMatchSnapshot();
212
196
  expect(generateConfig).toMatchObject({
213
197
  output: {
214
198
  libraryTarget: 'umd',
@@ -4,7 +4,6 @@ import { createApp } from '@modern-js/runtime-core';
4
4
  import '@testing-library/jest-dom';
5
5
 
6
6
  import ModernGarfishPlugin, {
7
- useMicroApps,
8
7
  useModuleApp,
9
8
  useModuleApps,
10
9
  } from '../src/runtime';
@@ -33,13 +32,8 @@ describe('plugin-garfish', () => {
33
32
  };
34
33
 
35
34
  const App = () => {
36
- const { MApp, apps, Components } = useMicroApps();
37
- const { Dashboard, TableList } = Components;
38
- const NMApp = useModuleApp();
39
- const NApps = useModuleApps();
35
+ const { MApp, apps, Dashboard, TableList } = useModuleApps();
40
36
 
41
- expect(NMApp === MApp).toBeTruthy();
42
- expect(Components === NApps).toBeTruthy();
43
37
 
44
38
  if (apps.length > 0) {
45
39
  expect(apps[0].name).toBe(tableListModuleInfo.name);
@@ -12,8 +12,8 @@ import {
12
12
  useLocation,
13
13
  MemoryRouter,
14
14
  } from '@modern-js/plugin-router';
15
- import ModernGarfishPlugin from '../src/runtime';
16
- import { useMicroApps } from '../src';
15
+ import ModernGarfishPlugin, { useModuleApp } from '../src/runtime';
16
+ import { useModuleApps } from '../src';
17
17
  import {
18
18
  TABLE_LIST_ESCAPE_NODE,
19
19
  TABLE_LIST_HTML,
@@ -82,7 +82,7 @@ describe('plugin-garfish', () => {
82
82
  });
83
83
 
84
84
  // testing-library.com/docs/example-react-router/
85
- test('legacyModule hooks Components', async () => {
85
+ test('useModuleApps hooks', async () => {
86
86
  const dashBoardModuleInfo = {
87
87
  name: 'Dashboard',
88
88
  entry: dashboardPath,
@@ -111,9 +111,7 @@ describe('plugin-garfish', () => {
111
111
  const App = () => {
112
112
  const HomeTitle = 'Micro home page';
113
113
  const Home = () => <div data-testid="home-title">{HomeTitle}</div>;
114
- const {
115
- Components: { Dashboard, TableList },
116
- } = useMicroApps();
114
+ const { Dashboard, TableList } = useModuleApps();
117
115
 
118
116
  const LocationDisplay = () => {
119
117
  const location = useLocation();
@@ -200,7 +198,9 @@ describe('plugin-garfish', () => {
200
198
  };
201
199
 
202
200
  const App = () => {
203
- const { MApp } = useMicroApps();
201
+ const { MApp } = useModuleApps();
202
+ const NMApp = useModuleApp();
203
+ expect(MApp).toBe(NMApp);
204
204
 
205
205
  return (
206
206
  <MemoryRouter>