@modern-js/plugin-garfish 1.3.0 → 1.4.1

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 (41) hide show
  1. package/.eslintrc.js +1 -1
  2. package/CHANGELOG.md +35 -0
  3. package/dist/js/modern/cli/index.js +204 -225
  4. package/dist/js/modern/cli/utils.js +85 -31
  5. package/dist/js/modern/index.js +2 -1
  6. package/dist/js/modern/runtime/index.js +2 -1
  7. package/dist/js/modern/runtime/loadable.js +36 -19
  8. package/dist/js/modern/runtime/plugin.js +24 -19
  9. package/dist/js/modern/runtime/utils/MApp.js +7 -10
  10. package/dist/js/modern/runtime/utils/apps.js +3 -9
  11. package/dist/js/node/cli/index.js +207 -222
  12. package/dist/js/node/cli/utils.js +89 -31
  13. package/dist/js/node/index.js +18 -3
  14. package/dist/js/node/runtime/index.js +25 -12
  15. package/dist/js/node/runtime/loadable.js +36 -19
  16. package/dist/js/node/runtime/plugin.js +21 -18
  17. package/dist/js/node/runtime/utils/MApp.js +7 -10
  18. package/dist/js/node/runtime/utils/apps.js +3 -9
  19. package/dist/js/treeshaking/cli/index.js +124 -121
  20. package/dist/js/treeshaking/cli/utils.js +33 -4
  21. package/dist/js/treeshaking/index.js +2 -1
  22. package/dist/js/treeshaking/runtime/index.js +2 -1
  23. package/dist/js/treeshaking/runtime/loadable.js +39 -25
  24. package/dist/js/treeshaking/runtime/plugin.js +29 -24
  25. package/dist/js/treeshaking/runtime/utils/MApp.js +23 -31
  26. package/dist/js/treeshaking/runtime/utils/apps.js +22 -24
  27. package/dist/types/cli/index.d.ts +18 -10
  28. package/dist/types/cli/utils.d.ts +5 -2
  29. package/dist/types/index.d.ts +2 -1
  30. package/dist/types/runtime/index.d.ts +3 -1
  31. package/dist/types/runtime/loadable.d.ts +2 -8
  32. package/dist/types/runtime/plugin.d.ts +29 -3
  33. package/dist/types/runtime/useModuleApps.d.ts +7 -9
  34. package/dist/types/runtime/utils/MApp.d.ts +1 -15
  35. package/dist/types/runtime/utils/apps.d.ts +1 -1
  36. package/package.json +21 -9
  37. package/tests/cli.test.tsx +203 -33
  38. package/tests/hooks.test.tsx +1 -0
  39. package/tests/index.test.tsx +6 -3
  40. package/tsconfig.json +3 -2
  41. package/tests/__snapshots__/cli.test.tsx.snap +0 -71
@@ -1,51 +1,221 @@
1
1
  import '@testing-library/jest-dom';
2
- import { PLUGIN_SCHEMAS } from '@modern-js/utils';
3
- import { makeProvider } from '../src/cli/utils';
4
- import GarfishPlugin, { initializer } from '../src/cli';
2
+ import { manager, useAppContext } from '@modern-js/core';
3
+ import WebpackChain from 'webpack-chain';
4
+ import GarfishPlugin, { externals, resolvedConfig } from '../src/cli';
5
+ import { getRuntimeConfig, makeRenderFunction, setRuntimeConfig } from '../src/cli/utils';
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
+ };
5
19
 
6
20
  jest.mock('@modern-js/core', () => {
7
21
  const originalModule = jest.requireActual('@modern-js/core');
8
22
  return {
9
23
  __esModule: true,
10
24
  ...originalModule,
11
- default: jest.fn(() => 'mocked baz'),
12
- useResolvedConfigContext: () => ({
25
+ useResolvedConfigContext: ()=>{
26
+ return mock_config_context.get();
27
+ }
28
+ };
29
+ });
30
+
31
+
32
+ describe('plugin-garfish cli', () => {
33
+ test('cli garfish basename', async () => {
34
+ expect(GarfishPlugin.name).toBe('@modern-js/plugin-garfish');
35
+ const basename = '/test';
36
+ const resolveConfig = {
37
+ resolved: {
38
+ runtime: {
39
+ router: {
40
+ historyOptions: { basename }
41
+ },
42
+ masterApp: {}
43
+ },
44
+ }
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
+ };
59
+
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);
69
+
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
+ }
83
+ }
84
+ });
85
+ });
86
+
87
+ test('cli get runtime config', ()=>{
88
+ const runtimeConfig = getRuntimeConfig({
13
89
  runtime: {
14
90
  masterApp: {
15
- manifest: {
16
- componentKey: 'test-dynamic-key',
17
- },
91
+ basename: '/test'
92
+ }
93
+ }
94
+ });
95
+ expect(runtimeConfig).toMatchObject({
96
+ masterApp: {
97
+ basename: '/test'
98
+ }
99
+ });
100
+ });
101
+
102
+ test('cli get runtime features config', ()=>{
103
+ const runtimeConfig = getRuntimeConfig({
104
+ runtime: {
105
+ masterApp: {
106
+ basename: '/test'
18
107
  },
19
- },
108
+ features: {
109
+ masterApp: {
110
+ basename: '/test2'
111
+ }
112
+ }
113
+ }
114
+ });
115
+
116
+ expect(runtimeConfig).toMatchObject({
117
+ masterApp: {
118
+ basename: '/test2'
119
+ }
120
+ });
121
+ });
122
+
123
+ test('cli set runtime config', ()=>{
124
+ const runtimeConfig = {
125
+ runtime: {
126
+ masterApp: {
127
+ basename: '/test'
128
+ }
129
+ }
130
+ };
131
+
132
+ setRuntimeConfig(runtimeConfig, 'masterApp', true);
133
+
134
+ expect(runtimeConfig.runtime.masterApp).toBe(true);
135
+ });
136
+
137
+ test('cli set runtime features config', ()=>{
138
+ const runtimeConfig = {
139
+ runtime: {
140
+ features: {
141
+ masterApp: {
142
+ basename: '/test'
143
+ }
144
+ }
145
+ }
146
+ };
147
+
148
+ setRuntimeConfig(runtimeConfig, 'masterApp', true);
149
+
150
+ expect(runtimeConfig.runtime.features.masterApp).toBe(true);
151
+ });
152
+
153
+ 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({
20
160
  deploy: {
21
- microFrontend: 'dynamicComponentKey',
161
+ microFrontend: {
162
+ externalBasicLibrary: true,
163
+ enableHtmlEntry: false,
164
+ },
22
165
  },
23
- }),
24
- };
25
- });
166
+ server: {
167
+ port: '8080'
168
+ }
169
+ });
26
170
 
27
- describe('plugin-garfish cli', () => {
28
- test('makeProvider', async () => {
29
- const garfishExport = makeProvider('modernJSExportComponent');
30
- expect(garfishExport).toMatchSnapshot();
31
- });
171
+ config[0].tools.webpack({}, {
172
+ chain: webpackConfig,
173
+ webpack: jest.fn(),
174
+ env: 'development'
175
+ });
32
176
 
33
- test('test modifyEntryExport', () => {
34
- const lifeCycle: any = initializer({
35
- validateSchema() {
36
- return PLUGIN_SCHEMAS['@modern-js/plugin-garfish'];
177
+ const generateConfig = webpackConfig.toConfig();
178
+ expect(generateConfig).toMatchObject({
179
+ output: {
180
+ libraryTarget: 'umd',
181
+ publicPath: '//localhost:8080/',
182
+ filename: 'index.js'
37
183
  },
38
- externals: { 'react-dom': 'react-dom', react: 'react' },
39
- componentKey: 'test-dynamic-key',
40
- })();
41
- const { exportStatement } = lifeCycle.modifyEntryExport({
42
- entrypoint: 'hello',
43
- exportStatement: '',
44
- });
45
- expect(exportStatement).toMatchSnapshot();
46
- });
184
+ externals,
185
+ optimization: { runtimeChunk: false, splitChunks: { chunks: 'async' } }
186
+ });
187
+ })
47
188
 
48
- test('cli garfish plugin', () => {
49
- expect(GarfishPlugin.name).toBe('@modern-js/plugin-garfish');
189
+ 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({
197
+ deploy: {
198
+ microFrontend: true,
199
+ },
200
+ server: {
201
+ port: '8080'
202
+ }
203
+ });
204
+
205
+ config[0].tools.webpack({}, {
206
+ chain: webpackConfig,
207
+ webpack: jest.fn(),
208
+ env: 'development'
209
+ });
210
+
211
+ const generateConfig = webpackConfig.toConfig();
212
+ expect(generateConfig).toMatchObject({
213
+ output: {
214
+ libraryTarget: 'umd',
215
+ publicPath: '//localhost:8080/'
216
+ }
217
+ });
218
+ expect(generateConfig.externals).toBeUndefined();
219
+ expect(generateConfig.output.filename).toBeUndefined();
50
220
  });
51
221
  });
@@ -2,6 +2,7 @@ import React from 'react';
2
2
  import { render } from '@testing-library/react';
3
3
  import { createApp } from '@modern-js/runtime-core';
4
4
  import '@testing-library/jest-dom';
5
+
5
6
  import ModernGarfishPlugin, {
6
7
  useMicroApps,
7
8
  useModuleApp,
@@ -12,7 +12,8 @@ import {
12
12
  useLocation,
13
13
  MemoryRouter,
14
14
  } from '@modern-js/plugin-router';
15
- import ModernGarfishPlugin, { useMicroApps } from '../src/runtime';
15
+ import ModernGarfishPlugin from '../src/runtime';
16
+ import { useMicroApps } from '../src';
16
17
  import {
17
18
  TABLE_LIST_ESCAPE_NODE,
18
19
  TABLE_LIST_HTML,
@@ -99,8 +100,10 @@ describe('plugin-garfish', () => {
99
100
  const microFrontendConfig = {
100
101
  apps: [tableListModuleInfo, dashBoardModuleInfo, userInfo],
101
102
  manifest: {
102
- LoadingComponent() {
103
- return <div data-testid="loading-id">loading</div>;
103
+ loadable: {
104
+ loading: ()=> {
105
+ return <div data-testid="loading-id">loading</div>;
106
+ }
104
107
  },
105
108
  },
106
109
  };
package/tsconfig.json CHANGED
@@ -7,7 +7,8 @@
7
7
  "isolatedModules": true,
8
8
  "esModuleInterop": true,
9
9
  "paths": {},
10
- "types": ["node", "jest","@types/jest"]
10
+ "types": ["node", "jest", "@types/jest"]
11
11
  },
12
- "include": ["src"]
12
+ "include": ["src"],
13
+ "exclude": ["**/*.test.tsx"]
13
14
  }
@@ -1,71 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`plugin-garfish cli makeProvider 1`] = `
4
- "
5
- export const provider = function ({basename, dom, ...props}) {
6
- return {
7
- render({basename, dom}) {
8
- const SubApp = render(props, basename);
9
- const node = dom.querySelector('#' + MOUNT_ID) || dom;
10
-
11
- bootstrap(SubApp, node);
12
- },
13
- destroy({ dom }) {
14
- const node = dom.querySelector('#' + MOUNT_ID) || dom;
15
-
16
- if (node) {
17
- unmountComponentAtNode(node);
18
- }
19
- },
20
- SubModuleComponent: (props) => {
21
- const SubApp = render(props, basename);
22
-
23
- return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
24
- },
25
- modernJSExportComponent: () => {
26
- const SubApp = render(props, basename);
27
-
28
- return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
29
- }
30
- }
31
- };
32
- if (typeof __GARFISH_EXPORTS__ !== 'undefined') {
33
- __GARFISH_EXPORTS__.provider = provider;
34
- }
35
- "
36
- `;
37
-
38
- exports[`plugin-garfish cli test modifyEntryExport 1`] = `
39
- "
40
- export const provider = function ({basename, dom, ...props}) {
41
- return {
42
- render({basename, dom}) {
43
- const SubApp = render(props, basename);
44
- const node = dom.querySelector('#' + MOUNT_ID) || dom;
45
-
46
- bootstrap(SubApp, node);
47
- },
48
- destroy({ dom }) {
49
- const node = dom.querySelector('#' + MOUNT_ID) || dom;
50
-
51
- if (node) {
52
- unmountComponentAtNode(node);
53
- }
54
- },
55
- SubModuleComponent: (props) => {
56
- const SubApp = render(props, basename);
57
-
58
- return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
59
- },
60
- test-dynamic-key: () => {
61
- const SubApp = render(props, basename);
62
-
63
- return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
64
- }
65
- }
66
- };
67
- if (typeof __GARFISH_EXPORTS__ !== 'undefined') {
68
- __GARFISH_EXPORTS__.provider = provider;
69
- }
70
- "
71
- `;