@module-federation/modern-js 0.0.0-next-20240819112150 → 0.0.0-next-20240821075411
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/dist/cjs/cli/constant.js +0 -6
- package/dist/cjs/cli/index.js +1 -6
- package/dist/cjs/cli/ssrPlugin.js +4 -4
- package/dist/cjs/cli/utils.js +3 -9
- package/dist/cjs/runtime/index.js +1 -4
- package/dist/cjs/ssr-runtime/plugin.js +7 -8
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm/cli/index.js +1 -6
- package/dist/esm/cli/ssrPlugin.js +2 -2
- package/dist/esm/cli/utils.js +2 -7
- package/dist/esm/runtime/index.js +1 -3
- package/dist/esm/ssr-runtime/plugin.js +10 -8
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/esm-node/cli/index.js +1 -6
- package/dist/esm-node/cli/ssrPlugin.js +2 -2
- package/dist/esm-node/cli/utils.js +2 -7
- package/dist/esm-node/runtime/index.js +1 -3
- package/dist/esm-node/ssr-runtime/plugin.js +7 -8
- package/dist/types/cli/constant.d.ts +0 -2
- package/dist/types/cli/utils.d.ts +0 -1
- package/dist/types/runtime/index.d.ts +0 -1
- package/dist/types/types/index.d.ts +0 -11
- package/package.json +11 -40
- package/dist/cjs/cli/ast/constant.js +0 -49
- package/dist/cjs/cli/ast/generateRoutes.js +0 -140
- package/dist/cjs/cli/ast/generateSerializableRoutes.js +0 -91
- package/dist/cjs/cli/ast/generateSlimRoutes.js +0 -106
- package/dist/cjs/cli/ast/index.js +0 -34
- package/dist/cjs/cli/bundler-plugins/SerializableRoutesPlugin.js +0 -65
- package/dist/cjs/cli/dataLoaderPlugin.js +0 -288
- package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -83
- package/dist/cjs/interfaces/route.js +0 -16
- package/dist/cjs/runtime/constant.js +0 -34
- package/dist/cjs/runtime/dataLoader.js +0 -87
- package/dist/cjs/runtime/utils.js +0 -43
- package/dist/cjs/runtime/withMFRouteId.js +0 -31
- package/dist/esm/cli/ast/constant.js +0 -18
- package/dist/esm/cli/ast/generateRoutes.js +0 -107
- package/dist/esm/cli/ast/generateSerializableRoutes.js +0 -58
- package/dist/esm/cli/ast/generateSlimRoutes.js +0 -75
- package/dist/esm/cli/ast/index.js +0 -8
- package/dist/esm/cli/bundler-plugins/SerializableRoutesPlugin.js +0 -45
- package/dist/esm/cli/dataLoaderPlugin.js +0 -325
- package/dist/esm/cli/server/dataLoaderPlugin.js +0 -146
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/constant.js +0 -8
- package/dist/esm/runtime/dataLoader.js +0 -125
- package/dist/esm/runtime/utils.js +0 -16
- package/dist/esm/runtime/withMFRouteId.js +0 -7
- package/dist/esm-node/cli/ast/constant.js +0 -18
- package/dist/esm-node/cli/ast/generateRoutes.js +0 -106
- package/dist/esm-node/cli/ast/generateSerializableRoutes.js +0 -57
- package/dist/esm-node/cli/ast/generateSlimRoutes.js +0 -72
- package/dist/esm-node/cli/ast/index.js +0 -8
- package/dist/esm-node/cli/bundler-plugins/SerializableRoutesPlugin.js +0 -30
- package/dist/esm-node/cli/dataLoaderPlugin.js +0 -252
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -63
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/constant.js +0 -8
- package/dist/esm-node/runtime/dataLoader.js +0 -63
- package/dist/esm-node/runtime/utils.js +0 -19
- package/dist/esm-node/runtime/withMFRouteId.js +0 -7
- package/dist/types/cli/ast/constant.d.ts +0 -8
- package/dist/types/cli/ast/generateRoutes.d.ts +0 -7
- package/dist/types/cli/ast/generateSerializableRoutes.d.ts +0 -5
- package/dist/types/cli/ast/generateSlimRoutes.d.ts +0 -7
- package/dist/types/cli/ast/index.d.ts +0 -3
- package/dist/types/cli/bundler-plugins/SerializableRoutesPlugin.d.ts +0 -12
- package/dist/types/cli/dataLoaderPlugin.d.ts +0 -6
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -5
- package/dist/types/interfaces/route.d.ts +0 -13
- package/dist/types/runtime/constant.d.ts +0 -3
- package/dist/types/runtime/dataLoader.d.ts +0 -7
- package/dist/types/runtime/utils.d.ts +0 -1
- package/dist/types/runtime/withMFRouteId.d.ts +0 -1
|
@@ -1,107 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,75 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { generateRoutes } from "./generateRoutes";
|
|
2
|
-
import { generateSlimRoutes } from "./generateSlimRoutes";
|
|
3
|
-
import { generateSerializableRoutes } from "./generateSerializableRoutes";
|
|
4
|
-
export {
|
|
5
|
-
generateRoutes,
|
|
6
|
-
generateSerializableRoutes,
|
|
7
|
-
generateSlimRoutes
|
|
8
|
-
};
|
|
@@ -1,45 +0,0 @@
|
|
|
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,325 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
3
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
4
|
-
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
5
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
6
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
7
|
-
import path from "path";
|
|
8
|
-
import { fs } from "@modern-js/utils";
|
|
9
|
-
import { transformName2Prefix } from "../runtime/utils";
|
|
10
|
-
import { PLUGIN_IDENTIFIER } from "../constant";
|
|
11
|
-
import { MF_FULL_ROUTES, MF_SLIM_ROUTES, MF_ROUTES_META } from "../runtime/constant";
|
|
12
|
-
import { generateRoutes, generateSlimRoutes } from "./ast";
|
|
13
|
-
import { MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, META_NAME } from "./constant";
|
|
14
|
-
import { getIPV4, replaceRemoteUrl } from "./utils";
|
|
15
|
-
import { SerializableRoutesPlugin, SERIALIZABLE_ROUTES } from "./bundler-plugins/SerializableRoutesPlugin";
|
|
16
|
-
function generateExtraExposeFiles(options) {
|
|
17
|
-
var routesFilePath = options.routesFilePath, mfConfig = options.mfConfig, isServer = options.isServer, baseName = options.baseName;
|
|
18
|
-
var outputDir = path.resolve(process.cwd(), "node_modules/.federation");
|
|
19
|
-
fs.ensureDirSync(outputDir);
|
|
20
|
-
var addSuffix = function(fileName) {
|
|
21
|
-
if (!isServer) {
|
|
22
|
-
return "".concat(fileName, ".jsx");
|
|
23
|
-
}
|
|
24
|
-
return "".concat(fileName, ".server.jsx");
|
|
25
|
-
};
|
|
26
|
-
var routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
27
|
-
var outputSlimRoutesPath = path.resolve(outputDir, addSuffix(MF_SLIM_ROUTES));
|
|
28
|
-
var outputFullRoutesPath = path.resolve(outputDir, addSuffix(MF_FULL_ROUTES));
|
|
29
|
-
var outputRoutesMetaPath = path.resolve(outputDir, "".concat(MF_ROUTES_META, ".js"));
|
|
30
|
-
generateSlimRoutes({
|
|
31
|
-
sourceCode: routesFileContent,
|
|
32
|
-
filePath: outputSlimRoutesPath,
|
|
33
|
-
prefix: transformName2Prefix(mfConfig.name),
|
|
34
|
-
baseName
|
|
35
|
-
});
|
|
36
|
-
generateRoutes({
|
|
37
|
-
sourceCode: routesFileContent,
|
|
38
|
-
filePath: outputFullRoutesPath,
|
|
39
|
-
prefix: transformName2Prefix(mfConfig.name),
|
|
40
|
-
baseName
|
|
41
|
-
});
|
|
42
|
-
fs.writeFileSync(outputRoutesMetaPath, "export const baseName = '".concat(baseName, "';"));
|
|
43
|
-
return {
|
|
44
|
-
outputSlimRoutesPath,
|
|
45
|
-
outputFullRoutesPath,
|
|
46
|
-
outputRoutesMetaPath
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
function addExpose(options) {
|
|
50
|
-
var mfConfig = options.mfConfig;
|
|
51
|
-
var _generateExtraExposeFiles = generateExtraExposeFiles(options), outputSlimRoutesPath = _generateExtraExposeFiles.outputSlimRoutesPath, outputFullRoutesPath = _generateExtraExposeFiles.outputFullRoutesPath, outputRoutesMetaPath = _generateExtraExposeFiles.outputRoutesMetaPath;
|
|
52
|
-
var fullRoutesKey = "./".concat(MF_FULL_ROUTES);
|
|
53
|
-
var slimRoutesKey = "./".concat(MF_SLIM_ROUTES);
|
|
54
|
-
var routeMetaKey = "./".concat(MF_ROUTES_META);
|
|
55
|
-
if (!mfConfig.exposes) {
|
|
56
|
-
var _obj;
|
|
57
|
-
mfConfig.exposes = (_obj = {}, _define_property(_obj, fullRoutesKey, outputFullRoutesPath), _define_property(_obj, slimRoutesKey, outputSlimRoutesPath), _define_property(_obj, routeMetaKey, outputRoutesMetaPath), _obj);
|
|
58
|
-
} else {
|
|
59
|
-
if (!Array.isArray(mfConfig.exposes)) {
|
|
60
|
-
if (!mfConfig.exposes[fullRoutesKey]) {
|
|
61
|
-
mfConfig.exposes[fullRoutesKey] = outputFullRoutesPath;
|
|
62
|
-
}
|
|
63
|
-
if (!mfConfig.exposes[slimRoutesKey]) {
|
|
64
|
-
mfConfig.exposes[slimRoutesKey] = outputSlimRoutesPath;
|
|
65
|
-
}
|
|
66
|
-
if (!mfConfig.exposes[routeMetaKey]) {
|
|
67
|
-
mfConfig.exposes[routeMetaKey] = outputRoutesMetaPath;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function addShared(options) {
|
|
73
|
-
var metaName = options.metaName, mfConfig = options.mfConfig;
|
|
74
|
-
var alias = "@".concat(metaName, "/runtime/router");
|
|
75
|
-
if (!mfConfig.shared) {
|
|
76
|
-
mfConfig.shared = _define_property({}, alias, {
|
|
77
|
-
singleton: true
|
|
78
|
-
});
|
|
79
|
-
} else {
|
|
80
|
-
if (!Array.isArray(mfConfig.shared)) {
|
|
81
|
-
mfConfig.shared[alias] = {
|
|
82
|
-
singleton: true
|
|
83
|
-
};
|
|
84
|
-
} else {
|
|
85
|
-
mfConfig.shared.push(alias);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function patchMfConfig(options) {
|
|
90
|
-
addShared(options);
|
|
91
|
-
addExpose(options);
|
|
92
|
-
}
|
|
93
|
-
function _fetchSSRByRouteIds(partialSSRRemotes, mfConfig, transformRuntimeFn) {
|
|
94
|
-
return __fetchSSRByRouteIds.apply(this, arguments);
|
|
95
|
-
}
|
|
96
|
-
function __fetchSSRByRouteIds() {
|
|
97
|
-
__fetchSSRByRouteIds = _async_to_generator(function(partialSSRRemotes, mfConfig, transformRuntimeFn) {
|
|
98
|
-
var partialMfConfig, runtimeInitOptions, remoteProviderRouteIds, collectIds;
|
|
99
|
-
return _ts_generator(this, function(_state) {
|
|
100
|
-
switch (_state.label) {
|
|
101
|
-
case 0:
|
|
102
|
-
partialMfConfig = {
|
|
103
|
-
name: mfConfig.name,
|
|
104
|
-
remotes: _object_spread({}, mfConfig.remotes)
|
|
105
|
-
};
|
|
106
|
-
runtimeInitOptions = transformRuntimeFn(partialMfConfig);
|
|
107
|
-
replaceRemoteUrl(partialMfConfig, "ipv4");
|
|
108
|
-
if (!runtimeInitOptions.remotes.length || !partialSSRRemotes.length) {
|
|
109
|
-
return [
|
|
110
|
-
2,
|
|
111
|
-
void 0
|
|
112
|
-
];
|
|
113
|
-
}
|
|
114
|
-
remoteProviderRouteIds = /* @__PURE__ */ new Set();
|
|
115
|
-
collectIds = function(route) {
|
|
116
|
-
remoteProviderRouteIds.add(route.id);
|
|
117
|
-
if (route.children) {
|
|
118
|
-
route.children.forEach(function(r) {
|
|
119
|
-
collectIds(r);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
return [
|
|
124
|
-
4,
|
|
125
|
-
Promise.all(runtimeInitOptions.remotes.map(function() {
|
|
126
|
-
var _ref = _async_to_generator(function(remote) {
|
|
127
|
-
var entry, ipv4, url, serializableRoutesUrl, serializableRoutesRet, serializableRoutes;
|
|
128
|
-
return _ts_generator(this, function(_state2) {
|
|
129
|
-
switch (_state2.label) {
|
|
130
|
-
case 0:
|
|
131
|
-
entry = "entry" in remote ? remote.entry : "";
|
|
132
|
-
if (!entry) {
|
|
133
|
-
return [
|
|
134
|
-
2,
|
|
135
|
-
void 0
|
|
136
|
-
];
|
|
137
|
-
}
|
|
138
|
-
ipv4 = getIPV4();
|
|
139
|
-
url = new URL(entry);
|
|
140
|
-
serializableRoutesUrl = url.href.replace(url.pathname, "/".concat(SERIALIZABLE_ROUTES)).replace("localhost", ipv4);
|
|
141
|
-
return [
|
|
142
|
-
4,
|
|
143
|
-
fetch(serializableRoutesUrl)
|
|
144
|
-
];
|
|
145
|
-
case 1:
|
|
146
|
-
serializableRoutesRet = _state2.sent();
|
|
147
|
-
return [
|
|
148
|
-
4,
|
|
149
|
-
serializableRoutesRet.json()
|
|
150
|
-
];
|
|
151
|
-
case 2:
|
|
152
|
-
serializableRoutes = _state2.sent();
|
|
153
|
-
serializableRoutes.forEach(function(serializableRoute) {
|
|
154
|
-
collectIds(serializableRoute);
|
|
155
|
-
});
|
|
156
|
-
return [
|
|
157
|
-
2
|
|
158
|
-
];
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
return function(remote) {
|
|
163
|
-
return _ref.apply(this, arguments);
|
|
164
|
-
};
|
|
165
|
-
}()))
|
|
166
|
-
];
|
|
167
|
-
case 1:
|
|
168
|
-
_state.sent();
|
|
169
|
-
return [
|
|
170
|
-
2,
|
|
171
|
-
_to_consumable_array(remoteProviderRouteIds)
|
|
172
|
-
];
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
return __fetchSSRByRouteIds.apply(this, arguments);
|
|
177
|
-
}
|
|
178
|
-
function _transformRuntimeOptions(buildOptions) {
|
|
179
|
-
var remotes = buildOptions.remotes || {};
|
|
180
|
-
var runtimeRemotes = Object.entries(remotes).map(function(remote) {
|
|
181
|
-
var _remote = _sliced_to_array(remote, 2), _alias = _remote[0], nameAndEntry = _remote[1];
|
|
182
|
-
var _nameAndEntry_split = _sliced_to_array(nameAndEntry.split("@"), 2), name = _nameAndEntry_split[0], entry = _nameAndEntry_split[1];
|
|
183
|
-
return {
|
|
184
|
-
name,
|
|
185
|
-
entry
|
|
186
|
-
};
|
|
187
|
-
});
|
|
188
|
-
return {
|
|
189
|
-
name: buildOptions.name,
|
|
190
|
-
remotes: runtimeRemotes
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userConfig) {
|
|
194
|
-
return {
|
|
195
|
-
name: "@modern-js/plugin-module-federation-data-loader",
|
|
196
|
-
pre: [
|
|
197
|
-
"@modern-js/plugin-module-federation-config"
|
|
198
|
-
],
|
|
199
|
-
post: [
|
|
200
|
-
"@modern-js/plugin-router",
|
|
201
|
-
"@modern-js/plugin-module-federation"
|
|
202
|
-
],
|
|
203
|
-
setup: function() {
|
|
204
|
-
var _ref = _async_to_generator(function(param) {
|
|
205
|
-
var useConfigContext, useAppContext, _modernjsConfig_server, _internalOptions_csrConfig, baseName, _userConfig_partialSSRRemotes, partialSSRRemotes, fetchSSRByRouteIds, _userConfig_metaName, metaName, _userConfig_serverPlugin, serverPlugin, transformRuntimeOptions, modernjsConfig, appContext, enableSSR, name, routesFilePath, transformRuntimeFn;
|
|
206
|
-
return _ts_generator(this, function(_state) {
|
|
207
|
-
useConfigContext = param.useConfigContext, useAppContext = param.useAppContext;
|
|
208
|
-
if (!enable) {
|
|
209
|
-
return [
|
|
210
|
-
2
|
|
211
|
-
];
|
|
212
|
-
}
|
|
213
|
-
baseName = userConfig.baseName, _userConfig_partialSSRRemotes = userConfig.partialSSRRemotes, partialSSRRemotes = _userConfig_partialSSRRemotes === void 0 ? [] : _userConfig_partialSSRRemotes, fetchSSRByRouteIds = userConfig.fetchSSRByRouteIds, _userConfig_metaName = userConfig.metaName, metaName = _userConfig_metaName === void 0 ? META_NAME : _userConfig_metaName, _userConfig_serverPlugin = userConfig.serverPlugin, serverPlugin = _userConfig_serverPlugin === void 0 ? "@module-federation/modern-js/data-loader-server" : _userConfig_serverPlugin, transformRuntimeOptions = userConfig.transformRuntimeOptions;
|
|
214
|
-
if (!baseName) {
|
|
215
|
-
throw new Error("".concat(PLUGIN_IDENTIFIER, " 'baseName' is required if you enable 'dataLoader'!"));
|
|
216
|
-
}
|
|
217
|
-
modernjsConfig = useConfigContext();
|
|
218
|
-
appContext = useAppContext();
|
|
219
|
-
enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
220
|
-
name = (_internalOptions_csrConfig = internalOptions.csrConfig) === null || _internalOptions_csrConfig === void 0 ? void 0 : _internalOptions_csrConfig.name;
|
|
221
|
-
routesFilePath = path.resolve(appContext.internalDirectory.replace(META_NAME, metaName || META_NAME), "./main/".concat(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME));
|
|
222
|
-
transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
223
|
-
return [
|
|
224
|
-
2,
|
|
225
|
-
{
|
|
226
|
-
_internalRuntimePlugins: function(param2) {
|
|
227
|
-
var entrypoint = param2.entrypoint, plugins = param2.plugins;
|
|
228
|
-
plugins.push({
|
|
229
|
-
name: "ssrDataLoader",
|
|
230
|
-
path: "@module-federation/modern-js/data-loader",
|
|
231
|
-
config: {}
|
|
232
|
-
});
|
|
233
|
-
return {
|
|
234
|
-
entrypoint,
|
|
235
|
-
plugins
|
|
236
|
-
};
|
|
237
|
-
},
|
|
238
|
-
_internalServerPlugins: function _internalServerPlugins(param2) {
|
|
239
|
-
var plugins = param2.plugins;
|
|
240
|
-
plugins.push({
|
|
241
|
-
name: serverPlugin,
|
|
242
|
-
options: transformRuntimeFn(internalOptions.csrConfig)
|
|
243
|
-
});
|
|
244
|
-
return {
|
|
245
|
-
plugins
|
|
246
|
-
};
|
|
247
|
-
},
|
|
248
|
-
config: /* @__PURE__ */ _async_to_generator(function() {
|
|
249
|
-
var fetchFn, ssrByRouteIds;
|
|
250
|
-
return _ts_generator(this, function(_state2) {
|
|
251
|
-
switch (_state2.label) {
|
|
252
|
-
case 0:
|
|
253
|
-
fetchFn = fetchSSRByRouteIds || _fetchSSRByRouteIds;
|
|
254
|
-
return [
|
|
255
|
-
4,
|
|
256
|
-
fetchFn(partialSSRRemotes, internalOptions.csrConfig, transformRuntimeFn)
|
|
257
|
-
];
|
|
258
|
-
case 1:
|
|
259
|
-
ssrByRouteIds = _state2.sent();
|
|
260
|
-
return [
|
|
261
|
-
2,
|
|
262
|
-
{
|
|
263
|
-
server: {
|
|
264
|
-
ssrByRouteIds
|
|
265
|
-
},
|
|
266
|
-
tools: {
|
|
267
|
-
rspack: function rspack(_config, param2) {
|
|
268
|
-
var isServer = param2.isServer;
|
|
269
|
-
patchMfConfig({
|
|
270
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
271
|
-
baseName,
|
|
272
|
-
metaName,
|
|
273
|
-
isServer,
|
|
274
|
-
routesFilePath
|
|
275
|
-
});
|
|
276
|
-
},
|
|
277
|
-
webpack: function webpack(_config, param2) {
|
|
278
|
-
var isServer = param2.isServer;
|
|
279
|
-
patchMfConfig({
|
|
280
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
281
|
-
baseName,
|
|
282
|
-
metaName,
|
|
283
|
-
isServer,
|
|
284
|
-
routesFilePath
|
|
285
|
-
});
|
|
286
|
-
},
|
|
287
|
-
bundlerChain: function bundlerChain(chain, param2) {
|
|
288
|
-
var isServer = param2.isServer;
|
|
289
|
-
if (!isServer && internalOptions.csrConfig && Object.keys(internalOptions.csrConfig.exposes).length) {
|
|
290
|
-
chain.plugin("SerializableRoutesPlugin").use(SerializableRoutesPlugin, [
|
|
291
|
-
{
|
|
292
|
-
routesFilePath,
|
|
293
|
-
prefix: transformName2Prefix(internalOptions.csrConfig.name)
|
|
294
|
-
}
|
|
295
|
-
]);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
},
|
|
299
|
-
source: {
|
|
300
|
-
define: {
|
|
301
|
-
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
];
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
})
|
|
309
|
-
}
|
|
310
|
-
];
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
return function(_) {
|
|
314
|
-
return _ref.apply(this, arguments);
|
|
315
|
-
};
|
|
316
|
-
}()
|
|
317
|
-
};
|
|
318
|
-
};
|
|
319
|
-
var dataLoaderPlugin_default = moduleFederationDataLoaderPlugin;
|
|
320
|
-
export {
|
|
321
|
-
dataLoaderPlugin_default as default,
|
|
322
|
-
generateRoutes,
|
|
323
|
-
generateSlimRoutes,
|
|
324
|
-
moduleFederationDataLoaderPlugin
|
|
325
|
-
};
|