Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround prerender in pageMap #227

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export default function createIntegration(args?: Options): AstroIntegration {

const chunkAnalyzer = new UnusedChunkAnalyzer();

const prerenderImports: string[] = [];

return {
name: '@astrojs/cloudflare',
hooks: {
Expand All @@ -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];
}
}
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
chunk.code = chunk.code.replace(importRegex, '');
if (pageId) {
const arrayRegex = new RegExp(`^^ +\\[".+", ${pageId}\\]$\\n`, 'gm');
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
chunk.code = chunk.code.replace(arrayRegex, '');
}
}
}
}
},
},
],
},
image: setImageConfig(args?.imageService ?? 'DEFAULT', config.image, command, logger),
Expand Down Expand Up @@ -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;
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved

if (typeof _config.vite.ssr?.external === 'undefined') vite.ssr.external = [];
if (typeof _config.vite.ssr?.external === 'boolean')
Expand Down
Loading