@astrojs/cloudflare 12.0.0-beta.0 → 12.0.0-beta.1

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.js CHANGED
@@ -50,6 +50,16 @@ export default function createIntegration(args) {
50
50
  // https://developers.cloudflare.com/pages/functions/module-support/
51
51
  // Allows imports of '.wasm', '.bin', and '.txt' file types
52
52
  cloudflareModulePlugin,
53
+ {
54
+ name: 'vite:cf-imports',
55
+ enforce: 'pre',
56
+ resolveId(source) {
57
+ if (source.startsWith('cloudflare:')) {
58
+ return { id: source, external: true };
59
+ }
60
+ return null;
61
+ },
62
+ },
53
63
  ],
54
64
  },
55
65
  integrations: [astroWhen()],
@@ -1,6 +1,6 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import { writeFile } from 'node:fs/promises';
3
- import { posix } from 'node:path';
3
+ import path from 'node:path';
4
4
  import { fileURLToPath } from 'node:url';
5
5
  import { prependForwardSlash, removeLeadingForwardSlash, removeTrailingForwardSlash, } from '@astrojs/internal-helpers/path';
6
6
  import glob from 'tiny-glob';
@@ -96,6 +96,28 @@ class PathTrie {
96
96
  this.dfs(childNode, [...path, segment], allPaths);
97
97
  }
98
98
  }
99
+ /**
100
+ * The reduce function is used to remove unnecessary paths from the trie.
101
+ * It receives a trie node to compare with the current node.
102
+ */
103
+ reduce(compNode, node) {
104
+ if (node.hasWildcardChild || compNode.hasWildcardChild)
105
+ return;
106
+ for (const [segment, childNode] of node.children) {
107
+ if (childNode.children.size === 0)
108
+ continue;
109
+ const compChildNode = compNode.children.get(segment);
110
+ if (compChildNode === undefined) {
111
+ childNode.hasWildcardChild = true;
112
+ continue;
113
+ }
114
+ this.reduce(compChildNode, childNode);
115
+ }
116
+ }
117
+ reduceAllPaths(compTrie) {
118
+ this.reduce(compTrie.root, this.root);
119
+ return this;
120
+ }
99
121
  getAllPaths() {
100
122
  const allPaths = [];
101
123
  this.dfs(this.root, [], allPaths);
@@ -129,7 +151,7 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
129
151
  continue;
130
152
  const staticPath = staticFile;
131
153
  const segments = removeLeadingForwardSlash(staticPath)
132
- .split(posix.sep)
154
+ .split(path.sep)
133
155
  .filter(Boolean)
134
156
  .map((s) => {
135
157
  return getParts(s);
@@ -142,6 +164,7 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
142
164
  const convertedPath = segmentsToCfSyntax(route.segments, _config);
143
165
  if (route.pathname === '/404' && route.prerender === true)
144
166
  hasPrerendered404 = true;
167
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
145
168
  switch (route.type) {
146
169
  case 'page':
147
170
  if (route.prerender === false)
@@ -170,7 +193,7 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
170
193
  if (page.pathname === '404')
171
194
  hasPrerendered404 = true;
172
195
  const pageSegments = removeLeadingForwardSlash(page.pathname)
173
- .split(posix.sep)
196
+ .split(path.posix.sep)
174
197
  .filter(Boolean)
175
198
  .map((s) => {
176
199
  return getParts(s);
@@ -181,7 +204,6 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
181
204
  for (const includePath of includePaths) {
182
205
  includeTrie.insert(includePath);
183
206
  }
184
- const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie.getAllPaths();
185
207
  const excludeTrie = new PathTrie();
186
208
  for (const excludePath of excludePaths) {
187
209
  /**
@@ -193,7 +215,12 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
193
215
  continue;
194
216
  excludeTrie.insert(excludePath);
195
217
  }
196
- const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie.getAllPaths();
218
+ const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie
219
+ .reduceAllPaths(excludeTrie)
220
+ .getAllPaths();
221
+ const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie
222
+ .reduceAllPaths(includeTrie)
223
+ .getAllPaths();
197
224
  /**
198
225
  * Cloudflare allows no more than 100 include/exclude rules combined
199
226
  * https://developers.cloudflare.com/pages/functions/routing/#limits
@@ -211,9 +238,8 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
211
238
  const AUTOMATIC_EXCLUDE_RULES_COUNT = deduplicatedExcludePaths.length;
212
239
  const EXTENDED_EXCLUDE_RULES_COUNT = excludeExtends?.length ?? 0;
213
240
  const EXCLUDE_RULES_COUNT = AUTOMATIC_EXCLUDE_RULES_COUNT + EXTENDED_EXCLUDE_RULES_COUNT;
214
- if (!hasPrerendered404 ||
215
- INCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT ||
216
- EXCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT) {
241
+ const OPTION2_TOTAL_COUNT = INCLUDE_RULES_COUNT + (includedPathsHaveWildcard ? EXCLUDE_RULES_COUNT : 0);
242
+ if (!hasPrerendered404 || OPTION2_TOTAL_COUNT > CLOUDFLARE_COMBINED_LIMIT) {
217
243
  await writeRoutesFileToOutDir(_config, logger, ['/*'].concat(includeExtends?.map((entry) => entry.pattern) ?? []), deduplicatedExcludePaths
218
244
  .map((path) => `${prependForwardSlash(path.join('/'))}`)
219
245
  .slice(0, CLOUDFLARE_COMBINED_LIMIT -
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": "12.0.0-beta.0",
4
+ "version": "12.0.0-beta.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -31,14 +31,14 @@
31
31
  "dependencies": {
32
32
  "@astrojs/internal-helpers": "0.4.1",
33
33
  "@astrojs/underscore-redirects": "^0.4.0-alpha.0",
34
- "@cloudflare/workers-types": "^4.20240903.0",
35
- "esbuild": "^0.23.1",
34
+ "@cloudflare/workers-types": "^4.20241022.0",
35
+ "@inox-tools/astro-when": "^0.2.4",
36
+ "esbuild": "^0.24.0",
36
37
  "estree-walker": "^3.0.3",
37
- "magic-string": "^0.30.11",
38
- "miniflare": "^3.20240821.1",
38
+ "magic-string": "^0.30.12",
39
+ "miniflare": "^3.20241022.0",
39
40
  "tiny-glob": "^0.2.9",
40
- "wrangler": "^3.75.0",
41
- "@inox-tools/astro-when": "^0.2.2"
41
+ "wrangler": "^3.84.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "astro": "^5.0.0-alpha.8"
@@ -48,9 +48,9 @@
48
48
  "cheerio": "1.0.0",
49
49
  "execa": "^8.0.1",
50
50
  "fast-glob": "^3.3.2",
51
- "rollup": "^4.21.2",
51
+ "rollup": "^4.24.3",
52
52
  "strip-ansi": "^7.1.0",
53
- "vite": "^5.4.3",
53
+ "vite": "6.0.0-beta.2",
54
54
  "@astrojs/test-utils": "0.0.1",
55
55
  "astro-scripts": "0.0.14"
56
56
  },