@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
  # @modern-js/plugin-garfish
2
2
 
3
+ ## 1.4.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 2520ea86: fix: garfish schema
8
+ - Updated dependencies [cc5e8001]
9
+ - Updated dependencies [2520ea86]
10
+ - Updated dependencies [6a2fd32e]
11
+ - Updated dependencies [db43dce6]
12
+ - Updated dependencies [e81fd9b7]
13
+ - Updated dependencies [1c411e71]
14
+ - @modern-js/core@1.4.6
15
+ - @modern-js/plugin-router@1.2.5
16
+ - @modern-js/utils@1.3.4
17
+
18
+ ## 1.4.0
19
+
20
+ ### Minor Changes
21
+
22
+ - 4c792f68: feat(plugin-garfish): Sub-applications automatically increment basename
23
+ feat(plugin-garfish): export common generate code function
24
+ fix(plugin-garfish): modify plugin-garfish schema config
25
+
26
+ ### Patch Changes
27
+
28
+ - 55e18278: chore: remove unused dependencies and devDependencies
29
+ - Updated dependencies [969f172f]
30
+ - Updated dependencies [4c792f68]
31
+ - Updated dependencies [4b5d4bf4]
32
+ - Updated dependencies [62f5b8c8]
33
+ - Updated dependencies [55e18278]
34
+ - Updated dependencies [4499a674]
35
+ - Updated dependencies [403f5169]
36
+ - Updated dependencies [a7f42f48]
37
+ - @modern-js/core@1.4.4
38
+ - @modern-js/utils@1.3.3
39
+ - @modern-js/runtime-core@1.2.4
40
+
3
41
  ## 1.3.1
4
42
 
5
43
  ### Patch Changes
@@ -5,70 +5,56 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
7
7
  import path from 'path';
8
- import { createRuntimeExportsUtils, getEntryOptions, PLUGIN_SCHEMAS } from '@modern-js/utils';
8
+ import { createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
9
9
  import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
10
10
  import { logger } from "../util";
11
- import { makeProvider, makeRenderFunction } from "./utils";
12
-
13
- function getRuntimeConfig(config) {
14
- var _config$runtime;
15
-
16
- if (config !== null && config !== void 0 && (_config$runtime = config.runtime) !== null && _config$runtime !== void 0 && _config$runtime.feature) {
17
- var _config$runtime2;
18
-
19
- return config === null || config === void 0 ? void 0 : (_config$runtime2 = config.runtime) === null || _config$runtime2 === void 0 ? void 0 : _config$runtime2.feature;
20
- }
21
-
22
- return config === null || config === void 0 ? void 0 : config.runtime;
23
- }
24
-
25
- function setRuntimeConfig(config, key, value) {
26
- var _config$runtime3, _config$runtime4;
11
+ import { getRuntimeConfig, makeProvider, makeRenderFunction, setRuntimeConfig } from "./utils";
12
+ export const externals = {
13
+ 'react-dom': 'react-dom',
14
+ react: 'react'
15
+ };
16
+ export const resolvedConfig = async config => {
17
+ const {
18
+ resolved
19
+ } = config;
20
+ const {
21
+ masterApp,
22
+ router
23
+ } = getRuntimeConfig(resolved);
24
+ const nConfig = {
25
+ resolved: _objectSpread({}, resolved)
26
+ };
27
27
 
28
- if (config !== null && config !== void 0 && config.runtime && config !== null && config !== void 0 && config.runtime[key]) {
29
- config.runtime[key] = value;
30
- }
28
+ if (masterApp) {
29
+ var _router$historyOption;
31
30
 
32
- if (config !== null && config !== void 0 && (_config$runtime3 = config.runtime) !== null && _config$runtime3 !== void 0 && _config$runtime3.feature && config !== null && config !== void 0 && (_config$runtime4 = config.runtime) !== null && _config$runtime4 !== void 0 && _config$runtime4.feature[key]) {
33
- config.runtime.feature[key] = value;
31
+ // basename does not exist use router's basename
32
+ setRuntimeConfig(nConfig.resolved, 'masterApp', Object.assign(typeof masterApp === 'object' ? _objectSpread({}, masterApp) : {}, {
33
+ basename: (router === null || router === void 0 ? void 0 : (_router$historyOption = router.historyOptions) === null || _router$historyOption === void 0 ? void 0 : _router$historyOption.basename) || '/'
34
+ }));
34
35
  }
35
- }
36
36
 
37
- const externals = {
38
- 'react-dom': 'react-dom',
39
- react: 'react'
37
+ logger(`resolvedConfig`, {
38
+ runtime: nConfig.resolved.runtime,
39
+ deploy: nConfig.resolved.deploy,
40
+ server: nConfig.resolved.server
41
+ });
42
+ return nConfig;
40
43
  };
41
- export default createPlugin(() => {
42
- const configMap = new Map();
44
+ export const initializer = ( // eslint-disable-next-line @typescript-eslint/no-shadow
45
+ {
46
+ resolvedConfig,
47
+ validateSchema
48
+ }, {
49
+ runtimePluginName: _runtimePluginName = '@modern-js/runtime/plugins',
50
+ defaultEnableHtmlEntry: _defaultEnableHtmlEntry = true,
51
+ defaultExternalBasicLibrary: _defaultExternalBasicLibrary = false
52
+ }) => () => {
43
53
  let pluginsExportsUtils;
44
54
  let runtimeExportsUtils;
45
55
  return {
46
- resolvedConfig(config) {
47
- const {
48
- resolved
49
- } = config;
50
- const {
51
- masterApp,
52
- router
53
- } = getRuntimeConfig(resolved);
54
- const nConfig = {
55
- resolved: _objectSpread({}, resolved)
56
- };
57
-
58
- if (masterApp) {
59
- // basename does not exist use router's basename
60
- setRuntimeConfig(nConfig.resolved, 'masterApp', Object.assign(typeof masterApp === 'object' ? _objectSpread({}, masterApp) : {}, {
61
- basename: (router === null || router === void 0 ? void 0 : router.basename) || '/'
62
- }));
63
- }
64
-
65
- logger(`resolvedConfig`, {
66
- runtime: nConfig.resolved.runtime,
67
- deploy: nConfig.resolved.deploy,
68
- server: nConfig.resolved.server
69
- });
70
- return nConfig;
71
- },
56
+ validateSchema,
57
+ resolvedConfig,
72
58
 
73
59
  config() {
74
60
  // eslint-disable-next-line react-hooks/rules-of-hooks
@@ -84,31 +70,34 @@ export default createPlugin(() => {
84
70
  tools: {
85
71
  webpack: (webpackConfig, {
86
72
  chain,
87
- webpack
73
+ webpack,
74
+ env: _env = process.env.NODE_ENV || 'development'
88
75
  }) => {
89
- var _resolvedConfig$deplo, _resolveWebpackConfig;
90
-
91
- const env = process.env.NODE_ENV; // eslint-disable-next-line react-hooks/rules-of-hooks
76
+ var _resolveOptions$deplo, _resolveWebpackConfig;
92
77
 
93
- const resolvedConfig = useResolvedConfigContext();
78
+ // eslint-disable-next-line react-hooks/rules-of-hooks
79
+ const resolveOptions = useResolvedConfigContext();
94
80
 
95
- if (resolvedConfig !== null && resolvedConfig !== void 0 && (_resolvedConfig$deplo = resolvedConfig.deploy) !== null && _resolvedConfig$deplo !== void 0 && _resolvedConfig$deplo.microFrontend) {
96
- var _resolvedConfig$deplo2, _resolvedConfig$deplo3;
81
+ if (resolveOptions !== null && resolveOptions !== void 0 && (_resolveOptions$deplo = resolveOptions.deploy) !== null && _resolveOptions$deplo !== void 0 && _resolveOptions$deplo.microFrontend) {
82
+ var _resolveOptions$serve, _resolveOptions$deplo2, _resolveOptions$deplo3;
97
83
 
98
84
  chain.output.libraryTarget('umd');
99
85
 
100
- if (resolvedConfig.server.port) {
101
- chain.output.publicPath(env === 'development' ? `//localhost:${resolvedConfig.server.port}/` : webpackConfig.output.publicPath);
86
+ if (resolveOptions !== null && resolveOptions !== void 0 && (_resolveOptions$serve = resolveOptions.server) !== null && _resolveOptions$serve !== void 0 && _resolveOptions$serve.port) {
87
+ chain.output.publicPath(_env === 'development' ? `//localhost:${resolveOptions.server.port}/` : webpackConfig.output.publicPath);
102
88
  } // add comments avoid sourcemap abnormal
103
89
 
104
90
 
105
- chain.plugin('banner').use(webpack.BannerPlugin, [{
106
- banner: 'Micro front-end'
107
- }]);
91
+ if (webpack.BannerPlugin) {
92
+ chain.plugin('banner').use(webpack.BannerPlugin, [{
93
+ banner: 'Micro front-end'
94
+ }]);
95
+ }
96
+
108
97
  const {
109
- enableHtmlEntry = true,
110
- externalBasicLibrary = true
111
- } = typeof (resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$deplo2 = resolvedConfig.deploy) === null || _resolvedConfig$deplo2 === void 0 ? void 0 : _resolvedConfig$deplo2.microFrontend) === 'object' ? resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$deplo3 = resolvedConfig.deploy) === null || _resolvedConfig$deplo3 === void 0 ? void 0 : _resolvedConfig$deplo3.microFrontend : {}; // external
98
+ enableHtmlEntry = _defaultEnableHtmlEntry,
99
+ externalBasicLibrary = _defaultExternalBasicLibrary
100
+ } = typeof (resolveOptions === null || resolveOptions === void 0 ? void 0 : (_resolveOptions$deplo2 = resolveOptions.deploy) === null || _resolveOptions$deplo2 === void 0 ? void 0 : _resolveOptions$deplo2.microFrontend) === 'object' ? resolveOptions === null || resolveOptions === void 0 ? void 0 : (_resolveOptions$deplo3 = resolveOptions.deploy) === null || _resolveOptions$deplo3 === void 0 ? void 0 : _resolveOptions$deplo3.microFrontend : {}; // external
112
101
 
113
102
  if (externalBasicLibrary) {
114
103
  chain.externals(externals);
@@ -129,7 +118,7 @@ export default createPlugin(() => {
129
118
  logger('webpackConfig', {
130
119
  output: resolveWebpackConfig.output,
131
120
  externals: resolveWebpackConfig.externals,
132
- env,
121
+ env: _env,
133
122
  alias: (_resolveWebpackConfig = resolveWebpackConfig.resolve) === null || _resolveWebpackConfig === void 0 ? void 0 : _resolveWebpackConfig.alias
134
123
  });
135
124
  }
@@ -139,40 +128,35 @@ export default createPlugin(() => {
139
128
 
140
129
  addRuntimeExports() {
141
130
  const mfPackage = path.resolve(__dirname, '../../../../');
142
- const addExportStatement = `export { default as garfish } from '${mfPackage}'`;
131
+ const addExportStatement = `export { default as garfish, default as masterApp } from '${mfPackage}'`;
143
132
  logger('exportStatement', addExportStatement);
144
133
  pluginsExportsUtils.addExport(addExportStatement);
145
134
  runtimeExportsUtils.addExport(`export * from '${mfPackage}'`);
146
135
  },
147
136
 
148
- validateSchema() {
149
- return PLUGIN_SCHEMAS['@modern-js/plugin-garfish'];
150
- },
151
-
152
137
  modifyEntryImports({
153
138
  entrypoint,
154
139
  imports
155
140
  }) {
156
141
  // eslint-disable-next-line react-hooks/rules-of-hooks
157
- const resolvedConfig = useResolvedConfigContext(); // support legacy config
158
-
142
+ const config = useResolvedConfigContext();
159
143
  const {
160
144
  masterApp
161
- } = getRuntimeConfig(resolvedConfig); // eslint-disable-next-line react-hooks/rules-of-hooks
145
+ } = getRuntimeConfig(config);
162
146
 
163
- const {
164
- packageName
165
- } = useAppContext();
166
- const masterAppConfig = getEntryOptions(entrypoint.entryName, masterApp, resolvedConfig.runtimeByEntries, packageName);
167
- configMap.set(entrypoint.entryName, masterAppConfig);
168
-
169
- if (masterAppConfig) {
147
+ if (masterApp) {
170
148
  imports.push({
171
- value: '@modern-js/runtime/plugins',
149
+ value: _runtimePluginName,
172
150
  specifiers: [{
173
151
  imported: 'garfish'
174
152
  }]
175
153
  });
154
+ imports.push({
155
+ value: _runtimePluginName,
156
+ specifiers: [{
157
+ imported: 'masterApp'
158
+ }]
159
+ });
176
160
  }
177
161
 
178
162
  imports.push({
@@ -193,14 +177,18 @@ export default createPlugin(() => {
193
177
  entrypoint,
194
178
  plugins
195
179
  }) {
196
- const masterAppConfig = configMap.get(entrypoint.entryName);
180
+ // eslint-disable-next-line react-hooks/rules-of-hooks
181
+ const config = useResolvedConfigContext();
182
+ const {
183
+ masterApp
184
+ } = getRuntimeConfig(config);
197
185
 
198
- if (masterAppConfig) {
199
- logger('garfishPlugin options', masterAppConfig);
186
+ if (masterApp) {
187
+ logger('garfishPlugin options', masterApp);
200
188
  plugins.push({
201
189
  name: 'garfish',
202
190
  args: 'masterApp',
203
- options: JSON.stringify(masterAppConfig)
191
+ options: masterApp === true ? JSON.stringify({}) : JSON.stringify(masterApp)
204
192
  });
205
193
  }
206
194
 
@@ -238,12 +226,12 @@ export default createPlugin(() => {
238
226
  entrypoint,
239
227
  exportStatement
240
228
  }) {
241
- var _resolvedConfig$deplo4;
229
+ var _config$deploy2;
242
230
 
243
231
  // eslint-disable-next-line react-hooks/rules-of-hooks
244
- const resolvedConfig = useResolvedConfigContext();
232
+ const config = useResolvedConfigContext();
245
233
 
246
- if (resolvedConfig !== null && resolvedConfig !== void 0 && (_resolvedConfig$deplo4 = resolvedConfig.deploy) !== null && _resolvedConfig$deplo4 !== void 0 && _resolvedConfig$deplo4.microFrontend) {
234
+ if (config !== null && config !== void 0 && (_config$deploy2 = config.deploy) !== null && _config$deploy2 !== void 0 && _config$deploy2.microFrontend) {
247
235
  const exportStatementCode = makeProvider();
248
236
  logger('exportStatement', exportStatementCode);
249
237
  return {
@@ -259,6 +247,16 @@ export default createPlugin(() => {
259
247
  }
260
248
 
261
249
  };
250
+ };
251
+ export default createPlugin(initializer({
252
+ resolvedConfig,
253
+
254
+ validateSchema() {
255
+ return PLUGIN_SCHEMAS['@modern-js/plugin-garfish'];
256
+ }
257
+
262
258
  }, {
259
+ runtimePluginName: '@modern-js/runtime/plugins'
260
+ }), {
263
261
  name: '@modern-js/plugin-garfish'
264
262
  });
@@ -1,31 +1,90 @@
1
1
  export const makeProvider = () => `
2
- export const provider = function ({basename, dom, ...props}) {
3
- return {
4
- render({basename, dom}) {
5
- const SubApp = render(props, basename);
6
- const node = dom.querySelector('#' + MOUNT_ID) || dom;
7
-
8
- bootstrap(SubApp, node);
9
- },
10
- destroy({ dom }) {
11
- const node = dom.querySelector('#' + MOUNT_ID) || dom;
12
-
13
- if (node) {
14
- unmountComponentAtNode(node);
15
- }
16
- },
17
- SubModuleComponent: (props) => {
18
- const SubApp = render(props, basename);
2
+ export const provider = function ({basename, dom, ...props}) {
3
+ return {
4
+ render({basename, dom}) {
5
+ console.log('App.config', App.config);
6
+ const SubApp = render({props, basename});
7
+ const node = dom.querySelector('#' + MOUNT_ID) || dom;
8
+ bootstrap(SubApp, node);
9
+ },
10
+ destroy({ dom }) {
11
+ const node = dom.querySelector('#' + MOUNT_ID) || dom;
19
12
 
20
- return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
13
+ if (node) {
14
+ unmountComponentAtNode(node);
21
15
  }
16
+ },
17
+ SubModuleComponent: (props) => {
18
+ const SubApp = render({props, basename});
19
+
20
+ return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
21
+ },
22
+ jupiter_submodule_app_key: (props) => {
23
+ const SubApp = render({props, basename});
24
+
25
+ return createPortal(<SubApp />, dom.querySelector('#' + MOUNT_ID) || dom);
22
26
  }
23
- };
24
- if (typeof __GARFISH_EXPORTS__ !== 'undefined') {
25
- __GARFISH_EXPORTS__.provider = provider;
26
27
  }
28
+ };
29
+
30
+ if (typeof __GARFISH_EXPORTS__ !== 'undefined') {
31
+ __GARFISH_EXPORTS__.provider = provider;
32
+ }
27
33
  `;
28
- export const makeRenderFunction = code => code.replace('IS_BROWSER', `
29
- IS_BROWSER &&
30
- typeof __GARFISH_EXPORTS__ === 'undefined'
31
- `).replace('(App)', '(() => <App {...(arguments[0] || {})} />)').replace('"basename":"/"', '"basename":arguments[1] || "/"');
34
+ export const makeRenderFunction = code => {
35
+ const inGarfishToRender = `
36
+ const { basename, props } = arguments[0] || {};
37
+ let renderByGarfish = false;
38
+ const renderByProvider = !!basename;
39
+
40
+ if (IS_BROWSER && window.Garfish && window.Garfish.activeApps && window.Garfish.activeApps.length !== 0) renderByGarfish = true;
41
+ if (IS_BROWSER && window.Garfish && window.Garfish.apps && Object.keys(window.Garfish.apps).length !== 0) renderByGarfish = true;
42
+ if (typeof __GARFISH_EXPORTS__ !== 'undefined') renderByGarfish = true;
43
+ if (renderByGarfish && !renderByProvider) return null;
44
+
45
+ function RouterPlugin (routerConfig) {
46
+ if (basename) {
47
+ routerConfig.basename = basename;
48
+ if (routerConfig.supportHtml5History !== false) {
49
+ if (!routerConfig.historyOptions) {
50
+ routerConfig.historyOptions = {
51
+ basename: basename
52
+ };
53
+ } else {
54
+ routerConfig.historyOptions.basename = basename;
55
+ }
56
+ }
57
+ }
58
+ return router(routerConfig);
59
+ }
60
+ `;
61
+ return inGarfishToRender + code.replace(`router(`, `RouterPlugin(`).replace('IS_BROWSER', `IS_BROWSER && !renderByGarfish`);
62
+ }; // support legacy config
63
+
64
+ export function getRuntimeConfig(config) {
65
+ var _config$runtime;
66
+
67
+ if (config !== null && config !== void 0 && (_config$runtime = config.runtime) !== null && _config$runtime !== void 0 && _config$runtime.features) {
68
+ var _config$runtime2;
69
+
70
+ return config === null || config === void 0 ? void 0 : (_config$runtime2 = config.runtime) === null || _config$runtime2 === void 0 ? void 0 : _config$runtime2.features;
71
+ }
72
+
73
+ return config === null || config === void 0 ? void 0 : config.runtime;
74
+ } // support legacy config
75
+
76
+ export function setRuntimeConfig(config, key, value) {
77
+ var _config$runtime3, _config$runtime4;
78
+
79
+ if (config !== null && config !== void 0 && (_config$runtime3 = config.runtime) !== null && _config$runtime3 !== void 0 && _config$runtime3.features && config !== null && config !== void 0 && (_config$runtime4 = config.runtime) !== null && _config$runtime4 !== void 0 && _config$runtime4.features[key]) {
80
+ config.runtime.features[key] = value;
81
+ return undefined;
82
+ }
83
+
84
+ if (config !== null && config !== void 0 && config.runtime && config !== null && config !== void 0 && config.runtime[key]) {
85
+ config.runtime[key] = value;
86
+ return undefined;
87
+ }
88
+
89
+ return undefined;
90
+ }
@@ -27,13 +27,21 @@ async function initOptions(manifest = {}, options) {
27
27
  let apps = options.apps || []; // use manifest modules
28
28
 
29
29
  if (manifest !== null && manifest !== void 0 && manifest.modules) {
30
- apps = manifest === null || manifest === void 0 ? void 0 : manifest.modules;
30
+ if ((manifest === null || manifest === void 0 ? void 0 : manifest.modules.length) > 0) {
31
+ apps = manifest === null || manifest === void 0 ? void 0 : manifest.modules;
32
+ }
33
+
31
34
  logger('manifest modules', apps);
32
35
  } // get module list
33
36
 
34
37
 
35
38
  if (manifest !== null && manifest !== void 0 && manifest.getAppList) {
36
- apps = await (manifest === null || manifest === void 0 ? void 0 : manifest.getAppList());
39
+ const getAppList = await (manifest === null || manifest === void 0 ? void 0 : manifest.getAppList());
40
+
41
+ if (getAppList.length > 0) {
42
+ apps = getAppList;
43
+ }
44
+
37
45
  logger('getAppList modules', apps);
38
46
  } // get inject modules list
39
47
 
@@ -68,7 +68,7 @@ export function generateMApp(options, manifest) {
68
68
  },
69
69
 
70
70
  errorLoadApp(error, ...args) {
71
- logger('MApp errorLoadApp', args);
71
+ logger('MApp errorLoadApp', error, args);
72
72
  setLoadingState({
73
73
  error
74
74
  });
@@ -76,7 +76,7 @@ export function generateMApp(options, manifest) {
76
76
  },
77
77
 
78
78
  errorMountApp(error, ...args) {
79
- logger('MApp errorMountApp', args);
79
+ logger('MApp errorMountApp', error, args);
80
80
  setLoadingState({
81
81
  error
82
82
  });
@@ -84,7 +84,7 @@ export function generateMApp(options, manifest) {
84
84
  },
85
85
 
86
86
  errorUnmountApp(error, ...args) {
87
- logger('MApp errorUnmountApp', args);
87
+ logger('MApp errorUnmountApp', error, args);
88
88
  setLoadingState({
89
89
  error
90
90
  });
@@ -27,7 +27,7 @@ import { Loadable } from "../loadable"; // type Provider = {
27
27
  import { jsx as _jsx } from "react/jsx-runtime";
28
28
  import { Fragment as _Fragment } from "react/jsx-runtime";
29
29
 
30
- function getAppInstance(appInfo, manifest) {
30
+ function getAppInstance(options, appInfo, manifest) {
31
31
  let locationHref = '';
32
32
 
33
33
  class MicroApp extends React.Component {
@@ -52,9 +52,6 @@ function getAppInstance(appInfo, manifest) {
52
52
  const {
53
53
  domId
54
54
  } = this.state;
55
- const {
56
- options
57
- } = Garfish;
58
55
 
59
56
  const loadAppOptions = _objectSpread(_objectSpread({}, appInfo), {}, {
60
57
  domGetter: `#${domId}`,
@@ -175,7 +172,7 @@ export function generateApps(options, manifest) {
175
172
 
176
173
  const apps = {};
177
174
  (_options$apps = options.apps) === null || _options$apps === void 0 ? void 0 : _options$apps.forEach(appInfo => {
178
- const Component = getAppInstance(appInfo, manifest);
175
+ const Component = getAppInstance(options, appInfo, manifest);
179
176
  appInfo.Component = Component;
180
177
  apps[appInfo.name] = Component;
181
178
  });