@modern-js/app-tools 2.27.1-alpha.0 → 2.28.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 (48) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/cjs/analyze/generateCode.js +2 -2
  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 +2 -2
  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 +2 -2
  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 +2 -2
  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 +2 -2
  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 +2 -2
  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/builder/shared/bundlerPlugins/RouterPlugin.d.ts +0 -8
  48. package/package.json +24 -24
@@ -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
  ],
@@ -11,81 +11,79 @@ Object.defineProperty(exports, "default", {
11
11
  const _utils = require("@modern-js/utils");
12
12
  const _schema = require("../schema");
13
13
  const _config = require("../config");
14
- const _default = ({ bundler }) => {
15
- return {
16
- name: "@modern-js/plugin-initialize",
17
- post: [
18
- "@modern-js/plugin-ssr",
19
- "@modern-js/plugin-document",
20
- "@modern-js/plugin-state",
21
- "@modern-js/plugin-router",
22
- "@modern-js/plugin-router-v5",
23
- "@modern-js/plugin-polyfill"
24
- ],
25
- setup(api) {
26
- const config = () => {
27
- const appContext = api.useAppContext();
14
+ const _default = ({ bundler }) => ({
15
+ name: "@modern-js/plugin-initialize",
16
+ post: [
17
+ "@modern-js/plugin-ssr",
18
+ "@modern-js/plugin-document",
19
+ "@modern-js/plugin-state",
20
+ "@modern-js/plugin-router",
21
+ "@modern-js/plugin-router-v5",
22
+ "@modern-js/plugin-polyfill"
23
+ ],
24
+ setup(api) {
25
+ const config = () => {
26
+ const appContext = api.useAppContext();
27
+ const userConfig = api.useConfigContext();
28
+ api.setAppContext({
29
+ ...appContext,
30
+ bundlerType: bundler
31
+ });
32
+ return (0, _config.checkIsLegacyConfig)(userConfig) ? (0, _config.createLegacyDefaultConfig)(appContext) : (0, _config.createDefaultConfig)(appContext, bundler);
33
+ };
34
+ const validateSchema = () => {
35
+ const userConfig = api.useConfigContext();
36
+ const schemas = (0, _config.checkIsLegacyConfig)(userConfig) ? _schema.legacySchema : _schema.schema;
37
+ return schemas.generate();
38
+ };
39
+ return {
40
+ config,
41
+ validateSchema,
42
+ async resolvedConfig({ resolved }) {
43
+ var _resolved_output_distPath;
44
+ let appContext = api.useAppContext();
28
45
  const userConfig = api.useConfigContext();
29
- api.setAppContext({
46
+ const port = await getServerPort(resolved);
47
+ appContext = {
30
48
  ...appContext,
31
- bundlerType: bundler
32
- });
33
- return (0, _config.checkIsLegacyConfig)(userConfig) ? (0, _config.createLegacyDefaultConfig)(appContext) : (0, _config.createDefaultConfig)(appContext, bundler);
34
- };
35
- const validateSchema = () => {
36
- const userConfig = api.useConfigContext();
37
- const schemas = (0, _config.checkIsLegacyConfig)(userConfig) ? _schema.legacySchema : _schema.schema;
38
- return schemas.generate();
39
- };
40
- return {
41
- config,
42
- validateSchema,
43
- async resolvedConfig({ resolved }) {
44
- var _resolved_output_distPath;
45
- let appContext = api.useAppContext();
46
- const userConfig = api.useConfigContext();
47
- const port = await getServerPort(resolved);
48
- appContext = {
49
- ...appContext,
50
- port,
51
- distDirectory: (0, _utils.ensureAbsolutePath)(appContext.distDirectory, ((_resolved_output_distPath = resolved.output.distPath) === null || _resolved_output_distPath === void 0 ? void 0 : _resolved_output_distPath.root) || "dist")
52
- };
53
- api.setAppContext(appContext);
54
- const normalizedConfig = (0, _config.checkIsLegacyConfig)(resolved) ? (0, _config.transformNormalizedConfig)(resolved) : resolved;
55
- resolved._raw = userConfig;
56
- resolved.server = {
57
- ...normalizedConfig.server || {},
58
- port
59
- };
60
- var _normalizedConfig_autoLoadPlugins;
61
- resolved.autoLoadPlugins = (_normalizedConfig_autoLoadPlugins = normalizedConfig.autoLoadPlugins) !== null && _normalizedConfig_autoLoadPlugins !== void 0 ? _normalizedConfig_autoLoadPlugins : false;
62
- stabilizeConfig(resolved, normalizedConfig, [
63
- "source",
64
- "bff",
65
- "dev",
66
- "html",
67
- "output",
68
- "tools",
69
- "testing",
70
- "plugins",
71
- "builderPlugins",
72
- "runtime",
73
- "runtimeByEntries",
74
- "deploy",
75
- "performance"
76
- ]);
77
- if (bundler === "webpack") {
78
- resolved.security = normalizedConfig.security || {};
79
- resolved.experiments = normalizedConfig.experiments;
80
- }
81
- return {
82
- resolved
83
- };
49
+ port,
50
+ distDirectory: (0, _utils.ensureAbsolutePath)(appContext.distDirectory, ((_resolved_output_distPath = resolved.output.distPath) === null || _resolved_output_distPath === void 0 ? void 0 : _resolved_output_distPath.root) || "dist")
51
+ };
52
+ api.setAppContext(appContext);
53
+ const normalizedConfig = (0, _config.checkIsLegacyConfig)(resolved) ? (0, _config.transformNormalizedConfig)(resolved) : resolved;
54
+ resolved._raw = userConfig;
55
+ resolved.server = {
56
+ ...normalizedConfig.server || {},
57
+ port
58
+ };
59
+ var _normalizedConfig_autoLoadPlugins;
60
+ resolved.autoLoadPlugins = (_normalizedConfig_autoLoadPlugins = normalizedConfig.autoLoadPlugins) !== null && _normalizedConfig_autoLoadPlugins !== void 0 ? _normalizedConfig_autoLoadPlugins : false;
61
+ stabilizeConfig(resolved, normalizedConfig, [
62
+ "source",
63
+ "bff",
64
+ "dev",
65
+ "html",
66
+ "output",
67
+ "tools",
68
+ "testing",
69
+ "plugins",
70
+ "builderPlugins",
71
+ "runtime",
72
+ "runtimeByEntries",
73
+ "deploy",
74
+ "performance"
75
+ ]);
76
+ if (bundler === "webpack") {
77
+ resolved.security = normalizedConfig.security || {};
78
+ resolved.experiments = normalizedConfig.experiments;
84
79
  }
85
- };
86
- }
87
- };
88
- };
80
+ return {
81
+ resolved
82
+ };
83
+ }
84
+ };
85
+ }
86
+ });
89
87
  function stabilizeConfig(resolve, config, keys) {
90
88
  keys.forEach((key) => {
91
89
  resolve[key] = config[key] || {};