@modern-js/plugin-garfish 1.4.2 → 1.4.4-beta.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,14 @@
1
1
  # @modern-js/plugin-garfish
2
2
 
3
+ ## 1.4.3
4
+
5
+ ### Patch Changes
6
+
7
+ - e27cefee: feat(plugin-garfish): compatible garfish hooks
8
+ - a4dd12c9: feat(plugin-garfish): support automatic parameter transfer
9
+ - Updated dependencies [5bf5868d]
10
+ - @modern-js/utils@1.3.5
11
+
3
12
  ## 1.4.2
4
13
 
5
14
  ### Patch Changes
@@ -6,257 +6,248 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
6
6
 
7
7
  import path from 'path';
8
8
  import { createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
9
- import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
10
9
  import { logger } from "../util";
11
10
  import { getRuntimeConfig, makeProvider, makeRenderFunction, setRuntimeConfig } from "./utils";
12
11
  export const externals = {
13
12
  'react-dom': 'react-dom',
14
13
  react: 'react'
15
14
  };
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
-
28
- if (masterApp) {
29
- var _router$historyOption;
30
-
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
- }));
35
- }
36
-
37
- logger(`resolvedConfig`, {
38
- runtime: nConfig.resolved.runtime,
39
- deploy: nConfig.resolved.deploy,
40
- server: nConfig.resolved.server
41
- });
42
- return nConfig;
43
- };
44
- export const initializer = ( // eslint-disable-next-line @typescript-eslint/no-shadow
45
- {
46
- resolvedConfig,
47
- validateSchema
48
- }, {
15
+ export default (({
49
16
  runtimePluginName: _runtimePluginName = '@modern-js/runtime/plugins',
50
- defaultEnableHtmlEntry: _defaultEnableHtmlEntry = true,
51
- defaultExternalBasicLibrary: _defaultExternalBasicLibrary = false
52
- }) => () => {
53
- let pluginsExportsUtils;
54
- let runtimeExportsUtils;
55
- return {
56
- validateSchema,
57
- resolvedConfig,
58
-
59
- config() {
60
- // eslint-disable-next-line react-hooks/rules-of-hooks
61
- const config = useAppContext();
62
- pluginsExportsUtils = createRuntimeExportsUtils(config.internalDirectory, 'plugins');
63
- runtimeExportsUtils = createRuntimeExportsUtils(config.internalDirectory, 'index');
64
- return {
65
- source: {
66
- alias: {
67
- '@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
68
- }
69
- },
70
- tools: {
71
- webpack: (webpackConfig, {
72
- chain,
73
- webpack,
74
- env: _env = process.env.NODE_ENV || 'development'
75
- }) => {
76
- var _resolveOptions$deplo, _resolveWebpackConfig;
17
+ mfPackagePath: _mfPackagePath = path.resolve(__dirname, '../../../../')
18
+ } = {}) => ({
19
+ name: '@modern-js/plugin-garfish',
20
+ setup: ({
21
+ useAppContext,
22
+ useResolvedConfigContext
23
+ }) => {
24
+ let pluginsExportsUtils;
25
+ let runtimeExportsUtils;
26
+ return {
27
+ validateSchema() {
28
+ return PLUGIN_SCHEMAS['@modern-js/plugin-garfish'];
29
+ },
30
+
31
+ resolvedConfig: async config => {
32
+ const {
33
+ resolved
34
+ } = config;
35
+ const {
36
+ masterApp,
37
+ router
38
+ } = getRuntimeConfig(resolved);
39
+ const nConfig = {
40
+ resolved: _objectSpread({}, resolved)
41
+ };
77
42
 
78
- // eslint-disable-next-line react-hooks/rules-of-hooks
79
- const resolveOptions = useResolvedConfigContext();
43
+ if (masterApp) {
44
+ var _router$historyOption;
80
45
 
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;
46
+ // basename does not exist use router's basename
47
+ setRuntimeConfig(nConfig.resolved, 'masterApp', Object.assign(typeof masterApp === 'object' ? _objectSpread({}, masterApp) : {}, {
48
+ basename: (router === null || router === void 0 ? void 0 : (_router$historyOption = router.historyOptions) === null || _router$historyOption === void 0 ? void 0 : _router$historyOption.basename) || (router === null || router === void 0 ? void 0 : router.basename) || '/'
49
+ }));
50
+ }
83
51
 
84
- chain.output.libraryTarget('umd');
52
+ logger(`resolvedConfig`, {
53
+ runtime: nConfig.resolved.runtime,
54
+ deploy: nConfig.resolved.deploy,
55
+ server: nConfig.resolved.server
56
+ });
57
+ return nConfig;
58
+ },
85
59
 
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);
88
- } // add comments avoid sourcemap abnormal
60
+ config() {
61
+ // eslint-disable-next-line react-hooks/rules-of-hooks
62
+ const config = useAppContext(); // eslint-disable-next-line react-hooks/rules-of-hooks
89
63
 
64
+ const resolveOptions = useResolvedConfigContext();
65
+ pluginsExportsUtils = createRuntimeExportsUtils(config.internalDirectory, 'plugins');
66
+ runtimeExportsUtils = createRuntimeExportsUtils(config.internalDirectory, 'index');
67
+ return {
68
+ source: {
69
+ alias: {
70
+ '@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
71
+ }
72
+ },
73
+ tools: {
74
+ webpack: (webpackConfig, {
75
+ chain,
76
+ webpack,
77
+ env: _env = process.env.NODE_ENV || 'development'
78
+ }) => {
79
+ var _resolveOptions$deplo, _resolveWebpackConfig;
80
+
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;
83
+
84
+ chain.output.libraryTarget('umd');
85
+ chain.devServer.headers({
86
+ 'Access-Control-Allow-Origin': '*'
87
+ });
90
88
 
91
- if (webpack.BannerPlugin) {
92
- chain.plugin('banner').use(webpack.BannerPlugin, [{
93
- banner: 'Micro front-end'
94
- }]);
95
- }
89
+ if (resolveOptions !== null && resolveOptions !== void 0 && (_resolveOptions$serve = resolveOptions.server) !== null && _resolveOptions$serve !== void 0 && _resolveOptions$serve.port) {
90
+ chain.output.publicPath(_env === 'development' ? `//localhost:${resolveOptions.server.port}/` : webpackConfig.output.publicPath);
91
+ } // add comments avoid sourcemap abnormal
96
92
 
97
- const {
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
101
93
 
102
- if (externalBasicLibrary) {
103
- chain.externals(externals);
104
- } // use html mode
94
+ if (webpack.BannerPlugin) {
95
+ chain.plugin('banner').use(webpack.BannerPlugin, [{
96
+ banner: 'Micro front-end'
97
+ }]);
98
+ }
105
99
 
100
+ const {
101
+ enableHtmlEntry = true,
102
+ externalBasicLibrary = false
103
+ } = 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
106
104
 
107
- if (!enableHtmlEntry) {
108
- chain.output.filename('index.js');
109
- chain.plugins.delete('html-main');
110
- chain.optimization.runtimeChunk(false);
111
- chain.optimization.splitChunks({
112
- chunks: 'async'
113
- });
105
+ if (externalBasicLibrary) {
106
+ chain.externals(externals);
107
+ } // use html mode
108
+
109
+
110
+ if (!enableHtmlEntry) {
111
+ chain.output.filename('index.js');
112
+ chain.plugins.delete('html-main');
113
+ chain.optimization.runtimeChunk(false);
114
+ chain.optimization.splitChunks({
115
+ chunks: 'async'
116
+ });
117
+ }
114
118
  }
115
- }
116
119
 
117
- const resolveWebpackConfig = chain.toConfig();
118
- logger('webpackConfig', {
119
- output: resolveWebpackConfig.output,
120
- externals: resolveWebpackConfig.externals,
121
- env: _env,
122
- alias: (_resolveWebpackConfig = resolveWebpackConfig.resolve) === null || _resolveWebpackConfig === void 0 ? void 0 : _resolveWebpackConfig.alias
123
- });
120
+ const resolveWebpackConfig = chain.toConfig();
121
+ logger('webpackConfig', {
122
+ output: resolveWebpackConfig.output,
123
+ externals: resolveWebpackConfig.externals,
124
+ env: _env,
125
+ alias: (_resolveWebpackConfig = resolveWebpackConfig.resolve) === null || _resolveWebpackConfig === void 0 ? void 0 : _resolveWebpackConfig.alias
126
+ });
127
+ }
124
128
  }
129
+ };
130
+ },
131
+
132
+ addRuntimeExports() {
133
+ const addExportStatement = `export { default as garfish, default as masterApp } from '${_mfPackagePath}'`;
134
+ logger('exportStatement', addExportStatement);
135
+ pluginsExportsUtils.addExport(addExportStatement);
136
+ runtimeExportsUtils.addExport(`export * from '${_mfPackagePath}'`);
137
+ },
138
+
139
+ modifyEntryImports({
140
+ entrypoint,
141
+ imports
142
+ }) {
143
+ // eslint-disable-next-line react-hooks/rules-of-hooks
144
+ const config = useResolvedConfigContext();
145
+ const {
146
+ masterApp
147
+ } = getRuntimeConfig(config);
148
+
149
+ if (masterApp) {
150
+ imports.push({
151
+ value: _runtimePluginName,
152
+ specifiers: [{
153
+ imported: 'garfish'
154
+ }]
155
+ });
156
+ imports.push({
157
+ value: _runtimePluginName,
158
+ specifiers: [{
159
+ imported: 'masterApp'
160
+ }]
161
+ });
125
162
  }
126
- };
127
- },
128
-
129
- addRuntimeExports() {
130
- const mfPackage = path.resolve(__dirname, '../../../../');
131
- const addExportStatement = `export { default as garfish, default as masterApp } from '${mfPackage}'`;
132
- logger('exportStatement', addExportStatement);
133
- pluginsExportsUtils.addExport(addExportStatement);
134
- runtimeExportsUtils.addExport(`export * from '${mfPackage}'`);
135
- },
136
-
137
- modifyEntryImports({
138
- entrypoint,
139
- imports
140
- }) {
141
- // eslint-disable-next-line react-hooks/rules-of-hooks
142
- const config = useResolvedConfigContext();
143
- const {
144
- masterApp
145
- } = getRuntimeConfig(config);
146
-
147
- if (masterApp) {
148
- imports.push({
149
- value: _runtimePluginName,
150
- specifiers: [{
151
- imported: 'garfish'
152
- }]
153
- });
163
+
154
164
  imports.push({
155
- value: _runtimePluginName,
165
+ value: 'react-dom',
156
166
  specifiers: [{
157
- imported: 'masterApp'
167
+ imported: 'unmountComponentAtNode'
168
+ }, {
169
+ imported: 'createPortal'
158
170
  }]
159
171
  });
160
- }
161
-
162
- imports.push({
163
- value: 'react-dom',
164
- specifiers: [{
165
- imported: 'unmountComponentAtNode'
166
- }, {
167
- imported: 'createPortal'
168
- }]
169
- });
170
- return {
171
- imports,
172
- entrypoint
173
- };
174
- },
175
-
176
- modifyEntryRuntimePlugins({
177
- entrypoint,
178
- plugins
179
- }) {
180
- // eslint-disable-next-line react-hooks/rules-of-hooks
181
- const config = useResolvedConfigContext();
182
- const {
183
- masterApp
184
- } = getRuntimeConfig(config);
185
-
186
- if (masterApp) {
187
- logger('garfishPlugin options', masterApp);
188
- plugins.push({
189
- name: 'garfish',
190
- args: 'masterApp',
191
- options: masterApp === true ? JSON.stringify({}) : JSON.stringify(masterApp)
192
- });
193
- }
172
+ return {
173
+ imports,
174
+ entrypoint
175
+ };
176
+ },
194
177
 
195
- return {
178
+ modifyEntryRuntimePlugins({
196
179
  entrypoint,
197
180
  plugins
198
- };
199
- },
200
-
201
- modifyEntryRenderFunction({
202
- entrypoint,
203
- code
204
- }) {
205
- var _config$deploy;
206
-
207
- // eslint-disable-next-line react-hooks/rules-of-hooks
208
- const config = useResolvedConfigContext();
181
+ }) {
182
+ // eslint-disable-next-line react-hooks/rules-of-hooks
183
+ const config = useResolvedConfigContext();
184
+ const {
185
+ masterApp
186
+ } = getRuntimeConfig(config);
187
+
188
+ if (masterApp) {
189
+ logger('garfishPlugin options', masterApp);
190
+ plugins.push({
191
+ name: 'garfish',
192
+ args: 'masterApp',
193
+ options: masterApp === true ? JSON.stringify({}) : JSON.stringify(masterApp)
194
+ });
195
+ }
209
196
 
210
- if (!(config !== null && config !== void 0 && (_config$deploy = config.deploy) !== null && _config$deploy !== void 0 && _config$deploy.microFrontend)) {
211
197
  return {
212
198
  entrypoint,
213
- code
199
+ plugins
214
200
  };
215
- }
201
+ },
216
202
 
217
- const nCode = makeRenderFunction(code);
218
- logger('makeRenderFunction', nCode);
219
- return {
203
+ modifyEntryRenderFunction({
220
204
  entrypoint,
221
- code: nCode
222
- };
223
- },
224
-
225
- modifyEntryExport({
226
- entrypoint,
227
- exportStatement
228
- }) {
229
- var _config$deploy2;
230
-
231
- // eslint-disable-next-line react-hooks/rules-of-hooks
232
- const config = useResolvedConfigContext();
233
-
234
- if (config !== null && config !== void 0 && (_config$deploy2 = config.deploy) !== null && _config$deploy2 !== void 0 && _config$deploy2.microFrontend) {
235
- const exportStatementCode = makeProvider();
236
- logger('exportStatement', exportStatementCode);
205
+ code
206
+ }) {
207
+ var _config$deploy;
208
+
209
+ // eslint-disable-next-line react-hooks/rules-of-hooks
210
+ const config = useResolvedConfigContext();
211
+
212
+ if (!(config !== null && config !== void 0 && (_config$deploy = config.deploy) !== null && _config$deploy !== void 0 && _config$deploy.microFrontend)) {
213
+ return {
214
+ entrypoint,
215
+ code
216
+ };
217
+ }
218
+
219
+ const nCode = makeRenderFunction(code);
220
+ logger('makeRenderFunction', nCode);
237
221
  return {
238
222
  entrypoint,
239
- exportStatement: exportStatementCode
223
+ code: nCode
240
224
  };
241
- }
225
+ },
242
226
 
243
- return {
227
+ modifyEntryExport({
244
228
  entrypoint,
245
229
  exportStatement
246
- };
247
- }
230
+ }) {
231
+ var _config$deploy2;
232
+
233
+ // eslint-disable-next-line react-hooks/rules-of-hooks
234
+ const config = useResolvedConfigContext();
235
+
236
+ if (config !== null && config !== void 0 && (_config$deploy2 = config.deploy) !== null && _config$deploy2 !== void 0 && _config$deploy2.microFrontend) {
237
+ const exportStatementCode = makeProvider();
238
+ logger('exportStatement', exportStatementCode);
239
+ return {
240
+ entrypoint,
241
+ exportStatement: exportStatementCode
242
+ };
243
+ }
248
244
 
249
- };
250
- };
251
- export default createPlugin(initializer({
252
- resolvedConfig,
245
+ return {
246
+ entrypoint,
247
+ exportStatement
248
+ };
249
+ }
253
250
 
254
- validateSchema() {
255
- return PLUGIN_SCHEMAS['@modern-js/plugin-garfish'];
251
+ };
256
252
  }
257
-
258
- }, {
259
- runtimePluginName: '@modern-js/runtime/plugins'
260
- }), {
261
- name: '@modern-js/plugin-garfish'
262
- });
253
+ }));
@@ -1,11 +1,13 @@
1
1
  export const makeProvider = () => `
2
- export const provider = function ({basename, dom, ...props}) {
2
+ export const provider = function ({basename, dom}) {
3
3
  return {
4
- render({basename, dom}) {
5
- console.log('App.config', App.config);
6
- const SubApp = render({props, basename});
4
+ render({basename, dom, props}) {
5
+ const SubApp = render({ props, basename });
7
6
  const node = dom.querySelector('#' + MOUNT_ID) || dom;
8
- bootstrap(SubApp, node);
7
+ const App = function () {
8
+ return React.createElement(SubApp, props)
9
+ };
10
+ bootstrap(App, node);
9
11
  },
10
12
  destroy({ dom }) {
11
13
  const node = dom.querySelector('#' + MOUNT_ID) || dom;
@@ -70,7 +72,7 @@ export function getRuntimeConfig(config) {
70
72
  return config === null || config === void 0 ? void 0 : (_config$runtime2 = config.runtime) === null || _config$runtime2 === void 0 ? void 0 : _config$runtime2.features;
71
73
  }
72
74
 
73
- return config === null || config === void 0 ? void 0 : config.runtime;
75
+ return (config === null || config === void 0 ? void 0 : config.runtime) || {};
74
76
  } // support legacy config
75
77
 
76
78
  export function setRuntimeConfig(config, key, value) {
@@ -1,3 +1,3 @@
1
1
  export { default } from "./plugin";
2
- export { useModuleApps, useMicroApps, useModuleApp } from "./useModuleApps";
2
+ export { useModuleApps, useModuleApp } from "./useModuleApps";
3
3
  export { default as Garfish } from 'garfish';
@@ -83,8 +83,7 @@ const GarfishPlugin = config => {
83
83
  return /*#__PURE__*/React.createElement('div');
84
84
  },
85
85
  apps: new Proxy({}, {
86
- get(_target, _p) {
87
- logger('apps init Component Render', _p);
86
+ get() {
88
87
  return () => /*#__PURE__*/React.createElement('div');
89
88
  }
90
89
 
@@ -1,12 +1,36 @@
1
- import { useContext } from 'react';
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
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
+
7
+ import React, { useContext } from 'react';
2
8
  import { logger } from "../util";
3
9
  import { GarfishContext } from "./utils/Context";
4
10
  export function useModuleApps() {
5
11
  const {
6
- apps
12
+ apps,
13
+ MApp,
14
+ appInfoList
7
15
  } = useContext(GarfishContext);
8
- logger('call useModuleApps', apps);
9
- return apps;
16
+ logger('call useModuleApps', _objectSpread({
17
+ MApp,
18
+ apps: appInfoList
19
+ }, apps));
20
+ const Info = new Proxy(_objectSpread({
21
+ MApp,
22
+ apps: appInfoList
23
+ }, apps), {
24
+ get(target, p, receiver) {
25
+ if (typeof p === 'string' && p in target) {
26
+ return Reflect.get(target, p, receiver);
27
+ }
28
+
29
+ return () => /*#__PURE__*/React.createElement('div');
30
+ }
31
+
32
+ });
33
+ return Info;
10
34
  }
11
35
  export function useModuleApp() {
12
36
  const {
@@ -14,21 +38,4 @@ export function useModuleApp() {
14
38
  } = useContext(GarfishContext);
15
39
  logger('call useModuleApps', MApp);
16
40
  return MApp;
17
- }
18
- export function useMicroApps() {
19
- const {
20
- MApp,
21
- appInfoList,
22
- apps
23
- } = useContext(GarfishContext);
24
- logger('call useMicroApps', {
25
- MApp,
26
- apps: appInfoList,
27
- Components: apps
28
- });
29
- return {
30
- MApp,
31
- apps: appInfoList,
32
- Components: apps
33
- };
34
41
  }