@modern-js/app-tools 2.26.0 → 2.27.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
|
|
@@ -8,9 +8,12 @@ Object.defineProperty(exports, "RouterPlugin", {
|
|
|
8
8
|
return RouterPlugin;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
12
|
+
const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
|
|
11
13
|
const _lodash = require("@modern-js/utils/lodash");
|
|
12
14
|
const _utils = require("@modern-js/utils");
|
|
13
15
|
const _constants = require("@modern-js/utils/universal/constants");
|
|
16
|
+
const _esbuild = require("esbuild");
|
|
14
17
|
const PLUGIN_NAME = "ModernjsRoutePlugin";
|
|
15
18
|
class RouterPlugin {
|
|
16
19
|
isTargetNodeOrWebWorker(target) {
|
|
@@ -36,7 +39,7 @@ class RouterPlugin {
|
|
|
36
39
|
}
|
|
37
40
|
return path;
|
|
38
41
|
};
|
|
39
|
-
const
|
|
42
|
+
const chunkToSourceAndMap = /* @__PURE__ */ new Map();
|
|
40
43
|
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|
41
44
|
compilation.hooks.processAssets.tapPromise({
|
|
42
45
|
name: PLUGIN_NAME,
|
|
@@ -45,7 +48,8 @@ class RouterPlugin {
|
|
|
45
48
|
const stats = compilation.getStats().toJson({
|
|
46
49
|
all: false,
|
|
47
50
|
chunkGroups: true,
|
|
48
|
-
chunks: true
|
|
51
|
+
chunks: true,
|
|
52
|
+
ids: true
|
|
49
53
|
});
|
|
50
54
|
const { chunks = [], namedChunkGroups } = stats;
|
|
51
55
|
if (!namedChunkGroups) {
|
|
@@ -70,8 +74,11 @@ class RouterPlugin {
|
|
|
70
74
|
if (!asset) {
|
|
71
75
|
continue;
|
|
72
76
|
}
|
|
73
|
-
const { map } = asset.sourceAndMap();
|
|
74
|
-
|
|
77
|
+
const { source, map } = asset.sourceAndMap();
|
|
78
|
+
chunkToSourceAndMap.set(chunkId, {
|
|
79
|
+
source,
|
|
80
|
+
map
|
|
81
|
+
});
|
|
75
82
|
}
|
|
76
83
|
});
|
|
77
84
|
compilation.hooks.processAssets.tapPromise({
|
|
@@ -119,18 +126,6 @@ class RouterPlugin {
|
|
|
119
126
|
const manifest = {
|
|
120
127
|
routeAssets
|
|
121
128
|
};
|
|
122
|
-
const injectedContent = `
|
|
123
|
-
;(function(){
|
|
124
|
-
window.${_constants.ROUTE_MANIFEST} = ${JSON.stringify(manifest, (k, v) => {
|
|
125
|
-
if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
|
|
126
|
-
return v.map((item) => {
|
|
127
|
-
return item.replace(publicPath, "");
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
return v;
|
|
131
|
-
})};
|
|
132
|
-
})();
|
|
133
|
-
`;
|
|
134
129
|
const entrypointsArray = Array.from(compilation.entrypoints.entries());
|
|
135
130
|
const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
|
|
136
131
|
const entryChunks = [
|
|
@@ -147,13 +142,38 @@ class RouterPlugin {
|
|
|
147
142
|
const file = entryChunkFiles[i];
|
|
148
143
|
const chunkId = entryChunkFileIds[i];
|
|
149
144
|
const asset = compilation.assets[file];
|
|
150
|
-
if (!asset) {
|
|
145
|
+
if (!asset || !chunkId) {
|
|
151
146
|
continue;
|
|
152
147
|
}
|
|
153
|
-
const
|
|
154
|
-
|
|
148
|
+
const relatedAssets = {};
|
|
149
|
+
Object.keys(routeAssets).forEach((routeId) => {
|
|
150
|
+
if (routeId.startsWith(`${chunkId}`)) {
|
|
151
|
+
relatedAssets[routeId] = routeAssets[routeId];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
const manifest2 = {
|
|
155
|
+
routeAssets: relatedAssets
|
|
156
|
+
};
|
|
157
|
+
const injectedContent = `
|
|
158
|
+
;(function(){
|
|
159
|
+
window.${_constants.ROUTE_MANIFEST} = ${JSON.stringify(manifest2, (k, v) => {
|
|
160
|
+
if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
|
|
161
|
+
return v.map((item) => {
|
|
162
|
+
return item.replace(publicPath, "");
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return v;
|
|
166
|
+
})};
|
|
167
|
+
})();
|
|
168
|
+
`;
|
|
169
|
+
const { source, map } = chunkToSourceAndMap.get(chunkId);
|
|
155
170
|
const newContent = `${injectedContent}${source.toString()}`;
|
|
156
|
-
const
|
|
171
|
+
const result = await (0, _esbuild.transform)(newContent, {
|
|
172
|
+
loader: _path.default.extname(file).slice(1),
|
|
173
|
+
sourcemap: true,
|
|
174
|
+
minify: process.env.NODE_ENV === "production"
|
|
175
|
+
});
|
|
176
|
+
const newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
|
|
157
177
|
compilation.updateAsset(
|
|
158
178
|
file,
|
|
159
179
|
newSource,
|
|
@@ -4,9 +4,12 @@ import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
|
4
4
|
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
5
5
|
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
6
6
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
7
|
+
import { _ as _ts_values } from "@swc/helpers/_/_ts_values";
|
|
8
|
+
import path from "path";
|
|
7
9
|
import { mergeWith } from "@modern-js/utils/lodash";
|
|
8
10
|
import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
|
|
9
11
|
import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
|
|
12
|
+
import { transform } from "esbuild";
|
|
10
13
|
var PLUGIN_NAME = "ModernjsRoutePlugin";
|
|
11
14
|
export var RouterPlugin = /* @__PURE__ */ function() {
|
|
12
15
|
"use strict";
|
|
@@ -36,24 +39,25 @@ export var RouterPlugin = /* @__PURE__ */ function() {
|
|
|
36
39
|
var webpack = compiler.webpack;
|
|
37
40
|
var Compilation = webpack.Compilation, sources = webpack.sources;
|
|
38
41
|
var RawSource = sources.RawSource, SourceMapSource = sources.SourceMapSource;
|
|
39
|
-
var normalizePath = function(
|
|
40
|
-
if (!
|
|
41
|
-
return "".concat(
|
|
42
|
+
var normalizePath = function(path2) {
|
|
43
|
+
if (!path2.endsWith("/")) {
|
|
44
|
+
return "".concat(path2, "/");
|
|
42
45
|
}
|
|
43
|
-
return
|
|
46
|
+
return path2;
|
|
44
47
|
};
|
|
45
|
-
var
|
|
48
|
+
var chunkToSourceAndMap = /* @__PURE__ */ new Map();
|
|
46
49
|
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, function(compilation) {
|
|
47
50
|
compilation.hooks.processAssets.tapPromise({
|
|
48
51
|
name: PLUGIN_NAME,
|
|
49
52
|
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING
|
|
50
53
|
}, /* @__PURE__ */ _async_to_generator(function() {
|
|
51
|
-
var stats, _stats_chunks, chunks, namedChunkGroups, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, entryChunkFileIds, i, file, chunkId, asset, map;
|
|
54
|
+
var stats, _stats_chunks, chunks, namedChunkGroups, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, entryChunkFileIds, i, file, chunkId, asset, _asset_sourceAndMap, source, map;
|
|
52
55
|
return _ts_generator(this, function(_state) {
|
|
53
56
|
stats = compilation.getStats().toJson({
|
|
54
57
|
all: false,
|
|
55
58
|
chunkGroups: true,
|
|
56
|
-
chunks: true
|
|
59
|
+
chunks: true,
|
|
60
|
+
ids: true
|
|
57
61
|
});
|
|
58
62
|
_stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
|
|
59
63
|
if (!namedChunkGroups) {
|
|
@@ -86,8 +90,11 @@ export var RouterPlugin = /* @__PURE__ */ function() {
|
|
|
86
90
|
if (!asset) {
|
|
87
91
|
continue;
|
|
88
92
|
}
|
|
89
|
-
|
|
90
|
-
|
|
93
|
+
_asset_sourceAndMap = asset.sourceAndMap(), source = _asset_sourceAndMap.source, map = _asset_sourceAndMap.map;
|
|
94
|
+
chunkToSourceAndMap.set(chunkId, {
|
|
95
|
+
source: source,
|
|
96
|
+
map: map
|
|
97
|
+
});
|
|
91
98
|
}
|
|
92
99
|
return [
|
|
93
100
|
2
|
|
@@ -98,127 +105,182 @@ export var RouterPlugin = /* @__PURE__ */ function() {
|
|
|
98
105
|
name: PLUGIN_NAME,
|
|
99
106
|
stage: Compilation.PROCESS_ASSETS_STAGE_REPORT
|
|
100
107
|
}, /* @__PURE__ */ _async_to_generator(function() {
|
|
101
|
-
var stats, publicPath, _stats_chunks, chunks, namedChunkGroups, routeAssets, prevManifestAsset, prevManifestStr, prevManifest, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, assets, referenceCssAssets, manifest,
|
|
108
|
+
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;
|
|
102
109
|
return _ts_generator(this, function(_state) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
110
|
+
switch (_state.label) {
|
|
111
|
+
case 0:
|
|
112
|
+
_loop = function(i2) {
|
|
113
|
+
var file, chunkId, asset, relatedAssets, manifest2, injectedContent, _chunkToSourceAndMap_get, source, map, newContent, result, newSource;
|
|
114
|
+
return _ts_generator(this, function(_state2) {
|
|
115
|
+
switch (_state2.label) {
|
|
116
|
+
case 0:
|
|
117
|
+
file = entryChunkFiles[i2];
|
|
118
|
+
chunkId = entryChunkFileIds[i2];
|
|
119
|
+
asset = compilation.assets[file];
|
|
120
|
+
if (!asset || !chunkId) {
|
|
121
|
+
return [
|
|
122
|
+
2,
|
|
123
|
+
"continue"
|
|
124
|
+
];
|
|
125
|
+
}
|
|
126
|
+
relatedAssets = {};
|
|
127
|
+
Object.keys(routeAssets).forEach(function(routeId) {
|
|
128
|
+
if (routeId.startsWith("".concat(chunkId))) {
|
|
129
|
+
relatedAssets[routeId] = routeAssets[routeId];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
manifest2 = {
|
|
133
|
+
routeAssets: relatedAssets
|
|
134
|
+
};
|
|
135
|
+
injectedContent = "\n ;(function(){\n window.".concat(ROUTE_MANIFEST, " = ").concat(JSON.stringify(manifest2, function(k, v) {
|
|
136
|
+
if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
|
|
137
|
+
return v.map(function(item) {
|
|
138
|
+
return item.replace(publicPath, "");
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return v;
|
|
142
|
+
}), ";\n })();\n ");
|
|
143
|
+
_chunkToSourceAndMap_get = chunkToSourceAndMap.get(chunkId), source = _chunkToSourceAndMap_get.source, map = _chunkToSourceAndMap_get.map;
|
|
144
|
+
newContent = "".concat(injectedContent).concat(source.toString());
|
|
145
|
+
return [
|
|
146
|
+
4,
|
|
147
|
+
transform(newContent, {
|
|
148
|
+
loader: path.extname(file).slice(1),
|
|
149
|
+
sourcemap: true,
|
|
150
|
+
minify: process.env.NODE_ENV === "production"
|
|
151
|
+
})
|
|
152
|
+
];
|
|
153
|
+
case 1:
|
|
154
|
+
result = _state2.sent();
|
|
155
|
+
newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
|
|
156
|
+
compilation.updateAsset(
|
|
157
|
+
file,
|
|
158
|
+
newSource,
|
|
159
|
+
// FIXME: The arguments third of updatgeAsset is a optional function in webpack.
|
|
160
|
+
void 0
|
|
161
|
+
);
|
|
162
|
+
return [
|
|
163
|
+
2
|
|
164
|
+
];
|
|
143
165
|
}
|
|
144
|
-
return Object.assign(source2, obj);
|
|
145
166
|
});
|
|
167
|
+
};
|
|
168
|
+
stats = compilation.getStats().toJson({
|
|
169
|
+
all: false,
|
|
170
|
+
publicPath: true,
|
|
171
|
+
assets: true,
|
|
172
|
+
chunkGroups: true,
|
|
173
|
+
chunks: true,
|
|
174
|
+
ids: true
|
|
175
|
+
});
|
|
176
|
+
publicPath = stats.publicPath, _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
|
|
177
|
+
routeAssets = {};
|
|
178
|
+
if (!namedChunkGroups) {
|
|
179
|
+
return [
|
|
180
|
+
2
|
|
181
|
+
];
|
|
146
182
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
_iterator.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
183
|
+
prevManifestAsset = compilation.getAsset(ROUTE_MANIFEST_FILE);
|
|
184
|
+
prevManifestStr = prevManifestAsset ? prevManifestAsset.source.source().toString() : JSON.stringify({
|
|
185
|
+
routeAssets: {}
|
|
186
|
+
});
|
|
187
|
+
prevManifest = JSON.parse(prevManifestStr);
|
|
188
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
189
|
+
try {
|
|
190
|
+
for (_iterator = Object.entries(namedChunkGroups)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
191
|
+
_step_value = _sliced_to_array(_step.value, 2), name = _step_value[0], chunkGroup = _step_value[1];
|
|
192
|
+
assets = chunkGroup.assets.map(function(asset) {
|
|
193
|
+
var filename = asset.name;
|
|
194
|
+
return publicPath ? normalizePath(publicPath) + filename : filename;
|
|
195
|
+
});
|
|
196
|
+
referenceCssAssets = assets.filter(function(asset) {
|
|
197
|
+
return /\.css$/.test(asset);
|
|
198
|
+
});
|
|
199
|
+
routeAssets[name] = {
|
|
200
|
+
chunkIds: chunkGroup.chunks,
|
|
201
|
+
assets: assets,
|
|
202
|
+
referenceCssAssets: referenceCssAssets
|
|
203
|
+
};
|
|
204
|
+
if (prevManifest.routeAssets[name]) {
|
|
205
|
+
mergeWith(routeAssets[name], prevManifest.routeAssets[name], function(obj, source) {
|
|
206
|
+
if (Array.isArray(obj)) {
|
|
207
|
+
return obj.concat(source);
|
|
208
|
+
}
|
|
209
|
+
return Object.assign(source, obj);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
} catch (err) {
|
|
214
|
+
_didIteratorError = true;
|
|
215
|
+
_iteratorError = err;
|
|
216
|
+
} finally {
|
|
217
|
+
try {
|
|
218
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
219
|
+
_iterator.return();
|
|
220
|
+
}
|
|
221
|
+
} finally {
|
|
222
|
+
if (_didIteratorError) {
|
|
223
|
+
throw _iteratorError;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
159
226
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
|
|
167
|
-
return v.map(function(item) {
|
|
168
|
-
return item.replace(publicPath, "");
|
|
227
|
+
manifest = {
|
|
228
|
+
routeAssets: routeAssets
|
|
229
|
+
};
|
|
230
|
+
entrypointsArray = Array.from(compilation.entrypoints.entries());
|
|
231
|
+
entryChunkIds = entrypointsArray.map(function(entrypoint) {
|
|
232
|
+
return entrypoint[0];
|
|
169
233
|
});
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
234
|
+
entryChunks = _to_consumable_array(chunks).filter(function(chunk) {
|
|
235
|
+
var _chunk_names;
|
|
236
|
+
return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some(function(name2) {
|
|
237
|
+
return entryChunkIds.includes(name2);
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
entryChunkFiles = entryChunks.map(function(chunk) {
|
|
241
|
+
return _to_consumable_array(chunk.files || []).find(function(fname) {
|
|
242
|
+
return fname.includes(".js");
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
entryChunkFileIds = entryChunks.map(function(chunk) {
|
|
246
|
+
return chunk.id;
|
|
247
|
+
});
|
|
248
|
+
i = 0;
|
|
249
|
+
_state.label = 1;
|
|
250
|
+
case 1:
|
|
251
|
+
if (!(i <= entryChunkFiles.length - 1))
|
|
252
|
+
return [
|
|
253
|
+
3,
|
|
254
|
+
4
|
|
255
|
+
];
|
|
256
|
+
return [
|
|
257
|
+
5,
|
|
258
|
+
_ts_values(_loop(i))
|
|
259
|
+
];
|
|
260
|
+
case 2:
|
|
261
|
+
_state.sent();
|
|
262
|
+
_state.label = 3;
|
|
263
|
+
case 3:
|
|
264
|
+
i++;
|
|
265
|
+
return [
|
|
266
|
+
3,
|
|
267
|
+
1
|
|
268
|
+
];
|
|
269
|
+
case 4:
|
|
270
|
+
if (prevManifestAsset) {
|
|
271
|
+
compilation.updateAsset(
|
|
272
|
+
ROUTE_MANIFEST_FILE,
|
|
273
|
+
new RawSource(JSON.stringify(manifest, null, 2)),
|
|
274
|
+
// FIXME: The arguments third of updatgeAsset is a optional function in webpack.
|
|
275
|
+
void 0
|
|
276
|
+
);
|
|
277
|
+
} else {
|
|
278
|
+
compilation.emitAsset(ROUTE_MANIFEST_FILE, new RawSource(JSON.stringify(manifest, null, 2)));
|
|
279
|
+
}
|
|
280
|
+
return [
|
|
281
|
+
2
|
|
282
|
+
];
|
|
218
283
|
}
|
|
219
|
-
return [
|
|
220
|
-
2
|
|
221
|
-
];
|
|
222
284
|
});
|
|
223
285
|
}));
|
|
224
286
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import path from "path";
|
|
1
2
|
import { mergeWith } from "@modern-js/utils/lodash";
|
|
2
3
|
import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
|
|
3
4
|
import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
|
|
5
|
+
import { transform } from "esbuild";
|
|
4
6
|
const PLUGIN_NAME = "ModernjsRoutePlugin";
|
|
5
7
|
export class RouterPlugin {
|
|
6
8
|
isTargetNodeOrWebWorker(target) {
|
|
@@ -20,13 +22,13 @@ export class RouterPlugin {
|
|
|
20
22
|
const { webpack } = compiler;
|
|
21
23
|
const { Compilation, sources } = webpack;
|
|
22
24
|
const { RawSource, SourceMapSource } = sources;
|
|
23
|
-
const normalizePath = (
|
|
24
|
-
if (!
|
|
25
|
-
return `${
|
|
25
|
+
const normalizePath = (path2) => {
|
|
26
|
+
if (!path2.endsWith("/")) {
|
|
27
|
+
return `${path2}/`;
|
|
26
28
|
}
|
|
27
|
-
return
|
|
29
|
+
return path2;
|
|
28
30
|
};
|
|
29
|
-
const
|
|
31
|
+
const chunkToSourceAndMap = /* @__PURE__ */ new Map();
|
|
30
32
|
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|
31
33
|
compilation.hooks.processAssets.tapPromise({
|
|
32
34
|
name: PLUGIN_NAME,
|
|
@@ -35,7 +37,8 @@ export class RouterPlugin {
|
|
|
35
37
|
const stats = compilation.getStats().toJson({
|
|
36
38
|
all: false,
|
|
37
39
|
chunkGroups: true,
|
|
38
|
-
chunks: true
|
|
40
|
+
chunks: true,
|
|
41
|
+
ids: true
|
|
39
42
|
});
|
|
40
43
|
const { chunks = [], namedChunkGroups } = stats;
|
|
41
44
|
if (!namedChunkGroups) {
|
|
@@ -60,8 +63,11 @@ export class RouterPlugin {
|
|
|
60
63
|
if (!asset) {
|
|
61
64
|
continue;
|
|
62
65
|
}
|
|
63
|
-
const { map } = asset.sourceAndMap();
|
|
64
|
-
|
|
66
|
+
const { source, map } = asset.sourceAndMap();
|
|
67
|
+
chunkToSourceAndMap.set(chunkId, {
|
|
68
|
+
source,
|
|
69
|
+
map
|
|
70
|
+
});
|
|
65
71
|
}
|
|
66
72
|
});
|
|
67
73
|
compilation.hooks.processAssets.tapPromise({
|
|
@@ -109,18 +115,6 @@ export class RouterPlugin {
|
|
|
109
115
|
const manifest = {
|
|
110
116
|
routeAssets
|
|
111
117
|
};
|
|
112
|
-
const injectedContent = `
|
|
113
|
-
;(function(){
|
|
114
|
-
window.${ROUTE_MANIFEST} = ${JSON.stringify(manifest, (k, v) => {
|
|
115
|
-
if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
|
|
116
|
-
return v.map((item) => {
|
|
117
|
-
return item.replace(publicPath, "");
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
return v;
|
|
121
|
-
})};
|
|
122
|
-
})();
|
|
123
|
-
`;
|
|
124
118
|
const entrypointsArray = Array.from(compilation.entrypoints.entries());
|
|
125
119
|
const entryChunkIds = entrypointsArray.map((entrypoint) => entrypoint[0]);
|
|
126
120
|
const entryChunks = [
|
|
@@ -137,13 +131,38 @@ export class RouterPlugin {
|
|
|
137
131
|
const file = entryChunkFiles[i];
|
|
138
132
|
const chunkId = entryChunkFileIds[i];
|
|
139
133
|
const asset = compilation.assets[file];
|
|
140
|
-
if (!asset) {
|
|
134
|
+
if (!asset || !chunkId) {
|
|
141
135
|
continue;
|
|
142
136
|
}
|
|
143
|
-
const
|
|
144
|
-
|
|
137
|
+
const relatedAssets = {};
|
|
138
|
+
Object.keys(routeAssets).forEach((routeId) => {
|
|
139
|
+
if (routeId.startsWith(`${chunkId}`)) {
|
|
140
|
+
relatedAssets[routeId] = routeAssets[routeId];
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
const manifest2 = {
|
|
144
|
+
routeAssets: relatedAssets
|
|
145
|
+
};
|
|
146
|
+
const injectedContent = `
|
|
147
|
+
;(function(){
|
|
148
|
+
window.${ROUTE_MANIFEST} = ${JSON.stringify(manifest2, (k, v) => {
|
|
149
|
+
if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
|
|
150
|
+
return v.map((item) => {
|
|
151
|
+
return item.replace(publicPath, "");
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return v;
|
|
155
|
+
})};
|
|
156
|
+
})();
|
|
157
|
+
`;
|
|
158
|
+
const { source, map } = chunkToSourceAndMap.get(chunkId);
|
|
145
159
|
const newContent = `${injectedContent}${source.toString()}`;
|
|
146
|
-
const
|
|
160
|
+
const result = await transform(newContent, {
|
|
161
|
+
loader: path.extname(file).slice(1),
|
|
162
|
+
sourcemap: true,
|
|
163
|
+
minify: process.env.NODE_ENV === "production"
|
|
164
|
+
});
|
|
165
|
+
const newSource = new SourceMapSource(result.code, file, result.map, source.toString(), map);
|
|
147
166
|
compilation.updateAsset(
|
|
148
167
|
file,
|
|
149
168
|
newSource,
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.
|
|
18
|
+
"version": "2.27.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.
|
|
77
|
-
"@modern-js/builder-plugin-esbuild": "2.
|
|
78
|
-
"@modern-js/builder-plugin-node-polyfill": "2.
|
|
79
|
-
"@modern-js/builder-shared": "2.
|
|
80
|
-
"@modern-js/builder-webpack-provider": "2.
|
|
81
|
-
"@modern-js/core": "2.
|
|
82
|
-
"@modern-js/new-action": "2.
|
|
83
|
-
"@modern-js/node-bundle-require": "2.
|
|
84
|
-
"@modern-js/plugin": "2.
|
|
85
|
-
"@modern-js/plugin-data-loader": "2.
|
|
86
|
-
"@modern-js/plugin-i18n": "2.
|
|
87
|
-
"@modern-js/plugin-lint": "2.
|
|
88
|
-
"@modern-js/prod-server": "2.
|
|
89
|
-
"@modern-js/server": "2.
|
|
90
|
-
"@modern-js/types": "2.
|
|
91
|
-
"@modern-js/upgrade": "2.
|
|
92
|
-
"@modern-js/utils": "2.
|
|
93
|
-
"@modern-js/server-core": "2.
|
|
76
|
+
"@modern-js/builder": "2.27.0",
|
|
77
|
+
"@modern-js/builder-plugin-esbuild": "2.27.0",
|
|
78
|
+
"@modern-js/builder-plugin-node-polyfill": "2.27.0",
|
|
79
|
+
"@modern-js/builder-shared": "2.27.0",
|
|
80
|
+
"@modern-js/builder-webpack-provider": "2.27.0",
|
|
81
|
+
"@modern-js/core": "2.27.0",
|
|
82
|
+
"@modern-js/new-action": "2.27.0",
|
|
83
|
+
"@modern-js/node-bundle-require": "2.27.0",
|
|
84
|
+
"@modern-js/plugin": "2.27.0",
|
|
85
|
+
"@modern-js/plugin-data-loader": "2.27.0",
|
|
86
|
+
"@modern-js/plugin-i18n": "2.27.0",
|
|
87
|
+
"@modern-js/plugin-lint": "2.27.0",
|
|
88
|
+
"@modern-js/prod-server": "2.27.0",
|
|
89
|
+
"@modern-js/server": "2.27.0",
|
|
90
|
+
"@modern-js/types": "2.27.0",
|
|
91
|
+
"@modern-js/upgrade": "2.27.0",
|
|
92
|
+
"@modern-js/utils": "2.27.0",
|
|
93
|
+
"@modern-js/server-core": "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-plugin-swc": "2.
|
|
103
|
-
"@modern-js/builder-rspack-provider": "2.
|
|
104
|
-
"@scripts/
|
|
105
|
-
"@scripts/
|
|
102
|
+
"@modern-js/builder-plugin-swc": "2.27.0",
|
|
103
|
+
"@modern-js/builder-rspack-provider": "2.27.0",
|
|
104
|
+
"@scripts/build": "2.27.0",
|
|
105
|
+
"@scripts/jest-config": "2.27.0"
|
|
106
106
|
},
|
|
107
107
|
"peerDependencies": {
|
|
108
|
-
"@modern-js/builder-rspack-provider": "^2.
|
|
108
|
+
"@modern-js/builder-rspack-provider": "^2.27.0"
|
|
109
109
|
},
|
|
110
110
|
"peerDependenciesMeta": {
|
|
111
111
|
"@modern-js/builder-rspack-provider": {
|