@torpor/adapter-cloudflare 0.2.0 → 0.2.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.
@@ -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"}
@@ -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 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,mBAAmB,KAAK,KAAK,YAAY,aAAa;CAE5D,MAA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AAMA,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.0",
3
+ "version": "0.2.2",
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.9",
18
- "@torpor/build": "^0.3.0"
20
+ "vite": "^7.2.2",
21
+ "@torpor/build": "^0.4.10"
19
22
  },
20
23
  "devDependencies": {
21
- "@trivago/prettier-plugin-sort-imports": "^5.2.2",
22
- "@typescript/native-preview": "7.0.0-dev.20251005.1",
23
- "oxlint-tsgolint": "^0.2.0",
24
- "tsdown": "^0.15.6",
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,248 +0,0 @@
1
- # @torpor/adapter-cloudflare
2
-
3
- ## 0.2.0
4
-
5
- ### Minor Changes
6
-
7
- - 51dbba7: Chore: drop CJS build and go ESM only
8
-
9
- ### Patch Changes
10
-
11
- - Updated dependencies [bbdc8d3]
12
- - Updated dependencies [e1ae2bb]
13
- - Updated dependencies [51dbba7]
14
- - Updated dependencies [62b0d2e]
15
- - @torpor/build@0.3.0
16
-
17
- ## 0.1.0
18
-
19
- ### Minor Changes
20
-
21
- - b716d5c: Feat: inline styles during SSR
22
-
23
- ### Patch Changes
24
-
25
- - Updated dependencies [a4f2573]
26
- - Updated dependencies [67e6ca6]
27
- - Updated dependencies [b716d5c]
28
- - @torpor/build@0.2.0
29
-
30
- ## 0.0.33
31
-
32
- ### Patch Changes
33
-
34
- - @torpor/build@0.1.31
35
-
36
- ## 0.0.32
37
-
38
- ### Patch Changes
39
-
40
- - @torpor/build@0.1.30
41
-
42
- ## 0.0.31
43
-
44
- ### Patch Changes
45
-
46
- - @torpor/build@0.1.29
47
-
48
- ## 0.0.30
49
-
50
- ### Patch Changes
51
-
52
- - Updated dependencies [fa85b57]
53
- - @torpor/build@0.1.28
54
-
55
- ## 0.0.29
56
-
57
- ### Patch Changes
58
-
59
- - Updated dependencies [bed55d5]
60
- - @torpor/build@0.1.27
61
-
62
- ## 0.0.28
63
-
64
- ### Patch Changes
65
-
66
- - Updated dependencies [348cd75]
67
- - @torpor/build@0.1.26
68
-
69
- ## 0.0.27
70
-
71
- ### Patch Changes
72
-
73
- - Updated dependencies [3fe0d0e]
74
- - Updated dependencies [f26603f]
75
- - @torpor/build@0.1.25
76
-
77
- ## 0.0.26
78
-
79
- ### Patch Changes
80
-
81
- - d6b06ab: Fix: move from Cloudflare Pages to Workers
82
- - Updated dependencies [6092a91]
83
- - @torpor/build@0.1.24
84
-
85
- ## 0.0.25
86
-
87
- ### Patch Changes
88
-
89
- - c37269f: Fix: move from Cloudflare Pages to Workers
90
-
91
- ## 0.0.24
92
-
93
- ### Patch Changes
94
-
95
- - 22f62d4: Fix: Site.viteConfig settings
96
- - Updated dependencies [22f62d4]
97
- - @torpor/build@0.1.23
98
-
99
- ## 0.0.23
100
-
101
- ### Patch Changes
102
-
103
- - Updated dependencies [4e0ec90]
104
- - @torpor/build@0.1.22
105
-
106
- ## 0.0.22
107
-
108
- ### Patch Changes
109
-
110
- - Updated dependencies [4296742]
111
- - @torpor/build@0.1.21
112
-
113
- ## 0.0.21
114
-
115
- ### Patch Changes
116
-
117
- - Updated dependencies [a61f30f]
118
- - @torpor/build@0.1.20
119
-
120
- ## 0.0.20
121
-
122
- ### Patch Changes
123
-
124
- - Updated dependencies [170c886]
125
- - @torpor/build@0.1.19
126
-
127
- ## 0.0.19
128
-
129
- ### Patch Changes
130
-
131
- - @torpor/build@0.1.18
132
-
133
- ## 0.0.18
134
-
135
- ### Patch Changes
136
-
137
- - Updated dependencies [7373f27]
138
- - Updated dependencies [679a448]
139
- - Updated dependencies [e5b761b]
140
- - @torpor/build@0.1.17
141
-
142
- ## 0.0.17
143
-
144
- ### Patch Changes
145
-
146
- - Updated dependencies [9332635]
147
- - @torpor/build@0.1.16
148
-
149
- ## 0.0.16
150
-
151
- ### Patch Changes
152
-
153
- - @torpor/build@0.1.15
154
-
155
- ## 0.0.15
156
-
157
- ### Patch Changes
158
-
159
- - Updated dependencies [e395c9e]
160
- - @torpor/build@0.1.14
161
-
162
- ## 0.0.14
163
-
164
- ### Patch Changes
165
-
166
- - 2272579: Feat: allow passing extra inputs to build
167
-
168
- ## 0.0.13
169
-
170
- ### Patch Changes
171
-
172
- - Updated dependencies [02eac8e]
173
- - @torpor/build@0.1.13
174
-
175
- ## 0.0.12
176
-
177
- ### Patch Changes
178
-
179
- - 018728f: Fix: build for server (not browser)
180
-
181
- ## 0.0.11
182
-
183
- ### Patch Changes
184
-
185
- - 3ed8a23: Chore: specify exports in packages
186
-
187
- ## 0.0.10
188
-
189
- ### Patch Changes
190
-
191
- - Updated dependencies [8de094b]
192
- - @torpor/build@0.1.12
193
-
194
- ## 0.0.9
195
-
196
- ### Patch Changes
197
-
198
- - 858538b: Fix: move adapter to globalThis, pass it to server loads
199
- - Updated dependencies [858538b]
200
- - @torpor/build@0.1.11
201
-
202
- ## 0.0.8
203
-
204
- ### Patch Changes
205
-
206
- - 5df2010: Fix: force disabling Vite preload
207
-
208
- ## 0.0.7
209
-
210
- ### Patch Changes
211
-
212
- - @torpor/build@0.1.10
213
-
214
- ## 0.0.6
215
-
216
- ### Patch Changes
217
-
218
- - 095c6c5: Feat: `adapter` object for adapter functionality
219
- - Updated dependencies [ff685cb]
220
- - @torpor/build@0.1.9
221
-
222
- ## 0.0.5
223
-
224
- ### Patch Changes
225
-
226
- - Updated dependencies [fe7444e]
227
- - @torpor/build@0.1.8
228
-
229
- ## 0.0.4
230
-
231
- ### Patch Changes
232
-
233
- - @torpor/build@0.1.7
234
-
235
- ## 0.0.3
236
-
237
- ### Patch Changes
238
-
239
- - 47c5277: Chore: update dependencies
240
- - Updated dependencies [47c5277]
241
- - @torpor/build@0.1.6
242
-
243
- ## 0.0.2
244
-
245
- ### Patch Changes
246
-
247
- - Updated dependencies [5c9d90f]
248
- - @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\t// TODO: Is this going to be the correct path after installing from npm?\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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ED
@@ -1,44 +0,0 @@
1
- // @ts-ignore
2
- import Server from "%SERVER_CLASS%";
3
- // @ts-ignore
4
- import { load } from "%SERVER_SCRIPT%";
5
-
6
- // This file has the above imports replaced and is then compiled to create a
7
- // Cloudflare Pages worker file
8
-
9
- const template = `%HTML_TEMPLATE%`;
10
-
11
- // Send all requests through Server to handle middleware, cookies, headers, etc
12
- const server = new Server();
13
- server.add("*", async (ev: any) => {
14
- try {
15
- // Render the app HTML (or fetch server data etc) via serverEntry's
16
- // exported `load` function
17
- return await load(ev, template);
18
- } catch (e: any) {
19
- // TODO: Message?
20
- console.log("ERROR:", e);
21
- return new Response(null, {
22
- status: 500,
23
- });
24
- }
25
- });
26
-
27
- export default {
28
- async fetch(request: Request, env: any): Promise<any> {
29
- // Put adapter-specific functionality in the `adapter` property of
30
- // globalThis for now
31
- // @ts-ignore
32
- globalThis.adapter = { env };
33
-
34
- const url = new URL(request.url);
35
-
36
- // Serve dist/client/assets statically
37
- if (url.pathname.startsWith("/assets/")) {
38
- return env.ASSETS.fetch(request);
39
- }
40
-
41
- // Every request (GET, POST, etc) goes through loadEndPoint
42
- return await server.fetch(request);
43
- },
44
- };
package/src/adapter.ts DELETED
@@ -1,113 +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
- // TODO: Is this going to be the correct path after installing from npm?
25
- const buildSrcFolder = path.resolve(site.root, "./node_modules/@torpor/build/src/");
26
- const adapterSrcFolder = path.resolve(
27
- site.root,
28
- "./node_modules/@torpor/adapter-cloudflare/src/",
29
- );
30
-
31
- // Compile _worker.ts
32
- let workerFile = path.join(adapterSrcFolder, "_worker.ts");
33
- let workerSource = await fs.readFile(workerFile, "utf-8");
34
-
35
- // Find the client script file in /assets
36
- let clientScript = (await fs.readdir(path.join(clientFolder, "assets"))).find((f) =>
37
- f.startsWith("clientEntry-"),
38
- );
39
- if (!clientScript) {
40
- throw new Error("clientEntry.js not found");
41
- }
42
- clientScript = `/assets/${clientScript}`;
43
-
44
- // Read and prepare site.html so that we can just splice components into it
45
- let siteHtml = path.join(clientFolder, "site.html");
46
- let template = await fs.readFile(siteHtml, "utf-8");
47
- template = prepareTemplate(template, clientScript);
48
-
49
- const serverClass = path.join(buildSrcFolder, "server", "Server.ts");
50
- const serverScript = path.join(serverFolder, "serverEntry.js");
51
-
52
- // Splice file names into _worker.ts
53
- workerSource = workerSource
54
- .replace("%SERVER_CLASS%", serverClass)
55
- .replace("%SERVER_SCRIPT%", serverScript)
56
- .replace("%HTML_TEMPLATE%", template);
57
-
58
- workerFile = path.join(tempFolder, "_worker.ts");
59
- await fs.mkdir(tempFolder);
60
- await fs.writeFile(workerFile, workerSource);
61
-
62
- const cloudflareConfig = structuredClone(site.viteConfig ?? {});
63
- cloudflareConfig.build ??= {};
64
- cloudflareConfig.build.outDir = cloudflareFolder;
65
- cloudflareConfig.build.rollupOptions ??= {};
66
- cloudflareConfig.build.rollupOptions.input = [workerFile, ...site.inputs];
67
- cloudflareConfig.build.rollupOptions.output = {
68
- entryFileNames: `[name].js`,
69
- chunkFileNames: `[name].js`,
70
- assetFileNames: `[name].[ext]`,
71
- };
72
- cloudflareConfig.build.rollupOptions.preserveEntrySignatures = "strict";
73
- // HACK: We don't need to minify as the worker isn't downloaded?
74
- // And it makes debugging easier
75
- cloudflareConfig.build.minify = false;
76
- // HACK: Build for SSR so we don't get document and window references
77
- cloudflareConfig.build.ssr = true;
78
- await build(defineConfig(cloudflareConfig));
79
-
80
- // Copy the client assets into the Cloudflare folder
81
- await fs.mkdir(path.join(cloudflareFolder, "assets"));
82
- for (let file of await fs.readdir(path.join(clientFolder, "assets"))) {
83
- await fs.copyFile(
84
- path.join(clientFolder, "assets", file),
85
- path.join(cloudflareFolder, "assets", file),
86
- );
87
- }
88
-
89
- // Build a wrangler.toml file for running with `wrangler dev`
90
- const wranglerConfig = `
91
- name = "torpor-miniflare"
92
- main = "./cloudflare/_worker.js"
93
- compatibility_date = "2025-01-01"
94
-
95
- [assets]
96
- directory = "./cloudflare"
97
- binding = "ASSETS"
98
- `;
99
- const wranglerFile = path.join(distFolder, "wrangler.toml");
100
- await fs.writeFile(wranglerFile, wranglerConfig);
101
-
102
- // Build a .assetsignore file
103
- const assetsIgnore = `
104
- **/node_modules
105
- **/.DS_Store
106
- **/.git
107
- _worker.js
108
- `;
109
- const assetsIgnoreFile = path.join(cloudflareFolder, ".assetsignore");
110
- await fs.writeFile(assetsIgnoreFile, assetsIgnore);
111
-
112
- await fs.rm(tempFolder, { recursive: true });
113
- }
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;