@modern-js/app-tools 2.26.1-alpha.0 → 2.27.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
  # @modern-js/app-tools
2
2
 
3
+ ## 2.27.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 8f5c6d6: fix: use PROCESS_ASSETS_STAGE_DEV_TOOLING instead of PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY
8
+ fix: 使用 PROCESS_ASSETS_STAGE_DEV_TOOLING 替代 PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY
9
+ - 5129771: fix: Optimize source-map generation in the router-plugin
10
+ fix: 优化 router plugin 中的 source-map 生成
11
+ - e5e6c26: perf: Optimize route manifest size for multiple entries
12
+ perf: 优化路由 manifest 在多 entry 场景下的体积
13
+ - Updated dependencies [645e111]
14
+ - Updated dependencies [91d14b8]
15
+ - Updated dependencies [8322a51]
16
+ - Updated dependencies [5376a22]
17
+ - Updated dependencies [d9080ed]
18
+ - Updated dependencies [67d0b0c]
19
+ - Updated dependencies [3f79dd7]
20
+ - Updated dependencies [6d7104d]
21
+ - @modern-js/builder-webpack-provider@2.27.0
22
+ - @modern-js/builder-rspack-provider@2.27.0
23
+ - @modern-js/builder-shared@2.27.0
24
+ - @modern-js/utils@2.27.0
25
+ - @modern-js/builder-plugin-node-polyfill@2.27.0
26
+ - @modern-js/builder-plugin-esbuild@2.27.0
27
+ - @modern-js/new-action@2.27.0
28
+ - @modern-js/builder@2.27.0
29
+ - @modern-js/plugin-i18n@2.27.0
30
+ - @modern-js/plugin-lint@2.27.0
31
+ - @modern-js/core@2.27.0
32
+ - @modern-js/plugin-data-loader@2.27.0
33
+ - @modern-js/server-core@2.27.0
34
+ - @modern-js/prod-server@2.27.0
35
+ - @modern-js/server@2.27.0
36
+ - @modern-js/node-bundle-require@2.27.0
37
+ - @modern-js/plugin@2.27.0
38
+ - @modern-js/upgrade@2.27.0
39
+ - @modern-js/types@2.27.0
40
+
3
41
  ## 2.26.0
4
42
 
5
43
  ### Patch Changes
@@ -184,7 +184,8 @@ const checkIsBuildCommands = () => {
184
184
  "start",
185
185
  "build",
186
186
  "inspect",
187
- "deploy"
187
+ "deploy",
188
+ "dev-worker"
188
189
  ];
189
190
  const command = (0, _utils.getCommand)();
190
191
  return buildCommands.includes(command);
@@ -87,8 +87,13 @@ function applyRouterPlugin(chain, options) {
87
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
88
  const routerManifest = Boolean(routerConfig === null || routerConfig === void 0 ? void 0 : routerConfig.manifest);
89
89
  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";
90
91
  if (existNestedRoutes || routerManifest || workerSSR) {
91
- chain.plugin("route-plugin").use(_bundlerPlugins.RouterPlugin);
92
+ chain.plugin("route-plugin").use(_bundlerPlugins.RouterPlugin, [
93
+ {
94
+ minimize
95
+ }
96
+ ]);
92
97
  }
93
98
  }
94
99
  function applyFilterEntriesBySSRConfig({ isProd, chain, appNormalizedConfig }) {
@@ -8,9 +8,13 @@ Object.defineProperty(exports, "RouterPlugin", {
8
8
  return RouterPlugin;
9
9
  }
10
10
  });
11
+ const _define_property = require("@swc/helpers/_/_define_property");
12
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
13
+ const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
11
14
  const _lodash = require("@modern-js/utils/lodash");
12
15
  const _utils = require("@modern-js/utils");
13
16
  const _constants = require("@modern-js/utils/universal/constants");
17
+ const _esbuild = require("esbuild");
14
18
  const PLUGIN_NAME = "ModernjsRoutePlugin";
15
19
  class RouterPlugin {
16
20
  isTargetNodeOrWebWorker(target) {
@@ -22,6 +26,22 @@ class RouterPlugin {
22
26
  }
23
27
  return false;
24
28
  }
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
+ }
25
45
  apply(compiler) {
26
46
  const { target } = compiler.options;
27
47
  if (this.isTargetNodeOrWebWorker(target)) {
@@ -36,7 +56,8 @@ class RouterPlugin {
36
56
  }
37
57
  return path;
38
58
  };
39
- const chunkToSourceMap = /* @__PURE__ */ new Map();
59
+ const chunkToSource = /* @__PURE__ */ new Map();
60
+ const chunkToMap = /* @__PURE__ */ new Map();
40
61
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
41
62
  compilation.hooks.processAssets.tapPromise({
42
63
  name: PLUGIN_NAME,
@@ -45,20 +66,14 @@ class RouterPlugin {
45
66
  const stats = compilation.getStats().toJson({
46
67
  all: false,
47
68
  chunkGroups: true,
48
- chunks: true
69
+ chunks: true,
70
+ ids: true
49
71
  });
50
72
  const { chunks = [], namedChunkGroups } = stats;
51
73
  if (!namedChunkGroups) {
52
74
  return;
53
75
  }
54
- const entrypointsArray = Array.from(compilation.entrypoints.entries());
55
- const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
56
- const entryChunks = [
57
- ...chunks
58
- ].filter((chunk) => {
59
- var _chunk_names;
60
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
61
- });
76
+ const entryChunks = this.getEntryChunks(compilation, chunks);
62
77
  const entryChunkFiles = entryChunks.map((chunk) => [
63
78
  ...chunk.files || []
64
79
  ].find((fname) => fname.includes(".js")));
@@ -71,7 +86,37 @@ class RouterPlugin {
71
86
  continue;
72
87
  }
73
88
  const { map } = asset.sourceAndMap();
74
- chunkToSourceMap.set(chunkId, map);
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
101
+ });
102
+ const { chunks = [], namedChunkGroups } = stats;
103
+ if (!namedChunkGroups) {
104
+ return;
105
+ }
106
+ const entryChunks = this.getEntryChunks(compilation, chunks);
107
+ const entryChunkFiles = entryChunks.map((chunk) => [
108
+ ...chunk.files || []
109
+ ].find((fname) => fname.includes(".js")));
110
+ const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
111
+ for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
112
+ const file = entryChunkFiles[i];
113
+ const chunkId = entryChunkFileIds[i];
114
+ const asset = compilation.assets[file];
115
+ if (!asset) {
116
+ continue;
117
+ }
118
+ const { source } = asset.sourceAndMap();
119
+ chunkToSource.set(chunkId, source);
75
120
  }
76
121
  });
77
122
  compilation.hooks.processAssets.tapPromise({
@@ -104,6 +149,7 @@ class RouterPlugin {
104
149
  const referenceCssAssets = assets.filter((asset) => /\.css$/.test(asset));
105
150
  routeAssets[name] = {
106
151
  chunkIds: chunkGroup.chunks,
152
+ chunkName: chunkGroup.name,
107
153
  assets,
108
154
  referenceCssAssets
109
155
  };
@@ -119,31 +165,29 @@ class RouterPlugin {
119
165
  const manifest = {
120
166
  routeAssets
121
167
  };
122
- const entrypointsArray = Array.from(compilation.entrypoints.entries());
123
- const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
124
- const entryChunks = [
125
- ...chunks
126
- ].filter((chunk) => {
127
- var _chunk_names;
128
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
129
- });
130
- const entryChunkFiles = entryChunks.map((chunk) => [
131
- ...chunk.files || []
132
- ].find((fname) => fname.includes(".js")));
168
+ const entryChunks = this.getEntryChunks(compilation, chunks);
169
+ const entryChunkFiles = this.getEntryChunkFiles(entryChunks);
133
170
  const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
134
- for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
171
+ for (let i = 0; i < entryChunkFiles.length; i++) {
135
172
  const file = entryChunkFiles[i];
173
+ const chunkNames = entryChunks[i].names;
136
174
  const chunkId = entryChunkFileIds[i];
137
175
  const asset = compilation.assets[file];
138
- if (!asset) {
176
+ if (!asset || !chunkId) {
139
177
  continue;
140
178
  }
141
- const relatedAssets = {};
142
- Object.keys(routeAssets).forEach((routeId) => {
143
- if (routeId.startsWith(`${chunkId}_`)) {
144
- relatedAssets[routeId] = routeAssets[routeId];
145
- }
146
- });
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
+ }
147
191
  const manifest2 = {
148
192
  routeAssets: relatedAssets
149
193
  };
@@ -159,10 +203,15 @@ class RouterPlugin {
159
203
  })};
160
204
  })();
161
205
  `;
162
- const { source } = asset.sourceAndMap();
163
- const map = chunkToSourceMap.get(chunkId);
206
+ const source = chunkToSource.get(chunkId);
207
+ const map = chunkToMap.get(chunkId);
164
208
  const newContent = `${injectedContent}${source.toString()}`;
165
- const newSource = new SourceMapSource(newContent, file, map, source.toString(), map);
209
+ const result = await (0, _esbuild.transform)(newContent, {
210
+ loader: _path.default.extname(file).slice(1),
211
+ sourcemap: true,
212
+ minify: this.minimize
213
+ });
214
+ const newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
166
215
  compilation.updateAsset(
167
216
  file,
168
217
  newSource,
@@ -183,4 +232,8 @@ class RouterPlugin {
183
232
  });
184
233
  });
185
234
  }
235
+ constructor(options) {
236
+ _define_property._(this, "minimize", false);
237
+ this.minimize = options.minimize;
238
+ }
186
239
  }
@@ -15,7 +15,7 @@ const _createServer = require("../utils/createServer");
15
15
  const _routes = require("../utils/routes");
16
16
  const _config = require("../utils/config");
17
17
  const _getServerInternalPlugins = require("../utils/getServerInternalPlugins");
18
- const dev = async (api, options) => {
18
+ const dev = async (api, options, devServerOptions = {}) => {
19
19
  var _normalizedConfig_tools;
20
20
  if (options.analyze) {
21
21
  process.env.BUNDLE_ANALYZE = "true";
@@ -58,7 +58,8 @@ const dev = async (api, options) => {
58
58
  pwd: appDirectory,
59
59
  config: normalizedConfig,
60
60
  serverConfigFile,
61
- internalPlugins: (0, _createServer.injectDataLoaderPlugin)(serverInternalPlugins)
61
+ internalPlugins: (0, _createServer.injectDataLoaderPlugin)(serverInternalPlugins),
62
+ ...devServerOptions
62
63
  };
63
64
  if (apiOnly) {
64
65
  var _normalizedConfig_dev;
package/dist/cjs/index.js CHANGED
@@ -13,6 +13,9 @@ _export(exports, {
13
13
  mergeConfig: function() {
14
14
  return _core.mergeConfig;
15
15
  },
16
+ dev: function() {
17
+ return _commands.dev;
18
+ },
16
19
  devCommand: function() {
17
20
  return devCommand;
18
21
  },
@@ -41,6 +44,7 @@ const _locale = require("./locale");
41
44
  const _restart = require("./utils/restart");
42
45
  const _generateWatchFiles = require("./utils/generateWatchFiles");
43
46
  const _core = require("@modern-js/core");
47
+ const _commands = require("./commands");
44
48
  _export_star._(require("./defineConfig"), exports);
45
49
  _export_star._(require("./types"), exports);
46
50
  const devCommand = async (program, api) => {
@@ -207,7 +207,8 @@ export var checkIsBuildCommands = function() {
207
207
  "start",
208
208
  "build",
209
209
  "inspect",
210
- "deploy"
210
+ "deploy",
211
+ "dev-worker"
211
212
  ];
212
213
  var command = getCommand();
213
214
  return buildCommands.includes(command);
@@ -127,8 +127,13 @@ function applyRouterPlugin(chain, options) {
127
127
  var routerConfig = normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig_runtime = normalizedConfig.runtime) === null || _normalizedConfig_runtime === void 0 ? void 0 : _normalizedConfig_runtime.router;
128
128
  var routerManifest = Boolean(routerConfig === null || routerConfig === void 0 ? void 0 : routerConfig.manifest);
129
129
  var workerSSR = Boolean((_normalizedConfig_deploy_worker = normalizedConfig.deploy.worker) === null || _normalizedConfig_deploy_worker === void 0 ? void 0 : _normalizedConfig_deploy_worker.ssr);
130
+ var minimize = !normalizedConfig.output.disableMinimize && process.env.NODE_ENV === "production";
130
131
  if (existNestedRoutes || routerManifest || workerSSR) {
131
- chain.plugin("route-plugin").use(RouterPlugin);
132
+ chain.plugin("route-plugin").use(RouterPlugin, [
133
+ {
134
+ minimize: minimize
135
+ }
136
+ ]);
132
137
  }
133
138
  }
134
139
  function applyFilterEntriesBySSRConfig(param) {
@@ -1,17 +1,23 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
3
  import { _ as _create_class } from "@swc/helpers/_/_create_class";
4
+ import { _ as _define_property } from "@swc/helpers/_/_define_property";
4
5
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
5
6
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
7
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
+ import { _ as _ts_values } from "@swc/helpers/_/_ts_values";
9
+ import path from "path";
7
10
  import { mergeWith } from "@modern-js/utils/lodash";
8
11
  import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
9
12
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
13
+ import { transform } from "esbuild";
10
14
  var PLUGIN_NAME = "ModernjsRoutePlugin";
11
15
  export var RouterPlugin = /* @__PURE__ */ function() {
12
16
  "use strict";
13
- function RouterPlugin2() {
17
+ function RouterPlugin2(options) {
14
18
  _class_call_check(this, RouterPlugin2);
19
+ _define_property(this, "minimize", false);
20
+ this.minimize = options.minimize;
15
21
  }
16
22
  _create_class(RouterPlugin2, [
17
23
  {
@@ -26,9 +32,36 @@ export var RouterPlugin = /* @__PURE__ */ function() {
26
32
  return false;
27
33
  }
28
34
  },
35
+ {
36
+ key: "getEntryChunks",
37
+ value: function getEntryChunks(compilation, chunks) {
38
+ var entrypointsArray = Array.from(compilation.entrypoints.entries());
39
+ var entryChunkIds = entrypointsArray.map(function(entrypoint) {
40
+ return entrypoint[0];
41
+ });
42
+ var entryChunks = _to_consumable_array(chunks).filter(function(chunk) {
43
+ var _chunk_names;
44
+ return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some(function(name) {
45
+ return entryChunkIds.includes(name);
46
+ });
47
+ });
48
+ return entryChunks;
49
+ }
50
+ },
51
+ {
52
+ key: "getEntryChunkFiles",
53
+ value: function getEntryChunkFiles(entryChunks) {
54
+ return entryChunks.map(function(chunk) {
55
+ return _to_consumable_array(chunk.files || []).find(function(fname) {
56
+ return fname.includes(".js");
57
+ });
58
+ });
59
+ }
60
+ },
29
61
  {
30
62
  key: "apply",
31
63
  value: function apply(compiler) {
64
+ var _this = this;
32
65
  var target = compiler.options.target;
33
66
  if (this.isTargetNodeOrWebWorker(target)) {
34
67
  return;
@@ -36,24 +69,27 @@ export var RouterPlugin = /* @__PURE__ */ function() {
36
69
  var webpack = compiler.webpack;
37
70
  var Compilation = webpack.Compilation, sources = webpack.sources;
38
71
  var RawSource = sources.RawSource, SourceMapSource = sources.SourceMapSource;
39
- var normalizePath = function(path) {
40
- if (!path.endsWith("/")) {
41
- return "".concat(path, "/");
72
+ var normalizePath = function(path2) {
73
+ if (!path2.endsWith("/")) {
74
+ return "".concat(path2, "/");
42
75
  }
43
- return path;
76
+ return path2;
44
77
  };
45
- var chunkToSourceMap = /* @__PURE__ */ new Map();
78
+ var chunkToSource = /* @__PURE__ */ new Map();
79
+ var chunkToMap = /* @__PURE__ */ new Map();
46
80
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, function(compilation) {
81
+ var _this1 = _this;
47
82
  compilation.hooks.processAssets.tapPromise({
48
83
  name: PLUGIN_NAME,
49
84
  stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING
50
85
  }, /* @__PURE__ */ _async_to_generator(function() {
51
- var stats, _stats_chunks, chunks, namedChunkGroups, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, entryChunkFileIds, i, file, chunkId, asset, map;
86
+ var stats, _stats_chunks, chunks, namedChunkGroups, entryChunks, entryChunkFiles, entryChunkFileIds, i, file, chunkId, asset, map;
52
87
  return _ts_generator(this, function(_state) {
53
88
  stats = compilation.getStats().toJson({
54
89
  all: false,
55
90
  chunkGroups: true,
56
- chunks: true
91
+ chunks: true,
92
+ ids: true
57
93
  });
58
94
  _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
59
95
  if (!namedChunkGroups) {
@@ -61,16 +97,7 @@ export var RouterPlugin = /* @__PURE__ */ function() {
61
97
  2
62
98
  ];
63
99
  }
64
- entrypointsArray = Array.from(compilation.entrypoints.entries());
65
- entryChunkIds = entrypointsArray.map(function(entrypoint) {
66
- return entrypoint[0];
67
- });
68
- entryChunks = _to_consumable_array(chunks).filter(function(chunk) {
69
- var _chunk_names;
70
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some(function(name) {
71
- return entryChunkIds.includes(name);
72
- });
73
- });
100
+ entryChunks = _this1.getEntryChunks(compilation, chunks);
74
101
  entryChunkFiles = entryChunks.map(function(chunk) {
75
102
  return _to_consumable_array(chunk.files || []).find(function(fname) {
76
103
  return fname.includes(".js");
@@ -87,126 +114,33 @@ export var RouterPlugin = /* @__PURE__ */ function() {
87
114
  continue;
88
115
  }
89
116
  map = asset.sourceAndMap().map;
90
- chunkToSourceMap.set(chunkId, map);
117
+ chunkToMap.set(chunkId, map);
91
118
  }
92
119
  return [
93
120
  2
94
121
  ];
95
122
  });
96
123
  }));
124
+ var _this2 = _this;
97
125
  compilation.hooks.processAssets.tapPromise({
98
126
  name: PLUGIN_NAME,
99
- stage: Compilation.PROCESS_ASSETS_STAGE_REPORT
127
+ stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
100
128
  }, /* @__PURE__ */ _async_to_generator(function() {
101
- var _loop, stats, publicPath, _stats_chunks, chunks, namedChunkGroups, routeAssets, prevManifestAsset, prevManifestStr, prevManifest, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, assets, referenceCssAssets, manifest, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, entryChunkFileIds, i;
129
+ var stats, _stats_chunks, chunks, namedChunkGroups, entryChunks, entryChunkFiles, entryChunkFileIds, i, file, chunkId, asset, source;
102
130
  return _ts_generator(this, function(_state) {
103
- _loop = function(i2) {
104
- var file = entryChunkFiles[i2];
105
- var chunkId = entryChunkFileIds[i2];
106
- var asset = compilation.assets[file];
107
- if (!asset) {
108
- return "continue";
109
- }
110
- var relatedAssets = {};
111
- Object.keys(routeAssets).forEach(function(routeId) {
112
- if (routeId.startsWith("".concat(chunkId, "_"))) {
113
- relatedAssets[routeId] = routeAssets[routeId];
114
- }
115
- });
116
- var manifest2 = {
117
- routeAssets: relatedAssets
118
- };
119
- var injectedContent = "\n ;(function(){\n window.".concat(ROUTE_MANIFEST, " = ").concat(JSON.stringify(manifest2, function(k, v) {
120
- if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
121
- return v.map(function(item) {
122
- return item.replace(publicPath, "");
123
- });
124
- }
125
- return v;
126
- }), ";\n })();\n ");
127
- var source = asset.sourceAndMap().source;
128
- var map = chunkToSourceMap.get(chunkId);
129
- var newContent = "".concat(injectedContent).concat(source.toString());
130
- var newSource = new SourceMapSource(newContent, file, map, source.toString(), map);
131
- compilation.updateAsset(
132
- file,
133
- newSource,
134
- // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
135
- void 0
136
- );
137
- };
138
131
  stats = compilation.getStats().toJson({
139
132
  all: false,
140
- publicPath: true,
141
- assets: true,
142
133
  chunkGroups: true,
143
134
  chunks: true,
144
135
  ids: true
145
136
  });
146
- publicPath = stats.publicPath, _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
147
- routeAssets = {};
137
+ _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
148
138
  if (!namedChunkGroups) {
149
139
  return [
150
140
  2
151
141
  ];
152
142
  }
153
- prevManifestAsset = compilation.getAsset(ROUTE_MANIFEST_FILE);
154
- prevManifestStr = prevManifestAsset ? prevManifestAsset.source.source().toString() : JSON.stringify({
155
- routeAssets: {}
156
- });
157
- prevManifest = JSON.parse(prevManifestStr);
158
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
159
- try {
160
- for (_iterator = Object.entries(namedChunkGroups)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
161
- _step_value = _sliced_to_array(_step.value, 2), name = _step_value[0], chunkGroup = _step_value[1];
162
- assets = chunkGroup.assets.map(function(asset) {
163
- var filename = asset.name;
164
- return publicPath ? normalizePath(publicPath) + filename : filename;
165
- });
166
- referenceCssAssets = assets.filter(function(asset) {
167
- return /\.css$/.test(asset);
168
- });
169
- routeAssets[name] = {
170
- chunkIds: chunkGroup.chunks,
171
- assets: assets,
172
- referenceCssAssets: referenceCssAssets
173
- };
174
- if (prevManifest.routeAssets[name]) {
175
- mergeWith(routeAssets[name], prevManifest.routeAssets[name], function(obj, source) {
176
- if (Array.isArray(obj)) {
177
- return obj.concat(source);
178
- }
179
- return Object.assign(source, obj);
180
- });
181
- }
182
- }
183
- } catch (err) {
184
- _didIteratorError = true;
185
- _iteratorError = err;
186
- } finally {
187
- try {
188
- if (!_iteratorNormalCompletion && _iterator.return != null) {
189
- _iterator.return();
190
- }
191
- } finally {
192
- if (_didIteratorError) {
193
- throw _iteratorError;
194
- }
195
- }
196
- }
197
- manifest = {
198
- routeAssets: routeAssets
199
- };
200
- entrypointsArray = Array.from(compilation.entrypoints.entries());
201
- entryChunkIds = entrypointsArray.map(function(entrypoint) {
202
- return entrypoint[0];
203
- });
204
- entryChunks = _to_consumable_array(chunks).filter(function(chunk) {
205
- var _chunk_names;
206
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some(function(name2) {
207
- return entryChunkIds.includes(name2);
208
- });
209
- });
143
+ entryChunks = _this2.getEntryChunks(compilation, chunks);
210
144
  entryChunkFiles = entryChunks.map(function(chunk) {
211
145
  return _to_consumable_array(chunk.files || []).find(function(fname) {
212
146
  return fname.includes(".js");
@@ -215,23 +149,200 @@ export var RouterPlugin = /* @__PURE__ */ function() {
215
149
  entryChunkFileIds = entryChunks.map(function(chunk) {
216
150
  return chunk.id;
217
151
  });
218
- for (i = 0; i <= entryChunkFiles.length - 1; i++)
219
- _loop(i);
220
- if (prevManifestAsset) {
221
- compilation.updateAsset(
222
- ROUTE_MANIFEST_FILE,
223
- new RawSource(JSON.stringify(manifest, null, 2)),
224
- // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
225
- void 0
226
- );
227
- } else {
228
- compilation.emitAsset(ROUTE_MANIFEST_FILE, new RawSource(JSON.stringify(manifest, null, 2)));
152
+ for (i = 0; i <= entryChunkFiles.length - 1; i++) {
153
+ file = entryChunkFiles[i];
154
+ chunkId = entryChunkFileIds[i];
155
+ asset = compilation.assets[file];
156
+ if (!asset) {
157
+ continue;
158
+ }
159
+ source = asset.sourceAndMap().source;
160
+ chunkToSource.set(chunkId, source);
229
161
  }
230
162
  return [
231
163
  2
232
164
  ];
233
165
  });
234
166
  }));
167
+ var _this3 = _this;
168
+ compilation.hooks.processAssets.tapPromise({
169
+ name: PLUGIN_NAME,
170
+ stage: Compilation.PROCESS_ASSETS_STAGE_REPORT
171
+ }, /* @__PURE__ */ _async_to_generator(function() {
172
+ var _loop, stats, publicPath, _stats_chunks, chunks, namedChunkGroups, routeAssets, prevManifestAsset, prevManifestStr, prevManifest, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, assets, referenceCssAssets, manifest, entryChunks, entryChunkFiles, entryChunkFileIds, i;
173
+ return _ts_generator(this, function(_state) {
174
+ switch (_state.label) {
175
+ case 0:
176
+ _loop = function(i2) {
177
+ var file, chunkNames, chunkId, asset, relatedAssets, manifest2, injectedContent, source, map, newContent, result, newSource;
178
+ return _ts_generator(this, function(_state2) {
179
+ switch (_state2.label) {
180
+ case 0:
181
+ file = entryChunkFiles[i2];
182
+ chunkNames = entryChunks[i2].names;
183
+ chunkId = entryChunkFileIds[i2];
184
+ asset = compilation.assets[file];
185
+ if (!asset || !chunkId) {
186
+ return [
187
+ 2,
188
+ "continue"
189
+ ];
190
+ }
191
+ relatedAssets = {};
192
+ if (entryChunkFiles.length > 1) {
193
+ Object.keys(routeAssets).forEach(function(routeId) {
194
+ var segments = routeId.split("_");
195
+ var chunkName = segments[0];
196
+ if (chunkNames === null || chunkNames === void 0 ? void 0 : chunkNames.includes(chunkName)) {
197
+ relatedAssets[routeId] = routeAssets[routeId];
198
+ }
199
+ });
200
+ } else {
201
+ relatedAssets = routeAssets;
202
+ }
203
+ manifest2 = {
204
+ routeAssets: relatedAssets
205
+ };
206
+ injectedContent = "\n ;(function(){\n window.".concat(ROUTE_MANIFEST, " = ").concat(JSON.stringify(manifest2, function(k, v) {
207
+ if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
208
+ return v.map(function(item) {
209
+ return item.replace(publicPath, "");
210
+ });
211
+ }
212
+ return v;
213
+ }), ";\n })();\n ");
214
+ source = chunkToSource.get(chunkId);
215
+ map = chunkToMap.get(chunkId);
216
+ newContent = "".concat(injectedContent).concat(source.toString());
217
+ return [
218
+ 4,
219
+ transform(newContent, {
220
+ loader: path.extname(file).slice(1),
221
+ sourcemap: true,
222
+ minify: _this3.minimize
223
+ })
224
+ ];
225
+ case 1:
226
+ result = _state2.sent();
227
+ newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
228
+ compilation.updateAsset(
229
+ file,
230
+ newSource,
231
+ // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
232
+ void 0
233
+ );
234
+ return [
235
+ 2
236
+ ];
237
+ }
238
+ });
239
+ };
240
+ stats = compilation.getStats().toJson({
241
+ all: false,
242
+ publicPath: true,
243
+ assets: true,
244
+ chunkGroups: true,
245
+ chunks: true,
246
+ ids: true
247
+ });
248
+ publicPath = stats.publicPath, _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
249
+ routeAssets = {};
250
+ if (!namedChunkGroups) {
251
+ return [
252
+ 2
253
+ ];
254
+ }
255
+ prevManifestAsset = compilation.getAsset(ROUTE_MANIFEST_FILE);
256
+ prevManifestStr = prevManifestAsset ? prevManifestAsset.source.source().toString() : JSON.stringify({
257
+ routeAssets: {}
258
+ });
259
+ prevManifest = JSON.parse(prevManifestStr);
260
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
261
+ try {
262
+ for (_iterator = Object.entries(namedChunkGroups)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
263
+ _step_value = _sliced_to_array(_step.value, 2), name = _step_value[0], chunkGroup = _step_value[1];
264
+ assets = chunkGroup.assets.map(function(asset) {
265
+ var filename = asset.name;
266
+ return publicPath ? normalizePath(publicPath) + filename : filename;
267
+ });
268
+ referenceCssAssets = assets.filter(function(asset) {
269
+ return /\.css$/.test(asset);
270
+ });
271
+ routeAssets[name] = {
272
+ chunkIds: chunkGroup.chunks,
273
+ chunkName: chunkGroup.name,
274
+ assets: assets,
275
+ referenceCssAssets: referenceCssAssets
276
+ };
277
+ if (prevManifest.routeAssets[name]) {
278
+ mergeWith(routeAssets[name], prevManifest.routeAssets[name], function(obj, source) {
279
+ if (Array.isArray(obj)) {
280
+ return obj.concat(source);
281
+ }
282
+ return Object.assign(source, obj);
283
+ });
284
+ }
285
+ }
286
+ } catch (err) {
287
+ _didIteratorError = true;
288
+ _iteratorError = err;
289
+ } finally {
290
+ try {
291
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
292
+ _iterator.return();
293
+ }
294
+ } finally {
295
+ if (_didIteratorError) {
296
+ throw _iteratorError;
297
+ }
298
+ }
299
+ }
300
+ manifest = {
301
+ routeAssets: routeAssets
302
+ };
303
+ entryChunks = _this3.getEntryChunks(compilation, chunks);
304
+ entryChunkFiles = _this3.getEntryChunkFiles(entryChunks);
305
+ entryChunkFileIds = entryChunks.map(function(chunk) {
306
+ return chunk.id;
307
+ });
308
+ i = 0;
309
+ _state.label = 1;
310
+ case 1:
311
+ if (!(i < entryChunkFiles.length))
312
+ return [
313
+ 3,
314
+ 4
315
+ ];
316
+ return [
317
+ 5,
318
+ _ts_values(_loop(i))
319
+ ];
320
+ case 2:
321
+ _state.sent();
322
+ _state.label = 3;
323
+ case 3:
324
+ i++;
325
+ return [
326
+ 3,
327
+ 1
328
+ ];
329
+ case 4:
330
+ if (prevManifestAsset) {
331
+ compilation.updateAsset(
332
+ ROUTE_MANIFEST_FILE,
333
+ new RawSource(JSON.stringify(manifest, null, 2)),
334
+ // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
335
+ void 0
336
+ );
337
+ } else {
338
+ compilation.emitAsset(ROUTE_MANIFEST_FILE, new RawSource(JSON.stringify(manifest, null, 2)));
339
+ }
340
+ return [
341
+ 2
342
+ ];
343
+ }
344
+ });
345
+ }));
235
346
  });
236
347
  }
237
348
  }
@@ -11,10 +11,12 @@ import { buildServerConfig } from "../utils/config";
11
11
  import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
12
12
  export var dev = function() {
13
13
  var _ref = _async_to_generator(function(api, options) {
14
- var _normalizedConfig_tools, normalizedConfig, appContext, hookRunners, appDirectory, distDirectory, port, apiOnly, serverConfigFile, metaName, serverInternalPlugins, serverOptions, _normalizedConfig_dev, app, host, server;
14
+ var devServerOptions, _normalizedConfig_tools, normalizedConfig, appContext, hookRunners, appDirectory, distDirectory, port, apiOnly, serverConfigFile, metaName, serverInternalPlugins, serverOptions, _normalizedConfig_dev, app, host, server;
15
+ var _arguments = arguments;
15
16
  return _ts_generator(this, function(_state) {
16
17
  switch (_state.label) {
17
18
  case 0:
19
+ devServerOptions = _arguments.length > 2 && _arguments[2] !== void 0 ? _arguments[2] : {};
18
20
  if (options.analyze) {
19
21
  process.env.BUNDLE_ANALYZE = "true";
20
22
  }
@@ -58,7 +60,7 @@ export var dev = function() {
58
60
  ];
59
61
  case 4:
60
62
  serverInternalPlugins = _state.sent();
61
- serverOptions = {
63
+ serverOptions = _object_spread({
62
64
  dev: _object_spread({
63
65
  port: port,
64
66
  https: normalizedConfig.dev.https,
@@ -75,7 +77,7 @@ export var dev = function() {
75
77
  config: normalizedConfig,
76
78
  serverConfigFile: serverConfigFile,
77
79
  internalPlugins: injectDataLoaderPlugin(serverInternalPlugins)
78
- };
80
+ }, devServerOptions);
79
81
  if (!apiOnly)
80
82
  return [
81
83
  3,
package/dist/esm/index.js CHANGED
@@ -14,6 +14,7 @@ import { i18n, localeKeys } from "./locale";
14
14
  import { restart } from "./utils/restart";
15
15
  import { generateWatchFiles } from "./utils/generateWatchFiles";
16
16
  export { mergeConfig } from "@modern-js/core";
17
+ export { dev } from "./commands";
17
18
  export * from "./defineConfig";
18
19
  export * from "./types";
19
20
  export var devCommand = function() {
@@ -31,7 +32,7 @@ export var devCommand = function() {
31
32
  devToolMetas = _state.sent();
32
33
  devProgram = program.command("dev").alias("start").usage("[options]").description(i18n.t(localeKeys.command.dev.describe)).option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("-e --entry [entry...]", i18n.t(localeKeys.command.dev.entry)).option("--analyze", i18n.t(localeKeys.command.shared.analyze)).option("--api-only", i18n.t(localeKeys.command.dev.apiOnly)).option("--web-only", i18n.t(localeKeys.command.dev.webOnly)).action(function() {
33
34
  var _ref2 = _async_to_generator(function(options) {
34
- var dev;
35
+ var dev2;
35
36
  return _ts_generator(this, function(_state2) {
36
37
  switch (_state2.label) {
37
38
  case 0:
@@ -40,10 +41,10 @@ export var devCommand = function() {
40
41
  import("./commands/dev")
41
42
  ];
42
43
  case 1:
43
- dev = _state2.sent().dev;
44
+ dev2 = _state2.sent().dev;
44
45
  return [
45
46
  4,
46
- dev(api, options)
47
+ dev2(api, options)
47
48
  ];
48
49
  case 2:
49
50
  _state2.sent();
@@ -143,7 +143,8 @@ export const checkIsBuildCommands = () => {
143
143
  "start",
144
144
  "build",
145
145
  "inspect",
146
- "deploy"
146
+ "deploy",
147
+ "dev-worker"
147
148
  ];
148
149
  const command = getCommand();
149
150
  return buildCommands.includes(command);
@@ -76,8 +76,13 @@ function applyRouterPlugin(chain, options) {
76
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
77
  const routerManifest = Boolean(routerConfig === null || routerConfig === void 0 ? void 0 : routerConfig.manifest);
78
78
  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";
79
80
  if (existNestedRoutes || routerManifest || workerSSR) {
80
- chain.plugin("route-plugin").use(RouterPlugin);
81
+ chain.plugin("route-plugin").use(RouterPlugin, [
82
+ {
83
+ minimize
84
+ }
85
+ ]);
81
86
  }
82
87
  }
83
88
  function applyFilterEntriesBySSRConfig({ isProd, chain, appNormalizedConfig }) {
@@ -1,6 +1,9 @@
1
+ import { _ as _define_property } from "@swc/helpers/_/_define_property";
2
+ import path from "path";
1
3
  import { mergeWith } from "@modern-js/utils/lodash";
2
4
  import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
3
5
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
6
+ import { transform } from "esbuild";
4
7
  const PLUGIN_NAME = "ModernjsRoutePlugin";
5
8
  export class RouterPlugin {
6
9
  isTargetNodeOrWebWorker(target) {
@@ -12,6 +15,22 @@ export class RouterPlugin {
12
15
  }
13
16
  return false;
14
17
  }
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
+ }
15
34
  apply(compiler) {
16
35
  const { target } = compiler.options;
17
36
  if (this.isTargetNodeOrWebWorker(target)) {
@@ -20,13 +39,14 @@ export class RouterPlugin {
20
39
  const { webpack } = compiler;
21
40
  const { Compilation, sources } = webpack;
22
41
  const { RawSource, SourceMapSource } = sources;
23
- const normalizePath = (path) => {
24
- if (!path.endsWith("/")) {
25
- return `${path}/`;
42
+ const normalizePath = (path2) => {
43
+ if (!path2.endsWith("/")) {
44
+ return `${path2}/`;
26
45
  }
27
- return path;
46
+ return path2;
28
47
  };
29
- const chunkToSourceMap = /* @__PURE__ */ new Map();
48
+ const chunkToSource = /* @__PURE__ */ new Map();
49
+ const chunkToMap = /* @__PURE__ */ new Map();
30
50
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
31
51
  compilation.hooks.processAssets.tapPromise({
32
52
  name: PLUGIN_NAME,
@@ -35,20 +55,14 @@ export class RouterPlugin {
35
55
  const stats = compilation.getStats().toJson({
36
56
  all: false,
37
57
  chunkGroups: true,
38
- chunks: true
58
+ chunks: true,
59
+ ids: true
39
60
  });
40
61
  const { chunks = [], namedChunkGroups } = stats;
41
62
  if (!namedChunkGroups) {
42
63
  return;
43
64
  }
44
- const entrypointsArray = Array.from(compilation.entrypoints.entries());
45
- const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
46
- const entryChunks = [
47
- ...chunks
48
- ].filter((chunk) => {
49
- var _chunk_names;
50
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
51
- });
65
+ const entryChunks = this.getEntryChunks(compilation, chunks);
52
66
  const entryChunkFiles = entryChunks.map((chunk) => [
53
67
  ...chunk.files || []
54
68
  ].find((fname) => fname.includes(".js")));
@@ -61,7 +75,37 @@ export class RouterPlugin {
61
75
  continue;
62
76
  }
63
77
  const { map } = asset.sourceAndMap();
64
- chunkToSourceMap.set(chunkId, map);
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
90
+ });
91
+ const { chunks = [], namedChunkGroups } = stats;
92
+ if (!namedChunkGroups) {
93
+ return;
94
+ }
95
+ const entryChunks = this.getEntryChunks(compilation, chunks);
96
+ const entryChunkFiles = entryChunks.map((chunk) => [
97
+ ...chunk.files || []
98
+ ].find((fname) => fname.includes(".js")));
99
+ const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
100
+ for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
101
+ const file = entryChunkFiles[i];
102
+ const chunkId = entryChunkFileIds[i];
103
+ const asset = compilation.assets[file];
104
+ if (!asset) {
105
+ continue;
106
+ }
107
+ const { source } = asset.sourceAndMap();
108
+ chunkToSource.set(chunkId, source);
65
109
  }
66
110
  });
67
111
  compilation.hooks.processAssets.tapPromise({
@@ -94,6 +138,7 @@ export class RouterPlugin {
94
138
  const referenceCssAssets = assets.filter((asset) => /\.css$/.test(asset));
95
139
  routeAssets[name] = {
96
140
  chunkIds: chunkGroup.chunks,
141
+ chunkName: chunkGroup.name,
97
142
  assets,
98
143
  referenceCssAssets
99
144
  };
@@ -109,31 +154,29 @@ export class RouterPlugin {
109
154
  const manifest = {
110
155
  routeAssets
111
156
  };
112
- const entrypointsArray = Array.from(compilation.entrypoints.entries());
113
- const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
114
- const entryChunks = [
115
- ...chunks
116
- ].filter((chunk) => {
117
- var _chunk_names;
118
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some((name) => entryChunkIds.includes(name));
119
- });
120
- const entryChunkFiles = entryChunks.map((chunk) => [
121
- ...chunk.files || []
122
- ].find((fname) => fname.includes(".js")));
157
+ const entryChunks = this.getEntryChunks(compilation, chunks);
158
+ const entryChunkFiles = this.getEntryChunkFiles(entryChunks);
123
159
  const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
124
- for (let i = 0; i <= entryChunkFiles.length - 1; i++) {
160
+ for (let i = 0; i < entryChunkFiles.length; i++) {
125
161
  const file = entryChunkFiles[i];
162
+ const chunkNames = entryChunks[i].names;
126
163
  const chunkId = entryChunkFileIds[i];
127
164
  const asset = compilation.assets[file];
128
- if (!asset) {
165
+ if (!asset || !chunkId) {
129
166
  continue;
130
167
  }
131
- const relatedAssets = {};
132
- Object.keys(routeAssets).forEach((routeId) => {
133
- if (routeId.startsWith(`${chunkId}_`)) {
134
- relatedAssets[routeId] = routeAssets[routeId];
135
- }
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
180
  const manifest2 = {
138
181
  routeAssets: relatedAssets
139
182
  };
@@ -149,10 +192,15 @@ export class RouterPlugin {
149
192
  })};
150
193
  })();
151
194
  `;
152
- const { source } = asset.sourceAndMap();
153
- const map = chunkToSourceMap.get(chunkId);
195
+ const source = chunkToSource.get(chunkId);
196
+ const map = chunkToMap.get(chunkId);
154
197
  const newContent = `${injectedContent}${source.toString()}`;
155
- const newSource = new SourceMapSource(newContent, file, map, source.toString(), map);
198
+ const result = await transform(newContent, {
199
+ loader: path.extname(file).slice(1),
200
+ sourcemap: true,
201
+ minify: this.minimize
202
+ });
203
+ const newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
156
204
  compilation.updateAsset(
157
205
  file,
158
206
  newSource,
@@ -173,4 +221,8 @@ export class RouterPlugin {
173
221
  });
174
222
  });
175
223
  }
224
+ constructor(options) {
225
+ _define_property(this, "minimize", false);
226
+ this.minimize = options.minimize;
227
+ }
176
228
  }
@@ -5,7 +5,7 @@ import { setServer, createServer, injectDataLoaderPlugin } from "../utils/create
5
5
  import { generateRoutes } from "../utils/routes";
6
6
  import { buildServerConfig } from "../utils/config";
7
7
  import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
8
- export const dev = async (api, options) => {
8
+ export const dev = async (api, options, devServerOptions = {}) => {
9
9
  var _normalizedConfig_tools;
10
10
  if (options.analyze) {
11
11
  process.env.BUNDLE_ANALYZE = "true";
@@ -48,7 +48,8 @@ export const dev = async (api, options) => {
48
48
  pwd: appDirectory,
49
49
  config: normalizedConfig,
50
50
  serverConfigFile,
51
- internalPlugins: injectDataLoaderPlugin(serverInternalPlugins)
51
+ internalPlugins: injectDataLoaderPlugin(serverInternalPlugins),
52
+ ...devServerOptions
52
53
  };
53
54
  if (apiOnly) {
54
55
  var _normalizedConfig_dev;
@@ -10,14 +10,15 @@ import { i18n, localeKeys } from "./locale";
10
10
  import { restart } from "./utils/restart";
11
11
  import { generateWatchFiles } from "./utils/generateWatchFiles";
12
12
  export { mergeConfig } from "@modern-js/core";
13
+ export { dev } from "./commands";
13
14
  export * from "./defineConfig";
14
15
  export * from "./types";
15
16
  export const devCommand = async (program, api) => {
16
17
  const runner = api.useHookRunners();
17
18
  const devToolMetas = await runner.registerDev();
18
19
  const devProgram = program.command("dev").alias("start").usage("[options]").description(i18n.t(localeKeys.command.dev.describe)).option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("-e --entry [entry...]", i18n.t(localeKeys.command.dev.entry)).option("--analyze", i18n.t(localeKeys.command.shared.analyze)).option("--api-only", i18n.t(localeKeys.command.dev.apiOnly)).option("--web-only", i18n.t(localeKeys.command.dev.webOnly)).action(async (options) => {
19
- const { dev } = await import("./commands/dev");
20
- await dev(api, options);
20
+ const { dev: dev2 } = await import("./commands/dev");
21
+ await dev2(api, options);
21
22
  });
22
23
  for (const meta of devToolMetas) {
23
24
  if (!meta.subCommands) {
@@ -1,4 +1,4 @@
1
- import type { Entrypoint, NestedRoute, PageRoute } from '@modern-js/types';
1
+ import type { Entrypoint, NestedRouteForCli, PageRoute } from '@modern-js/types';
2
2
  export declare const getClientRoutes: ({
3
3
  entrypoint,
4
4
  srcDirectory,
@@ -11,4 +11,4 @@ export declare const getClientRoutes: ({
11
11
  srcAlias: string;
12
12
  internalDirectory: string;
13
13
  internalDirAlias: string;
14
- }) => (NestedRoute | PageRoute)[];
14
+ }) => (NestedRouteForCli | PageRoute)[];
@@ -1,6 +1,6 @@
1
- import type { NestedRoute } from '@modern-js/types';
1
+ import type { NestedRouteForCli } from '@modern-js/types';
2
2
  export declare const getRouteId: (componentPath: string, routesDir: string, entryName: string, isMainEntry: boolean) => string;
3
3
  export declare const walk: (dirname: string, rootDir: string, alias: {
4
4
  name: string;
5
5
  basename: string;
6
- }, entryName: string, isMainEntry: boolean) => Promise<NestedRoute | null>;
6
+ }, entryName: string, isMainEntry: boolean) => Promise<NestedRouteForCli | null>;
@@ -1,4 +1,4 @@
1
- import type { Entrypoint, NestedRoute, PageRoute, RouteLegacy, SSRMode } from '@modern-js/types';
1
+ import type { Entrypoint, NestedRouteForCli, PageRoute, RouteLegacy, SSRMode } from '@modern-js/types';
2
2
  import type { AppNormalizedConfig, IAppContext, RuntimePlugin } from '../types';
3
3
  export declare const index: ({
4
4
  mountId,
@@ -28,7 +28,7 @@ export declare const html: (partials: {
28
28
  export declare const routesForServer: ({
29
29
  routes
30
30
  }: {
31
- routes: (NestedRoute | PageRoute)[];
31
+ routes: (NestedRouteForCli | PageRoute)[];
32
32
  }) => string;
33
33
  export declare const fileSystemRoutes: ({
34
34
  routes,
@@ -38,7 +38,7 @@ export declare const fileSystemRoutes: ({
38
38
  internalDirectory,
39
39
  splitRouteChunks
40
40
  }: {
41
- routes: RouteLegacy[] | (NestedRoute | PageRoute)[];
41
+ routes: RouteLegacy[] | (NestedRouteForCli | PageRoute)[];
42
42
  ssrMode?: SSRMode | undefined;
43
43
  nestedRoutesEntry?: string | undefined;
44
44
  entryName: string;
@@ -3,13 +3,21 @@ import type { Rspack } from '@modern-js/builder-rspack-provider';
3
3
  export interface RouteAssets {
4
4
  [routeId: string]: {
5
5
  chunkIds?: (string | number)[];
6
+ chunkName: string;
6
7
  assets?: string[];
7
8
  referenceCssAssets?: string[];
8
9
  };
9
10
  }
10
11
  type Compiler = webpack.Compiler | Rspack.Compiler;
12
+ type Options = {
13
+ minimize: boolean;
14
+ };
11
15
  export declare class RouterPlugin {
16
+ private minimize;
17
+ constructor(options: Options);
12
18
  private isTargetNodeOrWebWorker;
19
+ private getEntryChunks;
20
+ private getEntryChunkFiles;
13
21
  apply(compiler: Compiler): void;
14
22
  }
15
23
  export {};
@@ -1,4 +1,7 @@
1
1
  import { PluginAPI } from '@modern-js/core';
2
2
  import { DevOptions } from '../utils/types';
3
3
  import type { AppTools } from '../types';
4
- export declare const dev: (api: PluginAPI<AppTools<'shared'>>, options: DevOptions) => Promise<void>;
4
+ export interface ExtraServerOptions {
5
+ useSSRWorker?: boolean;
6
+ }
7
+ export declare const dev: (api: PluginAPI<AppTools<'shared'>>, options: DevOptions, devServerOptions?: ExtraServerOptions) => Promise<void>;
@@ -2,6 +2,7 @@ import { Command } from '@modern-js/utils';
2
2
  import { CliPlugin, PluginAPI } from '@modern-js/core';
3
3
  import { AppTools } from './types';
4
4
  export { mergeConfig } from '@modern-js/core';
5
+ export { dev } from './commands';
5
6
  export * from './defineConfig';
6
7
  export * from './types';
7
8
  export type { RuntimeUserConfig } from './types/config';
@@ -1,6 +1,6 @@
1
1
  import type { webpack } from '@modern-js/builder-webpack-provider';
2
2
  import type { AsyncWaterfall, AsyncWorkflow, ParallelWorkflow } from '@modern-js/plugin';
3
- import type { Entrypoint, HtmlPartials, NestedRoute, PageRoute, RouteLegacy, ServerRoute } from '@modern-js/types';
3
+ import type { Entrypoint, HtmlPartials, NestedRouteForCli, PageRoute, RouteLegacy, ServerRoute } from '@modern-js/types';
4
4
  import type { RegisterBuildPlatformResult, DevToolData } from '@modern-js/core';
5
5
  import type { Stats, MultiStats } from '@modern-js/builder-shared';
6
6
  import type { Rspack } from '@modern-js/builder-rspack-provider';
@@ -43,7 +43,7 @@ export type AppToolsHooks<B extends Bundler = 'webpack'> = {
43
43
  }>;
44
44
  modifyFileSystemRoutes: AsyncWaterfall<{
45
45
  entrypoint: Entrypoint;
46
- routes: RouteLegacy[] | (NestedRoute | PageRoute)[];
46
+ routes: RouteLegacy[] | (NestedRouteForCli | PageRoute)[];
47
47
  }>;
48
48
  modifyServerRoutes: AsyncWaterfall<{
49
49
  routes: ServerRoute[];
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.26.1-alpha.0",
18
+ "version": "2.27.1-alpha.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -73,24 +73,24 @@
73
73
  "esbuild": "0.17.19",
74
74
  "rspack-plugin-virtual-module": "0.1.0",
75
75
  "@swc/helpers": "0.5.1",
76
- "@modern-js/builder": "2.26.0",
77
- "@modern-js/builder-plugin-node-polyfill": "2.26.0",
78
- "@modern-js/builder-plugin-esbuild": "2.26.0",
79
- "@modern-js/core": "2.26.0",
80
- "@modern-js/builder-shared": "2.26.0",
81
- "@modern-js/builder-webpack-provider": "2.26.0",
82
- "@modern-js/new-action": "2.26.0",
83
- "@modern-js/node-bundle-require": "2.26.0",
84
- "@modern-js/plugin": "2.26.0",
85
- "@modern-js/plugin-lint": "2.26.0",
86
- "@modern-js/prod-server": "2.26.0",
87
- "@modern-js/plugin-i18n": "2.26.0",
88
- "@modern-js/server": "2.26.0",
89
- "@modern-js/types": "2.26.0",
90
- "@modern-js/utils": "2.26.0",
91
- "@modern-js/server-core": "2.26.0",
92
- "@modern-js/upgrade": "2.26.0",
93
- "@modern-js/plugin-data-loader": "2.26.0"
76
+ "@modern-js/builder": "2.27.0",
77
+ "@modern-js/builder-webpack-provider": "2.27.0",
78
+ "@modern-js/builder-plugin-esbuild": "2.27.0",
79
+ "@modern-js/new-action": "2.27.0",
80
+ "@modern-js/core": "2.27.0",
81
+ "@modern-js/builder-shared": "2.27.0",
82
+ "@modern-js/node-bundle-require": "2.27.0",
83
+ "@modern-js/builder-plugin-node-polyfill": "2.27.0",
84
+ "@modern-js/plugin-i18n": "2.27.0",
85
+ "@modern-js/prod-server": "2.27.0",
86
+ "@modern-js/plugin-lint": "2.27.0",
87
+ "@modern-js/plugin": "2.27.0",
88
+ "@modern-js/server": "2.27.0",
89
+ "@modern-js/types": "2.27.0",
90
+ "@modern-js/upgrade": "2.27.0",
91
+ "@modern-js/utils": "2.27.0",
92
+ "@modern-js/server-core": "2.27.0",
93
+ "@modern-js/plugin-data-loader": "2.27.0"
94
94
  },
95
95
  "devDependencies": {
96
96
  "@types/babel__traverse": "^7.14.2",
@@ -99,13 +99,13 @@
99
99
  "jest": "^29",
100
100
  "typescript": "^5",
101
101
  "webpack": "^5.88.1",
102
- "@modern-js/builder-rspack-provider": "2.26.0",
103
- "@modern-js/builder-plugin-swc": "2.26.0",
104
- "@scripts/build": "2.26.0",
105
- "@scripts/jest-config": "2.26.0"
102
+ "@modern-js/builder-rspack-provider": "2.27.0",
103
+ "@modern-js/builder-plugin-swc": "2.27.0",
104
+ "@scripts/jest-config": "2.27.0",
105
+ "@scripts/build": "2.27.0"
106
106
  },
107
107
  "peerDependencies": {
108
- "@modern-js/builder-rspack-provider": "^2.26.0"
108
+ "@modern-js/builder-rspack-provider": "^2.27.0"
109
109
  },
110
110
  "peerDependenciesMeta": {
111
111
  "@modern-js/builder-rspack-provider": {