@astrojs/cloudflare 6.5.1 → 6.6.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/README.md +13 -12
- package/dist/index.js +72 -29
- package/dist/server.directory.d.ts +1 -0
- package/dist/server.directory.js +1 -1
- package/package.json +8 -8
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.
|
|
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:
|
|
60
|
+
adapter: cloudflare({ mode: 'directory' }),
|
|
58
61
|
});
|
|
59
|
-
|
|
60
62
|
```
|
|
61
63
|
|
|
62
64
|
## Enabling Preview
|
|
@@ -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
|
|
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
|
-
|
|
123
|
-
|
|
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": ({ manifest, entryPoints }) => {
|
|
74
|
+
_entryPoints = entryPoints;
|
|
75
|
+
},
|
|
71
76
|
"astro:build:done": async ({ pages, routes, dir }) => {
|
|
72
|
-
var _a, _b;
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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, _p3) => {
|
|
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
|
-
|
|
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"];
|
|
@@ -161,12 +210,6 @@ function createIntegration(args) {
|
|
|
161
210
|
)
|
|
162
211
|
);
|
|
163
212
|
}
|
|
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
213
|
}
|
|
171
214
|
}
|
|
172
215
|
};
|
package/dist/server.directory.js
CHANGED
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.
|
|
4
|
+
"version": "6.6.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -34,25 +34,25 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@astrojs/underscore-redirects": "^0.1.0",
|
|
36
36
|
"@cloudflare/workers-types": "^4.20230518.0",
|
|
37
|
-
"esbuild": "^0.17.
|
|
37
|
+
"esbuild": "^0.17.19",
|
|
38
38
|
"tiny-glob": "^0.2.9"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"astro": "^2.
|
|
41
|
+
"astro": "^2.7.3"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"chai": "^4.3.
|
|
45
|
-
"cheerio": "
|
|
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.
|
|
48
|
+
"astro": "2.7.3",
|
|
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
|
}
|