@astrojs/cloudflare 10.0.0 → 10.0.2

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
@@ -88,14 +88,6 @@ export default function createIntegration(args) {
88
88
  find: 'react-dom/server',
89
89
  replacement: 'react-dom/server.browser',
90
90
  },
91
- {
92
- find: 'solid-js/web',
93
- replacement: 'solid-js/web/dist/server',
94
- },
95
- {
96
- find: 'solid-js',
97
- replacement: 'solid-js/dist/server',
98
- },
99
91
  ];
100
92
  if (Array.isArray(vite.resolve.alias)) {
101
93
  vite.resolve.alias = [...vite.resolve.alias, ...aliases];
@@ -105,10 +97,23 @@ export default function createIntegration(args) {
105
97
  vite.resolve.alias[alias.find] = alias.replacement;
106
98
  }
107
99
  }
100
+ vite.resolve.conditions ||= [];
101
+ // We need those conditions, previous these conditions where applied at the esbuild step which we removed
102
+ // https://github.com/withastro/astro/pull/7092
103
+ vite.resolve.conditions.push('workerd', 'worker');
108
104
  vite.ssr ||= {};
109
105
  vite.ssr.target = 'webworker';
110
106
  vite.ssr.noExternal = true;
111
- vite.ssr.external = _config.vite.ssr?.external ?? [];
107
+ if (typeof _config.vite.ssr?.external === 'undefined')
108
+ vite.ssr.external = [];
109
+ if (typeof _config.vite.ssr?.external === 'boolean')
110
+ vite.ssr.external = _config.vite.ssr?.external;
111
+ if (Array.isArray(_config.vite.ssr?.external))
112
+ // `@astrojs/vue` sets `@vue/server-renderer` to external
113
+ // https://github.com/withastro/astro/blob/e648c5575a8774af739231cfa9fc27a32086aa5f/packages/integrations/vue/src/index.ts#L119
114
+ // the cloudflare adapter needs to get all dependencies inlined, we use `noExternal` for that, but any `external` config overrides that
115
+ // therefore we need to remove `@vue/server-renderer` from the external config again
116
+ vite.ssr.external = _config.vite.ssr?.external.filter((entry) => entry !== '@vue/server-renderer');
112
117
  vite.build ||= {};
113
118
  vite.build.rollupOptions ||= {};
114
119
  vite.build.rollupOptions.output ||= {};
@@ -123,6 +128,15 @@ export default function createIntegration(args) {
123
128
  ...vite.define,
124
129
  };
125
130
  }
131
+ // we thought that vite config inside `if (target === 'server')` would not apply for client
132
+ // but it seems like the same `vite` reference is used for both
133
+ // so we need to reset the previous conflicting setting
134
+ // in the future we should look into a more robust solution
135
+ if (target === 'client') {
136
+ vite.resolve ||= {};
137
+ vite.resolve.conditions ||= [];
138
+ vite.resolve.conditions = vite.resolve.conditions.filter((c) => c !== 'workerd' && c !== 'worker');
139
+ }
126
140
  },
127
141
  'astro:build:done': async ({ pages, routes, dir, logger }) => {
128
142
  const PLATFORM_FILES = ['_headers', '_redirects', '_routes.json'];
@@ -58,6 +58,7 @@ class TrieNode {
58
58
  }
59
59
  class PathTrie {
60
60
  root;
61
+ returnHasWildcard = false;
61
62
  constructor() {
62
63
  this.root = new TrieNode();
63
64
  }
@@ -83,6 +84,7 @@ class PathTrie {
83
84
  */
84
85
  dfs(node, path, allPaths) {
85
86
  if (node.hasWildcardChild) {
87
+ this.returnHasWildcard = true;
86
88
  allPaths.push([...path, '*']);
87
89
  return;
88
90
  }
@@ -96,12 +98,44 @@ class PathTrie {
96
98
  getAllPaths() {
97
99
  const allPaths = [];
98
100
  this.dfs(this.root, [], allPaths);
99
- return allPaths;
101
+ return [allPaths, this.returnHasWildcard];
100
102
  }
101
103
  }
102
104
  export async function createRoutesFile(_config, logger, routes, pages, redirects, includeExtends, excludeExtends) {
103
105
  const includePaths = [];
104
106
  const excludePaths = [];
107
+ /**
108
+ * All files in the `_config.build.assets` path, e.g. `_astro`
109
+ * are considered static assets and should not be handled by the function
110
+ * therefore we exclude a wildcard for that, e.g. `/_astro/*`
111
+ */
112
+ const assetsPath = segmentsToCfSyntax([
113
+ [{ content: _config.build.assets, dynamic: false, spread: false }],
114
+ [{ content: '', dynamic: true, spread: false }],
115
+ ], _config);
116
+ excludePaths.push(assetsPath);
117
+ if (existsSync(fileURLToPath(_config.publicDir))) {
118
+ const staticFiles = await glob(`${fileURLToPath(_config.publicDir)}/**/*`, {
119
+ cwd: fileURLToPath(_config.publicDir),
120
+ filesOnly: true,
121
+ dot: true,
122
+ });
123
+ for (const staticFile of staticFiles) {
124
+ if (['_headers', '_redirects', '_routes.json'].includes(staticFile))
125
+ continue;
126
+ const staticPath = staticFile;
127
+ const segments = removeLeadingForwardSlash(staticPath)
128
+ .split(posix.sep)
129
+ .filter(Boolean)
130
+ .map((s) => {
131
+ return getParts(s);
132
+ });
133
+ excludePaths.push(segmentsToCfSyntax(segments, _config));
134
+ }
135
+ }
136
+ for (const redirect of redirects) {
137
+ excludePaths.push(segmentsToCfSyntax(redirect, _config));
138
+ }
105
139
  let hasPrerendered404 = false;
106
140
  for (const route of routes) {
107
141
  const convertedPath = segmentsToCfSyntax(route.segments, _config);
@@ -132,6 +166,8 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
132
166
  }
133
167
  }
134
168
  for (const page of pages) {
169
+ if (page.pathname === '404')
170
+ hasPrerendered404 = true;
135
171
  const pageSegments = removeLeadingForwardSlash(page.pathname)
136
172
  .split(posix.sep)
137
173
  .filter(Boolean)
@@ -140,66 +176,58 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
140
176
  });
141
177
  excludePaths.push(segmentsToCfSyntax(pageSegments, _config));
142
178
  }
143
- if (existsSync(fileURLToPath(_config.publicDir))) {
144
- const staticFiles = await glob(`${fileURLToPath(_config.publicDir)}/**/*`, {
145
- cwd: fileURLToPath(_config.publicDir),
146
- filesOnly: true,
147
- dot: true,
148
- });
149
- for (const staticFile of staticFiles) {
150
- if (['_headers', '_redirects', '_routes.json'].includes(staticFile))
151
- continue;
152
- const staticPath = staticFile;
153
- const segments = removeLeadingForwardSlash(staticPath)
154
- .split(posix.sep)
155
- .filter(Boolean)
156
- .map((s) => {
157
- return getParts(s);
158
- });
159
- excludePaths.push(segmentsToCfSyntax(segments, _config));
160
- }
161
- }
162
- /**
163
- * All files in the `_config.build.assets` path, e.g. `_astro`
164
- * are considered static assets and should not be handled by the function
165
- * therefore we exclude a wildcard for that, e.g. `/_astro/*`
166
- */
167
- const assetsPath = segmentsToCfSyntax([
168
- [{ content: _config.build.assets, dynamic: false, spread: false }],
169
- [{ content: '', dynamic: true, spread: false }],
170
- ], _config);
171
- excludePaths.push(assetsPath);
172
- for (const redirect of redirects) {
173
- excludePaths.push(segmentsToCfSyntax(redirect, _config));
174
- }
175
179
  const includeTrie = new PathTrie();
176
180
  for (const includePath of includePaths) {
177
181
  includeTrie.insert(includePath);
178
182
  }
179
- const deduplicatedIncludePaths = includeTrie.getAllPaths();
183
+ const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie.getAllPaths();
180
184
  const excludeTrie = new PathTrie();
181
185
  for (const excludePath of excludePaths) {
186
+ /**
187
+ * A excludePath with starts with a wildcard (*) is a catch-all
188
+ * that would mean all routes are static, that would be equal to a full SSG project
189
+ * the adapter is not needed in this case, so we do not consider such paths
190
+ */
191
+ if (excludePath[0] === '*')
192
+ continue;
182
193
  excludeTrie.insert(excludePath);
183
194
  }
184
- const deduplicatedExcludePaths = excludeTrie.getAllPaths();
195
+ const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie.getAllPaths();
185
196
  /**
186
197
  * Cloudflare allows no more than 100 include/exclude rules combined
187
198
  * https://developers.cloudflare.com/pages/functions/routing/#limits
188
199
  */
189
200
  const CLOUDFLARE_COMBINED_LIMIT = 100;
201
+ /**
202
+ * Caluclate the number of automated and extended include rules
203
+ */
204
+ const AUTOMATIC_INCLUDE_RULES_COUNT = deduplicatedIncludePaths.length;
205
+ const EXTENDED_INCLUDE_RULES_COUNT = includeExtends?.length ?? 0;
206
+ const INCLUDE_RULES_COUNT = AUTOMATIC_INCLUDE_RULES_COUNT + EXTENDED_INCLUDE_RULES_COUNT;
207
+ /**
208
+ * Caluclate the number of automated and extended exclude rules
209
+ */
210
+ const AUTOMATIC_EXCLUDE_RULES_COUNT = deduplicatedExcludePaths.length;
211
+ const EXTENDED_EXCLUDE_RULES_COUNT = excludeExtends?.length ?? 0;
212
+ const EXCLUDE_RULES_COUNT = AUTOMATIC_EXCLUDE_RULES_COUNT + EXTENDED_EXCLUDE_RULES_COUNT;
190
213
  if (!hasPrerendered404 ||
191
- deduplicatedIncludePaths.length + (includeExtends?.length ?? 0) > CLOUDFLARE_COMBINED_LIMIT ||
192
- deduplicatedExcludePaths.length + (excludeExtends?.length ?? 0) > CLOUDFLARE_COMBINED_LIMIT) {
214
+ INCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT ||
215
+ EXCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT) {
193
216
  await writeRoutesFileToOutDir(_config, logger, ['/*'].concat(includeExtends?.map((entry) => entry.pattern) ?? []), deduplicatedExcludePaths
194
217
  .map((path) => `${prependForwardSlash(path.join('/'))}`)
195
- .concat(excludeExtends?.map((entry) => entry.pattern) ?? [])
196
- .slice(0, 99));
218
+ .slice(0, CLOUDFLARE_COMBINED_LIMIT -
219
+ EXTENDED_INCLUDE_RULES_COUNT -
220
+ EXTENDED_EXCLUDE_RULES_COUNT -
221
+ 1)
222
+ .concat(excludeExtends?.map((entry) => entry.pattern) ?? []));
197
223
  }
198
224
  else {
199
225
  await writeRoutesFileToOutDir(_config, logger, deduplicatedIncludePaths
200
226
  .map((path) => `${prependForwardSlash(path.join('/'))}`)
201
- .concat(includeExtends?.map((entry) => entry.pattern) ?? []), deduplicatedExcludePaths
202
- .map((path) => `${prependForwardSlash(path.join('/'))}`)
203
- .concat(excludeExtends?.map((entry) => entry.pattern) ?? []));
227
+ .concat(includeExtends?.map((entry) => entry.pattern) ?? []), includedPathsHaveWildcard
228
+ ? deduplicatedExcludePaths
229
+ .map((path) => `${prependForwardSlash(path.join('/'))}`)
230
+ .concat(excludeExtends?.map((entry) => entry.pattern) ?? [])
231
+ : []);
204
232
  }
205
233
  }
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.0",
4
+ "version": "10.0.2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -35,7 +35,7 @@
35
35
  "miniflare": "^3.20240320.0",
36
36
  "esbuild": "^0.19.5",
37
37
  "tiny-glob": "^0.2.9",
38
- "wrangler": "^3.36.0"
38
+ "wrangler": "^3.39.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "astro": "^4.2.0"