diff options
author | 2024-04-23 07:09:33 +0200 | |
---|---|---|
committer | 2024-04-23 07:09:33 +0200 | |
commit | f13fdcd7b08e356cb5e38660995fd4cb2bb52aed (patch) | |
tree | 559689a8fc36719c7efb854f364a97a57fecd266 | |
parent | 2a3c29d11e1cc675fddbf25e3aa327db03d73113 (diff) | |
download | astro-f13fdcd7b08e356cb5e38660995fd4cb2bb52aed.tar.gz astro-f13fdcd7b08e356cb5e38660995fd4cb2bb52aed.tar.zst astro-f13fdcd7b08e356cb5e38660995fd4cb2bb52aed.zip |
fix(cloudflare): ignore client build during cleanup (#243)
Co-authored-by: Luiz Ferraz <luiz@lferraz.com>
3 files changed, 21 insertions, 2 deletions
diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 8e98a192b..a575b9b0a 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -367,8 +367,17 @@ export default function createIntegration(args?: Options): AstroIntegration { // Those modules are build only for prerendering routes. const chunksToDelete = chunkAnalyzer.getNonServerChunks(); for (const chunk of chunksToDelete) { - // Chunks are located on `./_worker.js` directory inside of the output directory - await unlink(new URL(`./_worker.js/${chunk}`, _config.outDir)); + try { + // Chunks are located on `./_worker.js` directory inside of the output directory + await unlink(new URL(`./_worker.js/${chunk}`, _config.outDir)); + } catch (error) { + logger.warn( + `Issue while trying to delete unused file from server bundle: ${new URL( + `./_worker.js/${chunk}`, + _config.outDir + ).toString()}` + ); + } } }, }, diff --git a/packages/integrations/cloudflare/src/utils/index.ts b/packages/integrations/cloudflare/src/utils/index.ts index 639e0e761..3221a25bc 100644 --- a/packages/integrations/cloudflare/src/utils/index.ts +++ b/packages/integrations/cloudflare/src/utils/index.ts @@ -22,6 +22,14 @@ export function mutatePageMapInPlace( if (arrayExpression.start && arrayExpression.end) { // @ts-expect-error - @types/estree seem to be wrong s.remove(arrayExpression.start, arrayExpression.end); + + // We need to check if there are any leftover commas, which are not part of the `ArrayExpression` node + // @ts-expect-error - @types/estree seem to be wrong + const endChar = s.slice(arrayExpression.end, arrayExpression.end + 1); + if (endChar.includes(',')) { + // @ts-expect-error - @types/estree seem to be wrong + s.remove(arrayExpression.end, arrayExpression.end + 1); + } } } } diff --git a/packages/integrations/cloudflare/src/utils/non-server-chunk-detector.ts b/packages/integrations/cloudflare/src/utils/non-server-chunk-detector.ts index c7e766802..26061c347 100644 --- a/packages/integrations/cloudflare/src/utils/non-server-chunk-detector.ts +++ b/packages/integrations/cloudflare/src/utils/non-server-chunk-detector.ts @@ -15,6 +15,8 @@ export class NonServerChunkDetector { return { name: 'non-server-chunk-detector', generateBundle: (_, bundle) => { + // Skip if we bundle for client + if (!bundle['index.js']) return; this.processBundle(bundle); }, }; |