@astrojs/cloudflare 6.5.1 → 6.6.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/README.md CHANGED
@@ -32,13 +32,12 @@ import cloudflare from '@astrojs/cloudflare';
32
32
 
33
33
  export default defineConfig({
34
34
  output: 'server',
35
- adapter: cloudflare()
35
+ adapter: cloudflare(),
36
36
  });
37
37
  ```
38
38
 
39
39
  ## Options
40
40
 
41
-
42
41
  ### Mode
43
42
 
44
43
  `mode: "advanced" | "directory"`
@@ -49,14 +48,17 @@ Cloudflare Pages has 2 different modes for deploying functions, `advanced` mode
49
48
 
50
49
  For most projects the adapter default of `advanced` will be sufficient; the `dist` folder will contain your compiled project. Switching to directory mode allows you to use [pages plugins](https://developers.cloudflare.com/pages/platform/functions/plugins/) such as [Sentry](https://developers.cloudflare.com/pages/platform/functions/plugins/sentry/) or write custom code to enable logging.
51
50
 
52
- In directory mode the adapter will compile the client side part of your app the same way, but moves the worker script into a `functions` folder in the project root. The adapter will only ever place a `[[path]].js` in that folder, allowing you to add additional plugins and pages middleware which can be checked into version control. Cloudflare documentation contains more information about [writing custom functions](https://developers.cloudflare.com/pages/platform/functions/).
51
+ In directory mode, the adapter will compile the client side part of your app the same way by default, but moves the worker script into a `functions` folder in the project root. In this case, the adapter will only ever place a `[[path]].js` in that folder, allowing you to add additional plugins and pages middleware which can be checked into version control.
52
+
53
+ With the build configuration `split: true`, the adapter instead compiles a separate bundle for each page. This option requires some manual maintenance of the `functions` folder. Files emitted by Astro will overwrite existing `functions` files with identical names, so you must choose unique file names for each file you manually add. Additionally, the adapter will never empty the `functions` folder of outdated files, so you must clean up the folder manually when you remove pages.
54
+
55
+ Note that this adapter does not support using [Cloudflare Pages Middleware](https://developers.cloudflare.com/pages/platform/functions/middleware/). Astro will bundle the [Astro middleware](https://docs.astro.build/en/guides/middleware/) into each page.
53
56
 
54
57
  ```ts
55
58
  // directory mode
56
59
  export default defineConfig({
57
- adapter: cloudflare({ mode: "directory" }),
60
+ adapter: cloudflare({ mode: 'directory' }),
58
61
  });
59
-
60
62
  ```
61
63
 
62
64
  ## Enabling Preview
@@ -64,7 +66,7 @@ export default defineConfig({
64
66
  In order for preview to work you must install `wrangler`
65
67
 
66
68
  ```sh
67
- $ pnpm install wrangler --save-dev
69
+ pnpm install wrangler --save-dev
68
70
  ```
69
71
 
70
72
  It's then possible to update the preview script in your `package.json` to `"preview": "wrangler pages dev ./dist"`. This will allow you to run your entire application locally with [Wrangler](https://github.com/cloudflare/wrangler2), which supports secrets, environment variables, KV namespaces, Durable Objects and [all other supported Cloudflare bindings](https://developers.cloudflare.com/pages/platform/functions/#adding-bindings).
@@ -74,7 +76,7 @@ It's then possible to update the preview script in your `package.json` to `"prev
74
76
  You can access all the Cloudflare bindings and environment variables from Astro components and API routes through the adapter API.
75
77
 
76
78
  ```js
77
- import { getRuntime } from "@astrojs/cloudflare/runtime";
79
+ import { getRuntime } from '@astrojs/cloudflare/runtime';
78
80
 
79
81
  getRuntime(Astro.request);
80
82
  ```
@@ -108,7 +110,6 @@ By default, `@astrojs/cloudflare` will generate a `_routes.json` file that lists
108
110
 
109
111
  ## Troubleshooting
110
112
 
111
-
112
113
  For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help!
113
114
 
114
115
  You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
@@ -119,14 +120,14 @@ Currently, errors during running your application in Wrangler are not very usefu
119
120
 
120
121
  ```js
121
122
  export default defineConfig({
122
- adapter: cloudflare(),
123
- output: 'server',
123
+ adapter: cloudflare(),
124
+ output: 'server',
124
125
 
125
126
  vite: {
126
127
  build: {
127
- minify: false
128
- }
129
- }
128
+ minify: false,
129
+ },
130
+ },
130
131
  });
131
132
  ```
132
133
 
package/dist/index.js CHANGED
@@ -2,13 +2,14 @@ import { createRedirectsFromAstroRoutes } from "@astrojs/underscore-redirects";
2
2
  import esbuild from "esbuild";
3
3
  import * as fs from "fs";
4
4
  import * as os from "os";
5
+ import { dirname } from "path";
5
6
  import glob from "tiny-glob";
6
7
  import { fileURLToPath, pathToFileURL } from "url";
7
8
  function getAdapter(isModeDirectory) {
8
9
  return isModeDirectory ? {
9
10
  name: "@astrojs/cloudflare",
10
11
  serverEntrypoint: "@astrojs/cloudflare/server.directory.js",
11
- exports: ["onRequest"]
12
+ exports: ["onRequest", "manifest"]
12
13
  } : {
13
14
  name: "@astrojs/cloudflare",
14
15
  serverEntrypoint: "@astrojs/cloudflare/server.advanced.js",
@@ -24,6 +25,7 @@ function createIntegration(args) {
24
25
  let _config;
25
26
  let _buildConfig;
26
27
  const isModeDirectory = (args == null ? void 0 : args.mode) === "directory";
28
+ let _entryPoints = /* @__PURE__ */ new Map();
27
29
  return {
28
30
  name: "@astrojs/cloudflare",
29
31
  hooks: {
@@ -68,30 +70,77 @@ function createIntegration(args) {
68
70
  vite.ssr.target = "webworker";
69
71
  }
70
72
  },
73
+ "astro:build:ssr": ({ entryPoints }) => {
74
+ _entryPoints = entryPoints;
75
+ },
71
76
  "astro:build:done": async ({ pages, routes, dir }) => {
72
- var _a, _b;
73
- const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server));
74
- const entryUrl = new URL(_buildConfig.serverEntry, _config.outDir);
75
- const buildPath = fileURLToPath(entryUrl);
76
- const finalBuildUrl = pathToFileURL(buildPath.replace(/\.mjs$/, ".js"));
77
- await esbuild.build({
78
- target: "es2020",
79
- platform: "browser",
80
- conditions: ["workerd", "worker", "browser"],
81
- entryPoints: [entryPath],
82
- outfile: buildPath,
83
- allowOverwrite: true,
84
- format: "esm",
85
- bundle: true,
86
- minify: ((_b = (_a = _config.vite) == null ? void 0 : _a.build) == null ? void 0 : _b.minify) !== false,
87
- banner: {
88
- js: SHIM
89
- },
90
- logOverride: {
91
- "ignored-bare-import": "silent"
77
+ var _a, _b, _c, _d;
78
+ const functionsUrl = new URL("functions/", _config.root);
79
+ if (isModeDirectory) {
80
+ await fs.promises.mkdir(functionsUrl, { recursive: true });
81
+ }
82
+ if (isModeDirectory && _buildConfig.split) {
83
+ const entryPointsRouteData = [..._entryPoints.keys()];
84
+ const entryPointsURL = [..._entryPoints.values()];
85
+ const entryPaths = entryPointsURL.map((entry) => fileURLToPath(entry));
86
+ const outputDir = fileURLToPath(new URL(".astro", _buildConfig.server));
87
+ const { outputFiles } = await esbuild.build({
88
+ target: "es2020",
89
+ platform: "browser",
90
+ conditions: ["workerd", "worker", "browser"],
91
+ entryPoints: entryPaths,
92
+ outdir: outputDir,
93
+ allowOverwrite: true,
94
+ format: "esm",
95
+ bundle: true,
96
+ minify: ((_b = (_a = _config.vite) == null ? void 0 : _a.build) == null ? void 0 : _b.minify) !== false,
97
+ banner: {
98
+ js: SHIM
99
+ },
100
+ logOverride: {
101
+ "ignored-bare-import": "silent"
102
+ },
103
+ write: false
104
+ });
105
+ for (const [index, outputFile] of outputFiles.entries()) {
106
+ const fileName = entryPointsRouteData[index].component.replace("src/pages/", "").replace(".astro", ".js").replace(/(\[\.\.\.)(\w+)(\])/g, (_match, _p1, p2) => {
107
+ return `[[${p2}]]`;
108
+ });
109
+ const fileUrl = new URL(fileName, functionsUrl);
110
+ const newFileDir = dirname(fileURLToPath(fileUrl));
111
+ if (!fs.existsSync(newFileDir)) {
112
+ fs.mkdirSync(newFileDir, { recursive: true });
113
+ }
114
+ await fs.promises.writeFile(fileUrl, outputFile.contents);
92
115
  }
93
- });
94
- await fs.promises.rename(buildPath, finalBuildUrl);
116
+ } else {
117
+ const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server));
118
+ const entryUrl = new URL(_buildConfig.serverEntry, _config.outDir);
119
+ const buildPath = fileURLToPath(entryUrl);
120
+ const finalBuildUrl = pathToFileURL(buildPath.replace(/\.mjs$/, ".js"));
121
+ await esbuild.build({
122
+ target: "es2020",
123
+ platform: "browser",
124
+ conditions: ["workerd", "worker", "browser"],
125
+ entryPoints: [entryPath],
126
+ outfile: buildPath,
127
+ allowOverwrite: true,
128
+ format: "esm",
129
+ bundle: true,
130
+ minify: ((_d = (_c = _config.vite) == null ? void 0 : _c.build) == null ? void 0 : _d.minify) !== false,
131
+ banner: {
132
+ js: SHIM
133
+ },
134
+ logOverride: {
135
+ "ignored-bare-import": "silent"
136
+ }
137
+ });
138
+ await fs.promises.rename(buildPath, finalBuildUrl);
139
+ if (isModeDirectory) {
140
+ const directoryUrl = new URL("[[path]].js", functionsUrl);
141
+ await fs.promises.rename(finalBuildUrl, directoryUrl);
142
+ }
143
+ }
95
144
  const serverUrl = new URL(_buildConfig.server);
96
145
  await fs.promises.rm(serverUrl, { recursive: true, force: true });
97
146
  const cloudflareSpecialFiles = ["_headers", "_redirects", "_routes.json"];
@@ -136,10 +185,12 @@ function createIntegration(args) {
136
185
  staticPathList.push(...redirects);
137
186
  }
138
187
  }
139
- const redirectRoutes = routes.filter((r) => r.type === "redirect");
188
+ const redirectRoutes = routes.filter((r) => r.type === "redirect").map((r) => {
189
+ return [r, ""];
190
+ });
140
191
  const trueRedirects = createRedirectsFromAstroRoutes({
141
192
  config: _config,
142
- routes: redirectRoutes,
193
+ routeToDynamicTargetMap: new Map(Array.from(redirectRoutes)),
143
194
  dir
144
195
  });
145
196
  if (!trueRedirects.empty()) {
@@ -161,12 +212,6 @@ function createIntegration(args) {
161
212
  )
162
213
  );
163
214
  }
164
- if (isModeDirectory) {
165
- const functionsUrl = new URL("functions/", _config.root);
166
- await fs.promises.mkdir(functionsUrl, { recursive: true });
167
- const directoryUrl = new URL("[[path]].js", functionsUrl);
168
- await fs.promises.rename(finalBuildUrl, directoryUrl);
169
- }
170
215
  }
171
216
  }
172
217
  };
@@ -6,4 +6,5 @@ export declare function createExports(manifest: SSRManifest): {
6
6
  next: (request: Request) => void;
7
7
  waitUntil: EventContext<unknown, any, unknown>['waitUntil'];
8
8
  } & Record<string, unknown>) => Promise<void | Response>;
9
+ manifest: SSRManifest;
9
10
  };
@@ -45,7 +45,7 @@ function createExports(manifest) {
45
45
  statusText: "Not found"
46
46
  });
47
47
  };
48
- return { onRequest };
48
+ return { onRequest, manifest };
49
49
  }
50
50
  export {
51
51
  createExports
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": "6.5.1",
4
+ "version": "6.6.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -32,27 +32,27 @@
32
32
  "runtime.d.ts"
33
33
  ],
34
34
  "dependencies": {
35
- "@astrojs/underscore-redirects": "^0.1.0",
35
+ "@astrojs/underscore-redirects": "^0.2.0",
36
36
  "@cloudflare/workers-types": "^4.20230518.0",
37
- "esbuild": "^0.17.12",
37
+ "esbuild": "^0.17.19",
38
38
  "tiny-glob": "^0.2.9"
39
39
  },
40
40
  "peerDependencies": {
41
- "astro": "^2.6.6"
41
+ "astro": "^2.8.2"
42
42
  },
43
43
  "devDependencies": {
44
- "chai": "^4.3.6",
45
- "cheerio": "^1.0.0-rc.11",
44
+ "chai": "^4.3.7",
45
+ "cheerio": "1.0.0-rc.12",
46
46
  "mocha": "^9.2.2",
47
- "slash": "^4.0.0",
48
47
  "wrangler": "^2.0.23",
49
- "astro": "2.6.6",
48
+ "astro": "2.8.2",
50
49
  "astro-scripts": "0.0.14"
51
50
  },
52
51
  "scripts": {
53
52
  "build": "astro-scripts build \"src/**/*.ts\" && tsc",
54
53
  "build:ci": "astro-scripts build \"src/**/*.ts\"",
55
54
  "dev": "astro-scripts dev \"src/**/*.ts\"",
56
- "test": "mocha --exit --timeout 30000 test/"
55
+ "test": "mocha --exit --timeout 30000 test/",
56
+ "test:match": "mocha --exit --timeout 30000 -g"
57
57
  }
58
58
  }