@modern-js/plugin-garfish 1.4.5 → 1.4.8

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/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  # @modern-js/plugin-garfish
2
2
 
3
+ ## 1.4.8
4
+
5
+ ### Patch Changes
6
+
7
+ - 36e702f8: fix(plugin-garfish): fix useModules component props type and cors config
8
+ - 04ae5262: chore: bump @modern-js/utils to v1.4.1 in dependencies
9
+ - 60f7d8bf: feat: add tests dir to npmignore
10
+ - e4cec1ce: types: fix config hook type
11
+ - d558d4b5: fix: should not import from @modern-js/utils
12
+ - Updated dependencies [b8599d09]
13
+ - Updated dependencies [6cffe99d]
14
+ - Updated dependencies [04ae5262]
15
+ - Updated dependencies [60f7d8bf]
16
+ - Updated dependencies [3bf4f8b0]
17
+ - @modern-js/utils@1.5.0
18
+ - @modern-js/plugin-router@1.2.10
19
+ - @modern-js/runtime-core@1.4.2
20
+
21
+ ## 1.4.6
22
+
23
+ ### Patch Changes
24
+
25
+ - 17d0cc46: feat: prebundle lodash to @modern-js/utils/lodash
26
+ - Updated dependencies [485375ae]
27
+ - Updated dependencies [77ff9754]
28
+ - Updated dependencies [d2d1d6b2]
29
+ - Updated dependencies [07a4887e]
30
+ - Updated dependencies [ea2ae711]
31
+ - Updated dependencies [17d0cc46]
32
+ - Updated dependencies [d2d1d6b2]
33
+ - @modern-js/plugin-router@1.2.9
34
+ - @modern-js/runtime-core@1.4.1
35
+ - @modern-js/utils@1.4.0
36
+
3
37
  ## 1.4.5
4
38
 
5
39
  ### Patch Changes
@@ -71,6 +71,11 @@ export default (({
71
71
  }
72
72
  },
73
73
  tools: {
74
+ devServer: {
75
+ headers: {
76
+ 'Access-Control-Allow-Origin': '*'
77
+ }
78
+ },
74
79
  webpack: (webpackConfig, {
75
80
  chain,
76
81
  webpack,
@@ -82,9 +87,6 @@ export default (({
82
87
  var _resolveOptions$serve, _resolveOptions$deplo2, _resolveOptions$deplo3;
83
88
 
84
89
  chain.output.libraryTarget('umd');
85
- chain.devServer.headers({
86
- 'Access-Control-Allow-Origin': '*'
87
- });
88
90
 
89
91
  if (resolveOptions !== null && resolveOptions !== void 0 && (_resolveOptions$serve = resolveOptions.server) !== null && _resolveOptions$serve !== void 0 && _resolveOptions$serve.port) {
90
92
  chain.output.publicPath(_env === 'development' ? `//localhost:${resolveOptions.server.port}/` : webpackConfig.output.publicPath);
@@ -37,6 +37,7 @@ function getAppInstance(options, appInfo, manifest) {
37
37
  appInstance: null,
38
38
  domId: generateSubAppContainerKey(appInfo)
39
39
  };
40
+ this.unregisterHistoryListener = void 0;
40
41
  }
41
42
 
42
43
  // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -114,7 +115,7 @@ function getAppInstance(options, appInfo, manifest) {
114
115
  await (appInstance === null || appInstance === void 0 ? void 0 : appInstance.mount());
115
116
  }
116
117
 
117
- history === null || history === void 0 ? void 0 : history.listen(() => {
118
+ this.unregisterHistoryListener = history === null || history === void 0 ? void 0 : history.listen(() => {
118
119
  if (locationHref !== history.location.pathname) {
119
120
  locationHref = history.location.pathname;
120
121
  const popStateEvent = new PopStateEvent('popstate');
@@ -134,6 +135,7 @@ function getAppInstance(options, appInfo, manifest) {
134
135
  const {
135
136
  appInstance
136
137
  } = this.state;
138
+ this.unregisterHistoryListener();
137
139
 
138
140
  if (appInstance) {
139
141
  const {
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Tips: this package will be bundled and running in the browser, do not import from `@modern-js/utils`.
3
+ */
1
4
  import createDebug from 'debug';
2
5
  export const logger = createDebug('modern-js:plugin-garfish');
3
6
  export const SUBMODULE_APP_COMPONENT_KEY = 'SubModuleComponent';
@@ -86,6 +86,11 @@ var _default = ({
86
86
  }
87
87
  },
88
88
  tools: {
89
+ devServer: {
90
+ headers: {
91
+ 'Access-Control-Allow-Origin': '*'
92
+ }
93
+ },
89
94
  webpack: (webpackConfig, {
90
95
  chain,
91
96
  webpack,
@@ -97,9 +102,6 @@ var _default = ({
97
102
  var _resolveOptions$serve, _resolveOptions$deplo2, _resolveOptions$deplo3;
98
103
 
99
104
  chain.output.libraryTarget('umd');
100
- chain.devServer.headers({
101
- 'Access-Control-Allow-Origin': '*'
102
- });
103
105
 
104
106
  if (resolveOptions !== null && resolveOptions !== void 0 && (_resolveOptions$serve = resolveOptions.server) !== null && _resolveOptions$serve !== void 0 && _resolveOptions$serve.port) {
105
107
  chain.output.publicPath(_env === 'development' ? `//localhost:${resolveOptions.server.port}/` : webpackConfig.output.publicPath);
@@ -43,6 +43,7 @@ function getAppInstance(options, appInfo, manifest) {
43
43
  appInstance: null,
44
44
  domId: (0, _util.generateSubAppContainerKey)(appInfo)
45
45
  };
46
+ this.unregisterHistoryListener = void 0;
46
47
  }
47
48
 
48
49
  // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -120,7 +121,7 @@ function getAppInstance(options, appInfo, manifest) {
120
121
  await (appInstance === null || appInstance === void 0 ? void 0 : appInstance.mount());
121
122
  }
122
123
 
123
- history === null || history === void 0 ? void 0 : history.listen(() => {
124
+ this.unregisterHistoryListener = history === null || history === void 0 ? void 0 : history.listen(() => {
124
125
  if (locationHref !== history.location.pathname) {
125
126
  locationHref = history.location.pathname;
126
127
  const popStateEvent = new PopStateEvent('popstate');
@@ -140,6 +141,7 @@ function getAppInstance(options, appInfo, manifest) {
140
141
  const {
141
142
  appInstance
142
143
  } = this.state;
144
+ this.unregisterHistoryListener();
143
145
 
144
146
  if (appInstance) {
145
147
  const {
@@ -11,6 +11,9 @@ var _debug = _interopRequireDefault(require("debug"));
11
11
 
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
13
 
14
+ /**
15
+ * Tips: this package will be bundled and running in the browser, do not import from `@modern-js/utils`.
16
+ */
14
17
  const logger = (0, _debug.default)('modern-js:plugin-garfish');
15
18
  exports.logger = logger;
16
19
  const SUBMODULE_APP_COMPONENT_KEY = 'SubModuleComponent';
@@ -94,6 +94,11 @@ export default (function () {
94
94
  }
95
95
  },
96
96
  tools: {
97
+ devServer: {
98
+ headers: {
99
+ 'Access-Control-Allow-Origin': '*'
100
+ }
101
+ },
97
102
  webpack: function webpack(webpackConfig, _ref3) {
98
103
  var _resolveOptions$deplo, _resolveWebpackConfig;
99
104
 
@@ -106,9 +111,6 @@ export default (function () {
106
111
  var _resolveOptions$serve, _resolveOptions$deplo2, _resolveOptions$deplo3;
107
112
 
108
113
  chain.output.libraryTarget('umd');
109
- chain.devServer.headers({
110
- 'Access-Control-Allow-Origin': '*'
111
- });
112
114
 
113
115
  if (resolveOptions !== null && resolveOptions !== void 0 && (_resolveOptions$serve = resolveOptions.server) !== null && _resolveOptions$serve !== void 0 && _resolveOptions$serve.port) {
114
116
  chain.output.publicPath(env === 'development' ? "//localhost:".concat(resolveOptions.server.port, "/") : webpackConfig.output.publicPath);
@@ -76,6 +76,7 @@ function getAppInstance(options, appInfo, manifest) {
76
76
  appInstance: null,
77
77
  domId: generateSubAppContainerKey(appInfo)
78
78
  };
79
+ _this.unregisterHistoryListener = void 0;
79
80
  return _this;
80
81
  }
81
82
 
@@ -174,7 +175,7 @@ function getAppInstance(options, appInfo, manifest) {
174
175
  return appInstance === null || appInstance === void 0 ? void 0 : appInstance.mount();
175
176
 
176
177
  case 22:
177
- history === null || history === void 0 ? void 0 : history.listen(function () {
178
+ this.unregisterHistoryListener = history === null || history === void 0 ? void 0 : history.listen(function () {
178
179
  if (locationHref !== history.location.pathname) {
179
180
  locationHref = history.location.pathname;
180
181
  var popStateEvent = new PopStateEvent('popstate');
@@ -218,6 +219,7 @@ function getAppInstance(options, appInfo, manifest) {
218
219
  switch (_context2.prev = _context2.next) {
219
220
  case 0:
220
221
  appInstance = this.state.appInstance;
222
+ this.unregisterHistoryListener();
221
223
 
222
224
  if (appInstance) {
223
225
  _appInfo = appInstance.appInfo;
@@ -231,7 +233,7 @@ function getAppInstance(options, appInfo, manifest) {
231
233
  }
232
234
  }
233
235
 
234
- case 2:
236
+ case 3:
235
237
  case "end":
236
238
  return _context2.stop();
237
239
  }
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Tips: this package will be bundled and running in the browser, do not import from `@modern-js/utils`.
3
+ */
1
4
  import createDebug from 'debug';
2
5
  export var logger = createDebug('modern-js:plugin-garfish');
3
6
  export var SUBMODULE_APP_COMPONENT_KEY = 'SubModuleComponent';
@@ -28,10 +28,11 @@ export declare type ModernGarfishConfig = {
28
28
  };
29
29
  export declare type MicroComponentProps = {
30
30
  loadable?: LoadableConfig;
31
+ [index: string]: any;
31
32
  };
32
33
  export declare type Config = Partial<Options> & ModernGarfishConfig;
33
- export declare type UseModuleApps = { [index in 'apps' | string]: index extends 'apps' ? ModulesInfo : React.FC<any> } & {
34
- readonly MApp: React.FC<any>;
34
+ export declare type UseModuleApps = { [index in 'apps' | string]: index extends 'apps' ? ModulesInfo : React.FC<MicroComponentProps> } & {
35
+ readonly MApp: React.FC<MicroComponentProps>;
35
36
  readonly apps: ModulesInfo;
36
37
  };
37
38
  export declare function useModuleApps(): UseModuleApps;
@@ -1,8 +1,8 @@
1
1
  import React from 'react';
2
2
  import Garfish from 'garfish';
3
- import { Manifest, ModulesInfo } from '../useModuleApps';
3
+ import { Manifest, MicroComponentProps, ModulesInfo } from '../useModuleApps';
4
4
  export interface AppMap {
5
- [key: string]: React.FC<any>;
5
+ [key: string]: React.FC<MicroComponentProps>;
6
6
  }
7
7
  export declare function generateApps(options: typeof Garfish.options, manifest?: Manifest): {
8
8
  apps: AppMap;
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Tips: this package will be bundled and running in the browser, do not import from `@modern-js/utils`.
3
+ */
1
4
  import createDebug from 'debug';
2
5
  import { ModuleInfo } from './runtime';
3
6
  export declare const logger: createDebug.Debugger;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.5",
14
+ "version": "1.4.8",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/runtime/index.d.ts",
17
17
  "typesVersions": {
@@ -50,20 +50,19 @@
50
50
  },
51
51
  "dependencies": {
52
52
  "@babel/runtime": "^7",
53
- "@modern-js/utils": "^1.3.7",
53
+ "@modern-js/utils": "^1.5.0",
54
54
  "@types/debug": "^4.1.7",
55
55
  "@types/react-loadable": "^5.5.6",
56
56
  "debug": "^4.3.2",
57
57
  "garfish": "^1.3.3",
58
58
  "hoist-non-react-statics": "^3.3.2",
59
- "lodash": "^4.17.21",
60
59
  "react-loadable": "^5.5.0"
61
60
  },
62
61
  "devDependencies": {
63
- "@modern-js/core": "1.6.1",
64
- "@modern-js/plugin-router": "^1.2.8",
65
- "@modern-js/runtime-core": "1.4.0",
66
- "@modern-js/types": "^1.3.6",
62
+ "@modern-js/core": "1.8.0",
63
+ "@modern-js/plugin-router": "^1.2.10",
64
+ "@modern-js/runtime-core": "1.4.2",
65
+ "@modern-js/types": "^1.5.0",
67
66
  "@scripts/build": "0.0.0",
68
67
  "@scripts/jest-config": "0.0.0",
69
68
  "@testing-library/jest-dom": "^5.16.1",
@@ -72,7 +71,6 @@
72
71
  "@testing-library/user-event": "^13.5.0",
73
72
  "@types/hoist-non-react-statics": "^3.3.1",
74
73
  "@types/jest": "^26.0.24",
75
- "@types/lodash": "^4.14.181",
76
74
  "@types/node": "^14",
77
75
  "@types/react": "^17",
78
76
  "@types/react-dom": "^17",
@@ -85,8 +83,8 @@
85
83
  "webpack-chain": "^6.5.1"
86
84
  },
87
85
  "peerDependencies": {
88
- "@modern-js/plugin-router": "^1.2.8",
89
- "@modern-js/runtime-core": "^1.4.0",
86
+ "@modern-js/plugin-router": "^1.2.10",
87
+ "@modern-js/runtime-core": "^1.4.2",
90
88
  "react": "^17"
91
89
  },
92
90
  "sideEffects": false,
@@ -1,89 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`plugin-garfish cli cli addRuntimeExports 1`] = `
4
- Array [
5
- "export { default as garfish, default as masterApp } from '@modern-js/test/plugin-garfish'",
6
- "export * from '@modern-js/test/plugin-garfish'",
7
- ]
8
- `;
9
-
10
- exports[`plugin-garfish cli cli get runtime config 1`] = `
11
- Object {
12
- "masterApp": Object {
13
- "basename": "/test",
14
- },
15
- }
16
- `;
17
-
18
- exports[`plugin-garfish cli cli get runtime features config 1`] = `
19
- Object {
20
- "masterApp": Object {
21
- "basename": "/test2",
22
- },
23
- }
24
- `;
25
-
26
- exports[`plugin-garfish cli cli makeRender function 1`] = `
27
- Object {
28
- "renderByProvider": true,
29
- "routerConfig": Object {
30
- "basename": "/test",
31
- "historyOptions": Object {
32
- "basename": "/test",
33
- },
34
- },
35
- }
36
- `;
37
-
38
- exports[`plugin-garfish cli cli set runtime config 1`] = `
39
- Object {
40
- "masterApp": true,
41
- }
42
- `;
43
-
44
- exports[`plugin-garfish cli cli set runtime features config 1`] = `
45
- Object {
46
- "features": Object {
47
- "masterApp": true,
48
- },
49
- }
50
- `;
51
-
52
- exports[`plugin-garfish cli webpack config close external and use js entry 1`] = `
53
- Object {
54
- "devServer": Object {
55
- "headers": Object {
56
- "Access-Control-Allow-Origin": "*",
57
- },
58
- },
59
- "externals": Object {
60
- "react": "react",
61
- "react-dom": "react-dom",
62
- },
63
- "optimization": Object {
64
- "runtimeChunk": false,
65
- "splitChunks": Object {
66
- "chunks": "async",
67
- },
68
- },
69
- "output": Object {
70
- "filename": "index.js",
71
- "libraryTarget": "umd",
72
- "publicPath": "//localhost:8080/",
73
- },
74
- }
75
- `;
76
-
77
- exports[`plugin-garfish cli webpack config default micro config 1`] = `
78
- Object {
79
- "devServer": Object {
80
- "headers": Object {
81
- "Access-Control-Allow-Origin": "*",
82
- },
83
- },
84
- "output": Object {
85
- "libraryTarget": "umd",
86
- "publicPath": "//localhost:8080/",
87
- },
88
- }
89
- `;
@@ -1,239 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { Hooks, manager, ToRunners } from '@modern-js/core';
3
- import WebpackChain from 'webpack-chain';
4
- import GarfishPlugin, { externals } from '../src/cli';
5
- import { getRuntimeConfig, makeRenderFunction, setRuntimeConfig } from '../src/cli/utils';
6
-
7
- const addExportList = [];
8
- jest.mock('@modern-js/utils', () => {
9
- const originalModule = jest.requireActual('@modern-js/utils');
10
- return {
11
- __esModule: true,
12
- ...originalModule,
13
- createRuntimeExportsUtils: ()=>({
14
- addExport: (val: any)=> {
15
- addExportList.push(val);
16
- },
17
- getPath: ()=> 'test',
18
- }),
19
- }
20
- });
21
-
22
- describe('plugin-garfish cli', () => {
23
- test('cli garfish basename', async () => {
24
- expect(GarfishPlugin().name).toBe('@modern-js/plugin-garfish');
25
-
26
- const main = manager.clone().usePlugin(GarfishPlugin);
27
- const runner = await main.init();
28
- await runner.prepare();
29
- const configHistoryOptions: any = await runner.resolvedConfig({
30
- resolved: {
31
- runtime: {
32
- router: {
33
- historyOptions: { basename: '/test' }
34
- },
35
- masterApp: {}
36
- },
37
- }
38
- } as any);
39
-
40
- expect(configHistoryOptions.resolved.runtime.masterApp.basename).toBe('/test');
41
-
42
- const configHistory: any = await runner.resolvedConfig({
43
- resolved: {
44
- runtime: {
45
- router: {
46
- basename: '/test2'
47
- },
48
- masterApp: {}
49
- },
50
- }
51
- } as any);
52
-
53
- expect(configHistory.resolved.runtime.masterApp.basename).toBe('/test2');
54
- });
55
-
56
- test('cli get runtime config', ()=>{
57
- const runtimeConfig = getRuntimeConfig({
58
- runtime: {
59
- masterApp: {
60
- basename: '/test'
61
- }
62
- }
63
- });
64
- expect(runtimeConfig).toMatchSnapshot();
65
- });
66
-
67
- test('cli get runtime features config', ()=>{
68
- const runtimeConfig = getRuntimeConfig({
69
- runtime: {
70
- masterApp: {
71
- basename: '/test'
72
- },
73
- features: {
74
- masterApp: {
75
- basename: '/test2'
76
- }
77
- }
78
- }
79
- });
80
-
81
- expect(runtimeConfig).toMatchSnapshot();
82
- });
83
-
84
- test('cli set runtime config', ()=>{
85
- const runtimeConfig = {
86
- runtime: {
87
- masterApp: {
88
- basename: '/test'
89
- }
90
- }
91
- };
92
-
93
- setRuntimeConfig(runtimeConfig, 'masterApp', true);
94
-
95
- expect(runtimeConfig.runtime).toMatchSnapshot();
96
- });
97
-
98
- test('cli set runtime features config', ()=>{
99
- const runtimeConfig = {
100
- runtime: {
101
- features: {
102
- masterApp: {
103
- basename: '/test'
104
- }
105
- }
106
- }
107
- };
108
-
109
- setRuntimeConfig(runtimeConfig, 'masterApp', true);
110
-
111
- expect(runtimeConfig.runtime).toMatchSnapshot();
112
- });
113
-
114
- test('cli makeRender function', ()=>{
115
- const code = `
116
- const router = (config)=> config;
117
- const App = {};
118
- const routerConfig = router({...App?.config?.router, ...App?.config?.features?.router});
119
- const resultConfig = {
120
- routerConfig,
121
- renderByProvider: true,
122
- };
123
-
124
- if (IS_BROWSER) {
125
- resultConfig.renderByProvider = false;
126
- }
127
- return resultConfig;
128
- `;
129
- const generateNewRenderFn = new Function('appInfo', 'IS_BROWSER', '__GARFISH_EXPORTS__', makeRenderFunction(code));
130
-
131
- // render byGarfish but don't provider appInfo
132
- expect(generateNewRenderFn(undefined, true, false)).toBe(null);
133
-
134
- // run alone
135
- expect(generateNewRenderFn(undefined, true)).toMatchObject({
136
- renderByProvider: false
137
- });
138
-
139
- // render ByGarfish and provider appInfo
140
- expect(generateNewRenderFn({ basename: '/test' }, true, true)).toMatchSnapshot();
141
- });
142
-
143
-
144
- test('webpack config close external and use js entry', async ()=>{
145
- const resolveConfig: any = {
146
- deploy: {
147
- microFrontend: {
148
- externalBasicLibrary: true,
149
- enableHtmlEntry: false,
150
- },
151
- },
152
- server: {
153
- port: 8080
154
- }
155
- };
156
-
157
- const main = manager.clone({
158
- useResolvedConfigContext: ()=>resolveConfig
159
- }).usePlugin(GarfishPlugin);
160
-
161
- const runner = await main.init();
162
- await runner.prepare();
163
- const config: any = await runner.config();
164
- const webpackConfig = new WebpackChain();
165
-
166
- config[0].tools.webpack({}, {
167
- chain: webpackConfig,
168
- webpack: jest.fn(),
169
- env: 'development'
170
- });
171
-
172
- const generateConfig = webpackConfig.toConfig();
173
- expect(generateConfig).toMatchSnapshot();
174
- expect(generateConfig).toMatchObject({
175
- output: {
176
- libraryTarget: 'umd',
177
- publicPath: '//localhost:8080/',
178
- filename: 'index.js'
179
- },
180
- externals,
181
- optimization: { runtimeChunk: false, splitChunks: { chunks: 'async' } }
182
- });
183
- })
184
-
185
- test('webpack config default micro config', async ()=>{
186
- const resolveConfig: any = {
187
- deploy: {
188
- microFrontend: true,
189
- },
190
- server: {
191
- port: '8080'
192
- }
193
- };
194
-
195
- const main = manager.clone({
196
- useResolvedConfigContext: ()=>resolveConfig
197
- }).usePlugin(GarfishPlugin);
198
- const runner = await main.init();
199
- await runner.prepare();
200
- const config: any = await runner.config();
201
- const webpackConfig = new WebpackChain();
202
-
203
- config[0].tools.webpack({}, {
204
- chain: webpackConfig,
205
- webpack: jest.fn(),
206
- env: 'development'
207
- });
208
-
209
- const generateConfig = webpackConfig.toConfig();
210
- expect(generateConfig).toMatchSnapshot();
211
- expect(generateConfig).toMatchObject({
212
- output: {
213
- libraryTarget: 'umd',
214
- publicPath: '//localhost:8080/'
215
- }
216
- });
217
- expect(generateConfig.externals).toBeUndefined();
218
- expect(generateConfig.output.filename).toBeUndefined();
219
- });
220
-
221
- test('cli addRuntimeExports', async ()=>{
222
- const resolveConfig: any = {};
223
- const mfPackagePath = '@modern-js/test/plugin-garfish';
224
- const plugin = GarfishPlugin({
225
- mfPackagePath,
226
- });
227
-
228
- const lifecycle = await plugin.setup({
229
- useResolvedConfigContext: () => resolveConfig,
230
- useAppContext: ()=>({
231
- internalDirectory: 'test'
232
- }),
233
- } as any);
234
-
235
- lifecycle && lifecycle.config();
236
- lifecycle && lifecycle.addRuntimeExports()
237
- expect(addExportList).toMatchSnapshot();
238
- });
239
- });
@@ -1,57 +0,0 @@
1
- import React from 'react';
2
- import { render } from '@testing-library/react';
3
- import { createApp } from '@modern-js/runtime-core';
4
- import '@testing-library/jest-dom';
5
-
6
- import ModernGarfishPlugin, {
7
- useModuleApp,
8
- useModuleApps,
9
- } from '../src/runtime';
10
-
11
- global.React = React;
12
-
13
- const dashboardPath = 'http://garfish-mock.com/dashboard';
14
- const tableListPath = 'http://garfish-mock.com/table-list';
15
-
16
- describe('plugin-garfish', () => {
17
- test('legacyModule hooks params', async () => {
18
- const dashBoardModuleInfo = {
19
- name: 'Dashboard',
20
- entry: dashboardPath,
21
- };
22
- const tableListModuleInfo = {
23
- name: 'TableList',
24
- entry: tableListPath,
25
- };
26
-
27
- const microFrontendConfig = {
28
- apps: [tableListModuleInfo, dashBoardModuleInfo],
29
- LoadingComponent() {
30
- return <div data-testid="loading-id">loading</div>;
31
- },
32
- };
33
-
34
- const App = () => {
35
- const { MApp, apps, Dashboard, TableList } = useModuleApps();
36
-
37
-
38
- if (apps.length > 0) {
39
- expect(apps[0].name).toBe(tableListModuleInfo.name);
40
- expect(apps[0].entry).toBe(tableListModuleInfo.entry);
41
- expect(apps[0].Component === TableList).toBeTruthy();
42
-
43
- expect(apps[1].name).toBe(dashBoardModuleInfo.name);
44
- expect(apps[1].entry).toBe(dashBoardModuleInfo.entry);
45
- expect(apps[1].Component === Dashboard).toBeTruthy();
46
- }
47
-
48
- return <div></div>;
49
- };
50
-
51
- const AppWrapper = createApp({
52
- plugins: [ModernGarfishPlugin(microFrontendConfig)],
53
- })(App);
54
-
55
- render(<AppWrapper />);
56
- });
57
- });
@@ -1,252 +0,0 @@
1
- import React from 'react';
2
- import { render, screen } from '@testing-library/react';
3
- import userEvent from '@testing-library/user-event';
4
- import { createApp } from '@modern-js/runtime-core';
5
- import fetchMock from 'jest-fetch-mock';
6
- import '@testing-library/jest-dom';
7
- // https://stackoverflow.com/questions/49034907/fetch-mock-does-not-mock-my-fetch
8
- import {
9
- Link,
10
- Route,
11
- Switch,
12
- useLocation,
13
- MemoryRouter,
14
- } from '@modern-js/plugin-router';
15
- import ModernGarfishPlugin, { useModuleApp } from '../src/runtime';
16
- import { useModuleApps } from '../src';
17
- import {
18
- TABLE_LIST_ESCAPE_NODE,
19
- TABLE_LIST_HTML,
20
- TABLE_LIST_ROOT_NODE,
21
- } from './resource/table';
22
- import {
23
- DASHBOARD_ESCAPE_NODE,
24
- DASHBOARD_HTML,
25
- DASHBOARD_ROOT_NODE,
26
- } from './resource/dashboard';
27
- import {
28
- USER_INFO_ESCAPE_NODE,
29
- USER_INFO_HTML,
30
- USER_INFO_ROOT_NODE,
31
- } from './resource/userInfo';
32
-
33
- global.React = React;
34
- global.fetch = fetchMock;
35
-
36
- function waitFor(delay = 200) {
37
- return new Promise(resolve => {
38
- setTimeout(() => {
39
- resolve(false);
40
- }, delay);
41
- });
42
- }
43
-
44
- const dashboardPath = 'http://garfish-mock.com/dashboard';
45
- const tableListPath = 'http://garfish-mock.com/table-list';
46
- const userInfoPath = 'http://garfish-mock.com/user-info';
47
-
48
- describe('plugin-garfish', () => {
49
- beforeEach(() => {
50
- // https://www.npmjs.com/package/jest-fetch-mock
51
- fetchMock.mockIf(/^https?:\/\/garfish-mock.com.*$/, req => {
52
- if (req.url.endsWith('/dashboard')) {
53
- return Promise.resolve({
54
- body: DASHBOARD_HTML,
55
- headers: {
56
- 'Content-Type': 'text/html',
57
- },
58
- });
59
- } else if (req.url.endsWith('/table-list')) {
60
- return Promise.resolve({
61
- body: TABLE_LIST_HTML,
62
- headers: {
63
- 'Content-Type': 'text/html',
64
- },
65
- });
66
- } else if (req.url.endsWith('/user-info')) {
67
- return Promise.resolve({
68
- body: USER_INFO_HTML,
69
- headers: {
70
- 'Content-Type': 'text/html',
71
- },
72
- });
73
- } else {
74
- return Promise.resolve({
75
- status: 404,
76
- body: 'Not Found',
77
- });
78
- }
79
- });
80
-
81
- fetchMock.doMock();
82
- });
83
-
84
- // testing-library.com/docs/example-react-router/
85
- test('useModuleApps hooks', async () => {
86
- const dashBoardModuleInfo = {
87
- name: 'Dashboard',
88
- entry: dashboardPath,
89
- };
90
- const tableListModuleInfo = {
91
- name: 'TableList',
92
- entry: tableListPath,
93
- };
94
- const userInfo = {
95
- name: 'UserInfo',
96
- activeWhen: '/user-info',
97
- entry: userInfoPath,
98
- };
99
-
100
- const microFrontendConfig = {
101
- apps: [tableListModuleInfo, dashBoardModuleInfo, userInfo],
102
- manifest: {
103
- loadable: {
104
- loading: ()=> {
105
- return <div data-testid="loading-id">loading</div>;
106
- }
107
- },
108
- },
109
- };
110
-
111
- const App = () => {
112
- const HomeTitle = 'Micro home page';
113
- const Home = () => <div data-testid="home-title">{HomeTitle}</div>;
114
- const { Dashboard, TableList } = useModuleApps();
115
-
116
- const LocationDisplay = () => {
117
- const location = useLocation();
118
- return <div data-testid="location-display">{location.pathname}</div>;
119
- };
120
-
121
- return (
122
- <MemoryRouter>
123
- <div id="app">
124
- <Link to="/">home</Link>
125
- <Link data-testid="dashboard-link" to="/dashboard">
126
- dashboard
127
- </Link>
128
- <Link data-testid="table-list-link" to="/table-list">
129
- table-list
130
- </Link>
131
- <Link data-testid="user-info-link" to="/user-info">
132
- user-info
133
- </Link>
134
- <Switch>
135
- <Route exact={true} path="/">
136
- <Home />
137
- </Route>
138
- <Route path="/dashboard">
139
- <Dashboard />
140
- </Route>
141
- <Route path="/table-list">
142
- <TableList />
143
- </Route>
144
- <LocationDisplay />
145
- </Switch>
146
- </div>
147
- </MemoryRouter>
148
- );
149
- };
150
-
151
- const AppWrapper = createApp({
152
- plugins: [ModernGarfishPlugin(microFrontendConfig)],
153
- })(App);
154
-
155
- const { unmount } = render(<AppWrapper />);
156
- expect(screen.getByTestId('home-title')).toBeInTheDocument();
157
- const leftClick = { button: 0 };
158
- userEvent.click(screen.getByTestId('dashboard-link'), leftClick);
159
- // expect(await screen.findByText('loading')).toBeInTheDocument();
160
- expect(
161
- await screen.findByText(DASHBOARD_ROOT_NODE.text),
162
- ).toBeInTheDocument();
163
- expect(
164
- await screen.findByText(DASHBOARD_ESCAPE_NODE.text),
165
- ).toBeInTheDocument();
166
-
167
- userEvent.click(screen.getByTestId('table-list-link'), leftClick);
168
- expect(screen.queryByText(DASHBOARD_ROOT_NODE.text)).toBeNull();
169
- expect(screen.queryByText(DASHBOARD_ESCAPE_NODE.text)).toBeNull();
170
- // expect(await screen.findByText('loading')).toBeInTheDocument();
171
- expect(
172
- await screen.findByText(TABLE_LIST_ROOT_NODE.text),
173
- ).toBeInTheDocument();
174
- expect(
175
- await screen.findByText(TABLE_LIST_ESCAPE_NODE.text),
176
- ).toBeInTheDocument();
177
-
178
- unmount();
179
- });
180
-
181
- test('legacyModule hooks MApp', async () => {
182
- const userInfo = {
183
- name: 'UserInfo',
184
- activeWhen: '/user-info',
185
- entry: userInfoPath,
186
- };
187
- const tableList2 = {
188
- name: 'tablist2',
189
- activeWhen: '/table-list',
190
- entry: tableListPath,
191
- };
192
-
193
- const microFrontendConfig = {
194
- apps: [userInfo, tableList2],
195
- LoadingComponent() {
196
- return <div data-testid="loading-id">loading</div>;
197
- },
198
- };
199
-
200
- const App = () => {
201
- const { MApp } = useModuleApps();
202
- const NMApp = useModuleApp();
203
- expect(MApp).toBe(NMApp);
204
-
205
- return (
206
- <MemoryRouter>
207
- <div id="app">
208
- <Link to="/">home</Link>
209
- <Link data-testid="dashboard-link" to="/dashboard">
210
- dashboard
211
- </Link>
212
- <Link data-testid="table-list-link" to="/table-list">
213
- table-list
214
- </Link>
215
- <Link data-testid="user-info-link" to="/user-info">
216
- user-info
217
- </Link>
218
- <MApp />
219
- </div>
220
- </MemoryRouter>
221
- );
222
- };
223
-
224
- const AppWrapper = createApp({
225
- plugins: [ModernGarfishPlugin(microFrontendConfig)],
226
- })(App);
227
-
228
- const { unmount } = render(<AppWrapper />);
229
-
230
- history.pushState(null, '', '/user-info');
231
- expect(
232
- await screen.findByText(USER_INFO_ROOT_NODE.text),
233
- ).toBeInTheDocument();
234
- expect(
235
- await screen.findByText(USER_INFO_ESCAPE_NODE.text),
236
- ).toBeInTheDocument();
237
-
238
- history.pushState(null, '', '/table-list');
239
- await waitFor();
240
- expect(screen.queryByText(USER_INFO_ROOT_NODE.text)).toBeNull();
241
- expect(screen.queryByText(USER_INFO_ESCAPE_NODE.text)).toBeNull();
242
-
243
- expect(
244
- await screen.findByText(TABLE_LIST_ROOT_NODE.text),
245
- ).toBeInTheDocument();
246
- expect(
247
- await screen.findByText(TABLE_LIST_ESCAPE_NODE.text),
248
- ).toBeInTheDocument();
249
-
250
- unmount();
251
- });
252
- });
@@ -1,44 +0,0 @@
1
- export const DASHBOARD_ROOT_NODE = {
2
- text: 'dashboard content',
3
- id: 'dashboard-id',
4
- };
5
- export const DASHBOARD_ESCAPE_NODE = {
6
- text: 'dashboard-escape text',
7
- id: 'dashboard-escape-id',
8
- };
9
-
10
- export const DASHBOARD_HTML = `
11
- <!DOCTYPE html>
12
- <html lang="en">
13
- <head>
14
- <title>dashboard app</title>
15
- </head>
16
- <body>
17
- <h1>dashboard</h1>
18
- <div id="root"></div>
19
- <script>
20
- if (typeof __GARFISH_EXPORTS__ !=='undefined') {
21
- __GARFISH_EXPORTS__.provider = ()=>{
22
- return {
23
- render ({ dom , basename }) {
24
- let newContent = document.createElement('div');
25
- newContent.innerHTML = "${DASHBOARD_ROOT_NODE.text}";
26
- newContent.setAttribute('id','${DASHBOARD_ROOT_NODE.id}')
27
- dom.querySelector('#root').appendChild(newContent);
28
-
29
- let escapeNode = document.createElement('div');
30
- escapeNode.innerHTML = "${DASHBOARD_ESCAPE_NODE.text}";
31
- escapeNode.setAttribute('id',"${DASHBOARD_ESCAPE_NODE.id}")
32
- document.body.appendChild(escapeNode);
33
- },
34
- destroy ({ dom , basename }){
35
- document.body.removeChild(document.querySelector("#${DASHBOARD_ESCAPE_NODE.id}"));
36
- dom.querySelector('#root').removeChild(dom.querySelector("#${DASHBOARD_ROOT_NODE.id}"));
37
- }
38
- }
39
- }
40
- }
41
- </script>
42
- </body>
43
- </html>
44
- `;
@@ -1,43 +0,0 @@
1
- export const TABLE_LIST_ROOT_NODE = {
2
- text: 'table-list content',
3
- id: 'table-list-id',
4
- };
5
- export const TABLE_LIST_ESCAPE_NODE = {
6
- text: 'table-list escape text',
7
- id: 'table-list-escape-id',
8
- };
9
- export const TABLE_LIST_HTML = `
10
- <!DOCTYPE html>
11
- <html lang="en">
12
- <head>
13
- <title>table-list app</title>
14
- </head>
15
- <body>
16
- <h1>table-list</h1>
17
- <div id="root"></div>
18
- <script>
19
- if (typeof __GARFISH_EXPORTS__ !=='undefined') {
20
- __GARFISH_EXPORTS__.provider = ()=>{
21
- return {
22
- render ({ dom , basename }) {
23
- let newContent = document.createElement('div');
24
- newContent.innerHTML = "${TABLE_LIST_ROOT_NODE.text}";
25
- newContent.setAttribute('id','${TABLE_LIST_ROOT_NODE.id}')
26
- dom.querySelector('#root').appendChild(newContent);
27
-
28
- let escapeNode = document.createElement('div');
29
- escapeNode.innerHTML = "${TABLE_LIST_ESCAPE_NODE.text}";
30
- escapeNode.setAttribute('id',"${TABLE_LIST_ESCAPE_NODE.id}")
31
- document.body.appendChild(escapeNode);
32
- },
33
- destroy ({ dom , basename }){
34
- document.body.removeChild(document.querySelector("#${TABLE_LIST_ESCAPE_NODE.id}"));
35
- dom.querySelector('#root').removeChild(dom.querySelector("#${TABLE_LIST_ROOT_NODE.id}"));
36
- }
37
- }
38
- }
39
- }
40
- </script>
41
- </body>
42
- </html>
43
- `;
@@ -1,44 +0,0 @@
1
- export const USER_INFO_ROOT_NODE = {
2
- text: 'userInfo content',
3
- id: 'userInfo-id',
4
- };
5
- export const USER_INFO_ESCAPE_NODE = {
6
- text: 'userInfo-escape text',
7
- id: 'userInfo-escape-id',
8
- };
9
-
10
- export const USER_INFO_HTML = `
11
- <!DOCTYPE html>
12
- <html lang="en">
13
- <head>
14
- <title>userInfo app</title>
15
- </head>
16
- <body>
17
- <h1>userInfo</h1>
18
- <div id="root"></div>
19
- <script>
20
- if (typeof __GARFISH_EXPORTS__ !=='undefined') {
21
- __GARFISH_EXPORTS__.provider = ()=>{
22
- return {
23
- render ({ dom , basename }) {
24
- let newContent = document.createElement('div');
25
- newContent.innerHTML = "${USER_INFO_ROOT_NODE.text}";
26
- newContent.setAttribute('id','${USER_INFO_ROOT_NODE.id}')
27
- dom.querySelector('#root').appendChild(newContent);
28
-
29
- let escapeNode = document.createElement('div');
30
- escapeNode.innerHTML = "${USER_INFO_ESCAPE_NODE.text}";
31
- escapeNode.setAttribute('id',"${USER_INFO_ESCAPE_NODE.id}")
32
- document.body.appendChild(escapeNode);
33
- },
34
- destroy ({ dom , basename }){
35
- document.body.removeChild(document.querySelector("#${USER_INFO_ESCAPE_NODE.id}"));
36
- dom.querySelector('#root').removeChild(dom.querySelector("#${USER_INFO_ROOT_NODE.id}"));
37
- }
38
- }
39
- }
40
- }
41
- </script>
42
- </body>
43
- </html>
44
- `;