@astrojs/cloudflare 10.0.3 → 10.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { AstroIntegration } from 'astro';
2
2
  export type { Runtime } from './entrypoints/server.js';
3
3
  export type Options = {
4
4
  /** Options for handling images. */
5
- imageService?: 'passthrough' | 'cloudflare' | 'compile';
5
+ imageService?: 'passthrough' | 'cloudflare' | 'compile' | 'custom';
6
6
  /** Configuration for `_routes.json` generation. A _routes.json file controls when your Function is invoked. This file will include three different properties:
7
7
  *
8
8
  * - version: Defines the version of the schema. Currently there is only one version of the schema (version 1), however, we may add more in the future and aim to be backwards compatible.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createReadStream } from 'node:fs';
2
- import { appendFile, rename, stat } from 'node:fs/promises';
2
+ import { appendFile, rename, stat, unlink } from 'node:fs/promises';
3
3
  import { createInterface } from 'node:readline/promises';
4
4
  import { appendForwardSlash, prependForwardSlash, removeLeadingForwardSlash, } from '@astrojs/internal-helpers/path';
5
5
  import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
@@ -7,9 +7,15 @@ import { AstroError } from 'astro/errors';
7
7
  import { getPlatformProxy } from 'wrangler';
8
8
  import { createRoutesFile, getParts } from './utils/generate-routes-json.js';
9
9
  import { setImageConfig } from './utils/image-config.js';
10
+ import { NonServerChunkDetector } from './utils/non-server-chunk-detector.js';
10
11
  import { wasmModuleLoader } from './utils/wasm-module-loader.js';
11
12
  export default function createIntegration(args) {
12
13
  let _config;
14
+ // Initialize the unused chunk analyzer as a shared state between hooks.
15
+ // The analyzer is used on earlier hooks to collect information about used hooks on a Vite plugin
16
+ // and then later after the full build to clean up unused chunks, so it has to be shared between them.
17
+ const chunkAnalyzer = new NonServerChunkDetector();
18
+ const prerenderImports = [];
13
19
  return {
14
20
  name: '@astrojs/cloudflare',
15
21
  hooks: {
@@ -27,6 +33,51 @@ export default function createIntegration(args) {
27
33
  wasmModuleLoader({
28
34
  disabled: !args?.wasmModuleImports,
29
35
  }),
36
+ chunkAnalyzer.getPlugin(),
37
+ {
38
+ name: 'dynamic-imports-analyzer',
39
+ enforce: 'post',
40
+ generateBundle(_, bundle) {
41
+ // Find all pages (ignore the ssr entrypoint) which are prerendered based on the dynamic imports of the prerender chunk
42
+ for (const chunk of Object.values(bundle)) {
43
+ if (chunk.type !== 'chunk')
44
+ continue;
45
+ const isPrerendered = chunk.dynamicImports.some((entry) => entry.includes('prerender') && chunk.name !== '_@astrojs-ssr-virtual-entry');
46
+ if (isPrerendered) {
47
+ prerenderImports.push([chunk.facadeModuleId ?? '', chunk.fileName]);
48
+ }
49
+ }
50
+ const entryChunk = bundle['index.js'];
51
+ if (entryChunk &&
52
+ entryChunk.type === 'chunk' &&
53
+ entryChunk.name === '_@astrojs-ssr-virtual-entry') {
54
+ // Update dynamicImports information, so that there are no imports listed which we remove later
55
+ entryChunk.dynamicImports = entryChunk.dynamicImports.filter((entry) => !prerenderImports.map((e) => e[1]).includes(entry));
56
+ // Clean the ssr entry file from prerendered imporst, since Astro adds them, which it shouldn't. But this is a current limitation in core, because the prerender meta info gets added later in the chain
57
+ for (const page of prerenderImports) {
58
+ // Find the dynamic import inside of the ssr entry file, which get generated by Astro: https://github.com/withastro/astro/blob/08cdd0919d3249a762822e4bba9e0c5d3966916c/packages/astro/src/core/build/plugins/plugin-ssr.ts#L56
59
+ const importRegex = new RegExp(`^const (_page\\d) = \\(\\) => import\\('.\\/${page[1]}'\\);$\\n`, 'gm');
60
+ let pageId;
61
+ const matches = entryChunk.code.matchAll(importRegex);
62
+ for (const match of matches) {
63
+ if (match[1]) {
64
+ pageId = match[1];
65
+ }
66
+ }
67
+ const pageSource = page[0].split(':')[1].replace('@_@', '.');
68
+ entryChunk.code = entryChunk.code.replace(importRegex, '');
69
+ if (pageId) {
70
+ // Find the page in the pageMap of the ssr entry file, which get generated by Astro: https://github.com/withastro/astro/blob/08cdd0919d3249a762822e4bba9e0c5d3966916c/packages/astro/src/core/build/plugins/plugin-ssr.ts#L65
71
+ const arrayRegex = new RegExp(`\\["${pageSource}", ?${pageId}\\],?`, 'gm');
72
+ entryChunk.code = entryChunk.code.replace(arrayRegex, '');
73
+ }
74
+ }
75
+ }
76
+ else {
77
+ // We don't want to handle this case, since it will always occur for the client build.
78
+ }
79
+ },
80
+ },
30
81
  ],
31
82
  },
32
83
  image: setImageConfig(args?.imageService ?? 'DEFAULT', config.image, command, logger),
@@ -217,6 +268,13 @@ export default function createIntegration(args) {
217
268
  logger.error('Failed to write _redirects file');
218
269
  }
219
270
  }
271
+ // Get chunks from the bundle that are not needed on the server and delete them
272
+ // Those modules are build only for prerendering routes.
273
+ const chunksToDelete = chunkAnalyzer.getNonServerChunks();
274
+ for (const chunk of chunksToDelete) {
275
+ // Chunks are located on `./_worker.js` directory inside of the output directory
276
+ await unlink(new URL(`./_worker.js/${chunk}`, _config.outDir));
277
+ }
220
278
  },
221
279
  },
222
280
  };
@@ -16,6 +16,8 @@ export function setImageConfig(service, config, command, logger) {
16
16
  service: sharpImageService(),
17
17
  endpoint: command === 'dev' ? undefined : '@astrojs/cloudflare/image-endpoint',
18
18
  };
19
+ case 'custom':
20
+ return { ...config };
19
21
  default:
20
22
  if (config.service.entrypoint === 'astro/assets/services/sharp' ||
21
23
  config.service.entrypoint === 'astro/assets/services/squoosh') {
@@ -0,0 +1,14 @@
1
+ import type { Plugin } from 'vite';
2
+ /**
3
+ * A Vite bundle analyzer that identifies chunks that are not used for server rendering.
4
+ *
5
+ * The chunks injected by Astro for prerendering are flagged as non-server chunks.
6
+ * Any chunks that is only used by a non-server chunk are also flagged as non-server chunks.
7
+ * This continues transitively until all non-server chunks are found.
8
+ */
9
+ export declare class NonServerChunkDetector {
10
+ private nonServerChunks?;
11
+ getPlugin(): Plugin;
12
+ private processBundle;
13
+ getNonServerChunks(): string[];
14
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * A Vite bundle analyzer that identifies chunks that are not used for server rendering.
3
+ *
4
+ * The chunks injected by Astro for prerendering are flagged as non-server chunks.
5
+ * Any chunks that is only used by a non-server chunk are also flagged as non-server chunks.
6
+ * This continues transitively until all non-server chunks are found.
7
+ */
8
+ export class NonServerChunkDetector {
9
+ nonServerChunks;
10
+ getPlugin() {
11
+ return {
12
+ name: 'non-server-chunk-detector',
13
+ generateBundle: (_, bundle) => {
14
+ this.processBundle(bundle);
15
+ },
16
+ };
17
+ }
18
+ processBundle(bundle) {
19
+ const chunkNamesToFiles = new Map();
20
+ const entryChunks = [];
21
+ const chunkToDependencies = new Map();
22
+ for (const chunk of Object.values(bundle)) {
23
+ if (chunk.type !== 'chunk')
24
+ continue;
25
+ // Construct a mapping from a chunk name to its file name
26
+ chunkNamesToFiles.set(chunk.name, chunk.fileName);
27
+ // Construct a mapping from a chunk file to all the modules it imports
28
+ chunkToDependencies.set(chunk.fileName, [...chunk.imports, ...chunk.dynamicImports]);
29
+ if (chunk.isEntry) {
30
+ // Entry chunks should always be kept around since they are to be imported by the runtime
31
+ entryChunks.push(chunk.fileName);
32
+ }
33
+ }
34
+ const chunkDecisions = new Map();
35
+ for (const entry of entryChunks) {
36
+ // Entry chunks are used on the server
37
+ chunkDecisions.set(entry, true);
38
+ }
39
+ for (const chunk of ['prerender', 'prerender@_@astro']) {
40
+ // Prerender chunks are not used on the server
41
+ const fileName = chunkNamesToFiles.get(chunk);
42
+ if (fileName) {
43
+ chunkDecisions.set(fileName, false);
44
+ }
45
+ }
46
+ // Start a stack of chunks that are used on the server
47
+ const chunksToWalk = [...entryChunks];
48
+ // Iterate over the chunks, traversing the transitive dependencies of the chunks used on the server
49
+ for (let chunk = chunksToWalk.pop(); chunk; chunk = chunksToWalk.pop()) {
50
+ for (const dep of chunkToDependencies.get(chunk) ?? []) {
51
+ // Skip dependencies already flagged, dependencies may be repeated and/or circular
52
+ if (chunkDecisions.has(dep))
53
+ continue;
54
+ // A dependency of a module used on the server is also used on the server
55
+ chunkDecisions.set(dep, true);
56
+ // Add the dependency to the stack so its own dependencies are also flagged
57
+ chunksToWalk.push(dep);
58
+ }
59
+ }
60
+ // Any chunk not flagged as used on the server is a non-server chunk
61
+ this.nonServerChunks = Array.from(chunkToDependencies.keys()).filter((chunk) => !chunkDecisions.get(chunk));
62
+ }
63
+ getNonServerChunks() {
64
+ return this.nonServerChunks ?? [];
65
+ }
66
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/cloudflare",
3
3
  "description": "Deploy your site to Cloudflare Workers/Pages",
4
- "version": "10.0.3",
4
+ "version": "10.2.0",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -28,11 +28,11 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@astrojs/underscore-redirects": "^0.3.3",
32
31
  "@astrojs/internal-helpers": "0.3.0",
32
+ "@astrojs/underscore-redirects": "^0.3.3",
33
33
  "@cloudflare/workers-types": "^4.20240320.1",
34
- "miniflare": "^3.20240320.0",
35
34
  "esbuild": "^0.19.5",
35
+ "miniflare": "^3.20240320.0",
36
36
  "tiny-glob": "^0.2.9",
37
37
  "wrangler": "^3.39.0"
38
38
  },
@@ -40,11 +40,13 @@
40
40
  "astro": "^4.2.0"
41
41
  },
42
42
  "devDependencies": {
43
+ "astro": "^4.5.8",
44
+ "cheerio": "1.0.0-rc.12",
43
45
  "execa": "^8.0.1",
44
46
  "fast-glob": "^3.3.2",
47
+ "rollup": "^4.14.0",
45
48
  "strip-ansi": "^7.1.0",
46
- "astro": "^4.5.8",
47
- "cheerio": "1.0.0-rc.12",
49
+ "vite": "^5.2.6",
48
50
  "@astrojs/test-utils": "0.0.1",
49
51
  "astro-scripts": "0.0.14"
50
52
  },