@torpor/adapter-cloudflare 0.2.1 → 0.2.3

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.
@@ -4,4 +4,4 @@ import { Adapter } from "@torpor/build";
4
4
  declare const _default: Adapter;
5
5
  //#endregion
6
6
  export { _default as cloudflare };
7
- //# sourceMappingURL=index.d.ts.map
7
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/adapter.ts"],"sourcesContent":[],"mappings":";;;cAMA,UAKuB"}
@@ -36,8 +36,8 @@ async function postbuild(site) {
36
36
  const tempFolder = path.join(distFolder, "temp");
37
37
  const cloudflareFolder = path.join(distFolder, "cloudflare");
38
38
  const buildSrcFolder = path.resolve(site.root, "./node_modules/@torpor/build/src/");
39
- const adapterSrcFolder = path.resolve(site.root, "./node_modules/@torpor/adapter-cloudflare/src/");
40
- let workerFile = path.join(adapterSrcFolder, "_worker.ts");
39
+ const adapterDistFolder = path.resolve(site.root, "./node_modules/@torpor/adapter-cloudflare/dist/");
40
+ let workerFile = path.join(adapterDistFolder, "_worker.ts");
41
41
  let workerSource = await promises.readFile(workerFile, "utf-8");
42
42
  let clientScript = (await promises.readdir(path.join(clientFolder, "assets"))).find((f) => f.startsWith("clientEntry-"));
43
43
  if (!clientScript) throw new Error("clientEntry.js not found");
@@ -91,4 +91,4 @@ _worker.js
91
91
 
92
92
  //#endregion
93
93
  export { adapter_default as cloudflare };
94
- //# sourceMappingURL=index.js.map
94
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["fs"],"sources":["../src/prepareTemplate.ts","../src/adapter.ts"],"sourcesContent":["export default function prepareTemplate(template: string, clientScript: string): string {\n\tlet result = template;\n\n\t// Put %COMPONENT_HEAD% before </head>\n\t// This is where styles will go\n\tlet headStart = result.indexOf(\"</head>\");\n\tif (headStart === -1) {\n\t\tthrow new Error(`Couldn't find <head> end tag`);\n\t}\n\tresult = result.substring(0, headStart) + \"%COMPONENT_HEAD%\" + result.substring(headStart);\n\n\t// Put %COMPONENT_BODY% inside <div id=\"app\"></div>\n\t// This is where the component's HTML will go\n\tlet bodyStart = regexIndexOf(result, /<div\\s+id=(\"app\"|'app'|app)\\s+/);\n\tbodyStart = result.indexOf(\">\", bodyStart) + 1;\n\tlet bodyEnd = result.indexOf(\"</div>\", bodyStart);\n\tif (bodyStart === -1 || bodyEnd === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\tresult = result.substring(0, bodyStart) + \"%COMPONENT_BODY%\" + result.substring(bodyEnd);\n\n\t// Add the clientEntry script at the very end, for hydrating and navigating\n\tresult += `<script type=\"module\" src=\"${clientScript}\"></script>`;\n\n\treturn result;\n}\n\n// From https://stackoverflow.com/a/274094\nfunction regexIndexOf(string: string, regex: RegExp, position?: number) {\n\tvar indexOf = string.substring(position || 0).search(regex);\n\treturn indexOf >= 0 ? indexOf + (position || 0) : indexOf;\n}\n","import { type Adapter, Site } from \"@torpor/build\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { build, defineConfig } from \"vite\";\nimport prepareTemplate from \"./prepareTemplate\";\n\nexport default {\n\tpostbuild,\n\tserve: (/* server: Server, site: Site*/) => {\n\t\tconsole.log(\"TODO: Cloudflare dev server\");\n\t},\n} satisfies Adapter as Adapter;\n\nasync function postbuild(site: Site): Promise<void> {\n\t// Build _worker.js as per\n\t// https://developers.cloudflare.com/pages/functions/advanced-mode/\n\n\tconst distFolder = path.resolve(site.root, \"dist\");\n\tconst clientFolder = path.join(distFolder, \"client\");\n\tconst serverFolder = path.join(distFolder, \"server\");\n\tconst tempFolder = path.join(distFolder, \"temp\");\n\tconst cloudflareFolder = path.join(distFolder, \"cloudflare\");\n\n\tconst buildSrcFolder = path.resolve(site.root, \"./node_modules/@torpor/build/src/\");\n\tconst adapterDistFolder = path.resolve(\n\t\tsite.root,\n\t\t\"./node_modules/@torpor/adapter-cloudflare/dist/\",\n\t);\n\n\t// Compile _worker.ts\n\tlet workerFile = path.join(adapterDistFolder, \"_worker.ts\");\n\tlet workerSource = await fs.readFile(workerFile, \"utf-8\");\n\n\t// Find the client script file in /assets\n\tlet clientScript = (await fs.readdir(path.join(clientFolder, \"assets\"))).find((f) =>\n\t\tf.startsWith(\"clientEntry-\"),\n\t);\n\tif (!clientScript) {\n\t\tthrow new Error(\"clientEntry.js not found\");\n\t}\n\tclientScript = `/assets/${clientScript}`;\n\n\t// Read and prepare site.html so that we can just splice components into it\n\tlet siteHtml = path.join(clientFolder, \"site.html\");\n\tlet template = await fs.readFile(siteHtml, \"utf-8\");\n\ttemplate = prepareTemplate(template, clientScript);\n\n\tconst serverClass = path.join(buildSrcFolder, \"server\", \"Server.ts\");\n\tconst serverScript = path.join(serverFolder, \"serverEntry.js\");\n\n\t// Splice file names into _worker.ts\n\tworkerSource = workerSource\n\t\t.replace(\"%SERVER_CLASS%\", serverClass)\n\t\t.replace(\"%SERVER_SCRIPT%\", serverScript)\n\t\t.replace(\"%HTML_TEMPLATE%\", template);\n\n\tworkerFile = path.join(tempFolder, \"_worker.ts\");\n\tawait fs.mkdir(tempFolder);\n\tawait fs.writeFile(workerFile, workerSource);\n\n\tconst cloudflareConfig = structuredClone(site.viteConfig ?? {});\n\tcloudflareConfig.build ??= {};\n\tcloudflareConfig.build.outDir = cloudflareFolder;\n\tcloudflareConfig.build.rollupOptions ??= {};\n\tcloudflareConfig.build.rollupOptions.input = [workerFile, ...site.inputs];\n\tcloudflareConfig.build.rollupOptions.output = {\n\t\tentryFileNames: `[name].js`,\n\t\tchunkFileNames: `[name].js`,\n\t\tassetFileNames: `[name].[ext]`,\n\t};\n\tcloudflareConfig.build.rollupOptions.preserveEntrySignatures = \"strict\";\n\t// HACK: We don't need to minify as the worker isn't downloaded?\n\t// And it makes debugging easier\n\tcloudflareConfig.build.minify = false;\n\t// HACK: Build for SSR so we don't get document and window references\n\tcloudflareConfig.build.ssr = true;\n\tawait build(defineConfig(cloudflareConfig));\n\n\t// Copy the client assets into the Cloudflare folder\n\tawait fs.mkdir(path.join(cloudflareFolder, \"assets\"));\n\tfor (let file of await fs.readdir(path.join(clientFolder, \"assets\"))) {\n\t\tawait fs.copyFile(\n\t\t\tpath.join(clientFolder, \"assets\", file),\n\t\t\tpath.join(cloudflareFolder, \"assets\", file),\n\t\t);\n\t}\n\n\t// Build a wrangler.toml file for running with `wrangler dev`\n\tconst wranglerConfig = `\nname = \"torpor-miniflare\"\nmain = \"./cloudflare/_worker.js\"\ncompatibility_date = \"2025-01-01\"\n\n[assets]\ndirectory = \"./cloudflare\"\nbinding = \"ASSETS\"\n\t`;\n\tconst wranglerFile = path.join(distFolder, \"wrangler.toml\");\n\tawait fs.writeFile(wranglerFile, wranglerConfig);\n\n\t// Build a .assetsignore file\n\tconst assetsIgnore = `\n**/node_modules\n**/.DS_Store\n**/.git\n_worker.js\n`;\n\tconst assetsIgnoreFile = path.join(cloudflareFolder, \".assetsignore\");\n\tawait fs.writeFile(assetsIgnoreFile, assetsIgnore);\n\n\tawait fs.rm(tempFolder, { recursive: true });\n}\n"],"mappings":";;;;;AAAA,SAAwB,gBAAgB,UAAkB,cAA8B;CACvF,IAAI,SAAS;CAIb,IAAI,YAAY,OAAO,QAAQ,UAAU;AACzC,KAAI,cAAc,GACjB,OAAM,IAAI,MAAM,+BAA+B;AAEhD,UAAS,OAAO,UAAU,GAAG,UAAU,GAAG,qBAAqB,OAAO,UAAU,UAAU;CAI1F,IAAI,YAAY,aAAa,QAAQ,iCAAiC;AACtE,aAAY,OAAO,QAAQ,KAAK,UAAU,GAAG;CAC7C,IAAI,UAAU,OAAO,QAAQ,UAAU,UAAU;AACjD,KAAI,cAAc,MAAM,YAAY,GACnC,OAAM,IAAI,MAAM,qCAAqC;AAEtD,UAAS,OAAO,UAAU,GAAG,UAAU,GAAG,qBAAqB,OAAO,UAAU,QAAQ;AAGxF,WAAU,8BAA8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OAAMA,SAAG,MAAM,WAAW;AAC1B,OAAMA,SAAG,UAAU,YAAY,aAAa;CAE5C,MAAM,mBAAmB,gBAAgB,KAAK,cAAc,EAAE,CAAC;AAC/D,kBAAiB,UAAU,EAAE;AAC7B,kBAAiB,MAAM,SAAS;AAChC,kBAAiB,MAAM,kBAAkB,EAAE;AAC3C,kBAAiB,MAAM,cAAc,QAAQ,CAAC,YAAY,GAAG,KAAK,OAAO;AACzE,kBAAiB,MAAM,cAAc,SAAS;EAC7C,gBAAgB;EAChB,gBAAgB;EAChB,gBAAgB;EAChB;AACD,kBAAiB,MAAM,cAAc,0BAA0B;AAG/D,kBAAiB,MAAM,SAAS;AAEhC,kBAAiB,MAAM,MAAM;AAC7B,OAAM,MAAM,aAAa,iBAAiB,CAAC;AAG3C,OAAMA,SAAG,MAAM,KAAK,KAAK,kBAAkB,SAAS,CAAC;AACrD,MAAK,IAAI,QAAQ,MAAMA,SAAG,QAAQ,KAAK,KAAK,cAAc,SAAS,CAAC,CACnE,OAAMA,SAAG,SACR,KAAK,KAAK,cAAc,UAAU,KAAK,EACvC,KAAK,KAAK,kBAAkB,UAAU,KAAK,CAC3C;CAIF,MAAM,iBAAiB;;;;;;;;;CASvB,MAAM,eAAe,KAAK,KAAK,YAAY,gBAAgB;AAC3D,OAAMA,SAAG,UAAU,cAAc,eAAe;CAGhD,MAAM,eAAe;;;;;;CAMrB,MAAM,mBAAmB,KAAK,KAAK,kBAAkB,gBAAgB;AACrE,OAAMA,SAAG,UAAU,kBAAkB,aAAa;AAElD,OAAMA,SAAG,GAAG,YAAY,EAAE,WAAW,MAAM,CAAC"}
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@torpor/adapter-cloudflare",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "A @torpor/build adapter for deploying to Cloudflare Pages",
5
5
  "type": "module",
6
6
  "exports": {
7
- "types": "./dist/index.d.ts",
8
- "import": "./dist/index.js"
7
+ "types": "./dist/index.d.mts",
8
+ "import": "./dist/index.mjs"
9
9
  },
10
+ "files": [
11
+ "dist"
12
+ ],
10
13
  "publishConfig": {
11
14
  "access": "public"
12
15
  },
@@ -14,19 +17,19 @@
14
17
  "author": "",
15
18
  "license": "ISC",
16
19
  "dependencies": {
17
- "vite": "^7.1.12",
18
- "@torpor/build": "^0.4.0"
20
+ "vite": "^7.2.2",
21
+ "@torpor/build": "^0.4.11"
19
22
  },
20
23
  "devDependencies": {
21
- "@trivago/prettier-plugin-sort-imports": "^5.2.2",
22
- "@typescript/native-preview": "7.0.0-dev.20251027.1",
23
- "oxlint-tsgolint": "^0.3.0",
24
- "tsdown": "^0.15.11",
24
+ "@trivago/prettier-plugin-sort-imports": "^6.0.0",
25
+ "@typescript/native-preview": "7.0.0-dev.20251112.1",
26
+ "oxlint-tsgolint": "^0.6.0",
27
+ "tsdown": "^0.16.4",
25
28
  "typescript": "^5.9.3"
26
29
  },
27
30
  "scripts": {
31
+ "check": "tsgo --noEmit && pnpm dlx oxlint --type-aware",
28
32
  "build": "tsgo --noEmit && tsdown",
29
- "build:watch": "tsdown --watch",
30
- "check": "tsgo --noEmit && pnpm dlx oxlint --type-aware"
33
+ "build:watch": "tsdown --watch"
31
34
  }
32
35
  }
package/CHANGELOG.md DELETED
@@ -1,256 +0,0 @@
1
- # @torpor/adapter-cloudflare
2
-
3
- ## 0.2.1
4
-
5
- ### Patch Changes
6
-
7
- - Updated dependencies [168dff3]
8
- - Updated dependencies [051998b]
9
- - @torpor/build@0.4.0
10
-
11
- ## 0.2.0
12
-
13
- ### Minor Changes
14
-
15
- - 51dbba7: Chore: drop CJS build and go ESM only
16
-
17
- ### Patch Changes
18
-
19
- - Updated dependencies [bbdc8d3]
20
- - Updated dependencies [e1ae2bb]
21
- - Updated dependencies [51dbba7]
22
- - Updated dependencies [62b0d2e]
23
- - @torpor/build@0.3.0
24
-
25
- ## 0.1.0
26
-
27
- ### Minor Changes
28
-
29
- - b716d5c: Feat: inline styles during SSR
30
-
31
- ### Patch Changes
32
-
33
- - Updated dependencies [a4f2573]
34
- - Updated dependencies [67e6ca6]
35
- - Updated dependencies [b716d5c]
36
- - @torpor/build@0.2.0
37
-
38
- ## 0.0.33
39
-
40
- ### Patch Changes
41
-
42
- - @torpor/build@0.1.31
43
-
44
- ## 0.0.32
45
-
46
- ### Patch Changes
47
-
48
- - @torpor/build@0.1.30
49
-
50
- ## 0.0.31
51
-
52
- ### Patch Changes
53
-
54
- - @torpor/build@0.1.29
55
-
56
- ## 0.0.30
57
-
58
- ### Patch Changes
59
-
60
- - Updated dependencies [fa85b57]
61
- - @torpor/build@0.1.28
62
-
63
- ## 0.0.29
64
-
65
- ### Patch Changes
66
-
67
- - Updated dependencies [bed55d5]
68
- - @torpor/build@0.1.27
69
-
70
- ## 0.0.28
71
-
72
- ### Patch Changes
73
-
74
- - Updated dependencies [348cd75]
75
- - @torpor/build@0.1.26
76
-
77
- ## 0.0.27
78
-
79
- ### Patch Changes
80
-
81
- - Updated dependencies [3fe0d0e]
82
- - Updated dependencies [f26603f]
83
- - @torpor/build@0.1.25
84
-
85
- ## 0.0.26
86
-
87
- ### Patch Changes
88
-
89
- - d6b06ab: Fix: move from Cloudflare Pages to Workers
90
- - Updated dependencies [6092a91]
91
- - @torpor/build@0.1.24
92
-
93
- ## 0.0.25
94
-
95
- ### Patch Changes
96
-
97
- - c37269f: Fix: move from Cloudflare Pages to Workers
98
-
99
- ## 0.0.24
100
-
101
- ### Patch Changes
102
-
103
- - 22f62d4: Fix: Site.viteConfig settings
104
- - Updated dependencies [22f62d4]
105
- - @torpor/build@0.1.23
106
-
107
- ## 0.0.23
108
-
109
- ### Patch Changes
110
-
111
- - Updated dependencies [4e0ec90]
112
- - @torpor/build@0.1.22
113
-
114
- ## 0.0.22
115
-
116
- ### Patch Changes
117
-
118
- - Updated dependencies [4296742]
119
- - @torpor/build@0.1.21
120
-
121
- ## 0.0.21
122
-
123
- ### Patch Changes
124
-
125
- - Updated dependencies [a61f30f]
126
- - @torpor/build@0.1.20
127
-
128
- ## 0.0.20
129
-
130
- ### Patch Changes
131
-
132
- - Updated dependencies [170c886]
133
- - @torpor/build@0.1.19
134
-
135
- ## 0.0.19
136
-
137
- ### Patch Changes
138
-
139
- - @torpor/build@0.1.18
140
-
141
- ## 0.0.18
142
-
143
- ### Patch Changes
144
-
145
- - Updated dependencies [7373f27]
146
- - Updated dependencies [679a448]
147
- - Updated dependencies [e5b761b]
148
- - @torpor/build@0.1.17
149
-
150
- ## 0.0.17
151
-
152
- ### Patch Changes
153
-
154
- - Updated dependencies [9332635]
155
- - @torpor/build@0.1.16
156
-
157
- ## 0.0.16
158
-
159
- ### Patch Changes
160
-
161
- - @torpor/build@0.1.15
162
-
163
- ## 0.0.15
164
-
165
- ### Patch Changes
166
-
167
- - Updated dependencies [e395c9e]
168
- - @torpor/build@0.1.14
169
-
170
- ## 0.0.14
171
-
172
- ### Patch Changes
173
-
174
- - 2272579: Feat: allow passing extra inputs to build
175
-
176
- ## 0.0.13
177
-
178
- ### Patch Changes
179
-
180
- - Updated dependencies [02eac8e]
181
- - @torpor/build@0.1.13
182
-
183
- ## 0.0.12
184
-
185
- ### Patch Changes
186
-
187
- - 018728f: Fix: build for server (not browser)
188
-
189
- ## 0.0.11
190
-
191
- ### Patch Changes
192
-
193
- - 3ed8a23: Chore: specify exports in packages
194
-
195
- ## 0.0.10
196
-
197
- ### Patch Changes
198
-
199
- - Updated dependencies [8de094b]
200
- - @torpor/build@0.1.12
201
-
202
- ## 0.0.9
203
-
204
- ### Patch Changes
205
-
206
- - 858538b: Fix: move adapter to globalThis, pass it to server loads
207
- - Updated dependencies [858538b]
208
- - @torpor/build@0.1.11
209
-
210
- ## 0.0.8
211
-
212
- ### Patch Changes
213
-
214
- - 5df2010: Fix: force disabling Vite preload
215
-
216
- ## 0.0.7
217
-
218
- ### Patch Changes
219
-
220
- - @torpor/build@0.1.10
221
-
222
- ## 0.0.6
223
-
224
- ### Patch Changes
225
-
226
- - 095c6c5: Feat: `adapter` object for adapter functionality
227
- - Updated dependencies [ff685cb]
228
- - @torpor/build@0.1.9
229
-
230
- ## 0.0.5
231
-
232
- ### Patch Changes
233
-
234
- - Updated dependencies [fe7444e]
235
- - @torpor/build@0.1.8
236
-
237
- ## 0.0.4
238
-
239
- ### Patch Changes
240
-
241
- - @torpor/build@0.1.7
242
-
243
- ## 0.0.3
244
-
245
- ### Patch Changes
246
-
247
- - 47c5277: Chore: update dependencies
248
- - Updated dependencies [47c5277]
249
- - @torpor/build@0.1.6
250
-
251
- ## 0.0.2
252
-
253
- ### Patch Changes
254
-
255
- - Updated dependencies [5c9d90f]
256
- - @torpor/build@0.1.5
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/adapter.ts"],"sourcesContent":[],"mappings":";;;cAMA,UAKuB"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","names":["fs"],"sources":["../src/prepareTemplate.ts","../src/adapter.ts"],"sourcesContent":["export default function prepareTemplate(template: string, clientScript: string): string {\n\tlet result = template;\n\n\t// Put %COMPONENT_HEAD% before </head>\n\t// This is where styles will go\n\tlet headStart = result.indexOf(\"</head>\");\n\tif (headStart === -1) {\n\t\tthrow new Error(`Couldn't find <head> end tag`);\n\t}\n\tresult = result.substring(0, headStart) + \"%COMPONENT_HEAD%\" + result.substring(headStart);\n\n\t// Put %COMPONENT_BODY% inside <div id=\"app\"></div>\n\t// This is where the component's HTML will go\n\tlet bodyStart = regexIndexOf(result, /<div\\s+id=(\"app\"|'app'|app)\\s+/);\n\tbodyStart = result.indexOf(\">\", bodyStart) + 1;\n\tlet bodyEnd = result.indexOf(\"</div>\", bodyStart);\n\tif (bodyStart === -1 || bodyEnd === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\tresult = result.substring(0, bodyStart) + \"%COMPONENT_BODY%\" + result.substring(bodyEnd);\n\n\t// Add the clientEntry script at the very end, for hydrating and navigating\n\tresult += `<script type=\"module\" src=\"${clientScript}\"></script>`;\n\n\treturn result;\n}\n\n// From https://stackoverflow.com/a/274094\nfunction regexIndexOf(string: string, regex: RegExp, position?: number) {\n\tvar indexOf = string.substring(position || 0).search(regex);\n\treturn indexOf >= 0 ? indexOf + (position || 0) : indexOf;\n}\n","import { type Adapter, Site } from \"@torpor/build\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { build, defineConfig } from \"vite\";\nimport prepareTemplate from \"./prepareTemplate\";\n\nexport default {\n\tpostbuild,\n\tserve: (/* server: Server, site: Site*/) => {\n\t\tconsole.log(\"TODO: Cloudflare dev server\");\n\t},\n} satisfies Adapter as Adapter;\n\nasync function postbuild(site: Site): Promise<void> {\n\t// Build _worker.js as per\n\t// https://developers.cloudflare.com/pages/functions/advanced-mode/\n\n\tconst distFolder = path.resolve(site.root, \"dist\");\n\tconst clientFolder = path.join(distFolder, \"client\");\n\tconst serverFolder = path.join(distFolder, \"server\");\n\tconst tempFolder = path.join(distFolder, \"temp\");\n\tconst cloudflareFolder = path.join(distFolder, \"cloudflare\");\n\n\tconst buildSrcFolder = path.resolve(site.root, \"./node_modules/@torpor/build/src/\");\n\tconst adapterSrcFolder = path.resolve(\n\t\tsite.root,\n\t\t\"./node_modules/@torpor/adapter-cloudflare/src/\",\n\t);\n\n\t// Compile _worker.ts\n\tlet workerFile = path.join(adapterSrcFolder, \"_worker.ts\");\n\tlet workerSource = await fs.readFile(workerFile, \"utf-8\");\n\n\t// Find the client script file in /assets\n\tlet clientScript = (await fs.readdir(path.join(clientFolder, \"assets\"))).find((f) =>\n\t\tf.startsWith(\"clientEntry-\"),\n\t);\n\tif (!clientScript) {\n\t\tthrow new Error(\"clientEntry.js not found\");\n\t}\n\tclientScript = `/assets/${clientScript}`;\n\n\t// Read and prepare site.html so that we can just splice components into it\n\tlet siteHtml = path.join(clientFolder, \"site.html\");\n\tlet template = await fs.readFile(siteHtml, \"utf-8\");\n\ttemplate = prepareTemplate(template, clientScript);\n\n\tconst serverClass = path.join(buildSrcFolder, \"server\", \"Server.ts\");\n\tconst serverScript = path.join(serverFolder, \"serverEntry.js\");\n\n\t// Splice file names into _worker.ts\n\tworkerSource = workerSource\n\t\t.replace(\"%SERVER_CLASS%\", serverClass)\n\t\t.replace(\"%SERVER_SCRIPT%\", serverScript)\n\t\t.replace(\"%HTML_TEMPLATE%\", template);\n\n\tworkerFile = path.join(tempFolder, \"_worker.ts\");\n\tawait fs.mkdir(tempFolder);\n\tawait fs.writeFile(workerFile, workerSource);\n\n\tconst cloudflareConfig = structuredClone(site.viteConfig ?? {});\n\tcloudflareConfig.build ??= {};\n\tcloudflareConfig.build.outDir = cloudflareFolder;\n\tcloudflareConfig.build.rollupOptions ??= {};\n\tcloudflareConfig.build.rollupOptions.input = [workerFile, ...site.inputs];\n\tcloudflareConfig.build.rollupOptions.output = {\n\t\tentryFileNames: `[name].js`,\n\t\tchunkFileNames: `[name].js`,\n\t\tassetFileNames: `[name].[ext]`,\n\t};\n\tcloudflareConfig.build.rollupOptions.preserveEntrySignatures = \"strict\";\n\t// HACK: We don't need to minify as the worker isn't downloaded?\n\t// And it makes debugging easier\n\tcloudflareConfig.build.minify = false;\n\t// HACK: Build for SSR so we don't get document and window references\n\tcloudflareConfig.build.ssr = true;\n\tawait build(defineConfig(cloudflareConfig));\n\n\t// Copy the client assets into the Cloudflare folder\n\tawait fs.mkdir(path.join(cloudflareFolder, \"assets\"));\n\tfor (let file of await fs.readdir(path.join(clientFolder, \"assets\"))) {\n\t\tawait fs.copyFile(\n\t\t\tpath.join(clientFolder, \"assets\", file),\n\t\t\tpath.join(cloudflareFolder, \"assets\", file),\n\t\t);\n\t}\n\n\t// Build a wrangler.toml file for running with `wrangler dev`\n\tconst wranglerConfig = `\nname = \"torpor-miniflare\"\nmain = \"./cloudflare/_worker.js\"\ncompatibility_date = \"2025-01-01\"\n\n[assets]\ndirectory = \"./cloudflare\"\nbinding = \"ASSETS\"\n\t`;\n\tconst wranglerFile = path.join(distFolder, \"wrangler.toml\");\n\tawait fs.writeFile(wranglerFile, wranglerConfig);\n\n\t// Build a .assetsignore file\n\tconst assetsIgnore = `\n**/node_modules\n**/.DS_Store\n**/.git\n_worker.js\n`;\n\tconst assetsIgnoreFile = path.join(cloudflareFolder, \".assetsignore\");\n\tawait fs.writeFile(assetsIgnoreFile, assetsIgnore);\n\n\tawait fs.rm(tempFolder, { recursive: true });\n}\n"],"mappings":";;;;;AAAA,SAAwB,gBAAgB,UAAkB,cAA8B;CACvF,IAAI,SAAS;CAIb,IAAI,YAAY,OAAO,QAAQ,UAAU;AACzC,KAAI,cAAc,GACjB,OAAM,IAAI,MAAM,+BAA+B;AAEhD,UAAS,OAAO,UAAU,GAAG,UAAU,GAAG,qBAAqB,OAAO,UAAU,UAAU;CAI1F,IAAI,YAAY,aAAa,QAAQ,iCAAiC;AACtE,aAAY,OAAO,QAAQ,KAAK,UAAU,GAAG;CAC7C,IAAI,UAAU,OAAO,QAAQ,UAAU,UAAU;AACjD,KAAI,cAAc,MAAM,YAAY,GACnC,OAAM,IAAI,MAAM,qCAAqC;AAEtD,UAAS,OAAO,UAAU,GAAG,UAAU,GAAG,qBAAqB,OAAO,UAAU,QAAQ;AAGxF,WAAU,8BAA8B,aAAa;AAErD,QAAO;;AAIR,SAAS,aAAa,QAAgB,OAAe,UAAmB;CACvE,IAAI,UAAU,OAAO,UAAU,YAAY,EAAE,CAAC,OAAO,MAAM;AAC3D,QAAO,WAAW,IAAI,WAAW,YAAY,KAAK;;;;;ACxBnD,sBAAe;CACd;CACA,aAA4C;AAC3C,UAAQ,IAAI,8BAA8B;;CAE3C;AAED,eAAe,UAAU,MAA2B;CAInD,MAAM,aAAa,KAAK,QAAQ,KAAK,MAAM,OAAO;CAClD,MAAM,eAAe,KAAK,KAAK,YAAY,SAAS;CACpD,MAAM,eAAe,KAAK,KAAK,YAAY,SAAS;CACpD,MAAM,aAAa,KAAK,KAAK,YAAY,OAAO;CAChD,MAAM,mBA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AAM,MAAM,aAAa,iBAAiB,CAAC;AAG3C,OAAMA,SAAG,MAAM,KAAK,KAAK,kBAAkB,SAAS,CAAC;AACrD,MAAK,IAAI,QAAQ,MAAMA,SAAG,QAAQ,KAAK,KAAK,cAAc,SAAS,CAAC,CACnE,OAAMA,SAAG,SACR,KAAK,KAAK,cAAc,UAAU,KAAK,EACvC,KAAK,KAAK,kBAAkB,UAAU,KAAK,CAC3C;CAIF,MAAM,iBAAiB;;;;;;;;;CASvB,MAAM,eAAe,KAAK,KAAK,YAAY,gBAAgB;AAC3D,OAAMA,SAAG,UAAU,cAAc,eAAe;CAGhD,MAAM,eAAe;;;;;;CAMrB,MAAM,mBAAmB,KAAK,KAAK,kBAAkB,gBAAgB;AACrE,OAAMA,SAAG,UAAU,kBAAkB,aAAa;AAElD,OAAMA,SAAG,GAAG,YAAY,EAAE,WAAW,MAAM,CAAC"}
package/src/adapter.ts DELETED
@@ -1,112 +0,0 @@
1
- import { type Adapter, Site } from "@torpor/build";
2
- import { promises as fs } from "node:fs";
3
- import path from "node:path";
4
- import { build, defineConfig } from "vite";
5
- import prepareTemplate from "./prepareTemplate";
6
-
7
- export default {
8
- postbuild,
9
- serve: (/* server: Server, site: Site*/) => {
10
- console.log("TODO: Cloudflare dev server");
11
- },
12
- } satisfies Adapter as Adapter;
13
-
14
- async function postbuild(site: Site): Promise<void> {
15
- // Build _worker.js as per
16
- // https://developers.cloudflare.com/pages/functions/advanced-mode/
17
-
18
- const distFolder = path.resolve(site.root, "dist");
19
- const clientFolder = path.join(distFolder, "client");
20
- const serverFolder = path.join(distFolder, "server");
21
- const tempFolder = path.join(distFolder, "temp");
22
- const cloudflareFolder = path.join(distFolder, "cloudflare");
23
-
24
- const buildSrcFolder = path.resolve(site.root, "./node_modules/@torpor/build/src/");
25
- const adapterSrcFolder = path.resolve(
26
- site.root,
27
- "./node_modules/@torpor/adapter-cloudflare/src/",
28
- );
29
-
30
- // Compile _worker.ts
31
- let workerFile = path.join(adapterSrcFolder, "_worker.ts");
32
- let workerSource = await fs.readFile(workerFile, "utf-8");
33
-
34
- // Find the client script file in /assets
35
- let clientScript = (await fs.readdir(path.join(clientFolder, "assets"))).find((f) =>
36
- f.startsWith("clientEntry-"),
37
- );
38
- if (!clientScript) {
39
- throw new Error("clientEntry.js not found");
40
- }
41
- clientScript = `/assets/${clientScript}`;
42
-
43
- // Read and prepare site.html so that we can just splice components into it
44
- let siteHtml = path.join(clientFolder, "site.html");
45
- let template = await fs.readFile(siteHtml, "utf-8");
46
- template = prepareTemplate(template, clientScript);
47
-
48
- const serverClass = path.join(buildSrcFolder, "server", "Server.ts");
49
- const serverScript = path.join(serverFolder, "serverEntry.js");
50
-
51
- // Splice file names into _worker.ts
52
- workerSource = workerSource
53
- .replace("%SERVER_CLASS%", serverClass)
54
- .replace("%SERVER_SCRIPT%", serverScript)
55
- .replace("%HTML_TEMPLATE%", template);
56
-
57
- workerFile = path.join(tempFolder, "_worker.ts");
58
- await fs.mkdir(tempFolder);
59
- await fs.writeFile(workerFile, workerSource);
60
-
61
- const cloudflareConfig = structuredClone(site.viteConfig ?? {});
62
- cloudflareConfig.build ??= {};
63
- cloudflareConfig.build.outDir = cloudflareFolder;
64
- cloudflareConfig.build.rollupOptions ??= {};
65
- cloudflareConfig.build.rollupOptions.input = [workerFile, ...site.inputs];
66
- cloudflareConfig.build.rollupOptions.output = {
67
- entryFileNames: `[name].js`,
68
- chunkFileNames: `[name].js`,
69
- assetFileNames: `[name].[ext]`,
70
- };
71
- cloudflareConfig.build.rollupOptions.preserveEntrySignatures = "strict";
72
- // HACK: We don't need to minify as the worker isn't downloaded?
73
- // And it makes debugging easier
74
- cloudflareConfig.build.minify = false;
75
- // HACK: Build for SSR so we don't get document and window references
76
- cloudflareConfig.build.ssr = true;
77
- await build(defineConfig(cloudflareConfig));
78
-
79
- // Copy the client assets into the Cloudflare folder
80
- await fs.mkdir(path.join(cloudflareFolder, "assets"));
81
- for (let file of await fs.readdir(path.join(clientFolder, "assets"))) {
82
- await fs.copyFile(
83
- path.join(clientFolder, "assets", file),
84
- path.join(cloudflareFolder, "assets", file),
85
- );
86
- }
87
-
88
- // Build a wrangler.toml file for running with `wrangler dev`
89
- const wranglerConfig = `
90
- name = "torpor-miniflare"
91
- main = "./cloudflare/_worker.js"
92
- compatibility_date = "2025-01-01"
93
-
94
- [assets]
95
- directory = "./cloudflare"
96
- binding = "ASSETS"
97
- `;
98
- const wranglerFile = path.join(distFolder, "wrangler.toml");
99
- await fs.writeFile(wranglerFile, wranglerConfig);
100
-
101
- // Build a .assetsignore file
102
- const assetsIgnore = `
103
- **/node_modules
104
- **/.DS_Store
105
- **/.git
106
- _worker.js
107
- `;
108
- const assetsIgnoreFile = path.join(cloudflareFolder, ".assetsignore");
109
- await fs.writeFile(assetsIgnoreFile, assetsIgnore);
110
-
111
- await fs.rm(tempFolder, { recursive: true });
112
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- import cloudflare from "./adapter";
2
-
3
- export { cloudflare };
@@ -1,32 +0,0 @@
1
- export default function prepareTemplate(template: string, clientScript: string): string {
2
- let result = template;
3
-
4
- // Put %COMPONENT_HEAD% before </head>
5
- // This is where styles will go
6
- let headStart = result.indexOf("</head>");
7
- if (headStart === -1) {
8
- throw new Error(`Couldn't find <head> end tag`);
9
- }
10
- result = result.substring(0, headStart) + "%COMPONENT_HEAD%" + result.substring(headStart);
11
-
12
- // Put %COMPONENT_BODY% inside <div id="app"></div>
13
- // This is where the component's HTML will go
14
- let bodyStart = regexIndexOf(result, /<div\s+id=("app"|'app'|app)\s+/);
15
- bodyStart = result.indexOf(">", bodyStart) + 1;
16
- let bodyEnd = result.indexOf("</div>", bodyStart);
17
- if (bodyStart === -1 || bodyEnd === -1) {
18
- throw new Error(`Couldn't find <div id="app"></div>`);
19
- }
20
- result = result.substring(0, bodyStart) + "%COMPONENT_BODY%" + result.substring(bodyEnd);
21
-
22
- // Add the clientEntry script at the very end, for hydrating and navigating
23
- result += `<script type="module" src="${clientScript}"></script>`;
24
-
25
- return result;
26
- }
27
-
28
- // From https://stackoverflow.com/a/274094
29
- function regexIndexOf(string: string, regex: RegExp, position?: number) {
30
- var indexOf = string.substring(position || 0).search(regex);
31
- return indexOf >= 0 ? indexOf + (position || 0) : indexOf;
32
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../../tsconfig.json",
3
- "compilerOptions": {
4
- // HACK:
5
- "skipLibCheck": true,
6
- "verbatimModuleSyntax": false
7
- },
8
- "include": ["src"]
9
- }
package/tsdown.config.ts DELETED
@@ -1,13 +0,0 @@
1
- import { type Options, defineConfig } from "tsdown";
2
-
3
- type Config =
4
- | Options
5
- | Options[]
6
- | ((overrideOptions: Options) => Options | Options[] | Promise<Options | Options[]>);
7
-
8
- export default defineConfig({
9
- entry: ["src/index.ts"],
10
- format: "esm",
11
- dts: true,
12
- sourcemap: true,
13
- }) satisfies Config as Config;
File without changes