@modern-js/plugin-garfish 1.3.1 → 1.4.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.
@@ -54,7 +54,7 @@ import { Loadable } from "../loadable"; // type Provider = {
54
54
  import { jsx as _jsx } from "react/jsx-runtime";
55
55
  import { Fragment as _Fragment } from "react/jsx-runtime";
56
56
 
57
- function getAppInstance(appInfo, manifest) {
57
+ function getAppInstance(options, appInfo, manifest) {
58
58
  var locationHref = '';
59
59
 
60
60
  var MicroApp = /*#__PURE__*/function (_React$Component) {
@@ -84,7 +84,7 @@ function getAppInstance(appInfo, manifest) {
84
84
  value: // eslint-disable-next-line @typescript-eslint/naming-convention
85
85
  function () {
86
86
  var _UNSAFE_componentWillMount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
87
- var _this$props, match, history, setLoadingState, userProps, domId, options, loadAppOptions, appInstance;
87
+ var _this$props, match, history, setLoadingState, userProps, domId, loadAppOptions, appInstance;
88
88
 
89
89
  return _regeneratorRuntime.wrap(function _callee$(_context) {
90
90
  while (1) {
@@ -92,7 +92,6 @@ function getAppInstance(appInfo, manifest) {
92
92
  case 0:
93
93
  _this$props = this.props, match = _this$props.match, history = _this$props.history, setLoadingState = _this$props.setLoadingState, userProps = _objectWithoutProperties(_this$props, _excluded);
94
94
  domId = this.state.domId;
95
- options = Garfish.options;
96
95
  loadAppOptions = _objectSpread(_objectSpread({}, appInfo), {}, {
97
96
  domGetter: "#".concat(domId),
98
97
  basename: path.join((options === null || options === void 0 ? void 0 : options.basename) || '/', (match === null || match === void 0 ? void 0 : match.path) || '/'),
@@ -128,21 +127,21 @@ function getAppInstance(appInfo, manifest) {
128
127
  logger("MicroApp Garfish.loadApp \"".concat(appInfo.name, "\""), {
129
128
  loadAppOptions: loadAppOptions
130
129
  });
131
- _context.prev = 6;
132
- _context.next = 9;
130
+ _context.prev = 5;
131
+ _context.next = 8;
133
132
  return Garfish.loadApp(appInfo.name, loadAppOptions);
134
133
 
135
- case 9:
134
+ case 8:
136
135
  appInstance = _context.sent;
137
136
 
138
137
  if (appInstance) {
139
- _context.next = 12;
138
+ _context.next = 11;
140
139
  break;
141
140
  }
142
141
 
143
142
  throw new Error("MicroApp Garfish.loadApp \"".concat(appInfo.name, "\" result is null"));
144
143
 
145
- case 12:
144
+ case 11:
146
145
  this.setState({
147
146
  appInstance: appInstance
148
147
  });
@@ -151,7 +150,7 @@ function getAppInstance(appInfo, manifest) {
151
150
  });
152
151
 
153
152
  if (!(appInstance.mounted && appInstance.appInfo.cache)) {
154
- _context.next = 20;
153
+ _context.next = 19;
155
154
  break;
156
155
  }
157
156
 
@@ -159,22 +158,22 @@ function getAppInstance(appInfo, manifest) {
159
158
  appInfo: appInstance.appInfo,
160
159
  appInstance: appInstance
161
160
  });
162
- _context.next = 18;
161
+ _context.next = 17;
163
162
  return appInstance === null || appInstance === void 0 ? void 0 : appInstance.show();
164
163
 
165
- case 18:
166
- _context.next = 23;
164
+ case 17:
165
+ _context.next = 22;
167
166
  break;
168
167
 
169
- case 20:
168
+ case 19:
170
169
  logger("MicroApp Garfish.loadApp \"".concat(appInfo.name, "\" mount"), {
171
170
  appInfo: appInstance.appInfo,
172
171
  appInstance: appInstance
173
172
  });
174
- _context.next = 23;
173
+ _context.next = 22;
175
174
  return appInstance === null || appInstance === void 0 ? void 0 : appInstance.mount();
176
175
 
177
- case 23:
176
+ case 22:
178
177
  history === null || history === void 0 ? void 0 : history.listen(function () {
179
178
  if (locationHref !== history.location.pathname) {
180
179
  locationHref = history.location.pathname;
@@ -183,23 +182,23 @@ function getAppInstance(appInfo, manifest) {
183
182
  logger("MicroApp Garfish.loadApp popstate");
184
183
  }
185
184
  });
186
- _context.next = 29;
185
+ _context.next = 28;
187
186
  break;
188
187
 
189
- case 26:
190
- _context.prev = 26;
191
- _context.t0 = _context["catch"](6);
188
+ case 25:
189
+ _context.prev = 25;
190
+ _context.t0 = _context["catch"](5);
192
191
  setLoadingState({
193
192
  isLoading: true,
194
193
  error: _context.t0
195
194
  });
196
195
 
197
- case 29:
196
+ case 28:
198
197
  case "end":
199
198
  return _context.stop();
200
199
  }
201
200
  }
202
- }, _callee, this, [[6, 26]]);
201
+ }, _callee, this, [[5, 25]]);
203
202
  }));
204
203
 
205
204
  function UNSAFE_componentWillMount() {
@@ -270,7 +269,7 @@ export function generateApps(options, manifest) {
270
269
 
271
270
  var apps = {};
272
271
  (_options$apps = options.apps) === null || _options$apps === void 0 ? void 0 : _options$apps.forEach(function (appInfo) {
273
- var Component = getAppInstance(appInfo, manifest);
272
+ var Component = getAppInstance(options, appInfo, manifest);
274
273
  appInfo.Component = Component;
275
274
  apps[appInfo.name] = Component;
276
275
  });
@@ -1,9 +1,25 @@
1
- import { NormalizedConfig } from '@modern-js/core';
1
+ import { createPlugin } from '@modern-js/core';
2
+ export declare const externals: {
3
+ 'react-dom': string;
4
+ react: string;
5
+ };
6
+ declare type Initializer = Parameters<typeof createPlugin>[0];
7
+ declare type NonVoidPromiseAble<T> = T extends void | Promise<any> ? never : T;
8
+ export declare type LifeCycle = NonVoidPromiseAble<ReturnType<Initializer>>;
9
+ export declare const resolvedConfig: NonNullable<LifeCycle['resolvedConfig']>;
10
+ export declare const initializer: (hooks: {
11
+ resolvedConfig: LifeCycle['resolvedConfig'];
12
+ validateSchema: LifeCycle['validateSchema'];
13
+ }, initializerConfig: {
14
+ runtimePluginName?: string;
15
+ defaultEnableHtmlEntry?: boolean;
16
+ defaultExternalBasicLibrary?: boolean;
17
+ }) => Initializer;
2
18
 
3
19
  declare const _default: import("@modern-js/core").AsyncPlugin<Partial<import("@modern-js/core").Progresses2Threads<{
4
20
  config: import("@modern-js/core").ParallelWorkflow<void, unknown>;
5
21
  resolvedConfig: import("@modern-js/core").AsyncWaterfall<{
6
- resolved: NormalizedConfig;
22
+ resolved: import("@modern-js/core").NormalizedConfig;
7
23
  }>;
8
24
  validateSchema: import("@modern-js/core").ParallelWorkflow<void, unknown>;
9
25
  prepare: import("@modern-js/core").AsyncWorkflow<void, void>;
@@ -1,2 +1,5 @@
1
+ import { NormalizedConfig } from '@modern-js/core';
1
2
  export declare const makeProvider: () => string;
2
- export declare const makeRenderFunction: (code: string) => string;
3
+ export declare const makeRenderFunction: (code: string) => string;
4
+ export declare function getRuntimeConfig(config: Partial<NormalizedConfig>): any;
5
+ export declare function setRuntimeConfig(config: Partial<NormalizedConfig>, key: string, value: any): undefined;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.3.1",
14
+ "version": "1.4.2",
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.1",
49
+ "@modern-js/utils": "^1.3.4",
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.1",
59
- "@modern-js/plugin-router": "^1.2.2",
60
- "@modern-js/runtime-core": "^1.2.2",
61
- "@modern-js/types": "^1.3.1",
58
+ "@modern-js/core": "^1.4.6",
59
+ "@modern-js/plugin-router": "^1.2.5",
60
+ "@modern-js/runtime-core": "^1.2.4",
61
+ "@modern-js/types": "^1.3.4",
62
62
  "@scripts/build": "0.0.0",
63
63
  "@scripts/jest-config": "0.0.0",
64
64
  "@testing-library/jest-dom": "^5.16.1",
@@ -70,7 +70,6 @@
70
70
  "@types/node": "^14",
71
71
  "@types/react": "^17",
72
72
  "@types/react-dom": "^17",
73
- "html-webpack-externals-plugin": "^3.8.0",
74
73
  "jest": "^27",
75
74
  "jest-fetch-mock": "^3.0.3",
76
75
  "react": "^17.0.2",
@@ -80,9 +79,9 @@
80
79
  "webpack-chain": "^6.5.1"
81
80
  },
82
81
  "peerDependencies": {
83
- "@modern-js/core": "^1.4.1",
84
- "@modern-js/plugin-router": "^1.2.2",
85
- "@modern-js/runtime-core": "^1.2.2"
82
+ "@modern-js/core": "^1.4.6",
83
+ "@modern-js/plugin-router": "^1.2.5",
84
+ "@modern-js/runtime-core": "^1.2.4"
86
85
  },
87
86
  "sideEffects": false,
88
87
  "modernConfig": {},
@@ -1,29 +1,221 @@
1
1
  import '@testing-library/jest-dom';
2
- import GarfishPlugin from '../src/cli';
3
-
4
- // jest.mock('@modern-js/core', () => {
5
- // const originalModule = jest.requireActual('@modern-js/core');
6
- // return {
7
- // __esModule: true,
8
- // ...originalModule,
9
- // default: jest.fn(() => 'mocked baz'),
10
- // useResolvedConfigContext: () => ({
11
- // runtime: {
12
- // masterApp: {
13
- // manifest: {
14
- // componentKey: 'test-dynamic-key',
15
- // },
16
- // },
17
- // },
18
- // deploy: {
19
- // microFrontend: 'dynamicComponentKey',
20
- // },
21
- // }),
22
- // };
23
- // });
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
+ };
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
+
24
31
 
25
32
  describe('plugin-garfish cli', () => {
26
- test('cli garfish plugin', () => {
33
+ test('cli garfish basename', async () => {
27
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({
89
+ runtime: {
90
+ masterApp: {
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'
107
+ },
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({
160
+ deploy: {
161
+ microFrontend: {
162
+ externalBasicLibrary: true,
163
+ enableHtmlEntry: false,
164
+ },
165
+ },
166
+ server: {
167
+ port: '8080'
168
+ }
169
+ });
170
+
171
+ config[0].tools.webpack({}, {
172
+ chain: webpackConfig,
173
+ webpack: jest.fn(),
174
+ env: 'development'
175
+ });
176
+
177
+ const generateConfig = webpackConfig.toConfig();
178
+ expect(generateConfig).toMatchObject({
179
+ output: {
180
+ libraryTarget: 'umd',
181
+ publicPath: '//localhost:8080/',
182
+ filename: 'index.js'
183
+ },
184
+ externals,
185
+ optimization: { runtimeChunk: false, splitChunks: { chunks: 'async' } }
186
+ });
187
+ })
188
+
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();
28
220
  });
29
221
  });