@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.
Files changed (49) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/cjs/analyze/generateCode.js +16 -7
  3. package/dist/cjs/analyze/getFileSystemEntry.js +2 -1
  4. package/dist/cjs/analyze/getServerRoutes.js +5 -4
  5. package/dist/cjs/analyze/index.js +209 -211
  6. package/dist/cjs/analyze/nestedRoutes.js +58 -15
  7. package/dist/cjs/analyze/templates.js +4 -4
  8. package/dist/cjs/builder/builder-rspack/adapterCopy.js +41 -43
  9. package/dist/cjs/builder/builder-webpack/adapterModern.js +23 -25
  10. package/dist/cjs/builder/generator/createBuilderProviderConfig.js +2 -1
  11. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +12 -15
  12. package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
  13. package/dist/cjs/commands/build.js +2 -1
  14. package/dist/cjs/commands/inspect.js +2 -1
  15. package/dist/cjs/commands/serve.js +2 -2
  16. package/dist/cjs/index.js +3 -2
  17. package/dist/cjs/initialize/index.js +70 -72
  18. package/dist/esm/analyze/generateCode.js +35 -10
  19. package/dist/esm/analyze/getFileSystemEntry.js +2 -1
  20. package/dist/esm/analyze/getServerRoutes.js +5 -4
  21. package/dist/esm/analyze/nestedRoutes.js +59 -13
  22. package/dist/esm/analyze/templates.js +4 -4
  23. package/dist/esm/builder/builder-rspack/adapterCopy.js +1 -1
  24. package/dist/esm/builder/generator/createBuilderProviderConfig.js +2 -1
  25. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +13 -16
  26. package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +41 -101
  27. package/dist/esm/commands/build.js +2 -2
  28. package/dist/esm/commands/inspect.js +2 -2
  29. package/dist/esm/commands/serve.js +2 -2
  30. package/dist/esm/index.js +3 -2
  31. package/dist/esm-node/analyze/generateCode.js +16 -7
  32. package/dist/esm-node/analyze/getFileSystemEntry.js +2 -1
  33. package/dist/esm-node/analyze/getServerRoutes.js +5 -4
  34. package/dist/esm-node/analyze/index.js +209 -211
  35. package/dist/esm-node/analyze/nestedRoutes.js +55 -15
  36. package/dist/esm-node/analyze/templates.js +4 -4
  37. package/dist/esm-node/builder/builder-rspack/adapterCopy.js +41 -43
  38. package/dist/esm-node/builder/builder-webpack/adapterModern.js +23 -25
  39. package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +2 -1
  40. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +12 -15
  41. package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
  42. package/dist/esm-node/commands/build.js +2 -1
  43. package/dist/esm-node/commands/inspect.js +2 -1
  44. package/dist/esm-node/commands/serve.js +2 -2
  45. package/dist/esm-node/index.js +3 -2
  46. package/dist/esm-node/initialize/index.js +70 -72
  47. package/dist/types/analyze/nestedRoutes.d.ts +2 -1
  48. package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +0 -8
  49. 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 walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
60
- var _route_children;
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 || routePath === void 0 ? void 0 : routePath.replace(/\$$/, "?"),
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 _route_children1;
99
- (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(childRoute);
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 _route_children2;
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
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(pageRoute);
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 _route_children3;
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
- (_route_children3 = route.children) === null || _route_children3 === void 0 ? void 0 : _route_children3.push(splatRoute);
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
- route.children = (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.filter((childRoute) => childRoute);
163
- if (route.children && route.children.length === 0 && !route.index) {
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 (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
167
- const childRoute = finalRoute.children[0];
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 || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
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 || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
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
- return {
18
- name: "builder-plugin-adapter-rspack-copy",
19
- setup(api) {
20
- let publicPath;
21
- api.modifyRspackConfig((config) => {
22
- var _config_builtins, _config_builtins_copy, _config_output;
23
- config.builtins = {
24
- ...config.builtins || {},
25
- copy: {
26
- patterns: [
27
- ...((_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) || [],
28
- ...createConfigBuiltinCopy(options)
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
- const HTML_REGEXP = /\.html?$/;
48
- const filepaths = (await _fsextra.default.readdir(publicDir)).map((file) => _path.default.resolve(publicDir, file));
49
- await Promise.all(filepaths.filter((file) => HTML_REGEXP.test(file)).map(async (file) => {
50
- const content = await _fsextra.default.readFile(file, "utf-8");
51
- if (publicPath) {
52
- await _fsextra.default.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, (0, _utils.removeTailSlash)(publicPath)));
53
- } else {
54
- _utils.logger.warn("Expect get a string from `publicPath`, but receive `undefined`.");
55
- await _fsextra.default.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, (0, _utils.removeTailSlash)("/")));
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
- return {
15
- name: "builder-plugin-adapter-modern",
16
- setup(api) {
17
- const { normalizedConfig: modernConfig, appContext } = options;
18
- api.modifyWebpackChain((chain, { CHAIN_ID }) => {
19
- chain.resolve.modules.add("node_modules").add((0, _path.join)(api.context.rootPath, "node_modules"));
20
- if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
21
- const defaultCopyPattern = (0, _createCopyPattern.createPublicPattern)(appContext, modernConfig, chain);
22
- chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
23
- var _args_;
24
- return [
25
- {
26
- patterns: [
27
- ...((_args_ = args[0]) === null || _args_ === void 0 ? void 0 : _args_.patterns) || [],
28
- defaultCopyPattern
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 || copy === void 0 ? void 0 : copy.patterns;
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 || server === void 0 ? void 0 : server.ssrByEntries) && typeof server.ssrByEntries === "object") {
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 || normalizedConfig === void 0 ? void 0 : (_normalizedConfig_runtime = normalizedConfig.runtime) === null || _normalizedConfig_runtime === void 0 ? void 0 : _normalizedConfig_runtime.router;
88
- const routerManifest = Boolean(routerConfig === null || routerConfig === void 0 ? void 0 : routerConfig.manifest);
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 _ref;
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 || outputConfig === void 0 ? void 0 : outputConfig.ssg) === true || typeof ((_ref = outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg) === null || _ref === void 0 ? void 0 : _ref[0]) === "function")) {
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 || outputConfig === void 0 ? void 0 : outputConfig.ssg)) {
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 || outputConfig === void 0 ? void 0 : outputConfig.ssg)) {
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
- if (!ssgEntries.includes(name) && (ssr && (ssrByEntries === null || ssrByEntries === void 0 ? void 0 : ssrByEntries[name]) === false || !ssr && !(ssrByEntries === null || ssrByEntries === void 0 ? void 0 : ssrByEntries[name]))) {
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 chunkToSource = /* @__PURE__ */ new Map();
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 entryChunks = this.getEntryChunks(compilation, chunks);
77
- const entryChunkFiles = entryChunks.map((chunk) => [
78
- ...chunk.files || []
79
- ].find((fname) => fname.includes(".js")));
80
- const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
81
- for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
82
- const file = entryChunkFiles[i];
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
- chunkToSource.set(chunkId, source);
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 entryChunks = this.getEntryChunks(compilation, chunks);
169
- const entryChunkFiles = this.getEntryChunkFiles(entryChunks);
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 < entryChunkFiles.length; 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
- let relatedAssets = {};
180
- if (entryChunkFiles.length > 1) {
181
- Object.keys(routeAssets).forEach((routeId) => {
182
- const segments = routeId.split("_");
183
- const chunkName = segments[0];
184
- if (chunkNames === null || chunkNames === void 0 ? void 0 : chunkNames.includes(chunkName)) {
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 = chunkToSource.get(chunkId);
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: this.minimize
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
- if (options === null || options === void 0 ? void 0 : options.analyze) {
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 || appContext === void 0 ? void 0 : appContext.builder.context.distPath, options.output),
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 || userConfig === void 0 ? void 0 : (_userConfig_source = userConfig.source) === null || _userConfig_source === void 0 ? void 0 : _userConfig_source.entriesDir, appContext.apiDirectory);
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 || options === void 0 ? void 0 : options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
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 || options === void 0 ? void 0 : options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
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
  ],