create-cloudflare 2.0.3 → 2.0.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.
Files changed (43) hide show
  1. package/dist/angular/templates/src/_routes.json +5 -0
  2. package/dist/angular/templates/src/main.server.ts +35 -0
  3. package/dist/angular/templates/tools/bundle.mjs +74 -0
  4. package/dist/angular/templates/tools/copy-client-files.mjs +4 -0
  5. package/dist/angular/templates/tools/copy-worker-files.mjs +10 -0
  6. package/dist/angular/templates/tools/paths.mjs +9 -0
  7. package/dist/angular/templates/tsconfig.server.json +5 -0
  8. package/dist/cli.js +45643 -0
  9. package/dist/tsconfig.tsbuildinfo +1 -0
  10. package/package.json +1 -1
  11. package/templates/chatgptPlugin/ts/.assets/example.png +0 -0
  12. package/templates/chatgptPlugin/ts/README.md +25 -0
  13. package/templates/chatgptPlugin/ts/package.json +16 -0
  14. package/templates/chatgptPlugin/ts/src/index.ts +33 -0
  15. package/templates/chatgptPlugin/ts/src/search.ts +63 -0
  16. package/templates/chatgptPlugin/ts/wrangler.toml +3 -0
  17. package/templates/common/js/.prettierrc +5 -0
  18. package/templates/common/js/package.json +14 -0
  19. package/templates/common/js/src/ab-test.js +41 -0
  20. package/templates/common/js/src/proxy.js +23 -0
  21. package/templates/common/js/src/redirect.js +13 -0
  22. package/templates/common/js/src/router.js +22 -0
  23. package/templates/common/js/src/worker.js +46 -0
  24. package/templates/common/js/wrangler.toml +40 -0
  25. package/templates/common/ts/.prettierrc +5 -0
  26. package/templates/common/ts/package.json +15 -0
  27. package/templates/common/ts/src/ab-test.ts +41 -0
  28. package/templates/common/ts/src/proxy.ts +23 -0
  29. package/templates/common/ts/src/redirect.ts +13 -0
  30. package/templates/common/ts/src/router.ts +22 -0
  31. package/templates/common/ts/src/worker.ts +46 -0
  32. package/templates/common/ts/tsconfig.json +101 -0
  33. package/templates/common/ts/worker-configuration.d.ts +16 -0
  34. package/templates/common/ts/wrangler.toml +40 -0
  35. package/templates/simple/js/.prettierrc +5 -0
  36. package/templates/simple/js/package.json +14 -0
  37. package/templates/simple/js/src/worker.js +15 -0
  38. package/templates/simple/js/wrangler.toml +40 -0
  39. package/templates/simple/ts/.prettierrc +5 -0
  40. package/templates/simple/ts/package.json +14 -0
  41. package/templates/simple/ts/src/worker.ts +32 -0
  42. package/templates/simple/ts/tsconfig.json +101 -0
  43. package/templates/simple/ts/wrangler.toml +40 -0
@@ -0,0 +1,5 @@
1
+ {
2
+ "version": 1,
3
+ "include": ["/*"],
4
+ "exclude": ["/*.*"]
5
+ }
@@ -0,0 +1,35 @@
1
+ import "zone.js/dist/zone-node";
2
+ import "@angular/platform-server/init";
3
+
4
+ import { bootstrapApplication } from "@angular/platform-browser";
5
+ import { renderApplication } from "@angular/platform-server";
6
+
7
+ import { AppComponent } from "./app/app.component";
8
+ import { config } from "./app/app.config.server";
9
+
10
+ interface Env {
11
+ ASSETS: { fetch: typeof fetch };
12
+ }
13
+
14
+ // We attach the Cloudflare `fetch()` handler to the global scope
15
+ // so that we can export it when we process the Angular output.
16
+ // See tools/bundle.mjs
17
+ (globalThis as any).__workerFetchHandler = async function fetch(
18
+ request: Request,
19
+ env: Env
20
+ ) {
21
+ const url = new URL(request.url);
22
+ console.log("render SSR", url.href);
23
+
24
+ // Get the root `index.html` content.
25
+ const indexUrl = new URL("/", url);
26
+ const indexResponse = await env.ASSETS.fetch(new Request(indexUrl));
27
+ const document = await indexResponse.text();
28
+
29
+ const content = await renderApplication(
30
+ () => bootstrapApplication(AppComponent, config),
31
+ { document, url: url.pathname }
32
+ );
33
+ // console.log("rendered SSR", content);
34
+ return new Response(content, indexResponse);
35
+ };
@@ -0,0 +1,74 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { worker as workerPath } from "./paths.mjs";
4
+ import * as esbuild from "esbuild";
5
+ import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
6
+ import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
7
+ import fg from "fast-glob";
8
+
9
+ // Process each of the JS files in the `_worker.js` directory
10
+ for (const entry of await fg("**/*.js", { cwd: workerPath, onlyFiles: true })) {
11
+ if (entry === "index.js") {
12
+ // This is the main bundle and gets special treatment
13
+ await bundleMain();
14
+ } else {
15
+ await bundleLazyModule(entry);
16
+ }
17
+ }
18
+
19
+ // Use esbuild to process the main entry-point.
20
+ // - shim Node.js APIs
21
+ // - convert `global` to `globalThis`
22
+ // - convert dynamic `require()` calls to `await import()` calls
23
+ // - ensure that the Cloudflare `fetch()` handler is exported
24
+ async function bundleMain() {
25
+ const result = await esbuild.build({
26
+ entryPoints: ["index.js"],
27
+ bundle: true,
28
+ format: "iife",
29
+ write: false,
30
+ absWorkingDir: workerPath,
31
+ define: {
32
+ global: "globalThis",
33
+ },
34
+ plugins: [
35
+ NodeGlobalsPolyfillPlugin({ buffer: true }),
36
+ NodeModulesPolyfillPlugin(),
37
+ ],
38
+ });
39
+
40
+ let main = result.outputFiles[0].text;
41
+
42
+ // Patch any dynamic imports (converting `require()` calls to `import()` calls).
43
+ main = main.replace(
44
+ 'installChunk(__require("./" + __webpack_require__.u(chunkId))',
45
+ 'promises.push(import("./" + __webpack_require__.u(chunkId)).then((mod) => installChunk(mod.default))'
46
+ );
47
+ // Export the fetch handler (grabbing it from the global).
48
+ main += "\nexport default { fetch : globalThis.__workerFetchHandler };";
49
+
50
+ await fs.writeFile(path.resolve(workerPath, "index.js"), main);
51
+ }
52
+
53
+ // Use esbuild to process the lazy load modules
54
+ // In particular we need to convert the CommonJS export syntax to ESM.
55
+ async function bundleLazyModule(filePath) {
56
+ const result = await esbuild.build({
57
+ entryPoints: [filePath],
58
+ bundle: true,
59
+ format: "cjs",
60
+ write: false,
61
+ absWorkingDir: workerPath,
62
+ define: {
63
+ global: "globalThis",
64
+ },
65
+ plugins: [NodeModulesPolyfillPlugin()],
66
+ });
67
+
68
+ let content = result.outputFiles[0].text;
69
+
70
+ // Export the fetch handler (grabbing it from the global).
71
+ content = "const exports = {};\n" + content + "\nexport default exports";
72
+
73
+ await fs.writeFile(path.resolve(workerPath, filePath), content);
74
+ }
@@ -0,0 +1,4 @@
1
+ // Copy the client-side files over so that they can be uploaded by the pages publish command.
2
+ import fs from "node:fs";
3
+ import { client, cloudflare } from "./paths.mjs";
4
+ fs.cpSync(client, cloudflare, { recursive: true });
@@ -0,0 +1,10 @@
1
+ // Copy the lazy loaded modules into the dist folder so that they can be
2
+ // uploaded along with the main Worker module.
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { ssr, worker } from "./paths.mjs";
6
+ fs.cpSync(ssr, worker, { recursive: true });
7
+ fs.renameSync(
8
+ path.resolve(worker, "main.js"),
9
+ path.resolve(worker, "index.js")
10
+ );
@@ -0,0 +1,9 @@
1
+ import path from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+
4
+ const dirname = path.dirname(fileURLToPath(import.meta.url));
5
+ export const root = path.resolve(dirname, "..");
6
+ export const client = path.resolve(root, "dist/browser");
7
+ export const ssr = path.resolve(root, "dist/server");
8
+ export const cloudflare = path.resolve(root, "dist/cloudflare");
9
+ export const worker = path.resolve(cloudflare, "_worker.js");
@@ -0,0 +1,5 @@
1
+ /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+ {
3
+ "extends": "./tsconfig.app.json",
4
+ "files": ["./src/main.server.ts"]
5
+ }