@module-federation/modern-js 0.0.0-next-20240819040425 → 0.0.0-next-20240820034939

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 (75) hide show
  1. package/dist/cjs/cli/ast/constant.js +49 -0
  2. package/dist/cjs/cli/ast/generateRoutes.js +140 -0
  3. package/dist/cjs/cli/ast/generateSerializableRoutes.js +91 -0
  4. package/dist/cjs/cli/ast/generateSlimRoutes.js +106 -0
  5. package/dist/cjs/cli/ast/index.js +34 -0
  6. package/dist/cjs/cli/bundler-plugins/SerializableRoutesPlugin.js +65 -0
  7. package/dist/cjs/cli/constant.js +6 -0
  8. package/dist/cjs/cli/dataLoaderPlugin.js +288 -0
  9. package/dist/cjs/cli/index.js +6 -1
  10. package/dist/cjs/cli/server/dataLoaderPlugin.js +83 -0
  11. package/dist/cjs/cli/ssrPlugin.js +4 -4
  12. package/dist/cjs/cli/utils.js +7 -3
  13. package/dist/cjs/interfaces/route.js +16 -0
  14. package/dist/cjs/runtime/constant.js +34 -0
  15. package/dist/cjs/runtime/dataLoader.js +87 -0
  16. package/dist/cjs/runtime/index.js +4 -1
  17. package/dist/cjs/runtime/utils.js +43 -0
  18. package/dist/cjs/runtime/withMFRouteId.js +31 -0
  19. package/dist/cjs/ssr-runtime/plugin.js +8 -7
  20. package/dist/esm/cli/ast/constant.js +18 -0
  21. package/dist/esm/cli/ast/generateRoutes.js +107 -0
  22. package/dist/esm/cli/ast/generateSerializableRoutes.js +58 -0
  23. package/dist/esm/cli/ast/generateSlimRoutes.js +75 -0
  24. package/dist/esm/cli/ast/index.js +8 -0
  25. package/dist/esm/cli/bundler-plugins/SerializableRoutesPlugin.js +45 -0
  26. package/dist/esm/cli/constant.js +4 -0
  27. package/dist/esm/cli/dataLoaderPlugin.js +325 -0
  28. package/dist/esm/cli/index.js +6 -1
  29. package/dist/esm/cli/server/dataLoaderPlugin.js +146 -0
  30. package/dist/esm/cli/ssrPlugin.js +2 -2
  31. package/dist/esm/cli/utils.js +5 -2
  32. package/dist/esm/interfaces/route.js +0 -0
  33. package/dist/esm/runtime/constant.js +8 -0
  34. package/dist/esm/runtime/dataLoader.js +125 -0
  35. package/dist/esm/runtime/index.js +3 -1
  36. package/dist/esm/runtime/utils.js +16 -0
  37. package/dist/esm/runtime/withMFRouteId.js +7 -0
  38. package/dist/esm/ssr-runtime/plugin.js +8 -10
  39. package/dist/esm-node/cli/ast/constant.js +18 -0
  40. package/dist/esm-node/cli/ast/generateRoutes.js +106 -0
  41. package/dist/esm-node/cli/ast/generateSerializableRoutes.js +57 -0
  42. package/dist/esm-node/cli/ast/generateSlimRoutes.js +72 -0
  43. package/dist/esm-node/cli/ast/index.js +8 -0
  44. package/dist/esm-node/cli/bundler-plugins/SerializableRoutesPlugin.js +30 -0
  45. package/dist/esm-node/cli/constant.js +4 -0
  46. package/dist/esm-node/cli/dataLoaderPlugin.js +252 -0
  47. package/dist/esm-node/cli/index.js +6 -1
  48. package/dist/esm-node/cli/server/dataLoaderPlugin.js +63 -0
  49. package/dist/esm-node/cli/ssrPlugin.js +2 -2
  50. package/dist/esm-node/cli/utils.js +5 -2
  51. package/dist/esm-node/interfaces/route.js +0 -0
  52. package/dist/esm-node/runtime/constant.js +8 -0
  53. package/dist/esm-node/runtime/dataLoader.js +63 -0
  54. package/dist/esm-node/runtime/index.js +3 -1
  55. package/dist/esm-node/runtime/utils.js +19 -0
  56. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  57. package/dist/esm-node/ssr-runtime/plugin.js +8 -7
  58. package/dist/types/cli/ast/constant.d.ts +8 -0
  59. package/dist/types/cli/ast/generateRoutes.d.ts +7 -0
  60. package/dist/types/cli/ast/generateSerializableRoutes.d.ts +5 -0
  61. package/dist/types/cli/ast/generateSlimRoutes.d.ts +7 -0
  62. package/dist/types/cli/ast/index.d.ts +3 -0
  63. package/dist/types/cli/bundler-plugins/SerializableRoutesPlugin.d.ts +12 -0
  64. package/dist/types/cli/constant.d.ts +2 -0
  65. package/dist/types/cli/dataLoaderPlugin.d.ts +6 -0
  66. package/dist/types/cli/server/dataLoaderPlugin.d.ts +5 -0
  67. package/dist/types/cli/utils.d.ts +2 -0
  68. package/dist/types/interfaces/route.d.ts +13 -0
  69. package/dist/types/runtime/constant.d.ts +3 -0
  70. package/dist/types/runtime/dataLoader.d.ts +7 -0
  71. package/dist/types/runtime/index.d.ts +1 -0
  72. package/dist/types/runtime/utils.d.ts +1 -0
  73. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  74. package/dist/types/types/index.d.ts +11 -0
  75. package/package.json +40 -11
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var withMFRouteId_exports = {};
20
+ __export(withMFRouteId_exports, {
21
+ withMFRouteId: () => withMFRouteId
22
+ });
23
+ module.exports = __toCommonJS(withMFRouteId_exports);
24
+ function withMFRouteId(id) {
25
+ const prefix = typeof MODERN_ROUTER_ID_PREFIX === "string" ? MODERN_ROUTER_ID_PREFIX : "";
26
+ return prefix + id;
27
+ }
28
+ // Annotate the CommonJS export names for ESM import in node:
29
+ 0 && (module.exports = {
30
+ withMFRouteId
31
+ });
@@ -34,15 +34,18 @@ module.exports = __toCommonJS(plugin_exports);
34
34
  var import_jsx_runtime = require("react/jsx-runtime");
35
35
  var import_hoist_non_react_statics = __toESM(require("hoist-non-react-statics"));
36
36
  var import_SSRLiveReload = require("./SSRLiveReload");
37
+ console.log("mfSSRPlugin trigger");
37
38
  const mfSSRPlugin = () => ({
38
39
  name: "@module-federation/modern-js",
40
+ pre: [
41
+ "@modern-js/plugin-router"
42
+ ],
39
43
  setup: () => {
40
44
  return {
41
- async init({ context }, next) {
45
+ async beforeRender() {
46
+ console.log(111, "beforeRender");
42
47
  if (typeof window !== "undefined") {
43
- return next({
44
- context
45
- });
48
+ return;
46
49
  }
47
50
  globalThis.shouldUpdate = false;
48
51
  const nodeUtils = await Promise.resolve().then(() => __toESM(require("@module-federation/node/utils")));
@@ -52,9 +55,7 @@ const mfSSRPlugin = () => ({
52
55
  await nodeUtils.flushChunks();
53
56
  globalThis.shouldUpdate = true;
54
57
  }
55
- return next({
56
- context
57
- });
58
+ return;
58
59
  },
59
60
  wrapRoot(App) {
60
61
  const AppWrapper = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
@@ -0,0 +1,18 @@
1
+ var IS_ROOT = "isRoot";
2
+ var ID = "id";
3
+ var COMPONENT = "component";
4
+ var LAZY_COMPONENT = "lazyImport";
5
+ var SHOULD_REVALIDATE = "shouldRevalidate";
6
+ var PRIVATE_COMPONENT = "_component";
7
+ var ELEMENT = "element";
8
+ var LOADER = "loader";
9
+ export {
10
+ COMPONENT,
11
+ ELEMENT,
12
+ ID,
13
+ IS_ROOT,
14
+ LAZY_COMPONENT,
15
+ LOADER,
16
+ PRIVATE_COMPONENT,
17
+ SHOULD_REVALIDATE
18
+ };
@@ -0,0 +1,107 @@
1
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ import fs from "fs";
3
+ import traverse from "@babel/traverse";
4
+ import * as babelParser from "@babel/parser";
5
+ import generate from "@babel/generator";
6
+ import * as t from "@babel/types";
7
+ function findTargetKeyNode(nodeProperties, key) {
8
+ return nodeProperties.find(function(p) {
9
+ return t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key;
10
+ });
11
+ }
12
+ function generateRoutes(param) {
13
+ var sourceCode = param.sourceCode, filePath = param.filePath, prefix = param.prefix, baseName = param.baseName;
14
+ var ast = babelParser.parse(sourceCode, {
15
+ sourceType: "module"
16
+ });
17
+ var lazyComponentDeclarations = [];
18
+ var componentDeclarations = [];
19
+ var componentId = 0;
20
+ traverse(ast, {
21
+ ImportDeclaration: function ImportDeclaration(path) {
22
+ var source = path.node.source.value;
23
+ var routeIdMatch = source.match(/routeId=([^&]+)/);
24
+ if (routeIdMatch) {
25
+ var originalRouteId = routeIdMatch[1];
26
+ var newRouteId = "".concat(prefix).concat(originalRouteId);
27
+ var newSource = source.replace(/routeId=[^&]+/, "routeId=".concat(newRouteId));
28
+ path.node.source = t.stringLiteral(newSource);
29
+ }
30
+ },
31
+ ObjectExpression: function ObjectExpression(path) {
32
+ var componentName = "";
33
+ var lazyComponentName = "";
34
+ if (!Array.isArray(path.node.properties)) {
35
+ return;
36
+ }
37
+ var idNode = findTargetKeyNode(path.node.properties, "id");
38
+ if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
39
+ idNode.value = t.stringLiteral("".concat(prefix).concat(idNode.value.value));
40
+ }
41
+ var isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
42
+ if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
43
+ isRootNode.value.value = false;
44
+ }
45
+ if (!isRootNode) {
46
+ var lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
47
+ if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
48
+ lazyComponentName = "LazyComponent_".concat(componentId);
49
+ var lazyDeclaration = t.variableDeclaration("const", [
50
+ t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
51
+ ]);
52
+ lazyComponentNode.value = t.identifier(lazyComponentName);
53
+ var componentNode = findTargetKeyNode(path.node.properties, "component");
54
+ if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
55
+ componentNode.value = t.callExpression(t.identifier("lazy"), [
56
+ t.identifier(lazyComponentName)
57
+ ]);
58
+ }
59
+ lazyComponentDeclarations.push(lazyDeclaration);
60
+ }
61
+ }
62
+ var componentNode1 = findTargetKeyNode(path.node.properties, "component");
63
+ if (componentNode1 && t.isObjectProperty(componentNode1) && t.isCallExpression(componentNode1.value) && t.isIdentifier(componentNode1.value.callee)) {
64
+ componentName = "Component_".concat(componentId);
65
+ var componentDeclaration = t.variableDeclaration("const", [
66
+ t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
67
+ t.identifier(lazyComponentName)
68
+ ]) : componentNode1.value)
69
+ ]);
70
+ componentDeclarations.push(componentDeclaration);
71
+ componentNode1.value = t.identifier(componentName);
72
+ }
73
+ if (lazyComponentName || componentName) {
74
+ componentId++;
75
+ if (componentName) {
76
+ var upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
77
+ var jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
78
+ path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
79
+ }
80
+ }
81
+ }
82
+ });
83
+ traverse(ast, {
84
+ Program: function Program(path) {
85
+ var lastImportIndex = path.get("body").reduce(function(lastIndex, p, index) {
86
+ if (t.isImportDeclaration(p.node)) {
87
+ lastIndex = index;
88
+ }
89
+ return lastIndex;
90
+ }, -1);
91
+ if (lastImportIndex >= 0) {
92
+ var lastImport = path.get("body.".concat(lastImportIndex));
93
+ _to_consumable_array(componentDeclarations).concat(_to_consumable_array(lazyComponentDeclarations)).forEach(function(declaration) {
94
+ if ("insertAfter" in lastImport) {
95
+ lastImport.insertAfter(declaration);
96
+ }
97
+ });
98
+ }
99
+ }
100
+ });
101
+ var _generate = generate(ast), newCode = _generate.code;
102
+ var finalCode = "".concat(newCode, "export const baseName = '").concat(baseName, "';");
103
+ fs.writeFileSync(filePath, finalCode);
104
+ }
105
+ export {
106
+ generateRoutes
107
+ };
@@ -0,0 +1,58 @@
1
+ import traverse from "@babel/traverse";
2
+ import * as babelParser from "@babel/parser";
3
+ import generate from "@babel/generator";
4
+ import * as t from "@babel/types";
5
+ import { COMPONENT, ID, SHOULD_REVALIDATE, LAZY_COMPONENT, PRIVATE_COMPONENT, LOADER } from "./constant";
6
+ function generateSerializableRoutes(param) {
7
+ var sourceCode = param.sourceCode, prefix = param.prefix;
8
+ var ast = babelParser.parse(sourceCode, {
9
+ sourceType: "module"
10
+ });
11
+ var removedKeys = [
12
+ COMPONENT,
13
+ SHOULD_REVALIDATE,
14
+ LAZY_COMPONENT,
15
+ PRIVATE_COMPONENT,
16
+ LOADER
17
+ ];
18
+ traverse(ast, {
19
+ ObjectExpression: function ObjectExpression(path1) {
20
+ if (!Array.isArray(path1.node.properties)) {
21
+ return;
22
+ }
23
+ path1.node.properties.forEach(function(prop1) {
24
+ if (t.isObjectProperty(prop1) && t.isStringLiteral(prop1.key) && t.isStringLiteral(prop1.value) && prop1.key.value === ID) {
25
+ prop1.value = t.stringLiteral("".concat(prefix).concat(prop1.value.value));
26
+ }
27
+ });
28
+ path1.node.properties = path1.node.properties.filter(function(p1) {
29
+ if (t.isObjectProperty(p1) && t.isStringLiteral(p1.key)) {
30
+ return !removedKeys.includes(p1.key.value);
31
+ } else {
32
+ return true;
33
+ }
34
+ });
35
+ }
36
+ });
37
+ var routesValue = "";
38
+ traverse(ast, {
39
+ VariableDeclarator: function VariableDeclarator(path) {
40
+ if (t.isVariableDeclarator(path.node) && t.isIdentifier(path.node.id) && path.node.id.name === "routes") {
41
+ var routesAst = path.node.init;
42
+ if (!routesAst) {
43
+ return;
44
+ }
45
+ var code = generate(routesAst, {
46
+ compact: true,
47
+ retainLines: false,
48
+ concise: true
49
+ }).code;
50
+ routesValue = eval("(" + code + ")");
51
+ }
52
+ }
53
+ });
54
+ return routesValue;
55
+ }
56
+ export {
57
+ generateSerializableRoutes
58
+ };
@@ -0,0 +1,75 @@
1
+ import fs from "fs";
2
+ import traverse from "@babel/traverse";
3
+ import * as babelParser from "@babel/parser";
4
+ import generate from "@babel/generator";
5
+ import * as t from "@babel/types";
6
+ import { COMPONENT, ID, SHOULD_REVALIDATE, LAZY_COMPONENT, PRIVATE_COMPONENT } from "./constant";
7
+ function generateSlimRoutes(param) {
8
+ var sourceCode = param.sourceCode, filePath = param.filePath, prefix = param.prefix, baseName = param.baseName;
9
+ var ast = babelParser.parse(sourceCode, {
10
+ sourceType: "module"
11
+ });
12
+ var removedKeys = [
13
+ COMPONENT,
14
+ SHOULD_REVALIDATE,
15
+ LAZY_COMPONENT,
16
+ PRIVATE_COMPONENT
17
+ ];
18
+ traverse(ast, {
19
+ ImportDeclaration: function ImportDeclaration(path) {
20
+ var source = path.node.source.value;
21
+ var routeIdMatch = source.match(/routeId=([^&]+)/);
22
+ if (routeIdMatch) {
23
+ var originalRouteId = routeIdMatch[1];
24
+ var newRouteId = "".concat(prefix).concat(originalRouteId);
25
+ var newSource = source.replace(/routeId=[^&]+/, "routeId=".concat(newRouteId));
26
+ path.node.source = t.stringLiteral(newSource);
27
+ }
28
+ },
29
+ ObjectExpression: function ObjectExpression(path) {
30
+ if (!Array.isArray(path.node.properties)) {
31
+ return;
32
+ }
33
+ path.node.properties.forEach(function(prop) {
34
+ if (t.isObjectProperty(prop) && t.isStringLiteral(prop.key) && t.isStringLiteral(prop.value) && prop.key.value === ID) {
35
+ prop.value = t.stringLiteral("".concat(prefix).concat(prop.value.value));
36
+ }
37
+ });
38
+ path.node.properties = path.node.properties.filter(function(p) {
39
+ if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
40
+ return !removedKeys.includes(p.key.value);
41
+ } else {
42
+ return true;
43
+ }
44
+ });
45
+ }
46
+ });
47
+ var tempCode = generate(ast).code;
48
+ var tempAst = babelParser.parse(tempCode, {
49
+ sourceType: "module"
50
+ });
51
+ var usedIdentifiers = /* @__PURE__ */ new Set();
52
+ traverse(tempAst, {
53
+ Identifier: function Identifier(path) {
54
+ if (t.isProperty(path.parent)) {
55
+ usedIdentifiers.add(path.node.name);
56
+ }
57
+ }
58
+ });
59
+ traverse(tempAst, {
60
+ ImportDeclaration: function ImportDeclaration(path) {
61
+ path.node.specifiers = path.node.specifiers.filter(function(specifier) {
62
+ return usedIdentifiers.has(specifier.local.name);
63
+ });
64
+ if (!path.node.specifiers.length) {
65
+ path.remove();
66
+ }
67
+ }
68
+ });
69
+ var _generate = generate(tempAst), newCode = _generate.code;
70
+ var finalCode = "".concat(newCode, "export const baseName = '").concat(baseName, "';");
71
+ fs.writeFileSync(filePath, finalCode);
72
+ }
73
+ export {
74
+ generateSlimRoutes
75
+ };
@@ -0,0 +1,8 @@
1
+ import { generateRoutes } from "./generateRoutes";
2
+ import { generateSlimRoutes } from "./generateSlimRoutes";
3
+ import { generateSerializableRoutes } from "./generateSerializableRoutes";
4
+ export {
5
+ generateRoutes,
6
+ generateSerializableRoutes,
7
+ generateSlimRoutes
8
+ };
@@ -0,0 +1,45 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import fs from "fs";
5
+ import { generateSerializableRoutes } from "../ast";
6
+ var SERIALIZABLE_ROUTES = "serializable-routes.json";
7
+ var SerializableRoutesPlugin = /* @__PURE__ */ function() {
8
+ "use strict";
9
+ function SerializableRoutesPlugin2(options) {
10
+ _class_call_check(this, SerializableRoutesPlugin2);
11
+ var routesFilePath = options.routesFilePath, prefix = options.prefix;
12
+ this.routesFilePath = routesFilePath;
13
+ this.prefix = prefix;
14
+ }
15
+ var _proto = SerializableRoutesPlugin2.prototype;
16
+ _proto.apply = function apply(compiler) {
17
+ var _this = this;
18
+ compiler.hooks.thisCompilation.tap("generateStats", function(compilation) {
19
+ var _this1 = _this;
20
+ compilation.hooks.processAssets.tapPromise({
21
+ name: "generateSerializableRoutes",
22
+ // @ts-ignore use runtime variable in case peer dep not installed
23
+ stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
24
+ }, /* @__PURE__ */ _async_to_generator(function() {
25
+ var sourceCode, routesCode;
26
+ return _ts_generator(this, function(_state) {
27
+ sourceCode = fs.readFileSync(_this1.routesFilePath, "utf-8");
28
+ routesCode = generateSerializableRoutes({
29
+ sourceCode,
30
+ prefix: _this1.prefix
31
+ });
32
+ compilation.emitAsset(SERIALIZABLE_ROUTES, new compiler.webpack.sources.RawSource(JSON.stringify(routesCode, null, 2)));
33
+ return [
34
+ 2
35
+ ];
36
+ });
37
+ }));
38
+ });
39
+ };
40
+ return SerializableRoutesPlugin2;
41
+ }();
42
+ export {
43
+ SERIALIZABLE_ROUTES,
44
+ SerializableRoutesPlugin
45
+ };
@@ -1,4 +1,8 @@
1
1
  var isDev = process.env.NODE_ENV === "development";
2
+ var MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME = "routes.js";
3
+ var META_NAME = "modern-js";
2
4
  export {
5
+ META_NAME,
6
+ MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
3
7
  isDev
4
8
  };