@astrojs/cloudflare 13.0.0-alpha.2 → 13.0.0-alpha.4

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.
@@ -5,37 +5,29 @@ import { fileURLToPath } from "node:url";
5
5
  import { cloudflare as cfVitePlugin } from "@cloudflare/vite-plugin";
6
6
  import colors from "piccolore";
7
7
  import { performance } from "node:perf_hooks";
8
- import { hasWranglerConfig, defaultCloudflareConfig } from "../wrangler.js";
9
- import { copyFileSync, existsSync } from "node:fs";
8
+ import { cloudflareConfigCustomizer } from "../wrangler.js";
10
9
  const createPreviewServer = async ({
11
10
  logger,
12
11
  base,
13
- outDir,
12
+ server,
14
13
  headers,
15
14
  port,
16
15
  host,
17
- createCodegenDir,
18
16
  root
19
17
  }) => {
20
18
  const startServerTime = performance.now();
21
19
  let previewServer;
22
- let cfPluginConfig = { viteEnvironment: { name: "ssr" } };
23
- if (!hasWranglerConfig(outDir)) {
24
- cfPluginConfig.config = defaultCloudflareConfig();
25
- const codegenDir = createCodegenDir();
26
- const devVarsPath = new URL(".dev.vars", outDir);
27
- const devVarsCodegenPath = new URL(".dev.vars", codegenDir);
28
- if (existsSync(devVarsPath)) {
29
- copyFileSync(devVarsPath, devVarsCodegenPath);
30
- }
31
- }
20
+ const cfPluginConfig = {
21
+ viteEnvironment: { name: "ssr" },
22
+ config: cloudflareConfigCustomizer()
23
+ };
32
24
  try {
33
25
  previewServer = await preview({
34
26
  configFile: false,
35
27
  base,
36
28
  appType: "mpa",
37
29
  build: {
38
- outDir: fileURLToPath(outDir)
30
+ outDir: fileURLToPath(server)
39
31
  },
40
32
  root: fileURLToPath(root),
41
33
  preview: {
@@ -91,7 +83,7 @@ function serverStart({
91
83
  host,
92
84
  base
93
85
  }) {
94
- const version = "13.0.0-alpha.2";
86
+ const version = "13.0.0-alpha.4";
95
87
  const localPrefix = `${colors.dim("\u2503")} Local `;
96
88
  const networkPrefix = `${colors.dim("\u2503")} Network `;
97
89
  const emptyPrefix = " ".repeat(11);
package/dist/index.d.ts CHANGED
@@ -37,45 +37,23 @@ export type Options = {
37
37
  */
38
38
  cloudflareModules?: boolean;
39
39
  /**
40
- * By default, Astro will be configured to use Cloudflare KV to store session data. If you want to use sessions,
41
- * you must create a KV namespace and declare it in your wrangler config file. You can do this with the wrangler command:
40
+ * By default, Astro will be configured to use Cloudflare KV to store session data. The KV namespace
41
+ * will be automatically provisioned when you deploy.
42
42
  *
43
- * ```sh
44
- * npx wrangler kv namespace create SESSION
45
- * ```
46
- *
47
- * This will log the id of the created namespace. You can then add it to your `wrangler.json` file like this:
48
- *
49
- * ```json
50
- * {
51
- * "kv_namespaces": [
52
- * {
53
- * "binding": "SESSION",
54
- * "id": "<your kv namespace id here>"
55
- * }
56
- * ]
57
- * }
58
- * ```
59
- * By default, the driver looks for the binding named `SESSION`, but you can override this by providing a different name here.
60
- *
61
- * See https://developers.cloudflare.com/kv/concepts/kv-namespaces/ for more details on using KV namespaces.
43
+ * By default, the binding is named `SESSION`, but you can override this by providing a different name here.
44
+ * If you define the binding manually in your wrangler config, Astro will use your configuration instead.
62
45
  *
46
+ * See https://developers.cloudflare.com/workers/wrangler/configuration/#automatic-provisioning for more details.
63
47
  */
64
48
  sessionKVBindingName?: string;
65
49
  /**
66
- * When configured as `cloudflare-binding`, the Cloudflare Images binding will be used to transform images:
67
- * - https://developers.cloudflare.com/images/transform-images/bindings/
68
- *
69
- * By default, this will use the "IMAGES" binding name, but this can be customised in your `wrangler.json`:
50
+ * When `imageService` is set to `cloudflare-binding`, the Cloudflare Images binding will be used
51
+ * to transform images. The binding will be automatically configured for you.
70
52
  *
71
- * ```json
72
- * {
73
- * "images": {
74
- * "binding": "IMAGES" // <-- this should match `imagesBindingName`
75
- * }
76
- * }
77
- * ```
53
+ * By default, the binding is named `IMAGES`, but you can override this by providing a different name here.
54
+ * If you define the binding manually in your wrangler config, Astro will use your configuration instead.
78
55
  *
56
+ * See https://developers.cloudflare.com/images/transform-images/bindings/ for more details.
79
57
  */
80
58
  imagesBindingName?: string;
81
59
  /**
package/dist/index.js CHANGED
@@ -1,24 +1,22 @@
1
- import { createReadStream, copyFileSync, existsSync, readFileSync } from "node:fs";
1
+ import { createReadStream, existsSync, readFileSync } from "node:fs";
2
2
  import { appendFile, stat } from "node:fs/promises";
3
3
  import { createRequire } from "node:module";
4
4
  import { createInterface } from "node:readline/promises";
5
5
  import { pathToFileURL } from "node:url";
6
- import {
7
- appendForwardSlash,
8
- prependForwardSlash,
9
- removeLeadingForwardSlash
10
- } from "@astrojs/internal-helpers/path";
6
+ import { removeLeadingForwardSlash } from "@astrojs/internal-helpers/path";
11
7
  import { createRedirectsFromAstroRoutes, printAsRedirects } from "@astrojs/underscore-redirects";
12
8
  import { cloudflare as cfVitePlugin } from "@cloudflare/vite-plugin";
13
9
  import { cloudflareModuleLoader } from "./utils/cloudflare-module-loader.js";
14
10
  import { createRoutesFile, getParts } from "./utils/generate-routes-json.js";
15
11
  import { setImageConfig } from "./utils/image-config.js";
16
12
  import { createConfigPlugin } from "./vite-plugin-config.js";
17
- import { hasWranglerConfig, defaultCloudflareConfig } from "./wrangler.js";
13
+ import {
14
+ cloudflareConfigCustomizer,
15
+ DEFAULT_SESSION_KV_BINDING_NAME,
16
+ DEFAULT_IMAGES_BINDING_NAME
17
+ } from "./wrangler.js";
18
18
  import { parse } from "dotenv";
19
- function wrapWithSlashes(path) {
20
- return prependForwardSlash(appendForwardSlash(path));
21
- }
19
+ import { sessionDrivers } from "astro/config";
22
20
  function createIntegration(args) {
23
21
  let _config;
24
22
  let finalBuildOutput;
@@ -26,57 +24,40 @@ function createIntegration(args) {
26
24
  args?.cloudflareModules ?? true
27
25
  );
28
26
  let _routes;
29
- const SESSION_KV_BINDING_NAME = args?.sessionKVBindingName ?? "SESSION";
27
+ const sessionKVBindingName = args?.sessionKVBindingName ?? DEFAULT_SESSION_KV_BINDING_NAME;
28
+ const imagesBindingName = args?.imagesBindingName ?? DEFAULT_IMAGES_BINDING_NAME;
30
29
  return {
31
30
  name: "@astrojs/cloudflare",
32
31
  hooks: {
33
- "astro:config:setup": ({
34
- command,
35
- config,
36
- updateConfig,
37
- logger,
38
- addWatchFile,
39
- createCodegenDir
40
- }) => {
32
+ "astro:config:setup": ({ command, config, updateConfig, logger, addWatchFile }) => {
41
33
  let session = config.session;
42
34
  if (args?.imageService === "cloudflare-binding") {
43
- const bindingName = args?.imagesBindingName ?? "IMAGES";
44
- logger.info(
45
- `Enabling image processing with Cloudflare Images for production with the "${bindingName}" Images binding.`
46
- );
47
35
  logger.info(
48
- `If you see the error "Invalid binding \`${bindingName}\`" in your build output, you need to add the binding to your wrangler config file.`
36
+ `Enabling image processing with Cloudflare Images for production with the "${imagesBindingName}" Images binding.`
49
37
  );
50
38
  }
51
39
  if (!session?.driver) {
52
40
  logger.info(
53
- `Enabling sessions with Cloudflare KV with the "${SESSION_KV_BINDING_NAME}" KV binding.`
54
- );
55
- logger.info(
56
- `If you see the error "Invalid binding \`${SESSION_KV_BINDING_NAME}\`" in your build output, you need to add the binding to your wrangler config file.`
41
+ `Enabling sessions with Cloudflare KV with the "${sessionKVBindingName}" KV binding.`
57
42
  );
58
43
  session = {
59
- ...session,
60
- driver: "cloudflare-kv-binding",
61
- options: {
62
- binding: SESSION_KV_BINDING_NAME,
63
- ...session?.options
64
- }
44
+ driver: sessionDrivers.cloudflareKVBinding({
45
+ binding: sessionKVBindingName
46
+ }),
47
+ cookie: session?.cookie,
48
+ ttl: session?.ttl
65
49
  };
66
50
  }
67
- const cfPluginConfig = { viteEnvironment: { name: "ssr" } };
68
- if (!hasWranglerConfig(config.root)) {
69
- cfPluginConfig.config = defaultCloudflareConfig();
70
- const codegenDir = createCodegenDir();
71
- const devVarsPath = new URL(".dev.vars", config.root);
72
- const devVarsCodegenPath = new URL(".dev.vars", codegenDir);
73
- if (existsSync(devVarsPath)) {
74
- copyFileSync(devVarsPath, devVarsCodegenPath);
75
- }
76
- }
51
+ const cfPluginConfig = {
52
+ viteEnvironment: { name: "ssr" },
53
+ config: cloudflareConfigCustomizer({
54
+ sessionKVBindingName: args?.sessionKVBindingName,
55
+ imagesBindingName: args?.imageService === "cloudflare-binding" ? args?.imagesBindingName : false
56
+ })
57
+ };
77
58
  updateConfig({
78
59
  build: {
79
- client: new URL(`.${wrapWithSlashes(config.base)}`, config.outDir),
60
+ client: new URL(`./client/`, config.outDir),
80
61
  server: new URL("./_worker.js/", config.outDir),
81
62
  serverEntry: "index.js",
82
63
  redirects: false
@@ -122,6 +103,7 @@ function createIntegration(args) {
122
103
  "astro > es-module-lexer",
123
104
  "astro > unstorage",
124
105
  "astro > neotraverse/modern",
106
+ "astro > piccolore",
125
107
  "astro/app",
126
108
  "astro/compiler-runtime"
127
109
  ],
@@ -136,8 +118,7 @@ function createIntegration(args) {
136
118
  } else if (environmentName === "client") {
137
119
  return {
138
120
  optimizeDeps: {
139
- include: ["astro/runtime/client/dev-toolbar/entrypoint.js"],
140
- exclude: ["astro:*"]
121
+ include: ["astro/runtime/client/dev-toolbar/entrypoint.js"]
141
122
  }
142
123
  };
143
124
  }
@@ -155,7 +136,7 @@ function createIntegration(args) {
155
136
  }
156
137
  },
157
138
  createConfigPlugin({
158
- sessionKVBindingName: SESSION_KV_BINDING_NAME
139
+ sessionKVBindingName
159
140
  })
160
141
  ]
161
142
  },
@@ -244,7 +225,7 @@ function createIntegration(args) {
244
225
  "astro:build:done": async ({ pages, dir, logger, assets }) => {
245
226
  let redirectsExists = false;
246
227
  try {
247
- const redirectsStat = await stat(new URL("./_redirects", _config.outDir));
228
+ const redirectsStat = await stat(new URL("./_redirects", _config.build.client));
248
229
  if (redirectsStat.isFile()) {
249
230
  redirectsExists = true;
250
231
  }
@@ -254,7 +235,7 @@ function createIntegration(args) {
254
235
  const redirects = [];
255
236
  if (redirectsExists) {
256
237
  const rl = createInterface({
257
- input: createReadStream(new URL("./_redirects", _config.outDir)),
238
+ input: createReadStream(new URL("./_redirects", _config.build.client)),
258
239
  crlfDelay: Number.POSITIVE_INFINITY
259
240
  });
260
241
  for await (const line of rl) {
@@ -270,7 +251,7 @@ function createIntegration(args) {
270
251
  }
271
252
  let routesExists = false;
272
253
  try {
273
- const routesStat = await stat(new URL("./_routes.json", _config.outDir));
254
+ const routesStat = await stat(new URL("./_routes.json", _config.build.client));
274
255
  if (routesStat.isFile()) {
275
256
  routesExists = true;
276
257
  }
@@ -302,7 +283,7 @@ function createIntegration(args) {
302
283
  if (!trueRedirects.empty()) {
303
284
  try {
304
285
  await appendFile(
305
- new URL("./_redirects", _config.outDir),
286
+ new URL("./_redirects", _config.build.client),
306
287
  printAsRedirects(trueRedirects)
307
288
  );
308
289
  } catch (_error) {
@@ -28,6 +28,31 @@ async function handle(request, env, context) {
28
28
  const locals = {
29
29
  cfContext: context
30
30
  };
31
+ Object.defineProperty(locals, "runtime", {
32
+ enumerable: false,
33
+ value: {
34
+ get env() {
35
+ throw new Error(
36
+ `Astro.locals.runtime.env has been removed in Astro v6. Use 'import { env } from "cloudflare:workers"' instead.`
37
+ );
38
+ },
39
+ get cf() {
40
+ throw new Error(
41
+ `Astro.locals.runtime.cf has been removed in Astro v6. Use 'Astro.request.cf' instead.`
42
+ );
43
+ },
44
+ get caches() {
45
+ throw new Error(
46
+ `Astro.locals.runtime.caches has been removed in Astro v6. Use the global 'caches' object instead.`
47
+ );
48
+ },
49
+ get ctx() {
50
+ throw new Error(
51
+ `Astro.locals.runtime.ctx has been removed in Astro v6. Use 'Astro.locals.cfContext' instead.`
52
+ );
53
+ }
54
+ }
55
+ });
31
56
  const response = await app.render(
32
57
  request,
33
58
  {
@@ -1,10 +1,14 @@
1
1
  import type { PluginConfig } from '@cloudflare/vite-plugin';
2
+ export declare const DEFAULT_SESSION_KV_BINDING_NAME = "SESSION";
3
+ export declare const DEFAULT_IMAGES_BINDING_NAME = "IMAGES";
4
+ export declare const DEFAULT_ASSETS_BINDING_NAME = "ASSETS";
5
+ interface CloudflareConfigOptions {
6
+ sessionKVBindingName?: string;
7
+ imagesBindingName?: string | false;
8
+ }
2
9
  /**
3
- * Checks whether a wrangler file exists at the given path
4
- * @param root
10
+ * Returns a config customizer that sets up the Astro Cloudflare defaults.
11
+ * Sets the main entrypoint and adds bindings for auto-provisioning.
5
12
  */
6
- export declare function hasWranglerConfig(root: URL): boolean;
7
- /**
8
- * Returns the default wrangler configuration used by Astro Cloudflare configuration.
9
- */
10
- export declare function defaultCloudflareConfig(): PluginConfig['config'];
13
+ export declare function cloudflareConfigCustomizer(options?: CloudflareConfigOptions): PluginConfig['config'];
14
+ export {};
package/dist/wrangler.js CHANGED
@@ -1,29 +1,34 @@
1
- import { existsSync } from "node:fs";
2
- function hasWranglerConfig(root) {
3
- return existsSync(new URL("wrangler.jsonc", root)) || existsSync(new URL("wrangler.toml", root)) || existsSync(new URL("wrangler.json", root));
4
- }
5
- function defaultCloudflareConfig() {
6
- return {
7
- // TODO: better way to handle name, maybe package.json#name ?
8
- name: "test-application",
9
- compatibility_date: "2025-05-21",
10
- main: "@astrojs/cloudflare/entrypoints/server",
11
- assets: {
12
- directory: "./dist",
13
- binding: "ASSETS"
14
- },
15
- images: {
16
- binding: "IMAGES"
17
- },
18
- kv_namespaces: [
19
- {
20
- binding: "SESSION",
21
- id: "SESSION"
1
+ const DEFAULT_SESSION_KV_BINDING_NAME = "SESSION";
2
+ const DEFAULT_IMAGES_BINDING_NAME = "IMAGES";
3
+ const DEFAULT_ASSETS_BINDING_NAME = "ASSETS";
4
+ function cloudflareConfigCustomizer(options) {
5
+ const sessionKVBindingName = options?.sessionKVBindingName ?? DEFAULT_SESSION_KV_BINDING_NAME;
6
+ const imagesBindingName = options?.imagesBindingName === false ? void 0 : options?.imagesBindingName ?? DEFAULT_IMAGES_BINDING_NAME;
7
+ return (config) => {
8
+ const hasSessionBinding = config.kv_namespaces?.some(
9
+ (kv) => kv.binding === sessionKVBindingName
10
+ );
11
+ const hasImagesBinding = config.images?.binding !== void 0;
12
+ const hasAssetsBinding = config.assets?.binding !== void 0;
13
+ return {
14
+ main: config.main ?? "@astrojs/cloudflare/entrypoints/server",
15
+ kv_namespaces: hasSessionBinding ? void 0 : [
16
+ {
17
+ binding: sessionKVBindingName
18
+ }
19
+ ],
20
+ images: hasImagesBinding || !imagesBindingName ? void 0 : {
21
+ binding: imagesBindingName
22
+ },
23
+ assets: hasAssetsBinding ? void 0 : {
24
+ binding: DEFAULT_ASSETS_BINDING_NAME
22
25
  }
23
- ]
26
+ };
24
27
  };
25
28
  }
26
29
  export {
27
- defaultCloudflareConfig,
28
- hasWranglerConfig
30
+ DEFAULT_ASSETS_BINDING_NAME,
31
+ DEFAULT_IMAGES_BINDING_NAME,
32
+ DEFAULT_SESSION_KV_BINDING_NAME,
33
+ cloudflareConfigCustomizer
29
34
  };
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": "13.0.0-alpha.2",
4
+ "version": "13.0.0-alpha.4",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -51,8 +51,8 @@
51
51
  "cheerio": "1.1.2",
52
52
  "devalue": "^5.5.0",
53
53
  "rollup": "^4.53.3",
54
- "astro": "6.0.0-alpha.2",
55
- "astro-scripts": "0.0.14"
54
+ "astro-scripts": "0.0.14",
55
+ "astro": "6.0.0-alpha.5"
56
56
  },
57
57
  "publishConfig": {
58
58
  "provenance": true