weapp-vite 7.3.0 → 7.5.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/README.md +154 -6
- package/bin/bootstrap.js +14 -1
- package/dist/adapters-CcHrsYaj.mjs +1 -0
- package/dist/analyze-ZJUvEdaY.mjs +248 -0
- package/dist/auto-import-components/resolvers.mjs +1 -2
- package/dist/auto-import-presets/wevu-router.mjs +1 -2
- package/dist/auto-import-presets/wevu.mjs +1 -2
- package/dist/auto-routes.mjs +2 -1
- package/dist/budget-BD7VqLCl.d.mts +178 -0
- package/dist/chunkStrategy-DRA5WpB-.mjs +1436 -0
- package/dist/cli-acceptance.d.mts +1 -0
- package/dist/cli-acceptance.mjs +21 -0
- package/dist/cli.mjs +1918 -2089
- package/dist/compatibility.mjs +1 -2
- package/dist/{config-1J4rToMR.d.mts → config-V0L-IAJf.d.mts} +419 -39
- package/dist/config.d.mts +2 -2
- package/dist/config.mjs +1 -1
- package/dist/constants-DEZdcsMn.mjs +85 -0
- package/dist/{createContext-cqr9Y4EV.mjs → createCompilerContextInstance-DJH_P_nZ.mjs} +17602 -15886
- package/dist/createContext-BqYzHhY9.mjs +12 -0
- package/dist/dashboard/analyze.d.mts +48 -0
- package/dist/dashboard/analyze.mjs +1 -0
- package/dist/dashboard/vite.d.mts +9 -0
- package/dist/dashboard/vite.mjs +44 -0
- package/dist/dashboard-DvqF3Key.mjs +1842 -0
- package/dist/dashboard.d.mts +3 -0
- package/dist/dashboard.mjs +1 -0
- package/dist/dashboardViteBridge-Dy3nAD4m.mjs +299 -0
- package/dist/dev-fR5GiJRO.mjs +63 -0
- package/dist/devProvider-BGBkw28J.mjs +1050 -0
- package/dist/docs/README.md +154 -6
- package/dist/docs/ai-workflows.md +4 -0
- package/dist/docs/doctor.md +38 -0
- package/dist/docs/getting-started.md +52 -1
- package/dist/docs/index.md +6 -2
- package/dist/docs/mcp.md +94 -1
- package/dist/docs/testing.md +29 -2
- package/dist/docs/troubleshooting.md +8 -0
- package/dist/docs/uni-app-component-libraries.md +8 -7
- package/dist/docs/upload.md +226 -0
- package/dist/docs/vite-plugin.md +171 -0
- package/dist/docs/vue-sfc.md +19 -0
- package/dist/docs/weapp-config.md +374 -2
- package/dist/docs/wevu-authoring.md +10 -0
- package/dist/doctor-worker.d.mts +1 -0
- package/dist/doctor-worker.mjs +59 -0
- package/dist/doctor.d.mts +144 -0
- package/dist/doctor.mjs +848 -0
- package/dist/eslint.mjs +1 -2
- package/dist/file-DRb4eWqw.mjs +1 -0
- package/dist/{file-CKB0Tn8Z.mjs → file-sC3jD7Fh.mjs} +16 -93
- package/dist/files-BqxFIPRp.mjs +45 -0
- package/dist/getInstance-DiGSzBNI.mjs +28 -0
- package/dist/getInstance-SiE6WZRh.mjs +1 -0
- package/dist/index-C9HjUX5c.d.mts +741 -0
- package/dist/index.d.mts +10 -8
- package/dist/index.mjs +3 -2
- package/dist/json.d.mts +1 -1
- package/dist/json.mjs +1 -2
- package/dist/lifecycle-BROTlwSL.mjs +320 -0
- package/dist/loadConfig-C3BQhz7F.mjs +59 -0
- package/dist/loadConfig-VGtBF4qm.mjs +1 -0
- package/dist/logger-DGn5iEVN.mjs +1 -0
- package/dist/{mcp-BG6TliEg.mjs → mcp-Cj8Maqo6.mjs} +16 -7
- package/dist/mcp-D3Yh24Cj.mjs +529 -0
- package/dist/mcp.d.mts +6 -3
- package/dist/mcp.mjs +1 -2
- package/dist/package-7swhyLmf.mjs +91 -0
- package/dist/path-doV773b7.mjs +103 -0
- package/dist/platform-CnkySBKz.mjs +175 -0
- package/dist/processCleanup-B6KA30Zc.mjs +77 -0
- package/dist/protocol-vg8HGJ4n.mjs +138 -0
- package/dist/{resolvers-icNxkpcy.mjs → resolvers-Dod-euqX.mjs} +4 -1
- package/dist/resolvers.mjs +1 -2
- package/dist/runtime.mjs +1 -2
- package/dist/session-PskhD-nZ.mjs +390 -0
- package/dist/test.d.mts +1 -0
- package/dist/test.mjs +201 -44
- package/dist/types.d.mts +2 -2
- package/dist/upload-D4msblBf.mjs +562 -0
- package/dist/upload-worker.d.mts +1 -0
- package/dist/upload-worker.mjs +161 -0
- package/dist/utils-CJSCgEj6.mjs +1094 -0
- package/dist/vite.d.mts +9 -0
- package/dist/vite.mjs +270 -0
- package/dist/volar.d.mts +2 -2
- package/dist/web-apis.mjs +1 -2
- package/package.json +85 -37
- package/dist/file-CvgkbGIx.mjs +0 -2
- package/dist/getInstance-CZ1s63zd.mjs +0 -2
- package/dist/logger-mt4mSTqV.mjs +0 -2
|
@@ -0,0 +1,1436 @@
|
|
|
1
|
+
import { a as parseSidecarModuleId, i as parseLogicalEntryId, o as parseSidecarSourceRequest } from "./protocol-vg8HGJ4n.mjs";
|
|
2
|
+
import { r as normalizeRelativePath } from "./path-doV773b7.mjs";
|
|
3
|
+
import { posix } from "pathe";
|
|
4
|
+
import { removeExtensionDeep } from "@weapp-core/shared";
|
|
5
|
+
import { Buffer } from "node:buffer";
|
|
6
|
+
import MagicString from "magic-string";
|
|
7
|
+
import remapping from "@jridgewell/remapping";
|
|
8
|
+
//#region src/runtime/chunkStrategy/state.ts
|
|
9
|
+
const sharedChunkDiagnostics = /* @__PURE__ */ new Map();
|
|
10
|
+
const takeImportersMap = /* @__PURE__ */ new Map();
|
|
11
|
+
const forceDuplicateSharedChunks = /* @__PURE__ */ new Set();
|
|
12
|
+
const sharedChunkNameCache = /* @__PURE__ */ new Map();
|
|
13
|
+
function resetTakeImportRegistry(options = {}) {
|
|
14
|
+
takeImportersMap.clear();
|
|
15
|
+
forceDuplicateSharedChunks.clear();
|
|
16
|
+
sharedChunkDiagnostics.clear();
|
|
17
|
+
if (!options.preserveSharedChunkNameCache) sharedChunkNameCache.clear();
|
|
18
|
+
}
|
|
19
|
+
function getTakeImporters(moduleId) {
|
|
20
|
+
return takeImportersMap.get(moduleId);
|
|
21
|
+
}
|
|
22
|
+
function markForceDuplicateSharedChunk(sharedName) {
|
|
23
|
+
if (!sharedName) return;
|
|
24
|
+
forceDuplicateSharedChunks.add(sharedName);
|
|
25
|
+
forceDuplicateSharedChunks.add(`${sharedName}.js`);
|
|
26
|
+
}
|
|
27
|
+
function isForceDuplicateSharedChunk(fileName) {
|
|
28
|
+
return forceDuplicateSharedChunks.has(fileName);
|
|
29
|
+
}
|
|
30
|
+
function hasForceDuplicateSharedChunks() {
|
|
31
|
+
return forceDuplicateSharedChunks.size > 0;
|
|
32
|
+
}
|
|
33
|
+
function recordSharedChunkDiagnostics(sharedName, ignoredMainImporters) {
|
|
34
|
+
if (!sharedName || ignoredMainImporters.length === 0) return;
|
|
35
|
+
const uniqueImporters = Array.from(new Set(ignoredMainImporters));
|
|
36
|
+
sharedChunkDiagnostics.set(sharedName, { ignoredMainImporters: uniqueImporters });
|
|
37
|
+
sharedChunkDiagnostics.set(`${sharedName}.js`, { ignoredMainImporters: uniqueImporters });
|
|
38
|
+
}
|
|
39
|
+
function consumeSharedChunkDiagnostics(fileName) {
|
|
40
|
+
const direct = sharedChunkDiagnostics.get(fileName);
|
|
41
|
+
if (direct) {
|
|
42
|
+
sharedChunkDiagnostics.delete(fileName);
|
|
43
|
+
return direct;
|
|
44
|
+
}
|
|
45
|
+
const withoutExt = fileName.replace(/\.[^./\\]+$/, "");
|
|
46
|
+
const fallback = sharedChunkDiagnostics.get(withoutExt);
|
|
47
|
+
if (fallback) {
|
|
48
|
+
sharedChunkDiagnostics.delete(withoutExt);
|
|
49
|
+
return fallback;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function getCachedSharedChunkName(moduleId) {
|
|
53
|
+
if (!sharedChunkNameCache.has(moduleId)) return;
|
|
54
|
+
return sharedChunkNameCache.get(moduleId) ?? null;
|
|
55
|
+
}
|
|
56
|
+
function setCachedSharedChunkName(moduleId, value) {
|
|
57
|
+
if (!moduleId) return;
|
|
58
|
+
sharedChunkNameCache.set(moduleId, value ?? null);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/utils/sourcemap.ts
|
|
62
|
+
function isEncodedSourceMapLike(value) {
|
|
63
|
+
return Boolean(value && typeof value === "object" && "version" in value && typeof value.version === "number" && "mappings" in value && "names" in value && "sources" in value);
|
|
64
|
+
}
|
|
65
|
+
function normalizeEncodedSourceMapLike(value) {
|
|
66
|
+
if (!value || typeof value !== "object") return null;
|
|
67
|
+
const sourceMap = value;
|
|
68
|
+
const numericVersion = typeof sourceMap.version === "number" ? sourceMap.version : typeof sourceMap.version === "string" ? Number(sourceMap.version) : NaN;
|
|
69
|
+
if (!Number.isFinite(numericVersion)) return null;
|
|
70
|
+
const normalized = {
|
|
71
|
+
...sourceMap,
|
|
72
|
+
version: numericVersion
|
|
73
|
+
};
|
|
74
|
+
return isEncodedSourceMapLike(normalized) ? normalized : null;
|
|
75
|
+
}
|
|
76
|
+
function composeSourceMaps(transformedMap, originalMap) {
|
|
77
|
+
if (isEncodedSourceMapLike(transformedMap) && isEncodedSourceMapLike(originalMap)) return remapping([transformedMap, originalMap], () => null);
|
|
78
|
+
if (isEncodedSourceMapLike(transformedMap)) return transformedMap;
|
|
79
|
+
if (isEncodedSourceMapLike(originalMap)) return originalMap;
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
/** 空来源名代表本轮转换输入;补齐身份和内容后再与上一层映射组合。 */
|
|
83
|
+
function labelSourceMapInput(value, fileName, code) {
|
|
84
|
+
const map = normalizeEncodedSourceMapLike(value);
|
|
85
|
+
if (!map?.sources.includes("")) return map;
|
|
86
|
+
return {
|
|
87
|
+
...map,
|
|
88
|
+
sources: map.sources.map((source) => source || fileName),
|
|
89
|
+
sourcesContent: map.sources.map((source, index) => map.sourcesContent?.[index] ?? (source === "" ? code : null))
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/utils/outputChunk.ts
|
|
94
|
+
const INLINE_SOURCE_MAP_RE = /\n?\/\/[#@]\s*sourceMappingURL=(data:application\/json\S*)\s*$/u;
|
|
95
|
+
function resolveInlineSourceMap(code) {
|
|
96
|
+
const match = INLINE_SOURCE_MAP_RE.exec(code);
|
|
97
|
+
if (typeof match?.index !== "number") return null;
|
|
98
|
+
const dataUrl = match[1];
|
|
99
|
+
const commaIndex = dataUrl.indexOf(",");
|
|
100
|
+
if (commaIndex < 0) return null;
|
|
101
|
+
try {
|
|
102
|
+
const metadata = dataUrl.slice(0, commaIndex);
|
|
103
|
+
const payload = dataUrl.slice(commaIndex + 1);
|
|
104
|
+
const json = metadata.includes(";base64") ? Buffer.from(payload, "base64").toString("utf8") : decodeURIComponent(payload);
|
|
105
|
+
const map = normalizeEncodedSourceMapLike(JSON.parse(json));
|
|
106
|
+
return map ? {
|
|
107
|
+
code: code.slice(0, match.index),
|
|
108
|
+
map,
|
|
109
|
+
start: match.index
|
|
110
|
+
} : null;
|
|
111
|
+
} catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function appendInlineSourceMap(code, map) {
|
|
116
|
+
return `${code}${code.endsWith("\n") ? "" : "\n"}//# sourceMappingURL=data:application/json;charset=utf-8;base64,${Buffer.from(JSON.stringify(map)).toString("base64")}`;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 返回不含 inline sourcemap 数据 URI 的实际脚本,供 AST 转换使用。
|
|
120
|
+
*/
|
|
121
|
+
function resolveOutputChunkTransformCode(chunk) {
|
|
122
|
+
return resolveInlineSourceMap(chunk.code)?.code ?? chunk.code;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* 应用 MagicString 改写,并把本轮映射与 chunk 既有映射组合。
|
|
126
|
+
*/
|
|
127
|
+
function applyMagicStringChunkRewrite(chunk, magicString, options) {
|
|
128
|
+
const previousCode = chunk.code;
|
|
129
|
+
const inlineSourceMap = resolveInlineSourceMap(previousCode);
|
|
130
|
+
if (inlineSourceMap) magicString.remove(inlineSourceMap.start, previousCode.length);
|
|
131
|
+
const nextCode = magicString.toString();
|
|
132
|
+
if (nextCode === (inlineSourceMap?.code ?? previousCode)) return false;
|
|
133
|
+
const previousMap = normalizeEncodedSourceMapLike(chunk.map) ?? inlineSourceMap?.map;
|
|
134
|
+
if (previousMap) {
|
|
135
|
+
const composedMap = composeSourceMaps(magicString.generateMap({
|
|
136
|
+
hires: options?.hires ?? "boundary",
|
|
137
|
+
includeContent: true,
|
|
138
|
+
source: chunk.fileName
|
|
139
|
+
}), previousMap);
|
|
140
|
+
chunk.map = composedMap;
|
|
141
|
+
chunk.code = inlineSourceMap && composedMap ? appendInlineSourceMap(nextCode, composedMap) : nextCode;
|
|
142
|
+
} else chunk.code = nextCode;
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 在 chunk 当前代码上执行一次可追踪编辑。
|
|
147
|
+
*/
|
|
148
|
+
function editOutputChunkCode(chunk, edit, options) {
|
|
149
|
+
const code = chunk.code;
|
|
150
|
+
const magicString = new MagicString(code);
|
|
151
|
+
edit(magicString, code);
|
|
152
|
+
return applyMagicStringChunkRewrite(chunk, magicString, options);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* 在 chunk 前方插入无源代码,同时保留原代码映射。
|
|
156
|
+
*/
|
|
157
|
+
function prependOutputChunkCode(chunk, code) {
|
|
158
|
+
if (!code) return false;
|
|
159
|
+
return editOutputChunkCode(chunk, (magicString) => magicString.prepend(code));
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 在 chunk 后方插入无源代码,同时保留原代码映射。
|
|
163
|
+
*/
|
|
164
|
+
function appendOutputChunkCode(chunk, code) {
|
|
165
|
+
if (!code) return false;
|
|
166
|
+
return editOutputChunkCode(chunk, (magicString) => magicString.append(code));
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 按原始代码坐标批量替换互不重叠的区间。
|
|
170
|
+
*/
|
|
171
|
+
function replaceOutputChunkCodeRanges(chunk, replacements) {
|
|
172
|
+
if (replacements.length === 0) return false;
|
|
173
|
+
const sorted = [...replacements].sort((left, right) => left.start - right.start);
|
|
174
|
+
let previousEnd = -1;
|
|
175
|
+
for (const replacement of sorted) {
|
|
176
|
+
if (replacement.start < 0 || replacement.end < replacement.start || replacement.end > chunk.code.length || replacement.start < previousEnd) throw new RangeError(`Invalid or overlapping output chunk replacement: ${replacement.start}-${replacement.end}`);
|
|
177
|
+
previousEnd = replacement.end;
|
|
178
|
+
}
|
|
179
|
+
return editOutputChunkCode(chunk, (magicString) => {
|
|
180
|
+
for (const replacement of sorted) magicString.overwrite(replacement.start, replacement.end, replacement.content);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 使用正则匹配的原始坐标改写 chunk,避免整段字符串替换破坏映射。
|
|
185
|
+
*/
|
|
186
|
+
function replaceOutputChunkCode(chunk, pattern, replace) {
|
|
187
|
+
const flags = pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`;
|
|
188
|
+
const matcher = new RegExp(pattern.source, flags);
|
|
189
|
+
const replacements = [];
|
|
190
|
+
for (const match of chunk.code.matchAll(matcher)) {
|
|
191
|
+
if (typeof match.index !== "number") continue;
|
|
192
|
+
const content = replace(match);
|
|
193
|
+
if (content == null || content === match[0]) continue;
|
|
194
|
+
replacements.push({
|
|
195
|
+
content,
|
|
196
|
+
end: match.index + match[0].length,
|
|
197
|
+
start: match.index
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return replaceOutputChunkCodeRanges(chunk, replacements);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 应用自带映射的完整代码转换,并组合 chunk 既有映射。
|
|
204
|
+
*/
|
|
205
|
+
function applyOutputChunkTransform(chunk, code, map) {
|
|
206
|
+
const previousInlineSourceMap = resolveInlineSourceMap(chunk.code);
|
|
207
|
+
const transformedInlineSourceMap = resolveInlineSourceMap(code);
|
|
208
|
+
const previousCode = previousInlineSourceMap?.code ?? chunk.code;
|
|
209
|
+
const transformedCode = transformedInlineSourceMap?.code ?? code;
|
|
210
|
+
if (transformedCode === previousCode) return false;
|
|
211
|
+
const previousMap = normalizeEncodedSourceMapLike(chunk.map) ?? previousInlineSourceMap?.map;
|
|
212
|
+
const transformedMap = normalizeEncodedSourceMapLike(map);
|
|
213
|
+
if (previousMap && !transformedMap) throw new Error(`Output chunk transform for ${chunk.fileName} must provide a source map`);
|
|
214
|
+
if (previousMap && transformedMap) {
|
|
215
|
+
const composedMap = composeSourceMaps(transformedMap, previousMap);
|
|
216
|
+
chunk.map = composedMap;
|
|
217
|
+
chunk.code = previousInlineSourceMap && composedMap ? appendInlineSourceMap(transformedCode, composedMap) : transformedCode;
|
|
218
|
+
} else chunk.code = transformedCode;
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* 将 generateBundle 阶段组合后的 chunk map 提交到 Rolldown 管理的 map asset。
|
|
223
|
+
*/
|
|
224
|
+
function syncOutputChunkSourceMapAssets(bundle) {
|
|
225
|
+
let synced = 0;
|
|
226
|
+
for (const output of Object.values(bundle)) {
|
|
227
|
+
if (output?.type !== "chunk") continue;
|
|
228
|
+
const map = normalizeEncodedSourceMapLike(output.map);
|
|
229
|
+
if (!map) continue;
|
|
230
|
+
const mapAsset = bundle[output.sourcemapFileName ?? `${output.fileName}.map`];
|
|
231
|
+
if (!mapAsset || mapAsset.type !== "asset") continue;
|
|
232
|
+
mapAsset.source = JSON.stringify(map);
|
|
233
|
+
synced += 1;
|
|
234
|
+
}
|
|
235
|
+
return synced;
|
|
236
|
+
}
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/runtime/chunkStrategy/utils.ts
|
|
239
|
+
function createRelativeImport(fromFile, toFile) {
|
|
240
|
+
const relative = posix.relative(posix.dirname(fromFile), toFile);
|
|
241
|
+
if (!relative || relative.startsWith(".")) return relative || "./";
|
|
242
|
+
return `./${relative}`;
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region src/runtime/chunkStrategy/bundle.ts
|
|
246
|
+
function escapeRegExp(value) {
|
|
247
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
248
|
+
}
|
|
249
|
+
function replaceChunkCodeAll(chunk, searchValue, replaceValue) {
|
|
250
|
+
if (!searchValue) return false;
|
|
251
|
+
const replaceExact = () => replaceOutputChunkCode(chunk, new RegExp(escapeRegExp(searchValue), "g"), () => replaceValue);
|
|
252
|
+
if (chunk.code.includes(searchValue)) return replaceExact();
|
|
253
|
+
if (searchValue.startsWith("./") && replaceValue.startsWith("./")) {
|
|
254
|
+
const trimmedSearch = searchValue.slice(2);
|
|
255
|
+
const trimmedReplace = replaceValue.slice(2);
|
|
256
|
+
if (trimmedSearch && chunk.code.includes(trimmedSearch)) return replaceOutputChunkCode(chunk, new RegExp(escapeRegExp(trimmedSearch), "g"), () => trimmedReplace);
|
|
257
|
+
}
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
function addDirectImporter(directImporters, imported, importer) {
|
|
261
|
+
if (typeof imported !== "string" || !imported || imported === importer) return;
|
|
262
|
+
let importers = directImporters.get(imported);
|
|
263
|
+
if (!importers) {
|
|
264
|
+
importers = /* @__PURE__ */ new Set();
|
|
265
|
+
directImporters.set(imported, importers);
|
|
266
|
+
}
|
|
267
|
+
importers.add(importer);
|
|
268
|
+
}
|
|
269
|
+
function removeDirectImporter(directImporters, imported, importer) {
|
|
270
|
+
const importers = directImporters.get(imported);
|
|
271
|
+
if (!importers) return;
|
|
272
|
+
importers.delete(importer);
|
|
273
|
+
if (!importers.size) directImporters.delete(imported);
|
|
274
|
+
}
|
|
275
|
+
function addCollectionImporters(directImporters, collection, importer) {
|
|
276
|
+
if (collection instanceof Set) {
|
|
277
|
+
for (const imported of collection) addDirectImporter(directImporters, imported, importer);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (Array.isArray(collection)) {
|
|
281
|
+
for (const imported of collection) addDirectImporter(directImporters, imported, importer);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (collection instanceof Map) for (const imported of collection.keys()) addDirectImporter(directImporters, imported, importer);
|
|
285
|
+
}
|
|
286
|
+
function isIdentifierChar(char) {
|
|
287
|
+
if (!char) return false;
|
|
288
|
+
const code = char.charCodeAt(0);
|
|
289
|
+
return code === 36 || code === 95 || code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
|
|
290
|
+
}
|
|
291
|
+
function readQuotedSpecifier(code, fromIndex) {
|
|
292
|
+
let index = fromIndex;
|
|
293
|
+
while (index < code.length && /\s/.test(code[index])) index += 1;
|
|
294
|
+
const quote = code[index];
|
|
295
|
+
if (quote !== "'" && quote !== "\"" && quote !== "`") return;
|
|
296
|
+
let endIndex = index + 1;
|
|
297
|
+
while (endIndex < code.length) {
|
|
298
|
+
if (code[endIndex] === quote && code[endIndex - 1] !== "\\") return code.slice(index + 1, endIndex);
|
|
299
|
+
endIndex += 1;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function collectBareImportTargets(code, targets) {
|
|
303
|
+
let searchIndex = 0;
|
|
304
|
+
while (searchIndex < code.length) {
|
|
305
|
+
const importIndex = code.indexOf("import", searchIndex);
|
|
306
|
+
if (importIndex === -1) return;
|
|
307
|
+
if (isIdentifierChar(code[importIndex - 1])) {
|
|
308
|
+
searchIndex = importIndex + 6;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const specifier = readQuotedSpecifier(code, importIndex + 6);
|
|
312
|
+
if (specifier) targets.add(specifier);
|
|
313
|
+
searchIndex = importIndex + 6;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function collectImportFromTargets(code, targets) {
|
|
317
|
+
let searchIndex = 0;
|
|
318
|
+
while (searchIndex < code.length) {
|
|
319
|
+
const importIndex = code.indexOf("import", searchIndex);
|
|
320
|
+
if (importIndex === -1) return;
|
|
321
|
+
if (isIdentifierChar(code[importIndex - 1])) {
|
|
322
|
+
searchIndex = importIndex + 6;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const fromIndex = code.indexOf("from", importIndex + 6);
|
|
326
|
+
if (fromIndex !== -1) {
|
|
327
|
+
const specifier = readQuotedSpecifier(code, fromIndex + 4);
|
|
328
|
+
if (specifier) targets.add(specifier);
|
|
329
|
+
}
|
|
330
|
+
searchIndex = importIndex + 6;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function collectExportFromTargets(code, targets) {
|
|
334
|
+
let searchIndex = 0;
|
|
335
|
+
while (searchIndex < code.length) {
|
|
336
|
+
const exportIndex = code.indexOf("export", searchIndex);
|
|
337
|
+
if (exportIndex === -1) return;
|
|
338
|
+
if (isIdentifierChar(code[exportIndex - 1])) {
|
|
339
|
+
searchIndex = exportIndex + 6;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
const fromIndex = code.indexOf("from", exportIndex + 6);
|
|
343
|
+
if (fromIndex !== -1) {
|
|
344
|
+
const specifier = readQuotedSpecifier(code, fromIndex + 4);
|
|
345
|
+
if (specifier) targets.add(specifier);
|
|
346
|
+
}
|
|
347
|
+
searchIndex = exportIndex + 6;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
function collectRequireTargets(code, targets) {
|
|
351
|
+
let searchIndex = 0;
|
|
352
|
+
while (searchIndex < code.length) {
|
|
353
|
+
const requireIndex = code.indexOf("require", searchIndex);
|
|
354
|
+
if (requireIndex === -1) return;
|
|
355
|
+
if (isIdentifierChar(code[requireIndex - 1])) {
|
|
356
|
+
searchIndex = requireIndex + 7;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const openParenIndex = code.indexOf("(", requireIndex + 7);
|
|
360
|
+
if (openParenIndex !== -1) {
|
|
361
|
+
const specifier = readQuotedSpecifier(code, openParenIndex + 1);
|
|
362
|
+
if (specifier) targets.add(specifier);
|
|
363
|
+
}
|
|
364
|
+
searchIndex = requireIndex + 7;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function collectDynamicImportTargets(code, targets) {
|
|
368
|
+
let searchIndex = 0;
|
|
369
|
+
while (searchIndex < code.length) {
|
|
370
|
+
const importIndex = code.indexOf("import(", searchIndex);
|
|
371
|
+
if (importIndex === -1) return;
|
|
372
|
+
if (isIdentifierChar(code[importIndex - 1])) {
|
|
373
|
+
searchIndex = importIndex + 7;
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
const specifier = readQuotedSpecifier(code, importIndex + 7);
|
|
377
|
+
if (specifier) targets.add(specifier);
|
|
378
|
+
searchIndex = importIndex + 7;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function collectCodeImportTargets(code, targets) {
|
|
382
|
+
collectBareImportTargets(code, targets);
|
|
383
|
+
collectImportFromTargets(code, targets);
|
|
384
|
+
collectExportFromTargets(code, targets);
|
|
385
|
+
collectRequireTargets(code, targets);
|
|
386
|
+
collectDynamicImportTargets(code, targets);
|
|
387
|
+
}
|
|
388
|
+
function addCodeImporters(codeImporters, code, importer) {
|
|
389
|
+
if (!code) return;
|
|
390
|
+
const targets = /* @__PURE__ */ new Set();
|
|
391
|
+
collectCodeImportTargets(code, targets);
|
|
392
|
+
for (const target of targets) {
|
|
393
|
+
if (!target || !target.startsWith(".")) continue;
|
|
394
|
+
addDirectImporter(codeImporters, posix.normalize(posix.join(posix.dirname(importer), target)), importer);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function removeImporterFromCodeImporters(codeImporters, importer) {
|
|
398
|
+
for (const [target, importers] of codeImporters) {
|
|
399
|
+
importers.delete(importer);
|
|
400
|
+
if (!importers.size) codeImporters.delete(target);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function replaceInArray(list, searchValues, replaceValue, shouldInsert) {
|
|
404
|
+
const values = Array.isArray(list) ? [...list] : [];
|
|
405
|
+
let replaced = false;
|
|
406
|
+
for (let index = 0; index < values.length; index++) {
|
|
407
|
+
const current = values[index];
|
|
408
|
+
if (searchValues.includes(current)) {
|
|
409
|
+
values[index] = replaceValue;
|
|
410
|
+
replaced = true;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if ((replaced || shouldInsert) && replaceValue && !values.includes(replaceValue)) values.push(replaceValue);
|
|
414
|
+
return values;
|
|
415
|
+
}
|
|
416
|
+
function updateViteMetadata(importerChunk, originalFileNames, newChunkFile, shouldInsert) {
|
|
417
|
+
const metadata = importerChunk.viteMetadata;
|
|
418
|
+
if (!metadata || typeof metadata !== "object") return;
|
|
419
|
+
for (const key of [
|
|
420
|
+
"importedChunks",
|
|
421
|
+
"importedScripts",
|
|
422
|
+
"importedScriptsByUrl"
|
|
423
|
+
]) {
|
|
424
|
+
const collection = metadata[key];
|
|
425
|
+
if (collection instanceof Set) {
|
|
426
|
+
let hadOriginal = false;
|
|
427
|
+
for (const originalFileName of originalFileNames) hadOriginal = collection.delete(originalFileName) || hadOriginal;
|
|
428
|
+
if (hadOriginal || shouldInsert) collection.add(newChunkFile);
|
|
429
|
+
} else if (Array.isArray(collection)) metadata[key] = replaceInArray(collection, originalFileNames, newChunkFile, shouldInsert);
|
|
430
|
+
else if (collection instanceof Map) {
|
|
431
|
+
let originalValue;
|
|
432
|
+
let hadOriginal = false;
|
|
433
|
+
for (const originalFileName of originalFileNames) {
|
|
434
|
+
if (!collection.has(originalFileName)) continue;
|
|
435
|
+
if (!hadOriginal) originalValue = collection.get(originalFileName);
|
|
436
|
+
hadOriginal = true;
|
|
437
|
+
collection.delete(originalFileName);
|
|
438
|
+
}
|
|
439
|
+
if (hadOriginal) collection.set(newChunkFile, originalValue);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
function createChunkImporterIndex(bundle, entries = Object.entries(bundle)) {
|
|
444
|
+
const directImporters = /* @__PURE__ */ new Map();
|
|
445
|
+
const codeImporters = /* @__PURE__ */ new Map();
|
|
446
|
+
const chunks = /* @__PURE__ */ new Map();
|
|
447
|
+
const addImporter = (target, importer) => {
|
|
448
|
+
if (!target || target === importer) return;
|
|
449
|
+
let importers = directImporters.get(target);
|
|
450
|
+
if (!importers) {
|
|
451
|
+
importers = /* @__PURE__ */ new Set();
|
|
452
|
+
directImporters.set(target, importers);
|
|
453
|
+
}
|
|
454
|
+
importers.add(importer);
|
|
455
|
+
};
|
|
456
|
+
for (const [fileName, output] of entries) {
|
|
457
|
+
if (output?.type !== "chunk") continue;
|
|
458
|
+
const chunk = output;
|
|
459
|
+
chunks.set(fileName, chunk);
|
|
460
|
+
for (const imported of chunk.imports ?? []) addImporter(imported, fileName);
|
|
461
|
+
for (const imported of chunk.dynamicImports ?? []) addImporter(imported, fileName);
|
|
462
|
+
addCodeImporters(codeImporters, chunk.code, fileName);
|
|
463
|
+
const metadata = chunk.viteMetadata;
|
|
464
|
+
if (metadata) {
|
|
465
|
+
addCollectionImporters(directImporters, metadata.importedChunks, fileName);
|
|
466
|
+
addCollectionImporters(directImporters, metadata.importedScripts ?? metadata.importedScriptsByUrl, fileName);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return {
|
|
470
|
+
directImporters,
|
|
471
|
+
codeImporters,
|
|
472
|
+
chunks
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
function removeChunkFromImporterIndex(index, fileName) {
|
|
476
|
+
index.chunks.delete(fileName);
|
|
477
|
+
index.directImporters.delete(fileName);
|
|
478
|
+
removeImporterFromCodeImporters(index.codeImporters, fileName);
|
|
479
|
+
for (const [target, importers] of index.directImporters) {
|
|
480
|
+
importers.delete(fileName);
|
|
481
|
+
if (!importers.size) index.directImporters.delete(target);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function updateChunkImporterIndex(index, importerFile, originalFileNames, newChunkFile, shouldInsert) {
|
|
485
|
+
if (!newChunkFile || originalFileNames.includes(newChunkFile)) return;
|
|
486
|
+
let hadOriginal = false;
|
|
487
|
+
for (const originalFileName of originalFileNames) {
|
|
488
|
+
hadOriginal = index.directImporters.get(originalFileName)?.has(importerFile) === true || hadOriginal;
|
|
489
|
+
removeDirectImporter(index.directImporters, originalFileName, importerFile);
|
|
490
|
+
}
|
|
491
|
+
if (hadOriginal || shouldInsert) addDirectImporter(index.directImporters, newChunkFile, importerFile);
|
|
492
|
+
}
|
|
493
|
+
function findChunkImporters(bundle, target, index = createChunkImporterIndex(bundle)) {
|
|
494
|
+
const importers = new Set(index.directImporters.get(target));
|
|
495
|
+
for (const importer of index.codeImporters.get(target) ?? []) importers.add(importer);
|
|
496
|
+
return Array.from(importers);
|
|
497
|
+
}
|
|
498
|
+
function ensureUniqueFileName(bundle, fileName) {
|
|
499
|
+
if (!bundle[fileName]) return fileName;
|
|
500
|
+
const { dir, name, ext } = posix.parse(fileName);
|
|
501
|
+
let index = 1;
|
|
502
|
+
let candidate = fileName;
|
|
503
|
+
while (bundle[candidate]) {
|
|
504
|
+
const nextName = `${name}.${index}`;
|
|
505
|
+
candidate = dir ? posix.join(dir, `${nextName}${ext}`) : `${nextName}${ext}`;
|
|
506
|
+
index += 1;
|
|
507
|
+
}
|
|
508
|
+
return candidate;
|
|
509
|
+
}
|
|
510
|
+
function updateImporters(bundle, importerToChunk, originalFileName, index) {
|
|
511
|
+
const originalFileNames = Array.isArray(originalFileName) ? originalFileName : [originalFileName];
|
|
512
|
+
for (const [importerFile, newChunkFile] of importerToChunk.entries()) {
|
|
513
|
+
const importer = bundle[importerFile];
|
|
514
|
+
if (!importer || importer.type !== "chunk") continue;
|
|
515
|
+
const importerChunk = importer;
|
|
516
|
+
const newImportPath = createRelativeImport(importerFile, newChunkFile);
|
|
517
|
+
let codeUpdated = false;
|
|
518
|
+
for (const originalFileName of originalFileNames) {
|
|
519
|
+
const originalImportPath = createRelativeImport(importerFile, originalFileName);
|
|
520
|
+
if (originalImportPath === newImportPath) continue;
|
|
521
|
+
codeUpdated = replaceChunkCodeAll(importerChunk, originalImportPath, newImportPath) || codeUpdated;
|
|
522
|
+
}
|
|
523
|
+
importerChunk.imports = replaceInArray(importerChunk.imports, originalFileNames, newChunkFile, codeUpdated);
|
|
524
|
+
importerChunk.dynamicImports = replaceInArray(importerChunk.dynamicImports, originalFileNames, newChunkFile, codeUpdated);
|
|
525
|
+
const implicitlyLoadedBefore = importerChunk.implicitlyLoadedBefore;
|
|
526
|
+
if (Array.isArray(implicitlyLoadedBefore)) importerChunk.implicitlyLoadedBefore = replaceInArray(implicitlyLoadedBefore, originalFileNames, newChunkFile, codeUpdated);
|
|
527
|
+
const referencedFiles = importerChunk.referencedFiles;
|
|
528
|
+
if (Array.isArray(referencedFiles)) importerChunk.referencedFiles = replaceInArray(referencedFiles, originalFileNames, newChunkFile, codeUpdated);
|
|
529
|
+
updateViteMetadata(importerChunk, originalFileNames, newChunkFile, codeUpdated);
|
|
530
|
+
if (index) {
|
|
531
|
+
updateChunkImporterIndex(index, importerFile, originalFileNames, newChunkFile, codeUpdated);
|
|
532
|
+
if (codeUpdated) {
|
|
533
|
+
removeImporterFromCodeImporters(index.codeImporters, importerFile);
|
|
534
|
+
addCodeImporters(index.codeImporters, importerChunk.code, importerFile);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
//#endregion
|
|
540
|
+
//#region src/runtime/chunkStrategy/collector.ts
|
|
541
|
+
function resolveChunkGraphSourceId(id) {
|
|
542
|
+
return parseLogicalEntryId(id)?.sourceId ?? parseSidecarModuleId(id)?.ownerId ?? parseSidecarSourceRequest(id)?.ownerId ?? id;
|
|
543
|
+
}
|
|
544
|
+
function resolveSubPackagePrefix(fileName, subPackageRoots) {
|
|
545
|
+
for (const root of subPackageRoots) {
|
|
546
|
+
if (!root) continue;
|
|
547
|
+
if (fileName === root || fileName.startsWith(`${root}/`)) return root;
|
|
548
|
+
}
|
|
549
|
+
return "";
|
|
550
|
+
}
|
|
551
|
+
function collectEffectivePrefixes(importer, options, state) {
|
|
552
|
+
const cached = state.cache.get(importer);
|
|
553
|
+
if (cached) return {
|
|
554
|
+
prefixes: [...cached.prefixes],
|
|
555
|
+
hasRealMain: cached.hasRealMain,
|
|
556
|
+
ignored: [...cached.ignored]
|
|
557
|
+
};
|
|
558
|
+
if (state.stack.has(importer)) return {
|
|
559
|
+
prefixes: [""],
|
|
560
|
+
hasRealMain: true,
|
|
561
|
+
ignored: []
|
|
562
|
+
};
|
|
563
|
+
state.stack.add(importer);
|
|
564
|
+
const { ctx, relativeAbsoluteSrcRoot, subPackageRoots, forceDuplicateTester } = options;
|
|
565
|
+
const sourceImporter = resolveChunkGraphSourceId(importer);
|
|
566
|
+
const relativeId = relativeAbsoluteSrcRoot(sourceImporter);
|
|
567
|
+
const subPackagePrefix = resolveSubPackagePrefix(relativeId, subPackageRoots);
|
|
568
|
+
if (subPackagePrefix) {
|
|
569
|
+
const result = {
|
|
570
|
+
prefixes: [subPackagePrefix],
|
|
571
|
+
hasRealMain: false,
|
|
572
|
+
ignored: []
|
|
573
|
+
};
|
|
574
|
+
state.cache.set(importer, result);
|
|
575
|
+
state.stack.delete(importer);
|
|
576
|
+
return {
|
|
577
|
+
prefixes: [...result.prefixes],
|
|
578
|
+
hasRealMain: result.hasRealMain,
|
|
579
|
+
ignored: []
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
const importerParents = ctx.getModuleInfo(importer)?.importers ?? [];
|
|
583
|
+
const forcedDuplicate = forceDuplicateTester?.(relativeId, sourceImporter) ?? false;
|
|
584
|
+
if (!importerParents.length) {
|
|
585
|
+
const result = forcedDuplicate ? {
|
|
586
|
+
prefixes: [],
|
|
587
|
+
hasRealMain: false,
|
|
588
|
+
ignored: [relativeId]
|
|
589
|
+
} : {
|
|
590
|
+
prefixes: [""],
|
|
591
|
+
hasRealMain: true,
|
|
592
|
+
ignored: []
|
|
593
|
+
};
|
|
594
|
+
state.cache.set(importer, result);
|
|
595
|
+
state.stack.delete(importer);
|
|
596
|
+
return {
|
|
597
|
+
prefixes: [...result.prefixes],
|
|
598
|
+
hasRealMain: result.hasRealMain,
|
|
599
|
+
ignored: [...result.ignored]
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
const aggregatedPrefixes = /* @__PURE__ */ new Set();
|
|
603
|
+
let hasRealMain = false;
|
|
604
|
+
const aggregatedIgnored = [];
|
|
605
|
+
for (const parent of importerParents) {
|
|
606
|
+
const collectorResult = collectEffectivePrefixes(parent, options, state);
|
|
607
|
+
for (const prefix of collectorResult.prefixes) aggregatedPrefixes.add(prefix);
|
|
608
|
+
if (collectorResult.hasRealMain) hasRealMain = true;
|
|
609
|
+
if (collectorResult.ignored.length) aggregatedIgnored.push(...collectorResult.ignored);
|
|
610
|
+
}
|
|
611
|
+
if (!aggregatedPrefixes.size) {
|
|
612
|
+
aggregatedPrefixes.add("");
|
|
613
|
+
hasRealMain = true;
|
|
614
|
+
}
|
|
615
|
+
const ignored = !aggregatedPrefixes.has("") && importerParents.length > 0 || forcedDuplicate && !aggregatedPrefixes.has("") ? [relativeId] : [];
|
|
616
|
+
const result = {
|
|
617
|
+
prefixes: Array.from(aggregatedPrefixes),
|
|
618
|
+
hasRealMain,
|
|
619
|
+
ignored: Array.from(/* @__PURE__ */ new Set([...aggregatedIgnored, ...ignored]))
|
|
620
|
+
};
|
|
621
|
+
state.cache.set(importer, result);
|
|
622
|
+
state.stack.delete(importer);
|
|
623
|
+
return {
|
|
624
|
+
prefixes: [...result.prefixes],
|
|
625
|
+
hasRealMain: result.hasRealMain,
|
|
626
|
+
ignored: [...result.ignored]
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
function summarizeImportPrefixes(options) {
|
|
630
|
+
const { ctx, importers, relativeAbsoluteSrcRoot, subPackageRoots, forceDuplicateTester } = options;
|
|
631
|
+
const summary = {};
|
|
632
|
+
const ignoredImporters = /* @__PURE__ */ new Set();
|
|
633
|
+
const state = {
|
|
634
|
+
cache: /* @__PURE__ */ new Map(),
|
|
635
|
+
stack: /* @__PURE__ */ new Set()
|
|
636
|
+
};
|
|
637
|
+
for (const importer of importers) {
|
|
638
|
+
const { prefixes, ignored } = collectEffectivePrefixes(importer, {
|
|
639
|
+
ctx,
|
|
640
|
+
relativeAbsoluteSrcRoot,
|
|
641
|
+
subPackageRoots,
|
|
642
|
+
forceDuplicateTester
|
|
643
|
+
}, state);
|
|
644
|
+
for (const prefix of prefixes) summary[prefix] = (summary[prefix] || 0) + 1;
|
|
645
|
+
for (const entry of ignored) ignoredImporters.add(entry);
|
|
646
|
+
}
|
|
647
|
+
return {
|
|
648
|
+
summary,
|
|
649
|
+
ignoredMainImporters: Array.from(ignoredImporters)
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function assertModuleScopedToRoot(options) {
|
|
653
|
+
const { moduleInfo, moduleRoot, relativeAbsoluteSrcRoot, subPackageRoots, moduleId } = options;
|
|
654
|
+
if (!moduleRoot || !moduleInfo?.importers?.length) return;
|
|
655
|
+
for (const importer of moduleInfo.importers) {
|
|
656
|
+
const sourceImporter = resolveChunkGraphSourceId(importer);
|
|
657
|
+
if (resolveSubPackagePrefix(relativeAbsoluteSrcRoot(sourceImporter), subPackageRoots) !== moduleRoot) {
|
|
658
|
+
const moduleLabel = relativeAbsoluteSrcRoot(moduleId);
|
|
659
|
+
const importerLabel = relativeAbsoluteSrcRoot(sourceImporter);
|
|
660
|
+
throw new Error(`[分包] 模块 "${moduleLabel}" 位于分包 "${moduleRoot}",但被 "${importerLabel}" 引用,请将该模块移动到主包或公共目录以进行跨分包共享。`);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
//#endregion
|
|
665
|
+
//#region src/runtime/chunkStrategy/sourcemap.ts
|
|
666
|
+
function isSourceLike(source) {
|
|
667
|
+
return typeof source === "string" || source instanceof Uint8Array || Buffer.isBuffer(source);
|
|
668
|
+
}
|
|
669
|
+
function cloneSourceLike(source) {
|
|
670
|
+
if (typeof source === "string") return source;
|
|
671
|
+
return Buffer.from(source);
|
|
672
|
+
}
|
|
673
|
+
function collectSourceMapKeys(fileName, chunk) {
|
|
674
|
+
const keys = /* @__PURE__ */ new Set();
|
|
675
|
+
if (fileName) keys.add(`${fileName}.map`);
|
|
676
|
+
if (typeof chunk.sourcemapFileName === "string" && chunk.sourcemapFileName) keys.add(chunk.sourcemapFileName);
|
|
677
|
+
return keys;
|
|
678
|
+
}
|
|
679
|
+
function findSourceMapAsset(bundle, candidateKeys) {
|
|
680
|
+
for (const key of candidateKeys) {
|
|
681
|
+
if (!key) continue;
|
|
682
|
+
const entry = bundle[key];
|
|
683
|
+
if (entry?.type === "asset") return {
|
|
684
|
+
asset: entry,
|
|
685
|
+
key
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
function resolveSourceMapSource(originalMap, assetSource) {
|
|
690
|
+
if (originalMap) {
|
|
691
|
+
if (typeof originalMap === "string") return originalMap;
|
|
692
|
+
if (originalMap instanceof Uint8Array || Buffer.isBuffer(originalMap)) return Buffer.from(originalMap);
|
|
693
|
+
return JSON.stringify(originalMap);
|
|
694
|
+
}
|
|
695
|
+
if (isSourceLike(assetSource)) return cloneSourceLike(assetSource);
|
|
696
|
+
}
|
|
697
|
+
function resolveEncodedSourceMap(originalMap, assetSource) {
|
|
698
|
+
const chunkMap = normalizeEncodedSourceMapLike(originalMap);
|
|
699
|
+
if (chunkMap) return chunkMap;
|
|
700
|
+
if (!isSourceLike(assetSource)) return null;
|
|
701
|
+
try {
|
|
702
|
+
const source = typeof assetSource === "string" ? assetSource : Buffer.from(assetSource).toString("utf8");
|
|
703
|
+
return normalizeEncodedSourceMapLike(JSON.parse(source));
|
|
704
|
+
} catch {
|
|
705
|
+
return null;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
function emitSourceMapAsset(ctx, targetFileName, sourceMapAssetInfo, fallbackSource) {
|
|
709
|
+
if (!targetFileName) return;
|
|
710
|
+
if (sourceMapAssetInfo?.asset && isSourceLike(sourceMapAssetInfo.asset.source)) {
|
|
711
|
+
const [name] = sourceMapAssetInfo.asset.names ?? [];
|
|
712
|
+
ctx.emitFile({
|
|
713
|
+
type: "asset",
|
|
714
|
+
fileName: targetFileName,
|
|
715
|
+
source: cloneSourceLike(sourceMapAssetInfo.asset.source),
|
|
716
|
+
...name ? { name } : {}
|
|
717
|
+
});
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
if (fallbackSource) ctx.emitFile({
|
|
721
|
+
type: "asset",
|
|
722
|
+
fileName: targetFileName,
|
|
723
|
+
source: cloneSourceLike(fallbackSource)
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
//#endregion
|
|
727
|
+
//#region src/runtime/chunkStrategy/apply/runtime.ts
|
|
728
|
+
const ROLLDOWN_RUNTIME_FILE_NAME$2 = "rolldown-runtime.js";
|
|
729
|
+
function applyRuntimeChunkLocalization(bundle, options) {
|
|
730
|
+
if (!this) throw new Error("applyRuntimeChunkLocalization 需要 PluginContext。");
|
|
731
|
+
const runtimeFileName = options.runtimeFileName ?? ROLLDOWN_RUNTIME_FILE_NAME$2;
|
|
732
|
+
const runtimeRecord = resolveRuntimeBundleRecord(bundle, runtimeFileName);
|
|
733
|
+
if (!runtimeRecord) return;
|
|
734
|
+
const { output: runtimeOutput, lookupKeys: runtimeLookupKeys } = runtimeRecord;
|
|
735
|
+
const runtimeSource = resolveRuntimeSource(runtimeOutput);
|
|
736
|
+
if (!runtimeSource) return;
|
|
737
|
+
const subPackageRoots = Array.from(options.subPackageRoots).filter(Boolean);
|
|
738
|
+
if (!subPackageRoots.length) return;
|
|
739
|
+
const runtimeLookupSet = new Set(runtimeLookupKeys);
|
|
740
|
+
const rootToImporters = /* @__PURE__ */ new Map();
|
|
741
|
+
for (const [fileName, output] of Object.entries(bundle)) {
|
|
742
|
+
if (output?.type !== "chunk") continue;
|
|
743
|
+
const root = resolveSubPackagePrefix(fileName, subPackageRoots);
|
|
744
|
+
if (!root) continue;
|
|
745
|
+
if (!chunkReferencesRuntime(output, runtimeFileName, runtimeLookupSet)) continue;
|
|
746
|
+
const existing = rootToImporters.get(root);
|
|
747
|
+
if (existing) existing.push(fileName);
|
|
748
|
+
else rootToImporters.set(root, [fileName]);
|
|
749
|
+
}
|
|
750
|
+
const forceRoots = Array.from(options.forceRoots ?? []).filter(Boolean);
|
|
751
|
+
for (const root of forceRoots) {
|
|
752
|
+
if (!subPackageRoots.includes(root)) continue;
|
|
753
|
+
if (!rootToImporters.has(root)) rootToImporters.set(root, []);
|
|
754
|
+
}
|
|
755
|
+
if (!rootToImporters.size) return;
|
|
756
|
+
const importerToRuntime = /* @__PURE__ */ new Map();
|
|
757
|
+
const duplicates = [];
|
|
758
|
+
for (const [root, importerFiles] of rootToImporters.entries()) {
|
|
759
|
+
const duplicateBaseName = posix.basename(runtimeFileName);
|
|
760
|
+
const uniqueFileName = ensureUniqueFileName(bundle, posix.join(root, duplicateBaseName));
|
|
761
|
+
this.emitFile({
|
|
762
|
+
type: "asset",
|
|
763
|
+
fileName: uniqueFileName,
|
|
764
|
+
source: cloneSourceLike(runtimeSource)
|
|
765
|
+
});
|
|
766
|
+
if (runtimeOutput.type === "chunk") options.onEmitChunk?.(uniqueFileName, runtimeOutput);
|
|
767
|
+
for (const importerFile of importerFiles) importerToRuntime.set(importerFile, uniqueFileName);
|
|
768
|
+
duplicates.push({
|
|
769
|
+
fileName: uniqueFileName,
|
|
770
|
+
importers: [...importerFiles]
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
updateImporters(bundle, importerToRuntime, runtimeLookupKeys);
|
|
774
|
+
options.onDuplicate?.({
|
|
775
|
+
runtimeFileName,
|
|
776
|
+
duplicates
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
function chunkReferencesRuntime(chunk, runtimeFileName, runtimeLookupSet) {
|
|
780
|
+
const hasRuntimeReference = (specifier) => runtimeLookupSet.has(specifier) || posix.basename(specifier) === runtimeFileName;
|
|
781
|
+
if (chunk.imports.some(hasRuntimeReference) || chunk.dynamicImports.some(hasRuntimeReference)) return true;
|
|
782
|
+
const metadata = chunk.viteMetadata;
|
|
783
|
+
if (metadata) {
|
|
784
|
+
const importedChunks = metadata.importedChunks;
|
|
785
|
+
if (hasInCollection(importedChunks, runtimeFileName)) return true;
|
|
786
|
+
for (const lookupKey of runtimeLookupSet) if (hasInCollection(importedChunks, lookupKey)) return true;
|
|
787
|
+
const importedScripts = metadata.importedScripts ?? metadata.importedScriptsByUrl;
|
|
788
|
+
if (hasInCollection(importedScripts, runtimeFileName)) return true;
|
|
789
|
+
for (const lookupKey of runtimeLookupSet) if (hasInCollection(importedScripts, lookupKey)) return true;
|
|
790
|
+
}
|
|
791
|
+
return (chunk.code ?? "").includes(runtimeFileName);
|
|
792
|
+
}
|
|
793
|
+
function resolveRuntimeBundleRecord(bundle, runtimeFileName) {
|
|
794
|
+
const direct = bundle[runtimeFileName];
|
|
795
|
+
if (isRuntimeBundleOutput(direct)) return {
|
|
796
|
+
output: direct,
|
|
797
|
+
lookupKeys: dedupeLookupKeys([runtimeFileName, resolveBundleOutputFileName(direct, runtimeFileName)])
|
|
798
|
+
};
|
|
799
|
+
for (const [key, output] of Object.entries(bundle)) {
|
|
800
|
+
if (!isRuntimeBundleOutput(output)) continue;
|
|
801
|
+
const fileName = resolveBundleOutputFileName(output, key);
|
|
802
|
+
if (fileName !== runtimeFileName && posix.basename(fileName) !== runtimeFileName) continue;
|
|
803
|
+
return {
|
|
804
|
+
output,
|
|
805
|
+
lookupKeys: dedupeLookupKeys([
|
|
806
|
+
runtimeFileName,
|
|
807
|
+
key,
|
|
808
|
+
fileName
|
|
809
|
+
])
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
function resolveRuntimeSource(output) {
|
|
814
|
+
if (output.type === "chunk") return output.code || void 0;
|
|
815
|
+
if (typeof output.source === "string" || output.source instanceof Uint8Array || Buffer.isBuffer(output.source)) return output.source;
|
|
816
|
+
if (output.source == null) return;
|
|
817
|
+
return String(output.source);
|
|
818
|
+
}
|
|
819
|
+
function hasInCollection(collection, value) {
|
|
820
|
+
if (!collection || !value) return false;
|
|
821
|
+
if (collection instanceof Set) return collection.has(value);
|
|
822
|
+
if (Array.isArray(collection)) return collection.includes(value);
|
|
823
|
+
if (collection instanceof Map) return collection.has(value);
|
|
824
|
+
return false;
|
|
825
|
+
}
|
|
826
|
+
function resolveBundleOutputFileName(output, fallback) {
|
|
827
|
+
if (typeof output.fileName === "string" && output.fileName) return output.fileName;
|
|
828
|
+
return fallback;
|
|
829
|
+
}
|
|
830
|
+
function dedupeLookupKeys(values) {
|
|
831
|
+
return Array.from(new Set(values.filter(Boolean)));
|
|
832
|
+
}
|
|
833
|
+
function isRuntimeBundleOutput(output) {
|
|
834
|
+
return output?.type === "chunk" || output?.type === "asset";
|
|
835
|
+
}
|
|
836
|
+
//#endregion
|
|
837
|
+
//#region src/runtime/chunkStrategy/constants.ts
|
|
838
|
+
const SHARED_CHUNK_VIRTUAL_PREFIX = "weapp_shared_virtual";
|
|
839
|
+
const SUB_PACKAGE_SHARED_DIR = "weapp-shared";
|
|
840
|
+
const DEFAULT_SHARED_CHUNK_STRATEGY = "hoist";
|
|
841
|
+
//#endregion
|
|
842
|
+
//#region src/runtime/chunkStrategy/apply/rewrite.ts
|
|
843
|
+
const ROLLDOWN_RUNTIME_FILE_NAME$1 = "rolldown-runtime.js";
|
|
844
|
+
const REGEXP_ESCAPE_PATTERN = /[.*+?^${}()|[\]\\]/g;
|
|
845
|
+
function createChunkSourceFileNameCandidates(fileName) {
|
|
846
|
+
const candidates = [fileName];
|
|
847
|
+
const normalized = resolveChunkSourceFileNameForRewrite(fileName);
|
|
848
|
+
if (normalized !== fileName) candidates.push(normalized);
|
|
849
|
+
return candidates;
|
|
850
|
+
}
|
|
851
|
+
function reserveUniqueFileName(reservedFileNames, fileName) {
|
|
852
|
+
if (!reservedFileNames.has(fileName)) {
|
|
853
|
+
reservedFileNames.add(fileName);
|
|
854
|
+
return fileName;
|
|
855
|
+
}
|
|
856
|
+
const { dir, name, ext } = posix.parse(fileName);
|
|
857
|
+
let index = 1;
|
|
858
|
+
let candidate = fileName;
|
|
859
|
+
while (reservedFileNames.has(candidate)) {
|
|
860
|
+
const nextName = `${name}.${index}`;
|
|
861
|
+
candidate = dir ? posix.join(dir, `${nextName}${ext}`) : `${nextName}${ext}`;
|
|
862
|
+
index += 1;
|
|
863
|
+
}
|
|
864
|
+
reservedFileNames.add(candidate);
|
|
865
|
+
return candidate;
|
|
866
|
+
}
|
|
867
|
+
function createChunkImportSpecifiersRewrite(sourceCode, options) {
|
|
868
|
+
const { sourceFileName, sourceFileNames, targetFileName, imports, dynamicImports, runtimeFileName, resolveImportTarget } = options;
|
|
869
|
+
const sourceFileNameCandidates = (sourceFileNames ?? [sourceFileName ?? ""]).filter(Boolean);
|
|
870
|
+
const specifiers = new Set([...imports, ...dynamicImports].filter(Boolean));
|
|
871
|
+
const magicString = new MagicString(sourceCode);
|
|
872
|
+
const rewrittenRanges = /* @__PURE__ */ new Set();
|
|
873
|
+
for (const specifier of specifiers) {
|
|
874
|
+
const targetImportPath = createRelativeImportPath(targetFileName, posix.basename(specifier) === ROLLDOWN_RUNTIME_FILE_NAME$1 ? runtimeFileName : resolveImportTarget?.(specifier) ?? specifier);
|
|
875
|
+
if (!targetImportPath) continue;
|
|
876
|
+
for (const sourceCandidate of sourceFileNameCandidates) {
|
|
877
|
+
const sourceImportPath = createRelativeImportPath(sourceCandidate, specifier);
|
|
878
|
+
if (!sourceImportPath || sourceImportPath === targetImportPath) continue;
|
|
879
|
+
replaceQuotedImportLiteralValue(magicString, sourceCode, sourceImportPath, targetImportPath, rewrittenRanges);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return magicString;
|
|
883
|
+
}
|
|
884
|
+
function resolveChunkSourceFileNameForRewrite(fileName) {
|
|
885
|
+
if (!fileName.startsWith(`weapp_shared_virtual/`)) return fileName;
|
|
886
|
+
return fileName.slice(21);
|
|
887
|
+
}
|
|
888
|
+
function replaceQuotedImportLiteralValue(magicString, sourceCode, sourcePath, targetPath, rewrittenRanges) {
|
|
889
|
+
const escapedSourcePath = escapeRegExpForPattern(sourcePath);
|
|
890
|
+
const pattern = new RegExp(`(['"\`])${escapedSourcePath}\\1`, "g");
|
|
891
|
+
for (const match of sourceCode.matchAll(pattern)) {
|
|
892
|
+
if (typeof match.index !== "number") continue;
|
|
893
|
+
const end = match.index + match[0].length;
|
|
894
|
+
const rangeKey = `${match.index}:${end}`;
|
|
895
|
+
if (rewrittenRanges.has(rangeKey)) continue;
|
|
896
|
+
const quote = match[1];
|
|
897
|
+
magicString.overwrite(match.index, end, `${quote}${targetPath}${quote}`);
|
|
898
|
+
rewrittenRanges.add(rangeKey);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
function escapeRegExpForPattern(value) {
|
|
902
|
+
return value.replace(REGEXP_ESCAPE_PATTERN, "\\$&");
|
|
903
|
+
}
|
|
904
|
+
function createRelativeImportPath(fromFileName, toFileName) {
|
|
905
|
+
const relativePath = posix.relative(posix.dirname(fromFileName), toFileName);
|
|
906
|
+
if (!relativePath || relativePath.startsWith(".")) return relativePath || "./";
|
|
907
|
+
return `./${relativePath}`;
|
|
908
|
+
}
|
|
909
|
+
//#endregion
|
|
910
|
+
//#region src/runtime/chunkStrategy/apply/shared/runtime.ts
|
|
911
|
+
const ROLLDOWN_RUNTIME_FILE_NAME = "rolldown-runtime.js";
|
|
912
|
+
const SLASHES_PATTERN = /[\\/]+/g;
|
|
913
|
+
function createCrossSubPackageDuplicateBaseName(sourceRoot, fileName) {
|
|
914
|
+
return `${sourceRoot.replace(SLASHES_PATTERN, "_")}.${posix.basename(fileName)}`;
|
|
915
|
+
}
|
|
916
|
+
function ensureLocalizedDuplicate(args) {
|
|
917
|
+
const { runtimeContext, pendingLocalizedDuplicates, root, sourceFileName } = args;
|
|
918
|
+
const sourceRoot = resolveSubPackagePrefix(sourceFileName, runtimeContext.subPackageRoots);
|
|
919
|
+
if (!sourceRoot || sourceRoot === root) return sourceFileName;
|
|
920
|
+
const key = `${root}::${sourceFileName}`;
|
|
921
|
+
const existing = runtimeContext.localizedDuplicateFileMap.get(key);
|
|
922
|
+
if (existing) return existing;
|
|
923
|
+
const sourceOutput = runtimeContext.bundle[sourceFileName];
|
|
924
|
+
if (sourceOutput?.type !== "chunk") return sourceFileName;
|
|
925
|
+
const duplicateBaseName = createCrossSubPackageDuplicateBaseName(sourceRoot, sourceFileName);
|
|
926
|
+
const targetFileName = reserveUniqueFileName(runtimeContext.reservedFileNames, posix.join(root, SUB_PACKAGE_SHARED_DIR, duplicateBaseName));
|
|
927
|
+
runtimeContext.localizedDuplicateFileMap.set(key, targetFileName);
|
|
928
|
+
pendingLocalizedDuplicates.push({
|
|
929
|
+
root,
|
|
930
|
+
sourceFileName,
|
|
931
|
+
targetFileName,
|
|
932
|
+
chunk: sourceOutput
|
|
933
|
+
});
|
|
934
|
+
return targetFileName;
|
|
935
|
+
}
|
|
936
|
+
function rewriteDuplicatedChunkSource(args) {
|
|
937
|
+
const runtimeFileName = posix.join(args.root, ROLLDOWN_RUNTIME_FILE_NAME);
|
|
938
|
+
const sourceMapKeys = collectSourceMapKeys(args.sourceFileName, args.chunk);
|
|
939
|
+
const sourceMapAssetInfo = findSourceMapAsset(args.runtimeContext.bundle, sourceMapKeys);
|
|
940
|
+
const rewrittenChunk = {
|
|
941
|
+
...args.chunk,
|
|
942
|
+
map: resolveEncodedSourceMap(args.chunk.map, sourceMapAssetInfo?.asset.source)
|
|
943
|
+
};
|
|
944
|
+
applyMagicStringChunkRewrite(rewrittenChunk, createChunkImportSpecifiersRewrite(rewrittenChunk.code ?? "", {
|
|
945
|
+
sourceFileNames: createChunkSourceFileNameCandidates(args.sourceFileName),
|
|
946
|
+
targetFileName: args.targetFileName,
|
|
947
|
+
imports: args.chunk.imports,
|
|
948
|
+
dynamicImports: args.chunk.dynamicImports,
|
|
949
|
+
runtimeFileName,
|
|
950
|
+
resolveImportTarget: (specifier) => {
|
|
951
|
+
return ensureLocalizedDuplicate({
|
|
952
|
+
runtimeContext: args.runtimeContext,
|
|
953
|
+
pendingLocalizedDuplicates: args.pendingLocalizedDuplicates,
|
|
954
|
+
root: args.root,
|
|
955
|
+
sourceFileName: specifier
|
|
956
|
+
});
|
|
957
|
+
}
|
|
958
|
+
}));
|
|
959
|
+
for (const specifier of [...args.chunk.imports, ...args.chunk.dynamicImports]) {
|
|
960
|
+
const localizedSpecifier = ensureLocalizedDuplicate({
|
|
961
|
+
runtimeContext: args.runtimeContext,
|
|
962
|
+
pendingLocalizedDuplicates: args.pendingLocalizedDuplicates,
|
|
963
|
+
root: args.root,
|
|
964
|
+
sourceFileName: specifier
|
|
965
|
+
});
|
|
966
|
+
if (!localizedSpecifier || localizedSpecifier === specifier) continue;
|
|
967
|
+
applyMagicStringChunkRewrite(rewrittenChunk, createChunkImportSpecifiersRewrite(rewrittenChunk.code, {
|
|
968
|
+
sourceFileName: args.targetFileName,
|
|
969
|
+
targetFileName: args.targetFileName,
|
|
970
|
+
imports: [specifier],
|
|
971
|
+
dynamicImports: [],
|
|
972
|
+
runtimeFileName,
|
|
973
|
+
resolveImportTarget: () => localizedSpecifier
|
|
974
|
+
}));
|
|
975
|
+
}
|
|
976
|
+
return rewrittenChunk;
|
|
977
|
+
}
|
|
978
|
+
function emitDuplicatedChunkAsset(args) {
|
|
979
|
+
if (args.emittedLocalizedDuplicateFiles.has(args.targetFileName)) return;
|
|
980
|
+
const duplicatedChunk = rewriteDuplicatedChunkSource({
|
|
981
|
+
runtimeContext: args.runtimeContext,
|
|
982
|
+
pendingLocalizedDuplicates: args.pendingLocalizedDuplicates,
|
|
983
|
+
root: args.root,
|
|
984
|
+
sourceFileName: args.sourceFileName,
|
|
985
|
+
targetFileName: args.targetFileName,
|
|
986
|
+
chunk: args.chunk
|
|
987
|
+
});
|
|
988
|
+
args.runtimeContext.pluginContext.emitFile({
|
|
989
|
+
type: "asset",
|
|
990
|
+
fileName: args.targetFileName,
|
|
991
|
+
source: duplicatedChunk.code
|
|
992
|
+
});
|
|
993
|
+
args.runtimeContext.onEmitChunk?.(args.targetFileName, duplicatedChunk);
|
|
994
|
+
const sourceMapKeys = collectSourceMapKeys(args.sourceFileName, args.chunk);
|
|
995
|
+
const sourceMapAssetInfo = findSourceMapAsset(args.runtimeContext.bundle, sourceMapKeys);
|
|
996
|
+
const unchangedSourceMap = duplicatedChunk.code === args.chunk.code ? resolveSourceMapSource(args.chunk.map, sourceMapAssetInfo?.asset.source) : void 0;
|
|
997
|
+
if (duplicatedChunk.map) {
|
|
998
|
+
const sourceMapFileName = reserveUniqueFileName(args.runtimeContext.reservedFileNames, `${args.targetFileName}.map`);
|
|
999
|
+
args.runtimeContext.pluginContext.emitFile({
|
|
1000
|
+
type: "asset",
|
|
1001
|
+
fileName: sourceMapFileName,
|
|
1002
|
+
source: JSON.stringify(duplicatedChunk.map)
|
|
1003
|
+
});
|
|
1004
|
+
} else if (unchangedSourceMap) {
|
|
1005
|
+
const sourceMapFileName = reserveUniqueFileName(args.runtimeContext.reservedFileNames, `${args.targetFileName}.map`);
|
|
1006
|
+
args.runtimeContext.pluginContext.emitFile({
|
|
1007
|
+
type: "asset",
|
|
1008
|
+
fileName: sourceMapFileName,
|
|
1009
|
+
source: cloneSourceLike(unchangedSourceMap)
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
args.emittedLocalizedDuplicateFiles.add(args.targetFileName);
|
|
1013
|
+
}
|
|
1014
|
+
function buildDuplicateDiagnostics(args) {
|
|
1015
|
+
const chunkBytes = typeof args.chunk.code === "string" ? Buffer.byteLength(args.chunk.code, "utf8") : void 0;
|
|
1016
|
+
return {
|
|
1017
|
+
chunkBytes,
|
|
1018
|
+
redundantBytes: typeof chunkBytes === "number" ? chunkBytes * Math.max(args.redundantDuplicateCount, 0) : void 0,
|
|
1019
|
+
requiresRuntimeLocalization: chunkReferencesRuntime(args.chunk, ROLLDOWN_RUNTIME_FILE_NAME, new Set(args.sourceRoot ? [posix.join(args.sourceRoot, ROLLDOWN_RUNTIME_FILE_NAME), ROLLDOWN_RUNTIME_FILE_NAME] : [ROLLDOWN_RUNTIME_FILE_NAME]))
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
//#endregion
|
|
1023
|
+
//#region src/runtime/chunkStrategy/apply/shared/leaks.ts
|
|
1024
|
+
function localizeCrossSubPackageChunkLeaks(bundle, options) {
|
|
1025
|
+
const { subPackageRoots, reservedFileNames, localizedDuplicateFileMap, onDuplicate } = options;
|
|
1026
|
+
if (subPackageRoots.length === 0) return;
|
|
1027
|
+
const entries = Object.entries(bundle);
|
|
1028
|
+
const importerIndex = options.importerIndex ?? createChunkImporterIndex(bundle, entries);
|
|
1029
|
+
for (const [fileName, output] of entries) {
|
|
1030
|
+
if (output?.type !== "chunk") continue;
|
|
1031
|
+
if (fileName.startsWith(`weapp_shared_virtual/`)) continue;
|
|
1032
|
+
if (posix.basename(fileName) === "rolldown-runtime.js") continue;
|
|
1033
|
+
const sourceRoot = resolveSubPackagePrefix(fileName, subPackageRoots);
|
|
1034
|
+
if (!sourceRoot) continue;
|
|
1035
|
+
const importers = findChunkImporters(bundle, fileName, importerIndex);
|
|
1036
|
+
if (importers.length === 0) continue;
|
|
1037
|
+
const crossRootImporters = /* @__PURE__ */ new Map();
|
|
1038
|
+
for (const importerFile of importers) {
|
|
1039
|
+
const importerRoot = resolveSubPackagePrefix(importerFile, subPackageRoots);
|
|
1040
|
+
if (!importerRoot || importerRoot === sourceRoot) continue;
|
|
1041
|
+
const list = crossRootImporters.get(importerRoot);
|
|
1042
|
+
if (list) list.push(importerFile);
|
|
1043
|
+
else crossRootImporters.set(importerRoot, [importerFile]);
|
|
1044
|
+
}
|
|
1045
|
+
if (crossRootImporters.size === 0) continue;
|
|
1046
|
+
const chunk = output;
|
|
1047
|
+
const sourceMapAssetInfo = findSourceMapAsset(bundle, collectSourceMapKeys(fileName, chunk));
|
|
1048
|
+
const resolvedSourceMap = resolveEncodedSourceMap(chunk.map, sourceMapAssetInfo?.asset.source);
|
|
1049
|
+
const importerToChunk = /* @__PURE__ */ new Map();
|
|
1050
|
+
const duplicates = [];
|
|
1051
|
+
for (const [targetRoot, importerFiles] of crossRootImporters.entries()) {
|
|
1052
|
+
const duplicateKey = `${targetRoot}::${fileName}`;
|
|
1053
|
+
const existingDuplicateFileName = localizedDuplicateFileMap.get(duplicateKey);
|
|
1054
|
+
const uniqueFileName = existingDuplicateFileName ?? (() => {
|
|
1055
|
+
const duplicateBaseName = createCrossSubPackageDuplicateBaseName(sourceRoot, fileName);
|
|
1056
|
+
const intendedFileName = posix.join(targetRoot, "weapp-shared", duplicateBaseName);
|
|
1057
|
+
const createdFileName = reserveUniqueFileName(reservedFileNames, intendedFileName);
|
|
1058
|
+
localizedDuplicateFileMap.set(duplicateKey, createdFileName);
|
|
1059
|
+
return createdFileName;
|
|
1060
|
+
})();
|
|
1061
|
+
const runtimeFileName = posix.join(targetRoot, ROLLDOWN_RUNTIME_FILE_NAME);
|
|
1062
|
+
if (!existingDuplicateFileName) {
|
|
1063
|
+
const duplicatedChunk = {
|
|
1064
|
+
...chunk,
|
|
1065
|
+
map: resolvedSourceMap
|
|
1066
|
+
};
|
|
1067
|
+
applyMagicStringChunkRewrite(duplicatedChunk, createChunkImportSpecifiersRewrite(duplicatedChunk.code ?? "", {
|
|
1068
|
+
sourceFileName: fileName,
|
|
1069
|
+
targetFileName: uniqueFileName,
|
|
1070
|
+
imports: chunk.imports,
|
|
1071
|
+
dynamicImports: chunk.dynamicImports,
|
|
1072
|
+
runtimeFileName
|
|
1073
|
+
}));
|
|
1074
|
+
this.emitFile({
|
|
1075
|
+
type: "asset",
|
|
1076
|
+
fileName: uniqueFileName,
|
|
1077
|
+
source: duplicatedChunk.code
|
|
1078
|
+
});
|
|
1079
|
+
options.onEmitChunk?.(uniqueFileName, duplicatedChunk);
|
|
1080
|
+
if (duplicatedChunk.map) {
|
|
1081
|
+
const sourceMapFileName = reserveUniqueFileName(reservedFileNames, `${uniqueFileName}.map`);
|
|
1082
|
+
this.emitFile({
|
|
1083
|
+
type: "asset",
|
|
1084
|
+
fileName: sourceMapFileName,
|
|
1085
|
+
source: JSON.stringify(duplicatedChunk.map)
|
|
1086
|
+
});
|
|
1087
|
+
} else if (duplicatedChunk.code === chunk.code) {
|
|
1088
|
+
const unchangedSourceMap = resolveSourceMapSource(chunk.map, sourceMapAssetInfo?.asset.source);
|
|
1089
|
+
if (unchangedSourceMap) {
|
|
1090
|
+
const sourceMapFileName = reserveUniqueFileName(reservedFileNames, `${uniqueFileName}.map`);
|
|
1091
|
+
this.emitFile({
|
|
1092
|
+
type: "asset",
|
|
1093
|
+
fileName: sourceMapFileName,
|
|
1094
|
+
source: cloneSourceLike(unchangedSourceMap)
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
for (const importerFile of importerFiles) importerToChunk.set(importerFile, uniqueFileName);
|
|
1100
|
+
duplicates.push({
|
|
1101
|
+
fileName: uniqueFileName,
|
|
1102
|
+
importers: [...importerFiles]
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
updateImporters(bundle, importerToChunk, fileName, importerIndex);
|
|
1106
|
+
const diagnostics = buildDuplicateDiagnostics({
|
|
1107
|
+
chunk,
|
|
1108
|
+
duplicates,
|
|
1109
|
+
redundantDuplicateCount: duplicates.length,
|
|
1110
|
+
sourceRoot
|
|
1111
|
+
});
|
|
1112
|
+
onDuplicate?.({
|
|
1113
|
+
sharedFileName: fileName,
|
|
1114
|
+
duplicates,
|
|
1115
|
+
requiresRuntimeLocalization: diagnostics.requiresRuntimeLocalization,
|
|
1116
|
+
chunkBytes: diagnostics.chunkBytes,
|
|
1117
|
+
redundantBytes: diagnostics.redundantBytes,
|
|
1118
|
+
retainedInMain: true
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
//#endregion
|
|
1123
|
+
//#region src/runtime/chunkStrategy/apply/shared.ts
|
|
1124
|
+
function applySharedChunkStrategy(bundle, options) {
|
|
1125
|
+
if (options.strategy !== "duplicate" && !hasForceDuplicateSharedChunks()) return;
|
|
1126
|
+
if (!this) throw new Error("applySharedChunkStrategy 需要 PluginContext。");
|
|
1127
|
+
const subPackageRoots = Array.from(options.subPackageRoots).filter(Boolean);
|
|
1128
|
+
const entries = Object.entries(bundle);
|
|
1129
|
+
if (!shouldAnalyzeSharedChunkBundle(entries, subPackageRoots)) return;
|
|
1130
|
+
const reservedFileNames = new Set(entries.map(([fileName]) => fileName));
|
|
1131
|
+
const localizedDuplicateFileMap = /* @__PURE__ */ new Map();
|
|
1132
|
+
const runtimeContext = {
|
|
1133
|
+
onEmitChunk: options.onEmitChunk,
|
|
1134
|
+
pluginContext: this,
|
|
1135
|
+
bundle,
|
|
1136
|
+
subPackageRoots,
|
|
1137
|
+
reservedFileNames,
|
|
1138
|
+
localizedDuplicateFileMap
|
|
1139
|
+
};
|
|
1140
|
+
const pendingLocalizedDuplicates = [];
|
|
1141
|
+
const emittedLocalizedDuplicateFiles = /* @__PURE__ */ new Set();
|
|
1142
|
+
const importerIndex = createChunkImporterIndex(bundle, entries);
|
|
1143
|
+
for (const [fileName, output] of entries) {
|
|
1144
|
+
if (!isSharedVirtualChunk(fileName, output)) continue;
|
|
1145
|
+
const originalSharedFileName = fileName;
|
|
1146
|
+
const chunk = output;
|
|
1147
|
+
const importers = findChunkImporters(bundle, fileName, importerIndex);
|
|
1148
|
+
if (importers.length === 0) continue;
|
|
1149
|
+
const sourceMapKeys = collectSourceMapKeys(fileName, chunk);
|
|
1150
|
+
const sourceMapAssetInfo = findSourceMapAsset(bundle, sourceMapKeys);
|
|
1151
|
+
const resolvedSourceMap = resolveEncodedSourceMap(chunk.map, sourceMapAssetInfo?.asset.source);
|
|
1152
|
+
if (resolvedSourceMap) chunk.map = resolvedSourceMap;
|
|
1153
|
+
const importerMap = /* @__PURE__ */ new Map();
|
|
1154
|
+
const mainImporters = [];
|
|
1155
|
+
let hasMainImporter = false;
|
|
1156
|
+
const shouldForceDuplicate = isForceDuplicateSharedChunk(originalSharedFileName);
|
|
1157
|
+
for (const importerFile of importers) {
|
|
1158
|
+
const root = resolveSubPackagePrefix(importerFile, subPackageRoots);
|
|
1159
|
+
if (!root) {
|
|
1160
|
+
hasMainImporter = true;
|
|
1161
|
+
mainImporters.push(importerFile);
|
|
1162
|
+
continue;
|
|
1163
|
+
}
|
|
1164
|
+
const duplicateBaseName = posix.basename(fileName);
|
|
1165
|
+
const uniqueFileName = reserveUniqueFileName(reservedFileNames, posix.join(root, SUB_PACKAGE_SHARED_DIR, duplicateBaseName));
|
|
1166
|
+
const existing = importerMap.get(root);
|
|
1167
|
+
if (existing) existing.importers.push(importerFile);
|
|
1168
|
+
else importerMap.set(root, {
|
|
1169
|
+
newFileName: uniqueFileName,
|
|
1170
|
+
importers: [importerFile]
|
|
1171
|
+
});
|
|
1172
|
+
}
|
|
1173
|
+
const importerToChunk = /* @__PURE__ */ new Map();
|
|
1174
|
+
const duplicates = [];
|
|
1175
|
+
const diagnostics = consumeSharedChunkDiagnostics(originalSharedFileName);
|
|
1176
|
+
const shouldRetainOriginalChunk = shouldForceDuplicate && hasMainImporter;
|
|
1177
|
+
if ((hasMainImporter || importerMap.size === 0) && (!shouldForceDuplicate || importerMap.size === 0)) {
|
|
1178
|
+
let finalFileName = chunk.fileName;
|
|
1179
|
+
if (fileName.startsWith(`weapp_shared_virtual/`)) {
|
|
1180
|
+
const newFileName = fileName.slice(21);
|
|
1181
|
+
const codeRewritten = applyMagicStringChunkRewrite(chunk, createChunkImportSpecifiersRewrite(chunk.code ?? "", {
|
|
1182
|
+
sourceFileNames: createChunkSourceFileNameCandidates(fileName),
|
|
1183
|
+
targetFileName: newFileName,
|
|
1184
|
+
imports: chunk.imports,
|
|
1185
|
+
dynamicImports: chunk.dynamicImports,
|
|
1186
|
+
runtimeFileName: ROLLDOWN_RUNTIME_FILE_NAME
|
|
1187
|
+
}));
|
|
1188
|
+
chunk.fileName = newFileName;
|
|
1189
|
+
if (typeof chunk.sourcemapFileName === "string" && chunk.sourcemapFileName) chunk.sourcemapFileName = `${newFileName}.map`;
|
|
1190
|
+
const targetKey = `${newFileName}.map`;
|
|
1191
|
+
emitSourceMapAsset(this, targetKey, codeRewritten ? void 0 : sourceMapAssetInfo, resolveSourceMapSource(chunk.map, void 0));
|
|
1192
|
+
for (const mapKey of sourceMapKeys) {
|
|
1193
|
+
if (!mapKey || mapKey === targetKey) continue;
|
|
1194
|
+
if (bundle[mapKey]) delete bundle[mapKey];
|
|
1195
|
+
}
|
|
1196
|
+
finalFileName = newFileName;
|
|
1197
|
+
}
|
|
1198
|
+
if (finalFileName !== originalSharedFileName) {
|
|
1199
|
+
const fallbackImporterMap = /* @__PURE__ */ new Map();
|
|
1200
|
+
for (const importerFile of importers) fallbackImporterMap.set(importerFile, finalFileName);
|
|
1201
|
+
updateImporters(bundle, fallbackImporterMap, originalSharedFileName, importerIndex);
|
|
1202
|
+
}
|
|
1203
|
+
options.onFallback?.({
|
|
1204
|
+
sharedFileName: originalSharedFileName,
|
|
1205
|
+
finalFileName,
|
|
1206
|
+
reason: hasMainImporter ? "main-package" : "no-subpackage",
|
|
1207
|
+
importers: [...importers]
|
|
1208
|
+
});
|
|
1209
|
+
continue;
|
|
1210
|
+
}
|
|
1211
|
+
if (shouldRetainOriginalChunk && fileName.startsWith(`weapp_shared_virtual/`)) {
|
|
1212
|
+
const newFileName = fileName.slice(21);
|
|
1213
|
+
const codeRewritten = applyMagicStringChunkRewrite(chunk, createChunkImportSpecifiersRewrite(chunk.code ?? "", {
|
|
1214
|
+
sourceFileNames: createChunkSourceFileNameCandidates(fileName),
|
|
1215
|
+
targetFileName: newFileName,
|
|
1216
|
+
imports: chunk.imports,
|
|
1217
|
+
dynamicImports: chunk.dynamicImports,
|
|
1218
|
+
runtimeFileName: ROLLDOWN_RUNTIME_FILE_NAME
|
|
1219
|
+
}));
|
|
1220
|
+
chunk.fileName = newFileName;
|
|
1221
|
+
if (typeof chunk.sourcemapFileName === "string" && chunk.sourcemapFileName) chunk.sourcemapFileName = `${newFileName}.map`;
|
|
1222
|
+
const targetKey = `${newFileName}.map`;
|
|
1223
|
+
emitSourceMapAsset(this, targetKey, codeRewritten ? void 0 : sourceMapAssetInfo, resolveSourceMapSource(chunk.map, void 0));
|
|
1224
|
+
for (const mapKey of sourceMapKeys) {
|
|
1225
|
+
if (!mapKey || mapKey === targetKey) continue;
|
|
1226
|
+
if (bundle[mapKey]) delete bundle[mapKey];
|
|
1227
|
+
}
|
|
1228
|
+
if (mainImporters.length) {
|
|
1229
|
+
const mainImporterMap = /* @__PURE__ */ new Map();
|
|
1230
|
+
for (const importerFile of mainImporters) mainImporterMap.set(importerFile, newFileName);
|
|
1231
|
+
updateImporters(bundle, mainImporterMap, originalSharedFileName, importerIndex);
|
|
1232
|
+
}
|
|
1233
|
+
options.onFallback?.({
|
|
1234
|
+
sharedFileName: originalSharedFileName,
|
|
1235
|
+
finalFileName: newFileName,
|
|
1236
|
+
reason: "main-package",
|
|
1237
|
+
importers: [...importers]
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
for (const [root, { newFileName, importers: importerFiles }] of importerMap.entries()) {
|
|
1241
|
+
emitDuplicatedChunkAsset({
|
|
1242
|
+
runtimeContext,
|
|
1243
|
+
pendingLocalizedDuplicates,
|
|
1244
|
+
emittedLocalizedDuplicateFiles,
|
|
1245
|
+
root,
|
|
1246
|
+
sourceFileName: fileName,
|
|
1247
|
+
targetFileName: newFileName,
|
|
1248
|
+
chunk
|
|
1249
|
+
});
|
|
1250
|
+
for (const importerFile of importerFiles) importerToChunk.set(importerFile, newFileName);
|
|
1251
|
+
duplicates.push({
|
|
1252
|
+
fileName: newFileName,
|
|
1253
|
+
importers: [...importerFiles]
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1256
|
+
updateImporters(bundle, importerToChunk, fileName, importerIndex);
|
|
1257
|
+
if (!shouldRetainOriginalChunk) {
|
|
1258
|
+
delete bundle[fileName];
|
|
1259
|
+
removeChunkFromImporterIndex(importerIndex, fileName);
|
|
1260
|
+
for (const mapKey of sourceMapKeys) if (mapKey && bundle[mapKey]) delete bundle[mapKey];
|
|
1261
|
+
}
|
|
1262
|
+
const duplicateDiagnostics = buildDuplicateDiagnostics({
|
|
1263
|
+
chunk,
|
|
1264
|
+
duplicates,
|
|
1265
|
+
redundantDuplicateCount: Math.max(duplicates.length - 1, 0)
|
|
1266
|
+
});
|
|
1267
|
+
options.onDuplicate?.({
|
|
1268
|
+
sharedFileName: originalSharedFileName,
|
|
1269
|
+
duplicates,
|
|
1270
|
+
ignoredMainImporters: diagnostics?.ignoredMainImporters,
|
|
1271
|
+
requiresRuntimeLocalization: duplicateDiagnostics.requiresRuntimeLocalization,
|
|
1272
|
+
chunkBytes: duplicateDiagnostics.chunkBytes,
|
|
1273
|
+
redundantBytes: duplicateDiagnostics.redundantBytes,
|
|
1274
|
+
retainedInMain: shouldRetainOriginalChunk
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
localizeCrossSubPackageChunkLeaks.call(this, bundle, {
|
|
1278
|
+
onEmitChunk: options.onEmitChunk,
|
|
1279
|
+
subPackageRoots,
|
|
1280
|
+
reservedFileNames,
|
|
1281
|
+
localizedDuplicateFileMap,
|
|
1282
|
+
importerIndex,
|
|
1283
|
+
onDuplicate: options.onDuplicate
|
|
1284
|
+
});
|
|
1285
|
+
for (const task of pendingLocalizedDuplicates) emitDuplicatedChunkAsset({
|
|
1286
|
+
runtimeContext,
|
|
1287
|
+
pendingLocalizedDuplicates,
|
|
1288
|
+
emittedLocalizedDuplicateFiles,
|
|
1289
|
+
...task
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
function isSharedVirtualChunk(fileName, output) {
|
|
1293
|
+
return output?.type === "chunk" && fileName.startsWith(`weapp_shared_virtual/`);
|
|
1294
|
+
}
|
|
1295
|
+
function shouldAnalyzeSharedChunkBundle(entries, subPackageRoots) {
|
|
1296
|
+
for (const [fileName, output] of entries) {
|
|
1297
|
+
if (isSharedVirtualChunk(fileName, output)) return true;
|
|
1298
|
+
if (subPackageRoots.length && output?.type === "chunk" && resolveSubPackagePrefix(fileName, subPackageRoots)) return true;
|
|
1299
|
+
}
|
|
1300
|
+
return false;
|
|
1301
|
+
}
|
|
1302
|
+
//#endregion
|
|
1303
|
+
//#region src/runtime/chunkStrategy/naming.ts
|
|
1304
|
+
function resolveTakeSharedChunkName(options) {
|
|
1305
|
+
const { ctx, relativeAbsoluteSrcRoot, subPackageRoots, importers } = options;
|
|
1306
|
+
if (!importers.length) return;
|
|
1307
|
+
const { summary } = summarizeImportPrefixes({
|
|
1308
|
+
ctx,
|
|
1309
|
+
importers,
|
|
1310
|
+
relativeAbsoluteSrcRoot,
|
|
1311
|
+
subPackageRoots
|
|
1312
|
+
});
|
|
1313
|
+
const keys = Object.keys(summary).filter(Boolean);
|
|
1314
|
+
if (!keys.length) return;
|
|
1315
|
+
const sharedName = createSharedChunkNameFromKeys(keys);
|
|
1316
|
+
markForceDuplicateSharedChunk(sharedName);
|
|
1317
|
+
return sharedName;
|
|
1318
|
+
}
|
|
1319
|
+
function createSharedChunkNameFromKeys(keys) {
|
|
1320
|
+
const sanitize = (value) => value.replace(/[\\/]+/g, "_");
|
|
1321
|
+
const combination = keys.filter(Boolean).map(sanitize).sort().join("+");
|
|
1322
|
+
const combinationSegment = combination ? `${combination}/` : "";
|
|
1323
|
+
return `${SHARED_CHUNK_VIRTUAL_PREFIX}/${combinationSegment}common`;
|
|
1324
|
+
}
|
|
1325
|
+
function resolveSharedChunkName(options) {
|
|
1326
|
+
const { id, ctx, relativeAbsoluteSrcRoot, subPackageRoots, strategy, sharedMode, resolveSharedMode, resolveSharedPath, forceDuplicateTester } = options;
|
|
1327
|
+
const cached = getCachedSharedChunkName(id);
|
|
1328
|
+
if (cached !== void 0) return cached ?? void 0;
|
|
1329
|
+
const subPackageRootList = Array.from(subPackageRoots);
|
|
1330
|
+
const moduleInfo = ctx.getModuleInfo(id);
|
|
1331
|
+
const relativeId = relativeAbsoluteSrcRoot(id);
|
|
1332
|
+
const resolvedMode = resolveSharedMode?.(relativeId, id) ?? sharedMode ?? "common";
|
|
1333
|
+
if (resolvedMode === "inline") {
|
|
1334
|
+
setCachedSharedChunkName(id, void 0);
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
if (resolvedMode === "path") {
|
|
1338
|
+
if (!moduleInfo?.importers || moduleInfo.importers.length <= 1) {
|
|
1339
|
+
setCachedSharedChunkName(id, void 0);
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
const moduleRoot = resolveSubPackagePrefix(relativeId, subPackageRootList);
|
|
1343
|
+
if (moduleRoot) assertModuleScopedToRoot({
|
|
1344
|
+
moduleInfo,
|
|
1345
|
+
moduleRoot,
|
|
1346
|
+
relativeAbsoluteSrcRoot,
|
|
1347
|
+
subPackageRoots: subPackageRootList,
|
|
1348
|
+
moduleId: id
|
|
1349
|
+
});
|
|
1350
|
+
const candidate = resolveSharedPath?.(id, relativeId) ?? relativeId;
|
|
1351
|
+
const normalized = normalizeRelativePath(candidate);
|
|
1352
|
+
if (!normalized || normalized.startsWith("..")) {
|
|
1353
|
+
setCachedSharedChunkName(id, void 0);
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
1356
|
+
const sharedName = removeExtensionDeep(normalized);
|
|
1357
|
+
if (!sharedName || sharedName === ".") {
|
|
1358
|
+
setCachedSharedChunkName(id, void 0);
|
|
1359
|
+
return;
|
|
1360
|
+
}
|
|
1361
|
+
setCachedSharedChunkName(id, sharedName);
|
|
1362
|
+
return sharedName;
|
|
1363
|
+
}
|
|
1364
|
+
const takeImporters = getTakeImporters(id);
|
|
1365
|
+
if (takeImporters?.size) {
|
|
1366
|
+
const takeSharedName = resolveTakeSharedChunkName({
|
|
1367
|
+
id,
|
|
1368
|
+
ctx,
|
|
1369
|
+
relativeAbsoluteSrcRoot,
|
|
1370
|
+
subPackageRoots: subPackageRootList,
|
|
1371
|
+
importers: Array.from(takeImporters)
|
|
1372
|
+
});
|
|
1373
|
+
if (takeSharedName) {
|
|
1374
|
+
setCachedSharedChunkName(id, takeSharedName);
|
|
1375
|
+
return takeSharedName;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
if (strategy === "hoist") {
|
|
1379
|
+
const moduleRoot = resolveSubPackagePrefix(relativeId, subPackageRootList);
|
|
1380
|
+
if (moduleRoot) {
|
|
1381
|
+
assertModuleScopedToRoot({
|
|
1382
|
+
moduleInfo,
|
|
1383
|
+
moduleRoot,
|
|
1384
|
+
relativeAbsoluteSrcRoot,
|
|
1385
|
+
subPackageRoots: subPackageRootList,
|
|
1386
|
+
moduleId: id
|
|
1387
|
+
});
|
|
1388
|
+
if (!moduleInfo?.importers || moduleInfo.importers.length <= 1) {
|
|
1389
|
+
setCachedSharedChunkName(id, void 0);
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
const sharedName = posix.join(moduleRoot, "common");
|
|
1393
|
+
setCachedSharedChunkName(id, sharedName);
|
|
1394
|
+
return sharedName;
|
|
1395
|
+
}
|
|
1396
|
+
if (!moduleInfo?.importers || moduleInfo.importers.length <= 1) {
|
|
1397
|
+
setCachedSharedChunkName(id, void 0);
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
setCachedSharedChunkName(id, "common");
|
|
1401
|
+
return "common";
|
|
1402
|
+
}
|
|
1403
|
+
if (!moduleInfo?.importers || moduleInfo.importers.length <= 1) {
|
|
1404
|
+
setCachedSharedChunkName(id, void 0);
|
|
1405
|
+
return;
|
|
1406
|
+
}
|
|
1407
|
+
const { summary, ignoredMainImporters } = summarizeImportPrefixes({
|
|
1408
|
+
ctx,
|
|
1409
|
+
importers: moduleInfo.importers,
|
|
1410
|
+
relativeAbsoluteSrcRoot,
|
|
1411
|
+
subPackageRoots: subPackageRootList,
|
|
1412
|
+
forceDuplicateTester
|
|
1413
|
+
});
|
|
1414
|
+
const keys = Object.keys(summary);
|
|
1415
|
+
if (keys.length === 0) {
|
|
1416
|
+
setCachedSharedChunkName(id, void 0);
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
if (keys.length === 1) {
|
|
1420
|
+
const prefix = keys[0];
|
|
1421
|
+
const sharedName = prefix ? posix.join(prefix, "common") : "common";
|
|
1422
|
+
setCachedSharedChunkName(id, sharedName);
|
|
1423
|
+
return sharedName;
|
|
1424
|
+
}
|
|
1425
|
+
const hasMainImporter = keys.includes("");
|
|
1426
|
+
if (strategy === "duplicate" && !hasMainImporter) {
|
|
1427
|
+
const sharedName = createSharedChunkNameFromKeys(keys);
|
|
1428
|
+
if (ignoredMainImporters.length) recordSharedChunkDiagnostics(sharedName, ignoredMainImporters);
|
|
1429
|
+
setCachedSharedChunkName(id, sharedName);
|
|
1430
|
+
return sharedName;
|
|
1431
|
+
}
|
|
1432
|
+
setCachedSharedChunkName(id, "common");
|
|
1433
|
+
return "common";
|
|
1434
|
+
}
|
|
1435
|
+
//#endregion
|
|
1436
|
+
export { syncOutputChunkSourceMapAssets as _, SUB_PACKAGE_SHARED_DIR as a, normalizeEncodedSourceMapLike as b, resolveSubPackagePrefix as c, applyOutputChunkTransform as d, editOutputChunkCode as f, resolveOutputChunkTransformCode as g, replaceOutputChunkCodeRanges as h, SHARED_CHUNK_VIRTUAL_PREFIX as i, appendOutputChunkCode as l, replaceOutputChunkCode as m, applySharedChunkStrategy as n, applyRuntimeChunkLocalization as o, prependOutputChunkCode as p, DEFAULT_SHARED_CHUNK_STRATEGY as r, assertModuleScopedToRoot as s, resolveSharedChunkName as t, applyMagicStringChunkRewrite as u, composeSourceMaps as v, resetTakeImportRegistry as x, labelSourceMapInput as y };
|