histoire
Version:
A fast story book powered by Vite
61 lines (60 loc) • 2.24 kB
JavaScript
/**
* Checks whether a module and its static dependencies are safe to share in the
* vendor chunk. Vite 8's Rolldown callback exposes dependency lookups but does
* not expose whole-graph enumeration, so this walks from the requested module.
*/
export function hasVendorDependencyClosure(id, getModuleInfo, isCandidate) {
const pending = [id];
const visited = new Set();
while (pending.length) {
const moduleId = pending.pop();
if (visited.has(moduleId))
continue;
visited.add(moduleId);
if (!isCandidate(moduleId))
return false;
const module = getModuleInfo(moduleId);
// A missing module cannot prove its dependency closure. Keep it out of
// vendor so the bundler preserves its natural ownership.
if (!module)
return false;
pending.push(...module.importedIds);
}
return true;
}
/**
* Admit only dependency-closed vendor modules. Forcing a package that imports
* generated project modules into vendor can create a chunk initialization cycle
* with Vue or other neutral dependencies. Reverse propagation also handles
* strongly connected imports without recursive traversal or partial memoization.
*/
export function createVendorModuleSet(moduleIds, getModuleInfo, isCandidate) {
const admitted = new Set();
const importers = new Map();
const blocked = new Set();
for (const id of moduleIds) {
if (isCandidate(id))
admitted.add(id);
else
blocked.add(id);
for (const dependency of getModuleInfo(id)?.importedIds ?? []) {
const parents = importers.get(dependency) ?? [];
parents.push(id);
importers.set(dependency, parents);
if (!isCandidate(dependency))
blocked.add(dependency);
}
}
const queue = [...blocked];
for (let index = 0; index < queue.length; index++) {
const id = queue[index];
admitted.delete(id);
for (const importer of importers.get(id) ?? []) {
if (blocked.has(importer))
continue;
blocked.add(importer);
queue.push(importer);
}
}
return admitted;
}