@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
@@ -2,52 +2,50 @@ import path from "path";
2
2
  import fs from "@modern-js/utils/fs-extra";
3
3
  import { logger, removeTailSlash } from "@modern-js/utils";
4
4
  import { createCopyInfo } from "../shared";
5
- export const builderPluginAdpaterCopy = (options) => {
6
- return {
7
- name: "builder-plugin-adapter-rspack-copy",
8
- setup(api) {
9
- let publicPath;
10
- api.modifyRspackConfig((config) => {
11
- var _config_builtins, _config_builtins_copy, _config_output;
12
- config.builtins = {
13
- ...config.builtins || {},
14
- copy: {
15
- patterns: [
16
- ...((_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) || [],
17
- ...createConfigBuiltinCopy(options)
18
- ]
19
- }
20
- };
21
- publicPath = (_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath;
22
- });
23
- api.onDevCompileDone(async () => {
24
- await transformHtmlFiles();
25
- });
26
- api.onAfterBuild(async () => {
27
- await transformHtmlFiles();
28
- });
29
- async function transformHtmlFiles() {
30
- var _normalizedConfig_output_distPath;
31
- const { normalizedConfig } = options;
32
- const publicDir = path.resolve(((_normalizedConfig_output_distPath = normalizedConfig.output.distPath) === null || _normalizedConfig_output_distPath === void 0 ? void 0 : _normalizedConfig_output_distPath.root) || "./dist", "./public");
33
- if (!fs.existsSync(publicDir) || !fs.statSync(publicDir).isDirectory()) {
34
- return;
5
+ export const builderPluginAdpaterCopy = (options) => ({
6
+ name: "builder-plugin-adapter-rspack-copy",
7
+ setup(api) {
8
+ let publicPath;
9
+ api.modifyRspackConfig((config) => {
10
+ var _config_builtins_copy, _config_builtins, _config_output;
11
+ config.builtins = {
12
+ ...config.builtins || {},
13
+ copy: {
14
+ patterns: [
15
+ ...((_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) || [],
16
+ ...createConfigBuiltinCopy(options)
17
+ ]
35
18
  }
36
- const HTML_REGEXP = /\.html?$/;
37
- const filepaths = (await fs.readdir(publicDir)).map((file) => path.resolve(publicDir, file));
38
- await Promise.all(filepaths.filter((file) => HTML_REGEXP.test(file)).map(async (file) => {
39
- const content = await fs.readFile(file, "utf-8");
40
- if (publicPath) {
41
- await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash(publicPath)));
42
- } else {
43
- logger.warn("Expect get a string from `publicPath`, but receive `undefined`.");
44
- await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash("/")));
45
- }
46
- }));
19
+ };
20
+ publicPath = (_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath;
21
+ });
22
+ api.onDevCompileDone(async () => {
23
+ await transformHtmlFiles();
24
+ });
25
+ api.onAfterBuild(async () => {
26
+ await transformHtmlFiles();
27
+ });
28
+ async function transformHtmlFiles() {
29
+ var _normalizedConfig_output_distPath;
30
+ const { normalizedConfig } = options;
31
+ const publicDir = path.resolve(((_normalizedConfig_output_distPath = normalizedConfig.output.distPath) === null || _normalizedConfig_output_distPath === void 0 ? void 0 : _normalizedConfig_output_distPath.root) || "./dist", "./public");
32
+ if (!fs.existsSync(publicDir) || !fs.statSync(publicDir).isDirectory()) {
33
+ return;
47
34
  }
35
+ const HTML_REGEXP = /\.html?$/;
36
+ const filepaths = (await fs.readdir(publicDir)).map((file) => path.resolve(publicDir, file));
37
+ await Promise.all(filepaths.filter((file) => HTML_REGEXP.test(file)).map(async (file) => {
38
+ const content = await fs.readFile(file, "utf-8");
39
+ if (publicPath) {
40
+ await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash(publicPath)));
41
+ } else {
42
+ logger.warn("Expect get a string from `publicPath`, but receive `undefined`.");
43
+ await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash("/")));
44
+ }
45
+ }));
48
46
  }
49
- };
50
- };
47
+ }
48
+ });
51
49
  function createConfigBuiltinCopy(options) {
52
50
  const { normalizedConfig, appContext } = options;
53
51
  const { publicDir } = createCopyInfo(appContext, normalizedConfig);
@@ -1,27 +1,25 @@
1
1
  import { join } from "path";
2
2
  import { createPublicPattern } from "./createCopyPattern";
3
- export const builderPluginAdapterModern = (options) => {
4
- return {
5
- name: "builder-plugin-adapter-modern",
6
- setup(api) {
7
- const { normalizedConfig: modernConfig, appContext } = options;
8
- api.modifyWebpackChain((chain, { CHAIN_ID }) => {
9
- chain.resolve.modules.add("node_modules").add(join(api.context.rootPath, "node_modules"));
10
- if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
11
- const defaultCopyPattern = createPublicPattern(appContext, modernConfig, chain);
12
- chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
13
- var _args_;
14
- return [
15
- {
16
- patterns: [
17
- ...((_args_ = args[0]) === null || _args_ === void 0 ? void 0 : _args_.patterns) || [],
18
- defaultCopyPattern
19
- ]
20
- }
21
- ];
22
- });
23
- }
24
- });
25
- }
26
- };
27
- };
3
+ export const builderPluginAdapterModern = (options) => ({
4
+ name: "builder-plugin-adapter-modern",
5
+ setup(api) {
6
+ const { normalizedConfig: modernConfig, appContext } = options;
7
+ api.modifyWebpackChain((chain, { CHAIN_ID }) => {
8
+ chain.resolve.modules.add("node_modules").add(join(api.context.rootPath, "node_modules"));
9
+ if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
10
+ const defaultCopyPattern = createPublicPattern(appContext, modernConfig, chain);
11
+ chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
12
+ var _args_;
13
+ return [
14
+ {
15
+ patterns: [
16
+ ...((_args_ = args[0]) === null || _args_ === void 0 ? void 0 : _args_.patterns) || [],
17
+ defaultCopyPattern
18
+ ]
19
+ }
20
+ ];
21
+ });
22
+ }
23
+ });
24
+ }
25
+ });
@@ -1,8 +1,9 @@
1
1
  import { createUploadPattern } from "../builder-webpack/createCopyPattern";
2
2
  function modifyOutputConfig(config, appContext) {
3
+ var _copy;
3
4
  const defaultCopyPattern = createUploadPattern(appContext, config);
4
5
  const { copy } = config.output;
5
- const copyOptions = Array.isArray(copy) ? copy : copy === null || copy === void 0 ? void 0 : copy.patterns;
6
+ const copyOptions = Array.isArray(copy) ? copy : (_copy = copy) === null || _copy === void 0 ? void 0 : _copy.patterns;
6
7
  const builderCopy = [
7
8
  ...copyOptions || [],
8
9
  defaultCopyPattern
@@ -47,12 +47,13 @@ export const builderPluginAdapterSSR = (options) => ({
47
47
  }
48
48
  });
49
49
  const isStreamingSSR = (userConfig) => {
50
+ var _server;
50
51
  const isStreaming = (ssr) => ssr && typeof ssr === "object" && ssr.mode === "stream";
51
52
  const { server } = userConfig;
52
53
  if (isStreaming(server.ssr)) {
53
54
  return true;
54
55
  }
55
- if ((server === null || server === void 0 ? void 0 : server.ssrByEntries) && typeof server.ssrByEntries === "object") {
56
+ if (((_server = server) === null || _server === void 0 ? void 0 : _server.ssrByEntries) && typeof server.ssrByEntries === "object") {
56
57
  for (const name of Object.keys(server.ssrByEntries)) {
57
58
  if (isStreaming(server.ssrByEntries[name])) {
58
59
  return true;
@@ -69,38 +70,33 @@ function applyAsyncChunkHtmlPlugin({ chain, modernConfig, CHAIN_ID, HtmlBundlerP
69
70
  }
70
71
  }
71
72
  function applyRouterPlugin(chain, options) {
72
- var _normalizedConfig_runtime, _normalizedConfig_deploy_worker;
73
+ var _normalizedConfig_runtime, _normalizedConfig, _routerConfig, _normalizedConfig_deploy_worker;
73
74
  const { appContext, normalizedConfig } = options;
74
75
  const { entrypoints } = appContext;
75
76
  const existNestedRoutes = entrypoints.some((entrypoint) => entrypoint.nestedRoutesEntry);
76
- const routerConfig = normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig_runtime = normalizedConfig.runtime) === null || _normalizedConfig_runtime === void 0 ? void 0 : _normalizedConfig_runtime.router;
77
- const routerManifest = Boolean(routerConfig === null || routerConfig === void 0 ? void 0 : routerConfig.manifest);
77
+ const routerConfig = (_normalizedConfig = normalizedConfig) === null || _normalizedConfig === void 0 ? void 0 : (_normalizedConfig_runtime = _normalizedConfig.runtime) === null || _normalizedConfig_runtime === void 0 ? void 0 : _normalizedConfig_runtime.router;
78
+ const routerManifest = Boolean((_routerConfig = routerConfig) === null || _routerConfig === void 0 ? void 0 : _routerConfig.manifest);
78
79
  const workerSSR = Boolean((_normalizedConfig_deploy_worker = normalizedConfig.deploy.worker) === null || _normalizedConfig_deploy_worker === void 0 ? void 0 : _normalizedConfig_deploy_worker.ssr);
79
- const minimize = !normalizedConfig.output.disableMinimize && process.env.NODE_ENV === "production";
80
80
  if (existNestedRoutes || routerManifest || workerSSR) {
81
- chain.plugin("route-plugin").use(RouterPlugin, [
82
- {
83
- minimize
84
- }
85
- ]);
81
+ chain.plugin("route-plugin").use(RouterPlugin);
86
82
  }
87
83
  }
88
84
  function applyFilterEntriesBySSRConfig({ isProd, chain, appNormalizedConfig }) {
89
- var _ref;
85
+ var _outputConfig, _this, _outputConfig1, _outputConfig2, _outputConfig3;
90
86
  const { server: serverConfig, output: outputConfig } = appNormalizedConfig;
91
87
  const entries = chain.entryPoints.entries();
92
- 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")) {
88
+ 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")) {
93
89
  return;
94
90
  }
95
91
  if (typeof entries === "undefined") {
96
92
  throw new Error("No entry found, one of src/routes/layout.tsx, src/App.tsx, src/index.tsx is required");
97
93
  }
98
94
  const entryNames = Object.keys(entries);
99
- if (isProd && entryNames.length === 1 && (outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg)) {
95
+ if (isProd && entryNames.length === 1 && ((_outputConfig2 = outputConfig) === null || _outputConfig2 === void 0 ? void 0 : _outputConfig2.ssg)) {
100
96
  return;
101
97
  }
102
98
  const ssgEntries = [];
103
- if (isProd && (outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg)) {
99
+ if (isProd && ((_outputConfig3 = outputConfig) === null || _outputConfig3 === void 0 ? void 0 : _outputConfig3.ssg)) {
104
100
  const { ssg } = outputConfig;
105
101
  entryNames.forEach((name) => {
106
102
  if (ssg[name]) {
@@ -110,7 +106,8 @@ function applyFilterEntriesBySSRConfig({ isProd, chain, appNormalizedConfig }) {
110
106
  }
111
107
  const { ssr, ssrByEntries } = serverConfig || {};
112
108
  entryNames.forEach((name) => {
113
- 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]))) {
109
+ var _ssrByEntries, _ssrByEntries1;
110
+ 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]))) {
114
111
  chain.entryPoints.delete(name);
115
112
  }
116
113
  });
@@ -1,4 +1,3 @@
1
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
2
1
  import path from "path";
3
2
  import { mergeWith } from "@modern-js/utils/lodash";
4
3
  import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
@@ -15,22 +14,6 @@ export class RouterPlugin {
15
14
  }
16
15
  return false;
17
16
  }
18
- getEntryChunks(compilation, chunks) {
19
- const entrypointsArray = Array.from(compilation.entrypoints.entries());
20
- const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
21
- const entryChunks = [
22
- ...chunks
23
- ].filter((chunk) => {
24
- var _chunk_names;
25
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
26
- });
27
- return entryChunks;
28
- }
29
- getEntryChunkFiles(entryChunks) {
30
- return entryChunks.map((chunk) => [
31
- ...chunk.files || []
32
- ].find((fname) => fname.includes(".js")));
33
- }
34
17
  apply(compiler) {
35
18
  const { target } = compiler.options;
36
19
  if (this.isTargetNodeOrWebWorker(target)) {
@@ -45,8 +28,7 @@ export class RouterPlugin {
45
28
  }
46
29
  return path2;
47
30
  };
48
- const chunkToSource = /* @__PURE__ */ new Map();
49
- const chunkToMap = /* @__PURE__ */ new Map();
31
+ const chunkToSourceAndMap = /* @__PURE__ */ new Map();
50
32
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
51
33
  compilation.hooks.processAssets.tapPromise({
52
34
  name: PLUGIN_NAME,
@@ -62,37 +44,14 @@ export class RouterPlugin {
62
44
  if (!namedChunkGroups) {
63
45
  return;
64
46
  }
65
- const entryChunks = this.getEntryChunks(compilation, chunks);
66
- const entryChunkFiles = entryChunks.map((chunk) => [
67
- ...chunk.files || []
68
- ].find((fname) => fname.includes(".js")));
69
- const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
70
- for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
71
- const file = entryChunkFiles[i];
72
- const chunkId = entryChunkFileIds[i];
73
- const asset = compilation.assets[file];
74
- if (!asset) {
75
- continue;
76
- }
77
- const { map } = asset.sourceAndMap();
78
- chunkToMap.set(chunkId, map);
79
- }
80
- });
81
- compilation.hooks.processAssets.tapPromise({
82
- name: PLUGIN_NAME,
83
- stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
84
- }, async () => {
85
- const stats = compilation.getStats().toJson({
86
- all: false,
87
- chunkGroups: true,
88
- chunks: true,
89
- ids: true
47
+ const entrypointsArray = Array.from(compilation.entrypoints.entries());
48
+ const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
49
+ const entryChunks = [
50
+ ...chunks
51
+ ].filter((chunk) => {
52
+ var _chunk_names;
53
+ return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
90
54
  });
91
- const { chunks = [], namedChunkGroups } = stats;
92
- if (!namedChunkGroups) {
93
- return;
94
- }
95
- const entryChunks = this.getEntryChunks(compilation, chunks);
96
55
  const entryChunkFiles = entryChunks.map((chunk) => [
97
56
  ...chunk.files || []
98
57
  ].find((fname) => fname.includes(".js")));
@@ -104,8 +63,11 @@ export class RouterPlugin {
104
63
  if (!asset) {
105
64
  continue;
106
65
  }
107
- const { source } = asset.sourceAndMap();
108
- chunkToSource.set(chunkId, source);
66
+ const { source, map } = asset.sourceAndMap();
67
+ chunkToSourceAndMap.set(chunkId, {
68
+ source,
69
+ map
70
+ });
109
71
  }
110
72
  });
111
73
  compilation.hooks.processAssets.tapPromise({
@@ -138,7 +100,6 @@ export class RouterPlugin {
138
100
  const referenceCssAssets = assets.filter((asset) => /\.css$/.test(asset));
139
101
  routeAssets[name] = {
140
102
  chunkIds: chunkGroup.chunks,
141
- chunkName: chunkGroup.name,
142
103
  assets,
143
104
  referenceCssAssets
144
105
  };
@@ -154,29 +115,31 @@ export class RouterPlugin {
154
115
  const manifest = {
155
116
  routeAssets
156
117
  };
157
- const entryChunks = this.getEntryChunks(compilation, chunks);
158
- const entryChunkFiles = this.getEntryChunkFiles(entryChunks);
118
+ const entrypointsArray = Array.from(compilation.entrypoints.entries());
119
+ const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
120
+ const entryChunks = [
121
+ ...chunks
122
+ ].filter((chunk) => {
123
+ var _chunk_names;
124
+ return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
125
+ });
126
+ const entryChunkFiles = entryChunks.map((chunk) => [
127
+ ...chunk.files || []
128
+ ].find((fname) => fname.includes(".js")));
159
129
  const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
160
- for (let i = 0; i < entryChunkFiles.length; i++) {
130
+ for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
161
131
  const file = entryChunkFiles[i];
162
- const chunkNames = entryChunks[i].names;
163
132
  const chunkId = entryChunkFileIds[i];
164
133
  const asset = compilation.assets[file];
165
134
  if (!asset || !chunkId) {
166
135
  continue;
167
136
  }
168
- let relatedAssets = {};
169
- if (entryChunkFiles.length > 1) {
170
- Object.keys(routeAssets).forEach((routeId) => {
171
- const segments = routeId.split("_");
172
- const chunkName = segments[0];
173
- if (chunkNames === null || chunkNames === void 0 ? void 0 : chunkNames.includes(chunkName)) {
174
- relatedAssets[routeId] = routeAssets[routeId];
175
- }
176
- });
177
- } else {
178
- relatedAssets = routeAssets;
179
- }
137
+ const relatedAssets = {};
138
+ Object.keys(routeAssets).forEach((routeId) => {
139
+ if (routeId.startsWith(`${chunkId}`)) {
140
+ relatedAssets[routeId] = routeAssets[routeId];
141
+ }
142
+ });
180
143
  const manifest2 = {
181
144
  routeAssets: relatedAssets
182
145
  };
@@ -192,13 +155,12 @@ export class RouterPlugin {
192
155
  })};
193
156
  })();
194
157
  `;
195
- const source = chunkToSource.get(chunkId);
196
- const map = chunkToMap.get(chunkId);
158
+ const { source, map } = chunkToSourceAndMap.get(chunkId);
197
159
  const newContent = `${injectedContent}${source.toString()}`;
198
160
  const result = await transform(newContent, {
199
161
  loader: path.extname(file).slice(1),
200
162
  sourcemap: true,
201
- minify: this.minimize
163
+ minify: process.env.NODE_ENV === "production"
202
164
  });
203
165
  const newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
204
166
  compilation.updateAsset(
@@ -221,8 +183,4 @@ export class RouterPlugin {
221
183
  });
222
184
  });
223
185
  }
224
- constructor(options) {
225
- _define_property(this, "minimize", false);
226
- this.minimize = options.minimize;
227
- }
228
186
  }
@@ -3,7 +3,8 @@ import { logger } from "@modern-js/utils";
3
3
  import { generateRoutes } from "../utils/routes";
4
4
  import { buildServerConfig } from "../utils/config";
5
5
  export const build = async (api, options) => {
6
- if (options === null || options === void 0 ? void 0 : options.analyze) {
6
+ var _options;
7
+ if ((_options = options) === null || _options === void 0 ? void 0 : _options.analyze) {
7
8
  process.env.BUNDLE_ANALYZE = "true";
8
9
  }
9
10
  let resolvedConfig = api.useResolvedConfigContext();
@@ -1,5 +1,6 @@
1
1
  import { join } from "path";
2
2
  export const inspect = async (api, options) => {
3
+ var _appContext;
3
4
  const appContext = api.useAppContext();
4
5
  if (!appContext.builder) {
5
6
  throw new Error("Expect the Builder to have been initialized, But the appContext.builder received `undefined`");
@@ -7,7 +8,7 @@ export const inspect = async (api, options) => {
7
8
  return appContext.builder.inspectConfig({
8
9
  env: options.env,
9
10
  verbose: options.verbose,
10
- outputPath: join(appContext === null || appContext === void 0 ? void 0 : appContext.builder.context.distPath, options.output),
11
+ outputPath: join((_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.builder.context.distPath, options.output),
11
12
  writeToDisk: true
12
13
  });
13
14
  };
@@ -4,13 +4,13 @@ import { printInstructions } from "../utils/printInstructions";
4
4
  import { injectDataLoaderPlugin } from "../utils/createServer";
5
5
  import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
6
6
  export const start = async (api) => {
7
- var _userConfig_source, _userConfig_output_distPath;
7
+ var _userConfig_source, _userConfig, _userConfig_output_distPath;
8
8
  const appContext = api.useAppContext();
9
9
  const userConfig = api.useResolvedConfigContext();
10
10
  const hookRunners = api.useHookRunners();
11
11
  const { appDirectory, port, serverConfigFile, metaName } = appContext;
12
12
  logger.log(chalk.cyan(`Starting the modern server...`));
13
- const apiOnly = await 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);
13
+ const apiOnly = await 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);
14
14
  const serverInternalPlugins = await getServerInternalPlugins(api);
15
15
  const app = await server({
16
16
  pwd: appDirectory,
@@ -60,6 +60,7 @@ export const buildCommand = async (program, api) => {
60
60
  export const appTools = (options = {
61
61
  bundler: "webpack"
62
62
  }) => {
63
+ var _options, _options1;
63
64
  return {
64
65
  name: "@modern-js/app-tools",
65
66
  post: [
@@ -75,10 +76,10 @@ export const appTools = (options = {
75
76
  registerHook: hooks,
76
77
  usePlugins: [
77
78
  initializePlugin({
78
- bundler: (options === null || options === void 0 ? void 0 : options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
79
+ bundler: ((_options = options) === null || _options === void 0 ? void 0 : _options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
79
80
  }),
80
81
  analyzePlugin({
81
- bundler: (options === null || options === void 0 ? void 0 : options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
82
+ bundler: ((_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.bundler) === "experimental-rspack" ? "rspack" : "webpack"
82
83
  }),
83
84
  lintPlugin()
84
85
  ],
@@ -1,81 +1,79 @@
1
1
  import { ensureAbsolutePath, getPort, isDev, isDevCommand } from "@modern-js/utils";
2
2
  import { legacySchema, schema } from "../schema";
3
3
  import { checkIsLegacyConfig, createDefaultConfig, createLegacyDefaultConfig, transformNormalizedConfig } from "../config";
4
- export default ({ bundler }) => {
5
- return {
6
- name: "@modern-js/plugin-initialize",
7
- post: [
8
- "@modern-js/plugin-ssr",
9
- "@modern-js/plugin-document",
10
- "@modern-js/plugin-state",
11
- "@modern-js/plugin-router",
12
- "@modern-js/plugin-router-v5",
13
- "@modern-js/plugin-polyfill"
14
- ],
15
- setup(api) {
16
- const config = () => {
17
- const appContext = api.useAppContext();
4
+ export default ({ bundler }) => ({
5
+ name: "@modern-js/plugin-initialize",
6
+ post: [
7
+ "@modern-js/plugin-ssr",
8
+ "@modern-js/plugin-document",
9
+ "@modern-js/plugin-state",
10
+ "@modern-js/plugin-router",
11
+ "@modern-js/plugin-router-v5",
12
+ "@modern-js/plugin-polyfill"
13
+ ],
14
+ setup(api) {
15
+ const config = () => {
16
+ const appContext = api.useAppContext();
17
+ const userConfig = api.useConfigContext();
18
+ api.setAppContext({
19
+ ...appContext,
20
+ bundlerType: bundler
21
+ });
22
+ return checkIsLegacyConfig(userConfig) ? createLegacyDefaultConfig(appContext) : createDefaultConfig(appContext, bundler);
23
+ };
24
+ const validateSchema = () => {
25
+ const userConfig = api.useConfigContext();
26
+ const schemas = checkIsLegacyConfig(userConfig) ? legacySchema : schema;
27
+ return schemas.generate();
28
+ };
29
+ return {
30
+ config,
31
+ validateSchema,
32
+ async resolvedConfig({ resolved }) {
33
+ var _resolved_output_distPath;
34
+ let appContext = api.useAppContext();
18
35
  const userConfig = api.useConfigContext();
19
- api.setAppContext({
36
+ const port = await getServerPort(resolved);
37
+ appContext = {
20
38
  ...appContext,
21
- bundlerType: bundler
22
- });
23
- return checkIsLegacyConfig(userConfig) ? createLegacyDefaultConfig(appContext) : createDefaultConfig(appContext, bundler);
24
- };
25
- const validateSchema = () => {
26
- const userConfig = api.useConfigContext();
27
- const schemas = checkIsLegacyConfig(userConfig) ? legacySchema : schema;
28
- return schemas.generate();
29
- };
30
- return {
31
- config,
32
- validateSchema,
33
- async resolvedConfig({ resolved }) {
34
- var _resolved_output_distPath;
35
- let appContext = api.useAppContext();
36
- const userConfig = api.useConfigContext();
37
- const port = await getServerPort(resolved);
38
- appContext = {
39
- ...appContext,
40
- port,
41
- distDirectory: ensureAbsolutePath(appContext.distDirectory, ((_resolved_output_distPath = resolved.output.distPath) === null || _resolved_output_distPath === void 0 ? void 0 : _resolved_output_distPath.root) || "dist")
42
- };
43
- api.setAppContext(appContext);
44
- const normalizedConfig = checkIsLegacyConfig(resolved) ? transformNormalizedConfig(resolved) : resolved;
45
- resolved._raw = userConfig;
46
- resolved.server = {
47
- ...normalizedConfig.server || {},
48
- port
49
- };
50
- var _normalizedConfig_autoLoadPlugins;
51
- resolved.autoLoadPlugins = (_normalizedConfig_autoLoadPlugins = normalizedConfig.autoLoadPlugins) !== null && _normalizedConfig_autoLoadPlugins !== void 0 ? _normalizedConfig_autoLoadPlugins : false;
52
- stabilizeConfig(resolved, normalizedConfig, [
53
- "source",
54
- "bff",
55
- "dev",
56
- "html",
57
- "output",
58
- "tools",
59
- "testing",
60
- "plugins",
61
- "builderPlugins",
62
- "runtime",
63
- "runtimeByEntries",
64
- "deploy",
65
- "performance"
66
- ]);
67
- if (bundler === "webpack") {
68
- resolved.security = normalizedConfig.security || {};
69
- resolved.experiments = normalizedConfig.experiments;
70
- }
71
- return {
72
- resolved
73
- };
39
+ port,
40
+ distDirectory: ensureAbsolutePath(appContext.distDirectory, ((_resolved_output_distPath = resolved.output.distPath) === null || _resolved_output_distPath === void 0 ? void 0 : _resolved_output_distPath.root) || "dist")
41
+ };
42
+ api.setAppContext(appContext);
43
+ const normalizedConfig = checkIsLegacyConfig(resolved) ? transformNormalizedConfig(resolved) : resolved;
44
+ resolved._raw = userConfig;
45
+ resolved.server = {
46
+ ...normalizedConfig.server || {},
47
+ port
48
+ };
49
+ var _normalizedConfig_autoLoadPlugins;
50
+ resolved.autoLoadPlugins = (_normalizedConfig_autoLoadPlugins = normalizedConfig.autoLoadPlugins) !== null && _normalizedConfig_autoLoadPlugins !== void 0 ? _normalizedConfig_autoLoadPlugins : false;
51
+ stabilizeConfig(resolved, normalizedConfig, [
52
+ "source",
53
+ "bff",
54
+ "dev",
55
+ "html",
56
+ "output",
57
+ "tools",
58
+ "testing",
59
+ "plugins",
60
+ "builderPlugins",
61
+ "runtime",
62
+ "runtimeByEntries",
63
+ "deploy",
64
+ "performance"
65
+ ]);
66
+ if (bundler === "webpack") {
67
+ resolved.security = normalizedConfig.security || {};
68
+ resolved.experiments = normalizedConfig.experiments;
74
69
  }
75
- };
76
- }
77
- };
78
- };
70
+ return {
71
+ resolved
72
+ };
73
+ }
74
+ };
75
+ }
76
+ });
79
77
  function stabilizeConfig(resolve, config, keys) {
80
78
  keys.forEach((key) => {
81
79
  resolve[key] = config[key] || {};
@@ -1,6 +1,7 @@
1
1
  import type { NestedRouteForCli } from '@modern-js/types';
2
2
  export declare const getRouteId: (componentPath: string, routesDir: string, entryName: string, isMainEntry: boolean) => string;
3
+ export declare const optimizeRoute: (routeTree: NestedRouteForCli) => NestedRouteForCli[];
3
4
  export declare const walk: (dirname: string, rootDir: string, alias: {
4
5
  name: string;
5
6
  basename: string;
6
- }, entryName: string, isMainEntry: boolean) => Promise<NestedRouteForCli | null>;
7
+ }, entryName: string, isMainEntry: boolean, oldVersion: boolean) => Promise<NestedRouteForCli | NestedRouteForCli[] | null>;