@modern-js/app-tools 2.27.1-alpha.0 → 2.28.1-alpha.0
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 +44 -0
- package/dist/cjs/analyze/generateCode.js +16 -7
- package/dist/cjs/analyze/getFileSystemEntry.js +2 -1
- package/dist/cjs/analyze/getServerRoutes.js +5 -4
- package/dist/cjs/analyze/index.js +209 -211
- package/dist/cjs/analyze/nestedRoutes.js +58 -15
- package/dist/cjs/analyze/templates.js +4 -4
- package/dist/cjs/builder/builder-rspack/adapterCopy.js +41 -43
- package/dist/cjs/builder/builder-webpack/adapterModern.js +23 -25
- package/dist/cjs/builder/generator/createBuilderProviderConfig.js +2 -1
- package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +12 -15
- package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
- package/dist/cjs/commands/build.js +2 -1
- package/dist/cjs/commands/inspect.js +2 -1
- package/dist/cjs/commands/serve.js +2 -2
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/initialize/index.js +70 -72
- package/dist/esm/analyze/generateCode.js +35 -10
- package/dist/esm/analyze/getFileSystemEntry.js +2 -1
- package/dist/esm/analyze/getServerRoutes.js +5 -4
- package/dist/esm/analyze/nestedRoutes.js +59 -13
- package/dist/esm/analyze/templates.js +4 -4
- package/dist/esm/builder/builder-rspack/adapterCopy.js +1 -1
- package/dist/esm/builder/generator/createBuilderProviderConfig.js +2 -1
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +13 -16
- package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +41 -101
- package/dist/esm/commands/build.js +2 -2
- package/dist/esm/commands/inspect.js +2 -2
- package/dist/esm/commands/serve.js +2 -2
- package/dist/esm/index.js +3 -2
- package/dist/esm-node/analyze/generateCode.js +16 -7
- package/dist/esm-node/analyze/getFileSystemEntry.js +2 -1
- package/dist/esm-node/analyze/getServerRoutes.js +5 -4
- package/dist/esm-node/analyze/index.js +209 -211
- package/dist/esm-node/analyze/nestedRoutes.js +55 -15
- package/dist/esm-node/analyze/templates.js +4 -4
- package/dist/esm-node/builder/builder-rspack/adapterCopy.js +41 -43
- package/dist/esm-node/builder/builder-webpack/adapterModern.js +23 -25
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +2 -1
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +12 -15
- package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
- package/dist/esm-node/commands/build.js +2 -1
- package/dist/esm-node/commands/inspect.js +2 -1
- package/dist/esm-node/commands/serve.js +2 -2
- package/dist/esm-node/index.js +3 -2
- package/dist/esm-node/initialize/index.js +70 -72
- package/dist/types/analyze/nestedRoutes.d.ts +2 -1
- package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +0 -8
- package/package.json +24 -24
|
@@ -13,6 +13,9 @@ _export(exports, {
|
|
|
13
13
|
getRouteId: function() {
|
|
14
14
|
return getRouteId;
|
|
15
15
|
},
|
|
16
|
+
optimizeRoute: function() {
|
|
17
|
+
return optimizeRoute;
|
|
18
|
+
},
|
|
16
19
|
walk: function() {
|
|
17
20
|
return walk;
|
|
18
21
|
}
|
|
@@ -56,8 +59,35 @@ const createRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
|
|
|
56
59
|
type: "nested"
|
|
57
60
|
};
|
|
58
61
|
};
|
|
59
|
-
const
|
|
60
|
-
|
|
62
|
+
const optimizeRoute = (routeTree) => {
|
|
63
|
+
if (!routeTree.children || routeTree.children.length === 0) {
|
|
64
|
+
return [
|
|
65
|
+
routeTree
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
const { children } = routeTree;
|
|
69
|
+
if (!routeTree._component) {
|
|
70
|
+
const newRoutes = children.map((child) => {
|
|
71
|
+
var _routeTree_path;
|
|
72
|
+
const routePath = `${(_routeTree_path = routeTree.path) !== null && _routeTree_path !== void 0 ? _routeTree_path : ""}${child.path ? `/${child.path}` : ""}`;
|
|
73
|
+
return {
|
|
74
|
+
...child,
|
|
75
|
+
path: routePath
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
if (newRoutes.length > 1) {
|
|
79
|
+
return newRoutes.flatMap((newRoute) => optimizeRoute(newRoute));
|
|
80
|
+
}
|
|
81
|
+
return newRoutes;
|
|
82
|
+
} else {
|
|
83
|
+
routeTree.children = children.flatMap(optimizeRoute);
|
|
84
|
+
return [
|
|
85
|
+
routeTree
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
|
|
90
|
+
var _routePath, _finalRoute_children;
|
|
61
91
|
if (!await _utils.fs.pathExists(dirname)) {
|
|
62
92
|
return null;
|
|
63
93
|
}
|
|
@@ -77,7 +107,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
77
107
|
}
|
|
78
108
|
routePath = replaceDynamicPath(routePath);
|
|
79
109
|
const route = {
|
|
80
|
-
path: routePath === null ||
|
|
110
|
+
path: (_routePath = routePath) === null || _routePath === void 0 ? void 0 : _routePath.replace(/\$$/, "?"),
|
|
81
111
|
children: [],
|
|
82
112
|
isRoot
|
|
83
113
|
};
|
|
@@ -93,10 +123,10 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
93
123
|
const itemWithoutExt = item.slice(0, -extname.length);
|
|
94
124
|
const isDirectory2 = (await _utils.fs.stat(itemPath)).isDirectory();
|
|
95
125
|
if (isDirectory2) {
|
|
96
|
-
const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry);
|
|
97
|
-
if (childRoute) {
|
|
98
|
-
var
|
|
99
|
-
(
|
|
126
|
+
const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
|
|
127
|
+
if (childRoute && !Array.isArray(childRoute)) {
|
|
128
|
+
var _route_children;
|
|
129
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
|
|
100
130
|
}
|
|
101
131
|
}
|
|
102
132
|
if (extname && (!_constants.JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
|
|
@@ -122,7 +152,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
122
152
|
pageConfigFile = itemPath;
|
|
123
153
|
}
|
|
124
154
|
if (itemWithoutExt === _constants.NESTED_ROUTE.PAGE_FILE) {
|
|
125
|
-
var
|
|
155
|
+
var _route_children1;
|
|
126
156
|
pageRoute = createIndexRoute({
|
|
127
157
|
_component: (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name)
|
|
128
158
|
}, rootDir, itemPath, entryName, isMainEntry);
|
|
@@ -132,13 +162,13 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
132
162
|
if (pageConfigFile) {
|
|
133
163
|
pageRoute.config = pageConfigFile;
|
|
134
164
|
}
|
|
135
|
-
(
|
|
165
|
+
(_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
|
|
136
166
|
}
|
|
137
167
|
if (itemWithoutExt === _constants.NESTED_ROUTE.SPLATE_LOADER_FILE) {
|
|
138
168
|
splatLoaderFile = itemPath;
|
|
139
169
|
}
|
|
140
170
|
if (itemWithoutExt === _constants.NESTED_ROUTE.SPLATE_FILE) {
|
|
141
|
-
var
|
|
171
|
+
var _route_children2;
|
|
142
172
|
splatRoute = createRoute({
|
|
143
173
|
_component: (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name),
|
|
144
174
|
path: "*"
|
|
@@ -146,7 +176,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
146
176
|
if (splatLoaderFile) {
|
|
147
177
|
splatRoute.loader = splatLoaderFile;
|
|
148
178
|
}
|
|
149
|
-
(
|
|
179
|
+
(_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
|
|
150
180
|
}
|
|
151
181
|
if (itemWithoutExt === _constants.NESTED_ROUTE.LOADING_FILE) {
|
|
152
182
|
route.loading = (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name);
|
|
@@ -159,12 +189,12 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
159
189
|
if (isPathlessLayout) {
|
|
160
190
|
delete finalRoute.path;
|
|
161
191
|
}
|
|
162
|
-
|
|
163
|
-
if (
|
|
192
|
+
const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
|
|
193
|
+
if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
|
|
164
194
|
return null;
|
|
165
195
|
}
|
|
166
|
-
if (
|
|
167
|
-
const childRoute =
|
|
196
|
+
if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
|
|
197
|
+
const childRoute = childRoutes[0];
|
|
168
198
|
if (childRoute.path === "*") {
|
|
169
199
|
const path = `${finalRoute.path || ""}/${childRoute.path || ""}`;
|
|
170
200
|
finalRoute = {
|
|
@@ -173,5 +203,18 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
173
203
|
};
|
|
174
204
|
}
|
|
175
205
|
}
|
|
206
|
+
if (splatRoute) {
|
|
207
|
+
var _childRoutes;
|
|
208
|
+
const slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex((childRoute) => childRoute === splatRoute);
|
|
209
|
+
if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
|
|
210
|
+
var _childRoutes1, _childRoutes2;
|
|
211
|
+
(_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
|
|
212
|
+
(_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (isRoot && !oldVersion) {
|
|
216
|
+
const optimizedRoutes = optimizeRoute(finalRoute);
|
|
217
|
+
return optimizedRoutes;
|
|
218
|
+
}
|
|
176
219
|
return finalRoute;
|
|
177
220
|
};
|
|
@@ -111,8 +111,8 @@ const routesForServer = ({ routes }) => {
|
|
|
111
111
|
const traverseRouteTree = (route) => {
|
|
112
112
|
let children;
|
|
113
113
|
if ("children" in route && route.children) {
|
|
114
|
-
var _route_children;
|
|
115
|
-
children = route === null ||
|
|
114
|
+
var _route_children, _route;
|
|
115
|
+
children = (_route = route) === null || _route === void 0 ? void 0 : (_route_children = _route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
116
116
|
}
|
|
117
117
|
let loader;
|
|
118
118
|
if (route.type === "nested") {
|
|
@@ -179,8 +179,8 @@ const fileSystemRoutes = async ({ routes, ssrMode, nestedRoutesEntry, entryName,
|
|
|
179
179
|
const traverseRouteTree = (route) => {
|
|
180
180
|
let children;
|
|
181
181
|
if ("children" in route && route.children) {
|
|
182
|
-
var _route_children;
|
|
183
|
-
children = route === null ||
|
|
182
|
+
var _route_children, _route;
|
|
183
|
+
children = (_route = route) === null || _route === void 0 ? void 0 : (_route_children = _route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
184
184
|
}
|
|
185
185
|
let loading;
|
|
186
186
|
let error;
|
|
@@ -13,52 +13,50 @@ const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
|
|
|
13
13
|
const _fsextra = /* @__PURE__ */ _interop_require_default._(require("@modern-js/utils/fs-extra"));
|
|
14
14
|
const _utils = require("@modern-js/utils");
|
|
15
15
|
const _shared = require("../shared");
|
|
16
|
-
const builderPluginAdpaterCopy = (options) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
config.builtins
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
]
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
publicPath = (_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath;
|
|
33
|
-
});
|
|
34
|
-
api.onDevCompileDone(async () => {
|
|
35
|
-
await transformHtmlFiles();
|
|
36
|
-
});
|
|
37
|
-
api.onAfterBuild(async () => {
|
|
38
|
-
await transformHtmlFiles();
|
|
39
|
-
});
|
|
40
|
-
async function transformHtmlFiles() {
|
|
41
|
-
var _normalizedConfig_output_distPath;
|
|
42
|
-
const { normalizedConfig } = options;
|
|
43
|
-
const publicDir = _path.default.resolve(((_normalizedConfig_output_distPath = normalizedConfig.output.distPath) === null || _normalizedConfig_output_distPath === void 0 ? void 0 : _normalizedConfig_output_distPath.root) || "./dist", "./public");
|
|
44
|
-
if (!_fsextra.default.existsSync(publicDir) || !_fsextra.default.statSync(publicDir).isDirectory()) {
|
|
45
|
-
return;
|
|
16
|
+
const builderPluginAdpaterCopy = (options) => ({
|
|
17
|
+
name: "builder-plugin-adapter-rspack-copy",
|
|
18
|
+
setup(api) {
|
|
19
|
+
let publicPath;
|
|
20
|
+
api.modifyRspackConfig((config) => {
|
|
21
|
+
var _config_builtins_copy, _config_builtins, _config_output;
|
|
22
|
+
config.builtins = {
|
|
23
|
+
...config.builtins || {},
|
|
24
|
+
copy: {
|
|
25
|
+
patterns: [
|
|
26
|
+
...((_config_builtins = config.builtins) === null || _config_builtins === void 0 ? void 0 : (_config_builtins_copy = _config_builtins.copy) === null || _config_builtins_copy === void 0 ? void 0 : _config_builtins_copy.patterns) || [],
|
|
27
|
+
...createConfigBuiltinCopy(options)
|
|
28
|
+
]
|
|
46
29
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
30
|
+
};
|
|
31
|
+
publicPath = (_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath;
|
|
32
|
+
});
|
|
33
|
+
api.onDevCompileDone(async () => {
|
|
34
|
+
await transformHtmlFiles();
|
|
35
|
+
});
|
|
36
|
+
api.onAfterBuild(async () => {
|
|
37
|
+
await transformHtmlFiles();
|
|
38
|
+
});
|
|
39
|
+
async function transformHtmlFiles() {
|
|
40
|
+
var _normalizedConfig_output_distPath;
|
|
41
|
+
const { normalizedConfig } = options;
|
|
42
|
+
const publicDir = _path.default.resolve(((_normalizedConfig_output_distPath = normalizedConfig.output.distPath) === null || _normalizedConfig_output_distPath === void 0 ? void 0 : _normalizedConfig_output_distPath.root) || "./dist", "./public");
|
|
43
|
+
if (!_fsextra.default.existsSync(publicDir) || !_fsextra.default.statSync(publicDir).isDirectory()) {
|
|
44
|
+
return;
|
|
58
45
|
}
|
|
46
|
+
const HTML_REGEXP = /\.html?$/;
|
|
47
|
+
const filepaths = (await _fsextra.default.readdir(publicDir)).map((file) => _path.default.resolve(publicDir, file));
|
|
48
|
+
await Promise.all(filepaths.filter((file) => HTML_REGEXP.test(file)).map(async (file) => {
|
|
49
|
+
const content = await _fsextra.default.readFile(file, "utf-8");
|
|
50
|
+
if (publicPath) {
|
|
51
|
+
await _fsextra.default.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, (0, _utils.removeTailSlash)(publicPath)));
|
|
52
|
+
} else {
|
|
53
|
+
_utils.logger.warn("Expect get a string from `publicPath`, but receive `undefined`.");
|
|
54
|
+
await _fsextra.default.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, (0, _utils.removeTailSlash)("/")));
|
|
55
|
+
}
|
|
56
|
+
}));
|
|
59
57
|
}
|
|
60
|
-
}
|
|
61
|
-
};
|
|
58
|
+
}
|
|
59
|
+
});
|
|
62
60
|
function createConfigBuiltinCopy(options) {
|
|
63
61
|
const { normalizedConfig, appContext } = options;
|
|
64
62
|
const { publicDir } = (0, _shared.createCopyInfo)(appContext, normalizedConfig);
|
|
@@ -10,28 +10,26 @@ Object.defineProperty(exports, "builderPluginAdapterModern", {
|
|
|
10
10
|
});
|
|
11
11
|
const _path = require("path");
|
|
12
12
|
const _createCopyPattern = require("./createCopyPattern");
|
|
13
|
-
const builderPluginAdapterModern = (options) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
};
|
|
13
|
+
const builderPluginAdapterModern = (options) => ({
|
|
14
|
+
name: "builder-plugin-adapter-modern",
|
|
15
|
+
setup(api) {
|
|
16
|
+
const { normalizedConfig: modernConfig, appContext } = options;
|
|
17
|
+
api.modifyWebpackChain((chain, { CHAIN_ID }) => {
|
|
18
|
+
chain.resolve.modules.add("node_modules").add((0, _path.join)(api.context.rootPath, "node_modules"));
|
|
19
|
+
if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
|
|
20
|
+
const defaultCopyPattern = (0, _createCopyPattern.createPublicPattern)(appContext, modernConfig, chain);
|
|
21
|
+
chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
|
|
22
|
+
var _args_;
|
|
23
|
+
return [
|
|
24
|
+
{
|
|
25
|
+
patterns: [
|
|
26
|
+
...((_args_ = args[0]) === null || _args_ === void 0 ? void 0 : _args_.patterns) || [],
|
|
27
|
+
defaultCopyPattern
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
@@ -10,9 +10,10 @@ Object.defineProperty(exports, "createBuilderProviderConfig", {
|
|
|
10
10
|
});
|
|
11
11
|
const _createCopyPattern = require("../builder-webpack/createCopyPattern");
|
|
12
12
|
function modifyOutputConfig(config, appContext) {
|
|
13
|
+
var _copy;
|
|
13
14
|
const defaultCopyPattern = (0, _createCopyPattern.createUploadPattern)(appContext, config);
|
|
14
15
|
const { copy } = config.output;
|
|
15
|
-
const copyOptions = Array.isArray(copy) ? copy : copy === null ||
|
|
16
|
+
const copyOptions = Array.isArray(copy) ? copy : (_copy = copy) === null || _copy === void 0 ? void 0 : _copy.patterns;
|
|
16
17
|
const builderCopy = [
|
|
17
18
|
...copyOptions || [],
|
|
18
19
|
defaultCopyPattern
|
|
@@ -58,12 +58,13 @@ const builderPluginAdapterSSR = (options) => ({
|
|
|
58
58
|
}
|
|
59
59
|
});
|
|
60
60
|
const isStreamingSSR = (userConfig) => {
|
|
61
|
+
var _server;
|
|
61
62
|
const isStreaming = (ssr) => ssr && typeof ssr === "object" && ssr.mode === "stream";
|
|
62
63
|
const { server } = userConfig;
|
|
63
64
|
if (isStreaming(server.ssr)) {
|
|
64
65
|
return true;
|
|
65
66
|
}
|
|
66
|
-
if ((server === null ||
|
|
67
|
+
if (((_server = server) === null || _server === void 0 ? void 0 : _server.ssrByEntries) && typeof server.ssrByEntries === "object") {
|
|
67
68
|
for (const name of Object.keys(server.ssrByEntries)) {
|
|
68
69
|
if (isStreaming(server.ssrByEntries[name])) {
|
|
69
70
|
return true;
|
|
@@ -80,38 +81,33 @@ function applyAsyncChunkHtmlPlugin({ chain, modernConfig, CHAIN_ID, HtmlBundlerP
|
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
83
|
function applyRouterPlugin(chain, options) {
|
|
83
|
-
var _normalizedConfig_runtime, _normalizedConfig_deploy_worker;
|
|
84
|
+
var _normalizedConfig_runtime, _normalizedConfig, _routerConfig, _normalizedConfig_deploy_worker;
|
|
84
85
|
const { appContext, normalizedConfig } = options;
|
|
85
86
|
const { entrypoints } = appContext;
|
|
86
87
|
const existNestedRoutes = entrypoints.some((entrypoint) => entrypoint.nestedRoutesEntry);
|
|
87
|
-
const routerConfig = normalizedConfig === null ||
|
|
88
|
-
const routerManifest = Boolean(routerConfig === null ||
|
|
88
|
+
const routerConfig = (_normalizedConfig = normalizedConfig) === null || _normalizedConfig === void 0 ? void 0 : (_normalizedConfig_runtime = _normalizedConfig.runtime) === null || _normalizedConfig_runtime === void 0 ? void 0 : _normalizedConfig_runtime.router;
|
|
89
|
+
const routerManifest = Boolean((_routerConfig = routerConfig) === null || _routerConfig === void 0 ? void 0 : _routerConfig.manifest);
|
|
89
90
|
const workerSSR = Boolean((_normalizedConfig_deploy_worker = normalizedConfig.deploy.worker) === null || _normalizedConfig_deploy_worker === void 0 ? void 0 : _normalizedConfig_deploy_worker.ssr);
|
|
90
|
-
const minimize = !normalizedConfig.output.disableMinimize && process.env.NODE_ENV === "production";
|
|
91
91
|
if (existNestedRoutes || routerManifest || workerSSR) {
|
|
92
|
-
chain.plugin("route-plugin").use(_bundlerPlugins.RouterPlugin
|
|
93
|
-
{
|
|
94
|
-
minimize
|
|
95
|
-
}
|
|
96
|
-
]);
|
|
92
|
+
chain.plugin("route-plugin").use(_bundlerPlugins.RouterPlugin);
|
|
97
93
|
}
|
|
98
94
|
}
|
|
99
95
|
function applyFilterEntriesBySSRConfig({ isProd, chain, appNormalizedConfig }) {
|
|
100
|
-
var
|
|
96
|
+
var _outputConfig, _this, _outputConfig1, _outputConfig2, _outputConfig3;
|
|
101
97
|
const { server: serverConfig, output: outputConfig } = appNormalizedConfig;
|
|
102
98
|
const entries = chain.entryPoints.entries();
|
|
103
|
-
if (isProd && ((outputConfig === null ||
|
|
99
|
+
if (isProd && (((_outputConfig = outputConfig) === null || _outputConfig === void 0 ? void 0 : _outputConfig.ssg) === true || typeof ((_this = (_outputConfig1 = outputConfig) === null || _outputConfig1 === void 0 ? void 0 : _outputConfig1.ssg) === null || _this === void 0 ? void 0 : _this[0]) === "function")) {
|
|
104
100
|
return;
|
|
105
101
|
}
|
|
106
102
|
if (typeof entries === "undefined") {
|
|
107
103
|
throw new Error("No entry found, one of src/routes/layout.tsx, src/App.tsx, src/index.tsx is required");
|
|
108
104
|
}
|
|
109
105
|
const entryNames = Object.keys(entries);
|
|
110
|
-
if (isProd && entryNames.length === 1 && (outputConfig === null ||
|
|
106
|
+
if (isProd && entryNames.length === 1 && ((_outputConfig2 = outputConfig) === null || _outputConfig2 === void 0 ? void 0 : _outputConfig2.ssg)) {
|
|
111
107
|
return;
|
|
112
108
|
}
|
|
113
109
|
const ssgEntries = [];
|
|
114
|
-
if (isProd && (outputConfig === null ||
|
|
110
|
+
if (isProd && ((_outputConfig3 = outputConfig) === null || _outputConfig3 === void 0 ? void 0 : _outputConfig3.ssg)) {
|
|
115
111
|
const { ssg } = outputConfig;
|
|
116
112
|
entryNames.forEach((name) => {
|
|
117
113
|
if (ssg[name]) {
|
|
@@ -121,7 +117,8 @@ function applyFilterEntriesBySSRConfig({ isProd, chain, appNormalizedConfig }) {
|
|
|
121
117
|
}
|
|
122
118
|
const { ssr, ssrByEntries } = serverConfig || {};
|
|
123
119
|
entryNames.forEach((name) => {
|
|
124
|
-
|
|
120
|
+
var _ssrByEntries, _ssrByEntries1;
|
|
121
|
+
if (!ssgEntries.includes(name) && (ssr && ((_ssrByEntries = ssrByEntries) === null || _ssrByEntries === void 0 ? void 0 : _ssrByEntries[name]) === false || !ssr && !((_ssrByEntries1 = ssrByEntries) === null || _ssrByEntries1 === void 0 ? void 0 : _ssrByEntries1[name]))) {
|
|
125
122
|
chain.entryPoints.delete(name);
|
|
126
123
|
}
|
|
127
124
|
});
|
|
@@ -8,7 +8,6 @@ Object.defineProperty(exports, "RouterPlugin", {
|
|
|
8
8
|
return RouterPlugin;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const _define_property = require("@swc/helpers/_/_define_property");
|
|
12
11
|
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
13
12
|
const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
|
|
14
13
|
const _lodash = require("@modern-js/utils/lodash");
|
|
@@ -26,22 +25,6 @@ class RouterPlugin {
|
|
|
26
25
|
}
|
|
27
26
|
return false;
|
|
28
27
|
}
|
|
29
|
-
getEntryChunks(compilation, chunks) {
|
|
30
|
-
const entrypointsArray = Array.from(compilation.entrypoints.entries());
|
|
31
|
-
const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
|
|
32
|
-
const entryChunks = [
|
|
33
|
-
...chunks
|
|
34
|
-
].filter((chunk) => {
|
|
35
|
-
var _chunk_names;
|
|
36
|
-
return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
|
|
37
|
-
});
|
|
38
|
-
return entryChunks;
|
|
39
|
-
}
|
|
40
|
-
getEntryChunkFiles(entryChunks) {
|
|
41
|
-
return entryChunks.map((chunk) => [
|
|
42
|
-
...chunk.files || []
|
|
43
|
-
].find((fname) => fname.includes(".js")));
|
|
44
|
-
}
|
|
45
28
|
apply(compiler) {
|
|
46
29
|
const { target } = compiler.options;
|
|
47
30
|
if (this.isTargetNodeOrWebWorker(target)) {
|
|
@@ -56,8 +39,7 @@ class RouterPlugin {
|
|
|
56
39
|
}
|
|
57
40
|
return path;
|
|
58
41
|
};
|
|
59
|
-
const
|
|
60
|
-
const chunkToMap = /* @__PURE__ */ new Map();
|
|
42
|
+
const chunkToSourceAndMap = /* @__PURE__ */ new Map();
|
|
61
43
|
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|
62
44
|
compilation.hooks.processAssets.tapPromise({
|
|
63
45
|
name: PLUGIN_NAME,
|
|
@@ -73,37 +55,14 @@ class RouterPlugin {
|
|
|
73
55
|
if (!namedChunkGroups) {
|
|
74
56
|
return;
|
|
75
57
|
}
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const chunkId = entryChunkFileIds[i];
|
|
84
|
-
const asset = compilation.assets[file];
|
|
85
|
-
if (!asset) {
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
const { map } = asset.sourceAndMap();
|
|
89
|
-
chunkToMap.set(chunkId, map);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
compilation.hooks.processAssets.tapPromise({
|
|
93
|
-
name: PLUGIN_NAME,
|
|
94
|
-
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
|
|
95
|
-
}, async () => {
|
|
96
|
-
const stats = compilation.getStats().toJson({
|
|
97
|
-
all: false,
|
|
98
|
-
chunkGroups: true,
|
|
99
|
-
chunks: true,
|
|
100
|
-
ids: true
|
|
58
|
+
const entrypointsArray = Array.from(compilation.entrypoints.entries());
|
|
59
|
+
const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
|
|
60
|
+
const entryChunks = [
|
|
61
|
+
...chunks
|
|
62
|
+
].filter((chunk) => {
|
|
63
|
+
var _chunk_names;
|
|
64
|
+
return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
|
|
101
65
|
});
|
|
102
|
-
const { chunks = [], namedChunkGroups } = stats;
|
|
103
|
-
if (!namedChunkGroups) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const entryChunks = this.getEntryChunks(compilation, chunks);
|
|
107
66
|
const entryChunkFiles = entryChunks.map((chunk) => [
|
|
108
67
|
...chunk.files || []
|
|
109
68
|
].find((fname) => fname.includes(".js")));
|
|
@@ -115,8 +74,11 @@ class RouterPlugin {
|
|
|
115
74
|
if (!asset) {
|
|
116
75
|
continue;
|
|
117
76
|
}
|
|
118
|
-
const { source } = asset.sourceAndMap();
|
|
119
|
-
|
|
77
|
+
const { source, map } = asset.sourceAndMap();
|
|
78
|
+
chunkToSourceAndMap.set(chunkId, {
|
|
79
|
+
source,
|
|
80
|
+
map
|
|
81
|
+
});
|
|
120
82
|
}
|
|
121
83
|
});
|
|
122
84
|
compilation.hooks.processAssets.tapPromise({
|
|
@@ -149,7 +111,6 @@ class RouterPlugin {
|
|
|
149
111
|
const referenceCssAssets = assets.filter((asset) => /\.css$/.test(asset));
|
|
150
112
|
routeAssets[name] = {
|
|
151
113
|
chunkIds: chunkGroup.chunks,
|
|
152
|
-
chunkName: chunkGroup.name,
|
|
153
114
|
assets,
|
|
154
115
|
referenceCssAssets
|
|
155
116
|
};
|
|
@@ -165,29 +126,31 @@ class RouterPlugin {
|
|
|
165
126
|
const manifest = {
|
|
166
127
|
routeAssets
|
|
167
128
|
};
|
|
168
|
-
const
|
|
169
|
-
const
|
|
129
|
+
const entrypointsArray = Array.from(compilation.entrypoints.entries());
|
|
130
|
+
const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
|
|
131
|
+
const entryChunks = [
|
|
132
|
+
...chunks
|
|
133
|
+
].filter((chunk) => {
|
|
134
|
+
var _chunk_names;
|
|
135
|
+
return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
|
|
136
|
+
});
|
|
137
|
+
const entryChunkFiles = entryChunks.map((chunk) => [
|
|
138
|
+
...chunk.files || []
|
|
139
|
+
].find((fname) => fname.includes(".js")));
|
|
170
140
|
const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
|
|
171
|
-
for (let i = 0; i
|
|
141
|
+
for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
|
|
172
142
|
const file = entryChunkFiles[i];
|
|
173
|
-
const chunkNames = entryChunks[i].names;
|
|
174
143
|
const chunkId = entryChunkFileIds[i];
|
|
175
144
|
const asset = compilation.assets[file];
|
|
176
145
|
if (!asset || !chunkId) {
|
|
177
146
|
continue;
|
|
178
147
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
relatedAssets[routeId] = routeAssets[routeId];
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
} else {
|
|
189
|
-
relatedAssets = routeAssets;
|
|
190
|
-
}
|
|
148
|
+
const relatedAssets = {};
|
|
149
|
+
Object.keys(routeAssets).forEach((routeId) => {
|
|
150
|
+
if (routeId.startsWith(`${chunkId}`)) {
|
|
151
|
+
relatedAssets[routeId] = routeAssets[routeId];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
191
154
|
const manifest2 = {
|
|
192
155
|
routeAssets: relatedAssets
|
|
193
156
|
};
|
|
@@ -203,13 +166,12 @@ class RouterPlugin {
|
|
|
203
166
|
})};
|
|
204
167
|
})();
|
|
205
168
|
`;
|
|
206
|
-
const source =
|
|
207
|
-
const map = chunkToMap.get(chunkId);
|
|
169
|
+
const { source, map } = chunkToSourceAndMap.get(chunkId);
|
|
208
170
|
const newContent = `${injectedContent}${source.toString()}`;
|
|
209
171
|
const result = await (0, _esbuild.transform)(newContent, {
|
|
210
172
|
loader: _path.default.extname(file).slice(1),
|
|
211
173
|
sourcemap: true,
|
|
212
|
-
minify:
|
|
174
|
+
minify: process.env.NODE_ENV === "production"
|
|
213
175
|
});
|
|
214
176
|
const newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
|
|
215
177
|
compilation.updateAsset(
|
|
@@ -232,8 +194,4 @@ class RouterPlugin {
|
|
|
232
194
|
});
|
|
233
195
|
});
|
|
234
196
|
}
|
|
235
|
-
constructor(options) {
|
|
236
|
-
_define_property._(this, "minimize", false);
|
|
237
|
-
this.minimize = options.minimize;
|
|
238
|
-
}
|
|
239
197
|
}
|
|
@@ -13,7 +13,8 @@ const _utils = require("@modern-js/utils");
|
|
|
13
13
|
const _routes = require("../utils/routes");
|
|
14
14
|
const _config = require("../utils/config");
|
|
15
15
|
const build = async (api, options) => {
|
|
16
|
-
|
|
16
|
+
var _options;
|
|
17
|
+
if ((_options = options) === null || _options === void 0 ? void 0 : _options.analyze) {
|
|
17
18
|
process.env.BUNDLE_ANALYZE = "true";
|
|
18
19
|
}
|
|
19
20
|
let resolvedConfig = api.useResolvedConfigContext();
|
|
@@ -10,6 +10,7 @@ Object.defineProperty(exports, "inspect", {
|
|
|
10
10
|
});
|
|
11
11
|
const _path = require("path");
|
|
12
12
|
const inspect = async (api, options) => {
|
|
13
|
+
var _appContext;
|
|
13
14
|
const appContext = api.useAppContext();
|
|
14
15
|
if (!appContext.builder) {
|
|
15
16
|
throw new Error("Expect the Builder to have been initialized, But the appContext.builder received `undefined`");
|
|
@@ -17,7 +18,7 @@ const inspect = async (api, options) => {
|
|
|
17
18
|
return appContext.builder.inspectConfig({
|
|
18
19
|
env: options.env,
|
|
19
20
|
verbose: options.verbose,
|
|
20
|
-
outputPath: (0, _path.join)(appContext === null ||
|
|
21
|
+
outputPath: (0, _path.join)((_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.builder.context.distPath, options.output),
|
|
21
22
|
writeToDisk: true
|
|
22
23
|
});
|
|
23
24
|
};
|
|
@@ -15,13 +15,13 @@ const _printInstructions = require("../utils/printInstructions");
|
|
|
15
15
|
const _createServer = require("../utils/createServer");
|
|
16
16
|
const _getServerInternalPlugins = require("../utils/getServerInternalPlugins");
|
|
17
17
|
const start = async (api) => {
|
|
18
|
-
var _userConfig_source, _userConfig_output_distPath;
|
|
18
|
+
var _userConfig_source, _userConfig, _userConfig_output_distPath;
|
|
19
19
|
const appContext = api.useAppContext();
|
|
20
20
|
const userConfig = api.useResolvedConfigContext();
|
|
21
21
|
const hookRunners = api.useHookRunners();
|
|
22
22
|
const { appDirectory, port, serverConfigFile, metaName } = appContext;
|
|
23
23
|
_utils.logger.log(_utils.chalk.cyan(`Starting the modern server...`));
|
|
24
|
-
const apiOnly = await (0, _utils.isApiOnly)(appContext.appDirectory, userConfig === null ||
|
|
24
|
+
const apiOnly = await (0, _utils.isApiOnly)(appContext.appDirectory, (_userConfig = userConfig) === null || _userConfig === void 0 ? void 0 : (_userConfig_source = _userConfig.source) === null || _userConfig_source === void 0 ? void 0 : _userConfig_source.entriesDir, appContext.apiDirectory);
|
|
25
25
|
const serverInternalPlugins = await (0, _getServerInternalPlugins.getServerInternalPlugins)(api);
|
|
26
26
|
const app = await (0, _prodserver.default)({
|
|
27
27
|
pwd: appDirectory,
|
package/dist/cjs/index.js
CHANGED
|
@@ -94,6 +94,7 @@ const buildCommand = async (program, api) => {
|
|
|
94
94
|
const appTools = (options = {
|
|
95
95
|
bundler: "webpack"
|
|
96
96
|
}) => {
|
|
97
|
+
var _options, _options1;
|
|
97
98
|
return {
|
|
98
99
|
name: "@modern-js/app-tools",
|
|
99
100
|
post: [
|
|
@@ -109,10 +110,10 @@ const appTools = (options = {
|
|
|
109
110
|
registerHook: _hooks.hooks,
|
|
110
111
|
usePlugins: [
|
|
111
112
|
(0, _initialize.default)({
|
|
112
|
-
bundler: (options === null ||
|
|
113
|
+
bundler: ((_options = options) === null || _options === void 0 ? void 0 : _options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
|
|
113
114
|
}),
|
|
114
115
|
(0, _analyze.default)({
|
|
115
|
-
bundler: (options === null ||
|
|
116
|
+
bundler: ((_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.bundler) === "experimental-rspack" ? "rspack" : "webpack"
|
|
116
117
|
}),
|
|
117
118
|
(0, _pluginlint.lintPlugin)()
|
|
118
119
|
],
|