aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar unknown <matthew@skypack.dev> 2022-06-17 16:04:50 -0400
committerGravatar unknown <matthew@skypack.dev> 2022-06-17 16:04:50 -0400
commite1712020d4e225ec33606a6629fedb17607c87b5 (patch)
treef20a8b43efd5b8f540acb5e27cecc392f2c22e49
parentb21c9576e8d2f7a6a13dddfee0d4456038162b32 (diff)
downloadastro-e1712020d4e225ec33606a6629fedb17607c87b5.tar.gz
astro-e1712020d4e225ec33606a6629fedb17607c87b5.tar.zst
astro-e1712020d4e225ec33606a6629fedb17607c87b5.zip
Fix hoisted script scanning
-rw-r--r--packages/astro/src/core/build/vite-plugin-analyzer.ts57
1 files changed, 38 insertions, 19 deletions
diff --git a/packages/astro/src/core/build/vite-plugin-analyzer.ts b/packages/astro/src/core/build/vite-plugin-analyzer.ts
index cc0fbae1d..8b8b663e6 100644
--- a/packages/astro/src/core/build/vite-plugin-analyzer.ts
+++ b/packages/astro/src/core/build/vite-plugin-analyzer.ts
@@ -18,25 +18,41 @@ export function vitePluginAnalyzer(
function hoistedScriptScanner() {
const uniqueHoistedIds = new Map<string, string>();
- return function(
- this: PluginContext,
- scripts: AstroPluginMetadata['astro']['scripts'],
- from: string
- ) {
- const hoistedScripts = new Set<string>();
- for(let i = 0; i < scripts.length; i++) {
- const hid = `${from.replace('/@fs', '')}?astro&type=script&index=${i}`;
- hoistedScripts.add(hid);
- }
+ const pageScripts = new Map<string, Set<string>>();
+
+ return {
+ scan(
+ this: PluginContext,
+ scripts: AstroPluginMetadata['astro']['scripts'],
+ from: string
+ ) {
+ const hoistedScripts = new Set<string>();
+ for(let i = 0; i < scripts.length; i++) {
+ const hid = `${from.replace('/@fs', '')}?astro&type=script&index=${i}`;
+ hoistedScripts.add(hid);
+ }
- for(const pageId of getTopLevelPages(from, this)) {
- const pageData = getPageDataByViteID(internals, pageId);
- if(!pageData) continue;
+ if (hoistedScripts.size) {
+ for(const pageId of getTopLevelPages(from, this)) {
+ for(const hid of hoistedScripts) {
+ if(pageScripts.has(pageId)) {
+ pageScripts.get(pageId)?.add(hid);
+ } else {
+ pageScripts.set(pageId, new Set([hid]));
+ }
+ }
+ }
+ }
+ },
- const { component } = pageData;
- const astroModuleId = prependForwardSlash(component);
+ finalize() {
+ for(const [pageId, hoistedScripts] of pageScripts) {
+ const pageData = getPageDataByViteID(internals, pageId);
+ if(!pageData) continue;
+
+ const { component } = pageData;
+ const astroModuleId = prependForwardSlash(component);
- if (hoistedScripts.size) {
const uniqueHoistedId = JSON.stringify(Array.from(hoistedScripts).sort());
let moduleId: string;
@@ -60,13 +76,13 @@ export function vitePluginAnalyzer(
}
}
}
- }
+ };
}
return {
name: '@astro/rollup-plugin-astro-analyzer',
generateBundle() {
- const scanHoisted = hoistedScriptScanner();
+ const hoistScanner = hoistedScriptScanner();
const ids = this.getModuleIds();
for(const id of ids) {
@@ -80,7 +96,7 @@ export function vitePluginAnalyzer(
}
// Scan hoisted scripts
- scanHoisted.call(this, astro.scripts, id);
+ hoistScanner.scan.call(this, astro.scripts, id);
if(astro.clientOnlyComponents.length) {
const clientOnlys: string[] = [];
@@ -99,6 +115,9 @@ export function vitePluginAnalyzer(
}
}
}
+
+ // Finalize hoisting
+ hoistScanner.finalize();
}
};
}