Skip to content

Commit

Permalink
fix(cloudflare): node_modules on prerender pages only (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed May 11, 2024
1 parent 8b06b1c commit de6f3eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-beds-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Fixes chunk issues when bundling node_modules dependencies
21 changes: 21 additions & 0 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ export default function createIntegration(args?: Options): AstroIntegration {
vite.build.rollupOptions.output.banner ||=
'globalThis.process ??= {}; globalThis.process.env ??= {};';

// @ts-expect-error
vite.build.rollupOptions.output.manualChunks = (id: string) => {
if (id.includes('node_modules')) {
if (id.indexOf('node_modules') !== -1) {
const basic = id.toString().split('node_modules/')[1];
const sub1 = basic.split('/')[0];
if (sub1 !== '.pnpm') {
return sub1.toString();
}
const name2 = basic.split('/')[1];
return name2.split('@')[name2[0] === '@' ? 1 : 0].toString();
}
}
};

vite.build.rollupOptions.external = _config.vite.build?.rollupOptions?.external ?? [];

// Cloudflare env is only available per request. This isn't feasible for code that access env vars
Expand All @@ -278,6 +293,12 @@ export default function createIntegration(args?: Options): AstroIntegration {
vite.resolve.conditions = vite.resolve.conditions.filter(
(c) => c !== 'workerd' && c !== 'worker'
);

vite.build ||= {};
vite.build.rollupOptions ||= {};
vite.build.rollupOptions.output ||= {};
// @ts-expect-error
vite.build.rollupOptions.output.manualChunks = undefined;
}
},
'astro:build:done': async ({ pages, routes, dir, logger }) => {
Expand Down

0 comments on commit de6f3eb

Please sign in to comment.