@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,146 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
3
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
-
import { getInstance, init } from "@module-federation/enhanced/runtime";
|
|
5
|
-
import { MF_SLIM_ROUTES } from "../../runtime/constant";
|
|
6
|
-
function getSlimRemotes(mfInstance) {
|
|
7
|
-
return _getSlimRemotes.apply(this, arguments);
|
|
8
|
-
}
|
|
9
|
-
function _getSlimRemotes() {
|
|
10
|
-
_getSlimRemotes = _async_to_generator(function(mfInstance) {
|
|
11
|
-
return _ts_generator(this, function(_state) {
|
|
12
|
-
switch (_state.label) {
|
|
13
|
-
case 0:
|
|
14
|
-
return [
|
|
15
|
-
4,
|
|
16
|
-
Promise.all(mfInstance.options.remotes.map(function() {
|
|
17
|
-
var _ref = _async_to_generator(function(remote) {
|
|
18
|
-
var _ref2, routes, baseName;
|
|
19
|
-
return _ts_generator(this, function(_state2) {
|
|
20
|
-
switch (_state2.label) {
|
|
21
|
-
case 0:
|
|
22
|
-
return [
|
|
23
|
-
4,
|
|
24
|
-
mfInstance.loadRemote("".concat(remote.name, "/").concat(MF_SLIM_ROUTES))
|
|
25
|
-
];
|
|
26
|
-
case 1:
|
|
27
|
-
_ref2 = _state2.sent(), routes = _ref2.routes, baseName = _ref2.baseName;
|
|
28
|
-
return [
|
|
29
|
-
2,
|
|
30
|
-
{
|
|
31
|
-
routes,
|
|
32
|
-
baseName
|
|
33
|
-
}
|
|
34
|
-
];
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
return function(remote) {
|
|
39
|
-
return _ref.apply(this, arguments);
|
|
40
|
-
};
|
|
41
|
-
}()))
|
|
42
|
-
];
|
|
43
|
-
case 1:
|
|
44
|
-
return [
|
|
45
|
-
2,
|
|
46
|
-
_state.sent()
|
|
47
|
-
];
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
return _getSlimRemotes.apply(this, arguments);
|
|
52
|
-
}
|
|
53
|
-
function dataLoaderPlugin_default(mfRuntimeOptions) {
|
|
54
|
-
return {
|
|
55
|
-
name: "MFDataLoaderServerPlugin",
|
|
56
|
-
pre: [
|
|
57
|
-
"@modern-js/plugin-inject-resource"
|
|
58
|
-
],
|
|
59
|
-
setup: function setup(api) {
|
|
60
|
-
var remotes = mfRuntimeOptions.remotes, name = mfRuntimeOptions.name;
|
|
61
|
-
if (!remotes.length) {
|
|
62
|
-
return {};
|
|
63
|
-
}
|
|
64
|
-
var isHandled = false;
|
|
65
|
-
return {
|
|
66
|
-
prepare: function prepare() {
|
|
67
|
-
var middlewares = api.useAppContext().middlewares;
|
|
68
|
-
middlewares.push({
|
|
69
|
-
name: "MFDataLoaderServerPlugin",
|
|
70
|
-
handler: function() {
|
|
71
|
-
var _ref = _async_to_generator(function(c, next) {
|
|
72
|
-
var serverManifest, loaderBundles, nestedRoutesJson, instance, slimRoutes;
|
|
73
|
-
return _ts_generator(this, function(_state) {
|
|
74
|
-
switch (_state.label) {
|
|
75
|
-
case 0:
|
|
76
|
-
console.log("isHandled : ", isHandled);
|
|
77
|
-
serverManifest = c.get("serverManifest");
|
|
78
|
-
loaderBundles = serverManifest.loaderBundles, nestedRoutesJson = serverManifest.nestedRoutesJson;
|
|
79
|
-
console.log("nestedRoutesJson: ", nestedRoutesJson);
|
|
80
|
-
console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
|
|
81
|
-
if (!isHandled)
|
|
82
|
-
return [
|
|
83
|
-
3,
|
|
84
|
-
2
|
|
85
|
-
];
|
|
86
|
-
return [
|
|
87
|
-
4,
|
|
88
|
-
next()
|
|
89
|
-
];
|
|
90
|
-
case 1:
|
|
91
|
-
_state.sent();
|
|
92
|
-
return [
|
|
93
|
-
3,
|
|
94
|
-
5
|
|
95
|
-
];
|
|
96
|
-
case 2:
|
|
97
|
-
instance = getInstance() || init({
|
|
98
|
-
name,
|
|
99
|
-
remotes
|
|
100
|
-
});
|
|
101
|
-
return [
|
|
102
|
-
4,
|
|
103
|
-
getSlimRemotes(instance)
|
|
104
|
-
];
|
|
105
|
-
case 3:
|
|
106
|
-
slimRoutes = _state.sent();
|
|
107
|
-
slimRoutes.forEach(function(slimRoute) {
|
|
108
|
-
var _loaderBundles_main_routes, _nestedRoutesJson_default_main;
|
|
109
|
-
var routes = slimRoute.routes, baseName = slimRoute.baseName;
|
|
110
|
-
routes[0].path = "/".concat(baseName);
|
|
111
|
-
(_loaderBundles_main_routes = loaderBundles.main.routes).push.apply(_loaderBundles_main_routes, _to_consumable_array(routes));
|
|
112
|
-
(_nestedRoutesJson_default_main = nestedRoutesJson.default.main).push.apply(_nestedRoutesJson_default_main, _to_consumable_array(routes));
|
|
113
|
-
});
|
|
114
|
-
console.log("push loaderBundles.main.routes done ");
|
|
115
|
-
isHandled = true;
|
|
116
|
-
return [
|
|
117
|
-
4,
|
|
118
|
-
next()
|
|
119
|
-
];
|
|
120
|
-
case 4:
|
|
121
|
-
_state.sent();
|
|
122
|
-
_state.label = 5;
|
|
123
|
-
case 5:
|
|
124
|
-
return [
|
|
125
|
-
2
|
|
126
|
-
];
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
return function(c, next) {
|
|
131
|
-
return _ref.apply(this, arguments);
|
|
132
|
-
};
|
|
133
|
-
}(),
|
|
134
|
-
before: [
|
|
135
|
-
"render"
|
|
136
|
-
]
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
;
|
|
144
|
-
export {
|
|
145
|
-
dataLoaderPlugin_default as default
|
|
146
|
-
};
|
|
File without changes
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
3
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
|
-
import { getInstance, loadRemote } from "@module-federation/enhanced/runtime";
|
|
6
|
-
import { MF_FULL_ROUTES } from "./constant";
|
|
7
|
-
globalThis.mfRemoteRoutes = globalThis.mfRemoteRoutes || [];
|
|
8
|
-
var _globalThis_mfHasLoadedRemoteRoutes;
|
|
9
|
-
globalThis.mfHasLoadedRemoteRoutes = (_globalThis_mfHasLoadedRemoteRoutes = globalThis.mfHasLoadedRemoteRoutes) !== null && _globalThis_mfHasLoadedRemoteRoutes !== void 0 ? _globalThis_mfHasLoadedRemoteRoutes : false;
|
|
10
|
-
function loadRoutes() {
|
|
11
|
-
return _loadRoutes.apply(this, arguments);
|
|
12
|
-
}
|
|
13
|
-
function _loadRoutes() {
|
|
14
|
-
_loadRoutes = _async_to_generator(function() {
|
|
15
|
-
var _getInstance, remotes, remoteModuleIds, traverse;
|
|
16
|
-
return _ts_generator(this, function(_state) {
|
|
17
|
-
switch (_state.label) {
|
|
18
|
-
case 0:
|
|
19
|
-
if (globalThis.mfHasLoadedRemoteRoutes) {
|
|
20
|
-
return [
|
|
21
|
-
2
|
|
22
|
-
];
|
|
23
|
-
}
|
|
24
|
-
remotes = (_getInstance = getInstance()) === null || _getInstance === void 0 ? void 0 : _getInstance.options.remotes;
|
|
25
|
-
if (!remotes) {
|
|
26
|
-
return [
|
|
27
|
-
2
|
|
28
|
-
];
|
|
29
|
-
}
|
|
30
|
-
remoteModuleIds = remotes.map(function(remote) {
|
|
31
|
-
return "".concat(remote.name, "/").concat(MF_FULL_ROUTES);
|
|
32
|
-
});
|
|
33
|
-
traverse = function(cRoutes, prefix) {
|
|
34
|
-
cRoutes.forEach(function(i) {
|
|
35
|
-
i.id = prefix + i.id;
|
|
36
|
-
var Comp = i.component;
|
|
37
|
-
i.element = /* @__PURE__ */ _jsx(Comp, {});
|
|
38
|
-
if (i.children) {
|
|
39
|
-
traverse(i.children, prefix);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
return [
|
|
44
|
-
4,
|
|
45
|
-
Promise.all(remoteModuleIds.map(function() {
|
|
46
|
-
var _ref = _async_to_generator(function(remoteModuleId) {
|
|
47
|
-
var _globalThis_mfRemoteRoutes, remoteName, _ref2, routes, baseName;
|
|
48
|
-
return _ts_generator(this, function(_state2) {
|
|
49
|
-
switch (_state2.label) {
|
|
50
|
-
case 0:
|
|
51
|
-
remoteName = remoteModuleId.split("/".concat(MF_FULL_ROUTES))[0];
|
|
52
|
-
return [
|
|
53
|
-
4,
|
|
54
|
-
loadRemote(remoteModuleId)
|
|
55
|
-
];
|
|
56
|
-
case 1:
|
|
57
|
-
_ref2 = _state2.sent(), routes = _ref2.routes, baseName = _ref2.baseName;
|
|
58
|
-
routes[0].path = "/".concat(baseName);
|
|
59
|
-
routes[0].isRoot = false;
|
|
60
|
-
(_globalThis_mfRemoteRoutes = globalThis.mfRemoteRoutes).push.apply(_globalThis_mfRemoteRoutes, _to_consumable_array(routes));
|
|
61
|
-
return [
|
|
62
|
-
2
|
|
63
|
-
];
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
return function(remoteModuleId) {
|
|
68
|
-
return _ref.apply(this, arguments);
|
|
69
|
-
};
|
|
70
|
-
}()))
|
|
71
|
-
];
|
|
72
|
-
case 1:
|
|
73
|
-
_state.sent();
|
|
74
|
-
globalThis.mfHasLoadedRemoteRoutes = true;
|
|
75
|
-
return [
|
|
76
|
-
2,
|
|
77
|
-
globalThis.mfRemoteRoutes
|
|
78
|
-
];
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
return _loadRoutes.apply(this, arguments);
|
|
83
|
-
}
|
|
84
|
-
var ssrDataLoaderPlugin = function() {
|
|
85
|
-
return {
|
|
86
|
-
name: "@modern-js/plugin-mf-data-loader",
|
|
87
|
-
post: [
|
|
88
|
-
"@modern-js/plugin-router"
|
|
89
|
-
],
|
|
90
|
-
setup: function() {
|
|
91
|
-
return {
|
|
92
|
-
beforeRender: function beforeRender(param) {
|
|
93
|
-
var context = param.context;
|
|
94
|
-
return _async_to_generator(function() {
|
|
95
|
-
return _ts_generator(this, function(_state) {
|
|
96
|
-
switch (_state.label) {
|
|
97
|
-
case 0:
|
|
98
|
-
console.log("init");
|
|
99
|
-
return [
|
|
100
|
-
4,
|
|
101
|
-
loadRoutes()
|
|
102
|
-
];
|
|
103
|
-
case 1:
|
|
104
|
-
_state.sent();
|
|
105
|
-
return [
|
|
106
|
-
2
|
|
107
|
-
];
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
})();
|
|
111
|
-
},
|
|
112
|
-
modifyRoutes: function(routes) {
|
|
113
|
-
var _routes;
|
|
114
|
-
console.log("modifyRoutes");
|
|
115
|
-
(_routes = routes).push.apply(_routes, _to_consumable_array(globalThis.mfRemoteRoutes));
|
|
116
|
-
console.log("routes: ", routes);
|
|
117
|
-
return routes;
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
export {
|
|
124
|
-
ssrDataLoaderPlugin
|
|
125
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
function transformName2Prefix(name) {
|
|
3
|
-
var NameTransformSymbol = {
|
|
4
|
-
AT: "@",
|
|
5
|
-
HYPHEN: "-",
|
|
6
|
-
SLASH: "/",
|
|
7
|
-
UNDERLINE: "_"
|
|
8
|
-
};
|
|
9
|
-
var _obj;
|
|
10
|
-
var NameTransformMap = (_obj = {}, _define_property(_obj, NameTransformSymbol.AT, "SCOPE"), _define_property(_obj, NameTransformSymbol.HYPHEN, "HYPHEN"), _define_property(_obj, NameTransformSymbol.SLASH, "SLASH"), _define_property(_obj, NameTransformSymbol.UNDERLINE, "UNDERLINE"), _obj);
|
|
11
|
-
var SPLIT_SYMBOL = "@";
|
|
12
|
-
return "".concat(name.replace(new RegExp("".concat(NameTransformSymbol.AT), "g"), NameTransformMap[NameTransformSymbol.AT]).replace(new RegExp("".concat(NameTransformSymbol.HYPHEN), "g"), NameTransformMap[NameTransformSymbol.HYPHEN]).replace(new RegExp("".concat(NameTransformSymbol.SLASH), "g"), NameTransformMap[NameTransformSymbol.SLASH]).replace(new RegExp("".concat(NameTransformSymbol.UNDERLINE), "g"), NameTransformMap[NameTransformSymbol.UNDERLINE])).concat(SPLIT_SYMBOL);
|
|
13
|
-
}
|
|
14
|
-
export {
|
|
15
|
-
transformName2Prefix
|
|
16
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const IS_ROOT = "isRoot";
|
|
2
|
-
const ID = "id";
|
|
3
|
-
const COMPONENT = "component";
|
|
4
|
-
const LAZY_COMPONENT = "lazyImport";
|
|
5
|
-
const SHOULD_REVALIDATE = "shouldRevalidate";
|
|
6
|
-
const PRIVATE_COMPONENT = "_component";
|
|
7
|
-
const ELEMENT = "element";
|
|
8
|
-
const 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
|
-
};
|
|
@@ -1,106 +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
|
-
function findTargetKeyNode(nodeProperties, key) {
|
|
7
|
-
return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
|
|
8
|
-
}
|
|
9
|
-
function generateRoutes({ sourceCode, filePath, prefix, baseName }) {
|
|
10
|
-
const ast = babelParser.parse(sourceCode, {
|
|
11
|
-
sourceType: "module"
|
|
12
|
-
});
|
|
13
|
-
const lazyComponentDeclarations = [];
|
|
14
|
-
const componentDeclarations = [];
|
|
15
|
-
let componentId = 0;
|
|
16
|
-
traverse(ast, {
|
|
17
|
-
ImportDeclaration(path) {
|
|
18
|
-
const source = path.node.source.value;
|
|
19
|
-
const routeIdMatch = source.match(/routeId=([^&]+)/);
|
|
20
|
-
if (routeIdMatch) {
|
|
21
|
-
const originalRouteId = routeIdMatch[1];
|
|
22
|
-
const newRouteId = `${prefix}${originalRouteId}`;
|
|
23
|
-
const newSource = source.replace(/routeId=[^&]+/, `routeId=${newRouteId}`);
|
|
24
|
-
path.node.source = t.stringLiteral(newSource);
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
ObjectExpression(path) {
|
|
28
|
-
let componentName = "";
|
|
29
|
-
let lazyComponentName = "";
|
|
30
|
-
if (!Array.isArray(path.node.properties)) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const idNode = findTargetKeyNode(path.node.properties, "id");
|
|
34
|
-
if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
|
|
35
|
-
idNode.value = t.stringLiteral(`${prefix}${idNode.value.value}`);
|
|
36
|
-
}
|
|
37
|
-
const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
|
|
38
|
-
if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
|
|
39
|
-
isRootNode.value.value = false;
|
|
40
|
-
}
|
|
41
|
-
if (!isRootNode) {
|
|
42
|
-
const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
|
|
43
|
-
if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
|
|
44
|
-
lazyComponentName = `LazyComponent_${componentId}`;
|
|
45
|
-
const lazyDeclaration = t.variableDeclaration("const", [
|
|
46
|
-
t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
|
|
47
|
-
]);
|
|
48
|
-
lazyComponentNode.value = t.identifier(lazyComponentName);
|
|
49
|
-
const componentNode2 = findTargetKeyNode(path.node.properties, "component");
|
|
50
|
-
if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
|
|
51
|
-
componentNode2.value = t.callExpression(t.identifier("lazy"), [
|
|
52
|
-
t.identifier(lazyComponentName)
|
|
53
|
-
]);
|
|
54
|
-
}
|
|
55
|
-
lazyComponentDeclarations.push(lazyDeclaration);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
const componentNode = findTargetKeyNode(path.node.properties, "component");
|
|
59
|
-
if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
|
|
60
|
-
componentName = `Component_${componentId}`;
|
|
61
|
-
const componentDeclaration = t.variableDeclaration("const", [
|
|
62
|
-
t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
|
|
63
|
-
t.identifier(lazyComponentName)
|
|
64
|
-
]) : componentNode.value)
|
|
65
|
-
]);
|
|
66
|
-
componentDeclarations.push(componentDeclaration);
|
|
67
|
-
componentNode.value = t.identifier(componentName);
|
|
68
|
-
}
|
|
69
|
-
if (lazyComponentName || componentName) {
|
|
70
|
-
componentId++;
|
|
71
|
-
if (componentName) {
|
|
72
|
-
const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
|
|
73
|
-
const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
|
|
74
|
-
path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
traverse(ast, {
|
|
80
|
-
Program(path) {
|
|
81
|
-
const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
|
|
82
|
-
if (t.isImportDeclaration(p.node)) {
|
|
83
|
-
lastIndex = index;
|
|
84
|
-
}
|
|
85
|
-
return lastIndex;
|
|
86
|
-
}, -1);
|
|
87
|
-
if (lastImportIndex >= 0) {
|
|
88
|
-
const lastImport = path.get(`body.${lastImportIndex}`);
|
|
89
|
-
[
|
|
90
|
-
...componentDeclarations,
|
|
91
|
-
...lazyComponentDeclarations
|
|
92
|
-
].forEach((declaration) => {
|
|
93
|
-
if ("insertAfter" in lastImport) {
|
|
94
|
-
lastImport.insertAfter(declaration);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
const { code: newCode } = generate(ast);
|
|
101
|
-
const finalCode = `${newCode}export const baseName = '${baseName}';`;
|
|
102
|
-
fs.writeFileSync(filePath, finalCode);
|
|
103
|
-
}
|
|
104
|
-
export {
|
|
105
|
-
generateRoutes
|
|
106
|
-
};
|
|
@@ -1,57 +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({ sourceCode, prefix }) {
|
|
7
|
-
const ast = babelParser.parse(sourceCode, {
|
|
8
|
-
sourceType: "module"
|
|
9
|
-
});
|
|
10
|
-
const removedKeys = [
|
|
11
|
-
COMPONENT,
|
|
12
|
-
SHOULD_REVALIDATE,
|
|
13
|
-
LAZY_COMPONENT,
|
|
14
|
-
PRIVATE_COMPONENT,
|
|
15
|
-
LOADER
|
|
16
|
-
];
|
|
17
|
-
traverse(ast, {
|
|
18
|
-
ObjectExpression(path1) {
|
|
19
|
-
if (!Array.isArray(path1.node.properties)) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
path1.node.properties.forEach((prop1) => {
|
|
23
|
-
if (t.isObjectProperty(prop1) && t.isStringLiteral(prop1.key) && t.isStringLiteral(prop1.value) && prop1.key.value === ID) {
|
|
24
|
-
prop1.value = t.stringLiteral(`${prefix}${prop1.value.value}`);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
path1.node.properties = path1.node.properties.filter((p1) => {
|
|
28
|
-
if (t.isObjectProperty(p1) && t.isStringLiteral(p1.key)) {
|
|
29
|
-
return !removedKeys.includes(p1.key.value);
|
|
30
|
-
} else {
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
let routesValue = "";
|
|
37
|
-
traverse(ast, {
|
|
38
|
-
VariableDeclarator(path) {
|
|
39
|
-
if (t.isVariableDeclarator(path.node) && t.isIdentifier(path.node.id) && path.node.id.name === "routes") {
|
|
40
|
-
const routesAst = path.node.init;
|
|
41
|
-
if (!routesAst) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const { code } = generate(routesAst, {
|
|
45
|
-
compact: true,
|
|
46
|
-
retainLines: false,
|
|
47
|
-
concise: true
|
|
48
|
-
});
|
|
49
|
-
routesValue = eval("(" + code + ")");
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return routesValue;
|
|
54
|
-
}
|
|
55
|
-
export {
|
|
56
|
-
generateSerializableRoutes
|
|
57
|
-
};
|
|
@@ -1,72 +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({ sourceCode, filePath, prefix, baseName }) {
|
|
8
|
-
const ast = babelParser.parse(sourceCode, {
|
|
9
|
-
sourceType: "module"
|
|
10
|
-
});
|
|
11
|
-
const removedKeys = [
|
|
12
|
-
COMPONENT,
|
|
13
|
-
SHOULD_REVALIDATE,
|
|
14
|
-
LAZY_COMPONENT,
|
|
15
|
-
PRIVATE_COMPONENT
|
|
16
|
-
];
|
|
17
|
-
traverse(ast, {
|
|
18
|
-
ImportDeclaration(path) {
|
|
19
|
-
const source = path.node.source.value;
|
|
20
|
-
const routeIdMatch = source.match(/routeId=([^&]+)/);
|
|
21
|
-
if (routeIdMatch) {
|
|
22
|
-
const originalRouteId = routeIdMatch[1];
|
|
23
|
-
const newRouteId = `${prefix}${originalRouteId}`;
|
|
24
|
-
const newSource = source.replace(/routeId=[^&]+/, `routeId=${newRouteId}`);
|
|
25
|
-
path.node.source = t.stringLiteral(newSource);
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
ObjectExpression(path) {
|
|
29
|
-
if (!Array.isArray(path.node.properties)) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
path.node.properties.forEach((prop) => {
|
|
33
|
-
if (t.isObjectProperty(prop) && t.isStringLiteral(prop.key) && t.isStringLiteral(prop.value) && prop.key.value === ID) {
|
|
34
|
-
prop.value = t.stringLiteral(`${prefix}${prop.value.value}`);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
path.node.properties = path.node.properties.filter((p) => {
|
|
38
|
-
if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
|
|
39
|
-
return !removedKeys.includes(p.key.value);
|
|
40
|
-
} else {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
const tempCode = generate(ast).code;
|
|
47
|
-
const tempAst = babelParser.parse(tempCode, {
|
|
48
|
-
sourceType: "module"
|
|
49
|
-
});
|
|
50
|
-
const usedIdentifiers = /* @__PURE__ */ new Set();
|
|
51
|
-
traverse(tempAst, {
|
|
52
|
-
Identifier(path) {
|
|
53
|
-
if (t.isProperty(path.parent)) {
|
|
54
|
-
usedIdentifiers.add(path.node.name);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
traverse(tempAst, {
|
|
59
|
-
ImportDeclaration(path) {
|
|
60
|
-
path.node.specifiers = path.node.specifiers.filter((specifier) => usedIdentifiers.has(specifier.local.name));
|
|
61
|
-
if (!path.node.specifiers.length) {
|
|
62
|
-
path.remove();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
const { code: newCode } = generate(tempAst);
|
|
67
|
-
const finalCode = `${newCode}export const baseName = '${baseName}';`;
|
|
68
|
-
fs.writeFileSync(filePath, finalCode);
|
|
69
|
-
}
|
|
70
|
-
export {
|
|
71
|
-
generateSlimRoutes
|
|
72
|
-
};
|
|
@@ -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,30 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import { generateSerializableRoutes } from "../ast";
|
|
3
|
-
const SERIALIZABLE_ROUTES = "serializable-routes.json";
|
|
4
|
-
class SerializableRoutesPlugin {
|
|
5
|
-
apply(compiler) {
|
|
6
|
-
compiler.hooks.thisCompilation.tap("generateStats", (compilation) => {
|
|
7
|
-
compilation.hooks.processAssets.tapPromise({
|
|
8
|
-
name: "generateSerializableRoutes",
|
|
9
|
-
// @ts-ignore use runtime variable in case peer dep not installed
|
|
10
|
-
stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
|
|
11
|
-
}, async () => {
|
|
12
|
-
const sourceCode = fs.readFileSync(this.routesFilePath, "utf-8");
|
|
13
|
-
const routesCode = generateSerializableRoutes({
|
|
14
|
-
sourceCode,
|
|
15
|
-
prefix: this.prefix
|
|
16
|
-
});
|
|
17
|
-
compilation.emitAsset(SERIALIZABLE_ROUTES, new compiler.webpack.sources.RawSource(JSON.stringify(routesCode, null, 2)));
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
constructor(options) {
|
|
22
|
-
const { routesFilePath, prefix } = options;
|
|
23
|
-
this.routesFilePath = routesFilePath;
|
|
24
|
-
this.prefix = prefix;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
export {
|
|
28
|
-
SERIALIZABLE_ROUTES,
|
|
29
|
-
SerializableRoutesPlugin
|
|
30
|
-
};
|