From 330d4f0012ef56f3f5d4926590ed301016a59030 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Sun, 7 Apr 2024 22:20:09 +0200 Subject: [PATCH 1/7] workaround core --- packages/cloudflare/src/index.ts | 48 +++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 32ec920c..941ab4f6 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -67,6 +67,8 @@ export default function createIntegration(args?: Options): AstroIntegration { const chunkAnalyzer = new UnusedChunkAnalyzer(); + const prerenderImports: string[] = []; + return { name: '@astrojs/cloudflare', hooks: { @@ -88,6 +90,50 @@ export default function createIntegration(args?: Options): AstroIntegration { disabled: !args?.wasmModuleImports, }), chunkAnalyzer.getPlugin(), + { + name: 'prerender-scanner', + enforce: 'post', + generateBundle(_, bundle) { + for (const chunk of Object.values(bundle)) { + if (chunk.type !== 'chunk') continue; + if (chunk.dynamicImports.some((entry) => entry.includes('prerender'))) { + prerenderImports.push(chunk.fileName); + } + } + }, + }, + { + name: 'prerender-worker', + enforce: 'post', + generateBundle(_, bundle) { + for (const chunk of Object.values(bundle)) { + if (chunk.type !== 'chunk') continue; + if (chunk.name === '_@astrojs-ssr-virtual-entry') { + chunk.dynamicImports = chunk.dynamicImports.filter( + (entry) => !prerenderImports.includes(entry) + ); + for (const page of prerenderImports) { + const importRegex = new RegExp( + `^const (_page\\d) = \\(\\) => import\\('.\\/${page}'\\);$\\n`, + 'gm' + ); + let pageId: string | undefined; + const matches = chunk.code.matchAll(importRegex); + for (const match of matches) { + if (match[1]) { + pageId = match[1]; + } + } + chunk.code = chunk.code.replace(importRegex, ''); + if (pageId) { + const arrayRegex = new RegExp(`^^ +\\[".+", ${pageId}\\]$\\n`, 'gm'); + chunk.code = chunk.code.replace(arrayRegex, ''); + } + } + } + } + }, + }, ], }, image: setImageConfig(args?.imageService ?? 'DEFAULT', config.image, command, logger), @@ -173,7 +219,7 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr ||= {}; vite.ssr.target = 'webworker'; - vite.ssr.noExternal = true; + vite.ssr.noExternal ||= true; if (typeof _config.vite.ssr?.external === 'undefined') vite.ssr.external = []; if (typeof _config.vite.ssr?.external === 'boolean') From 3327108bf96f1f26ac22884b86e3b4089483a60a Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Tue, 9 Apr 2024 07:15:58 +0200 Subject: [PATCH 2/7] fixes typo --- packages/cloudflare/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 941ab4f6..a28935f5 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -126,7 +126,7 @@ export default function createIntegration(args?: Options): AstroIntegration { } chunk.code = chunk.code.replace(importRegex, ''); if (pageId) { - const arrayRegex = new RegExp(`^^ +\\[".+", ${pageId}\\]$\\n`, 'gm'); + const arrayRegex = new RegExp(`^ +\\[".+", ${pageId}\\]$\\n`, 'gm'); chunk.code = chunk.code.replace(arrayRegex, ''); } } From 32d8f4372e92adc3c19ef15735e429f1f3342d2b Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Tue, 9 Apr 2024 07:22:02 +0200 Subject: [PATCH 3/7] combines into one plugin --- packages/cloudflare/src/index.ts | 54 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index a28935f5..6bdf09ed 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -94,43 +94,43 @@ export default function createIntegration(args?: Options): AstroIntegration { name: 'prerender-scanner', enforce: 'post', generateBundle(_, bundle) { + // Loop through all chunks and find out which pages are prerendered for (const chunk of Object.values(bundle)) { if (chunk.type !== 'chunk') continue; if (chunk.dynamicImports.some((entry) => entry.includes('prerender'))) { prerenderImports.push(chunk.fileName); } } - }, - }, - { - name: 'prerender-worker', - enforce: 'post', - generateBundle(_, bundle) { - for (const chunk of Object.values(bundle)) { - if (chunk.type !== 'chunk') continue; - if (chunk.name === '_@astrojs-ssr-virtual-entry') { - chunk.dynamicImports = chunk.dynamicImports.filter( - (entry) => !prerenderImports.includes(entry) + + const entryChunk = bundle['index.js']; + if ( + entryChunk && + entryChunk.type === 'chunk' && + entryChunk.name === '_@astrojs-ssr-virtual-entry' + ) { + entryChunk.dynamicImports = entryChunk.dynamicImports.filter( + (entry) => !prerenderImports.includes(entry) + ); + for (const page of prerenderImports) { + const importRegex = new RegExp( + `^const (_page\\d) = \\(\\) => import\\('.\\/${page}'\\);$\\n`, + 'gm' ); - for (const page of prerenderImports) { - const importRegex = new RegExp( - `^const (_page\\d) = \\(\\) => import\\('.\\/${page}'\\);$\\n`, - 'gm' - ); - let pageId: string | undefined; - const matches = chunk.code.matchAll(importRegex); - for (const match of matches) { - if (match[1]) { - pageId = match[1]; - } - } - chunk.code = chunk.code.replace(importRegex, ''); - if (pageId) { - const arrayRegex = new RegExp(`^ +\\[".+", ${pageId}\\]$\\n`, 'gm'); - chunk.code = chunk.code.replace(arrayRegex, ''); + let pageId: string | undefined; + const matches = entryChunk.code.matchAll(importRegex); + for (const match of matches) { + if (match[1]) { + pageId = match[1]; } } + entryChunk.code = entryChunk.code.replace(importRegex, ''); + if (pageId) { + const arrayRegex = new RegExp(`^ +\\[".+", ${pageId}\\]$\\n`, 'gm'); + entryChunk.code = entryChunk.code.replace(arrayRegex, ''); + } } + } else { + logger.error("Couldn't find the virtual entry chunk"); } }, }, From aa319bd9e28fe9ab2fa2515d29e556d55ef5a3e7 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Wed, 10 Apr 2024 10:32:03 +0200 Subject: [PATCH 4/7] use safer regex --- packages/cloudflare/src/index.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 6bdf09ed..8f8fecc8 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -67,7 +67,7 @@ export default function createIntegration(args?: Options): AstroIntegration { const chunkAnalyzer = new UnusedChunkAnalyzer(); - const prerenderImports: string[] = []; + const prerenderImports: string[][] = []; return { name: '@astrojs/cloudflare', @@ -98,7 +98,7 @@ export default function createIntegration(args?: Options): AstroIntegration { for (const chunk of Object.values(bundle)) { if (chunk.type !== 'chunk') continue; if (chunk.dynamicImports.some((entry) => entry.includes('prerender'))) { - prerenderImports.push(chunk.fileName); + prerenderImports.push([chunk.facadeModuleId ?? '', chunk.fileName]); } } @@ -109,13 +109,14 @@ export default function createIntegration(args?: Options): AstroIntegration { entryChunk.name === '_@astrojs-ssr-virtual-entry' ) { entryChunk.dynamicImports = entryChunk.dynamicImports.filter( - (entry) => !prerenderImports.includes(entry) + (entry) => !prerenderImports.map((e) => e[1]).includes(entry) ); for (const page of prerenderImports) { const importRegex = new RegExp( - `^const (_page\\d) = \\(\\) => import\\('.\\/${page}'\\);$\\n`, + `^const (_page\\d) = \\(\\) => import\\('.\\/${page[1]}'\\);$\\n`, 'gm' ); + let pageId: string | undefined; const matches = entryChunk.code.matchAll(importRegex); for (const match of matches) { @@ -123,9 +124,10 @@ export default function createIntegration(args?: Options): AstroIntegration { pageId = match[1]; } } + const pageSource = page[0].split(':')[1].replace('@_@', '.'); entryChunk.code = entryChunk.code.replace(importRegex, ''); if (pageId) { - const arrayRegex = new RegExp(`^ +\\[".+", ${pageId}\\]$\\n`, 'gm'); + const arrayRegex = new RegExp(`\\["${pageSource}", ${pageId}\\]`, 'gm'); entryChunk.code = entryChunk.code.replace(arrayRegex, ''); } } From ae51601a6e24fa908a10712caa82e5326fce108d Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Wed, 10 Apr 2024 10:40:43 +0200 Subject: [PATCH 5/7] use safer regex --- packages/cloudflare/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 8f8fecc8..a1887fbc 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -127,7 +127,7 @@ export default function createIntegration(args?: Options): AstroIntegration { const pageSource = page[0].split(':')[1].replace('@_@', '.'); entryChunk.code = entryChunk.code.replace(importRegex, ''); if (pageId) { - const arrayRegex = new RegExp(`\\["${pageSource}", ${pageId}\\]`, 'gm'); + const arrayRegex = new RegExp(`\\["${pageSource}", ?${pageId}\\],?`, 'gm'); entryChunk.code = entryChunk.code.replace(arrayRegex, ''); } } From 5112757c2cac0578f4a5457fec1411cddbf2f661 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Wed, 10 Apr 2024 10:47:47 +0200 Subject: [PATCH 6/7] ignore ssr entry --- packages/cloudflare/src/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index a1887fbc..1f3e7128 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -97,7 +97,13 @@ export default function createIntegration(args?: Options): AstroIntegration { // Loop through all chunks and find out which pages are prerendered for (const chunk of Object.values(bundle)) { if (chunk.type !== 'chunk') continue; - if (chunk.dynamicImports.some((entry) => entry.includes('prerender'))) { + if ( + chunk.dynamicImports.some( + (entry) => + entry.includes('prerender') && + chunk.name !== '_@astrojs-ssr-virtual-entry' + ) + ) { prerenderImports.push([chunk.facadeModuleId ?? '', chunk.fileName]); } } From 9277080fdedba100a5e8ff86da1975e10573316a Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Wed, 10 Apr 2024 10:48:57 +0200 Subject: [PATCH 7/7] Update packages/cloudflare/src/index.ts --- packages/cloudflare/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 1f3e7128..ced5b6b7 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -227,7 +227,7 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr ||= {}; vite.ssr.target = 'webworker'; - vite.ssr.noExternal ||= true; + vite.ssr.noExternal = true; if (typeof _config.vite.ssr?.external === 'undefined') vite.ssr.external = []; if (typeof _config.vite.ssr?.external === 'boolean')