@umijs/plugins 4.0.15 → 4.0.16

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.
@@ -42,6 +42,8 @@ __export(slave_exports, {
42
42
  default: () => slave_default
43
43
  });
44
44
  module.exports = __toCommonJS(slave_exports);
45
+ var import_http_proxy_middleware = require("@umijs/bundler-webpack/compiled/http-proxy-middleware");
46
+ var import_utils = require("@umijs/utils");
45
47
  var import_assert = __toESM(require("assert"));
46
48
  var import_fs = require("fs");
47
49
  var import_path = require("path");
@@ -49,6 +51,42 @@ var import_umi = require("umi");
49
51
  var import_plugin_utils = require("umi/plugin-utils");
50
52
  var import_withTmpPath = require("../utils/withTmpPath");
51
53
  var import_constants = require("./constants");
54
+ function getCurrentLocalDevServerEntry(api, req) {
55
+ const port = api.appData.port;
56
+ const hostname = req.hostname;
57
+ const protocol = req.protocol;
58
+ return `${protocol}://${hostname}${port ? ":" : ""}${port}/local-dev-server`;
59
+ }
60
+ function handleOriginalHtml(api, microAppEntry, originalHtml) {
61
+ const appName = api.pkg.name;
62
+ (0, import_assert.default)(appName, "[@umijs/plugin-qiankun]: You should have name in package.json");
63
+ const $ = import_utils.cheerio.load(originalHtml);
64
+ $("head").prepend(`<script type="extra-qiankun-config">${JSON.stringify({
65
+ master: {
66
+ apps: [
67
+ {
68
+ name: appName,
69
+ entry: microAppEntry,
70
+ extraSource: microAppEntry
71
+ }
72
+ ],
73
+ routes: [
74
+ {
75
+ microApp: appName,
76
+ name: appName,
77
+ path: "/" + appName,
78
+ extraSource: microAppEntry
79
+ }
80
+ ],
81
+ prefetch: false
82
+ }
83
+ })}<\/script>`);
84
+ return api.applyPlugins({
85
+ key: "modifyMasterHTML",
86
+ type: api.ApplyPluginsType.modify,
87
+ initialValue: $.html()
88
+ });
89
+ }
52
90
  function isSlaveEnable(opts) {
53
91
  var _a, _b;
54
92
  const slaveCfg = (_b = (_a = opts.userConfig) == null ? void 0 : _a.qiankun) == null ? void 0 : _b.slave;
@@ -194,6 +232,56 @@ export { connectMaster } from './connectMaster';
194
232
  },
195
233
  before: "model"
196
234
  });
235
+ api.addMiddlewares(async () => {
236
+ return async (req, res, next) => {
237
+ var _a;
238
+ const qiankunConfig = api.config.qiankun || {};
239
+ const masterEntry = (_a = qiankunConfig.slave) == null ? void 0 : _a.masterEntry;
240
+ const isMasterApp = qiankunConfig.master.enable;
241
+ const { proxyToMasterEnabled } = await api.applyPlugins({
242
+ key: "shouldProxyToMaster",
243
+ type: api.ApplyPluginsType.modify,
244
+ initialValue: { proxyToMasterEnabled: true, req }
245
+ }) ?? {};
246
+ if (masterEntry && proxyToMasterEnabled) {
247
+ if (isMasterApp) {
248
+ api.logger.warn("You should not set masterEntry in master app ");
249
+ return next();
250
+ }
251
+ return (0, import_http_proxy_middleware.createProxyMiddleware)((pathname) => pathname !== "/local-dev-server", {
252
+ target: masterEntry,
253
+ secure: false,
254
+ ignorePath: false,
255
+ followRedirects: false,
256
+ changeOrigin: true,
257
+ selfHandleResponse: true,
258
+ onProxyRes: (0, import_http_proxy_middleware.responseInterceptor)(async (responseBuffer, proxyRes, req2, res2) => {
259
+ if (proxyRes.statusCode === 302) {
260
+ const hostname = req2.hostname;
261
+ const port = process.env.PORT;
262
+ const goto = `${hostname}:${port}`;
263
+ const redirectUrl = proxyRes.headers.location.replace(encodeURIComponent(new URL(masterEntry).hostname), encodeURIComponent(goto)) || masterEntry;
264
+ const redirectMessage = `[@umijs/plugin-qiankun]: redirect to ${redirectUrl}`;
265
+ api.logger.info(redirectMessage);
266
+ res2.statusCode = 302;
267
+ res2.setHeader("location", redirectUrl);
268
+ return redirectMessage;
269
+ }
270
+ const microAppEntry = getCurrentLocalDevServerEntry(api, req2);
271
+ const originalHtml = responseBuffer.toString("utf8");
272
+ const html = handleOriginalHtml(api, microAppEntry, originalHtml);
273
+ return html;
274
+ }),
275
+ onError(err, _, res2) {
276
+ api.logger.error(err);
277
+ res2.set("content-type", "text/plain; charset=UTF-8");
278
+ res2.end(`[@umijs/plugin-qiankun] \u4EE3\u7406\u5230 ${masterEntry} \u65F6\u51FA\u9519\u4E86\uFF0C\u8BF7\u5C1D\u8BD5 ${masterEntry} \u662F\u5426\u662F\u53EF\u4EE5\u6B63\u5E38\u8BBF\u95EE\u7684\uFF0C\u7136\u540E\u91CD\u65B0\u542F\u52A8\u9879\u76EE\u8BD5\u8BD5\u3002(\u6CE8\u610F\u5982\u679C\u51FA\u73B0\u8DE8\u57DF\u95EE\u9898\uFF0C\u8BF7\u4FEE\u6539\u672C\u5730 host \uFF0C\u901A\u8FC7\u4E00\u4E2A\u548C\u4E3B\u5E94\u7528\u76F8\u540C\u7684\u4E00\u7EA7\u57DF\u540D\u7684\u57DF\u540D\u6765\u8BBF\u95EE 127.0.0.1)`);
279
+ }
280
+ })(req, res, next);
281
+ }
282
+ return next();
283
+ };
284
+ });
197
285
  };
198
286
  // Annotate the CommonJS export names for ESM import in node:
199
287
  0 && (module.exports = {});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugins",
3
- "version": "4.0.15",
3
+ "version": "4.0.16",
4
4
  "description": "@umijs/plugins",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/plugins#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",
@@ -27,7 +27,7 @@
27
27
  "@ant-design/antd-theme-variable": "^1.0.0",
28
28
  "@ant-design/icons": "^4.7.0",
29
29
  "@ant-design/pro-layout": "^7.0.1-beta.28",
30
- "@umijs/bundler-utils": "4.0.15",
30
+ "@umijs/bundler-utils": "4.0.16",
31
31
  "antd-dayjs-webpack-plugin": "^1.0.6",
32
32
  "axios": "^0.27.2",
33
33
  "babel-plugin-import": "^1.13.5",
@@ -46,7 +46,7 @@
46
46
  "warning": "^4.0.3"
47
47
  },
48
48
  "devDependencies": {
49
- "umi": "4.0.15"
49
+ "umi": "4.0.16"
50
50
  },
51
51
  "publishConfig": {
52
52
  "access": "public"