0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

refactor(plugin-analyzer): code refactor (#11307)

* refactor(plugin-analyzer): code refactor

* chore: changeset

* chore: update

* chore: update

* chore: update
This commit is contained in:
Simon He 2024-06-21 21:18:52 +08:00 committed by GitHub
parent 04121f4530
commit 89b46b8e6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -32,7 +32,7 @@ export function vitePluginAnalyzer(
): VitePlugin { ): VitePlugin {
function hoistedScriptScanner() { function hoistedScriptScanner() {
const uniqueHoistedIds = new Map<string, string>(); const uniqueHoistedIds = new Map<string, string>();
const pageScripts = new Map< const pageScriptsMap = new Map<
string, string,
{ {
hoistedSet: Set<string>; hoistedSet: Set<string>;
@ -54,20 +54,22 @@ export function vitePluginAnalyzer(
if (hoistedScripts.size) { if (hoistedScripts.size) {
for (const parentInfo of getParentModuleInfos(from, this, isPropagatedAsset)) { for (const parentInfo of getParentModuleInfos(from, this, isPropagatedAsset)) {
if (isPropagatedAsset(parentInfo.id)) { if (isPropagatedAsset(parentInfo.id)) {
if (!internals.propagatedScriptsMap.has(parentInfo.id)) {
internals.propagatedScriptsMap.set(parentInfo.id, new Set());
}
const propagatedScripts = internals.propagatedScriptsMap.get(parentInfo.id)!;
for (const hid of hoistedScripts) { for (const hid of hoistedScripts) {
if (!internals.propagatedScriptsMap.has(parentInfo.id)) { propagatedScripts.add(hid);
internals.propagatedScriptsMap.set(parentInfo.id, new Set());
}
internals.propagatedScriptsMap.get(parentInfo.id)?.add(hid);
} }
} else if (moduleIsTopLevelPage(parentInfo)) { } else if (moduleIsTopLevelPage(parentInfo)) {
if (!pageScriptsMap.has(parentInfo.id)) {
pageScriptsMap.set(parentInfo.id, {
hoistedSet: new Set(),
});
}
const pageScripts = pageScriptsMap.get(parentInfo.id)!;
for (const hid of hoistedScripts) { for (const hid of hoistedScripts) {
if (!pageScripts.has(parentInfo.id)) { pageScripts.hoistedSet.add(hid);
pageScripts.set(parentInfo.id, {
hoistedSet: new Set(),
});
}
pageScripts.get(parentInfo.id)?.hoistedSet.add(hid);
} }
} }
} }
@ -83,7 +85,7 @@ export function vitePluginAnalyzer(
} }
} }
for (const [pageId, { hoistedSet }] of pageScripts) { for (const [pageId, { hoistedSet }] of pageScriptsMap) {
const pageData = getPageDataByViteID(internals, pageId); const pageData = getPageDataByViteID(internals, pageId);
if (!pageData) continue; if (!pageData) continue;