create-cloudflare 2.12.0 → 2.12.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/templates/qwik/c3.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { endSection } from "@cloudflare/cli";
|
|
1
|
+
import { crash, endSection } from "@cloudflare/cli";
|
|
2
2
|
import { brandColor } from "@cloudflare/cli/colors";
|
|
3
3
|
import { spinner } from "@cloudflare/cli/interactive";
|
|
4
|
-
import {
|
|
4
|
+
import { loadTemplateSnippets, transformFile } from "helpers/codemod";
|
|
5
5
|
import { runCommand, runFrameworkGenerator } from "helpers/command";
|
|
6
6
|
import { usesTypescript } from "helpers/files";
|
|
7
7
|
import { detectPackageManager } from "helpers/packages";
|
|
8
|
+
import * as recast from "recast";
|
|
8
9
|
import { quoteShellArgs } from "../../src/common";
|
|
9
10
|
import type { TemplateConfig } from "../../src/templates";
|
|
10
|
-
import type * as recast from "recast";
|
|
11
11
|
import type { C3Context } from "types";
|
|
12
12
|
|
|
13
13
|
const { npm, npx } = detectPackageManager();
|
|
@@ -23,6 +23,7 @@ const configure = async (ctx: C3Context) => {
|
|
|
23
23
|
await runCommand(cmd);
|
|
24
24
|
|
|
25
25
|
addBindingsProxy(ctx);
|
|
26
|
+
populateCloudflareEnv();
|
|
26
27
|
};
|
|
27
28
|
|
|
28
29
|
const addBindingsProxy = (ctx: C3Context) => {
|
|
@@ -35,43 +36,90 @@ const addBindingsProxy = (ctx: C3Context) => {
|
|
|
35
36
|
const s = spinner();
|
|
36
37
|
s.start("Updating `vite.config.ts`");
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
let env = {};
|
|
41
|
-
|
|
42
|
-
if(process.env.NODE_ENV === 'development') {
|
|
43
|
-
const { getPlatformProxy } = await import('wrangler');
|
|
44
|
-
const platformProxy = await getPlatformProxy();
|
|
45
|
-
env = platformProxy.env;
|
|
46
|
-
}
|
|
47
|
-
`;
|
|
39
|
+
const snippets = loadTemplateSnippets(ctx);
|
|
40
|
+
const b = recast.types.builders;
|
|
48
41
|
|
|
49
42
|
transformFile("vite.config.ts", {
|
|
43
|
+
// Insert the env declaration after the last import (but before the rest of the body)
|
|
50
44
|
visitProgram: function (n) {
|
|
51
45
|
const lastImportIndex = n.node.body.findLastIndex(
|
|
52
46
|
(t) => t.type === "ImportDeclaration"
|
|
53
47
|
);
|
|
54
|
-
n.get("body"
|
|
48
|
+
const lastImport = n.get("body", lastImportIndex);
|
|
49
|
+
lastImport.insertAfter(...snippets.getPlatformProxyTs);
|
|
50
|
+
|
|
51
|
+
return this.traverse(n);
|
|
52
|
+
},
|
|
53
|
+
// Pass the `platform` object from the declaration to the `qwikCity` plugin
|
|
54
|
+
visitCallExpression: function (n) {
|
|
55
|
+
const callee = n.node.callee as recast.types.namedTypes.Identifier;
|
|
56
|
+
if (callee.name !== "qwikCity") {
|
|
57
|
+
return this.traverse(n);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The config object passed to `qwikCity`
|
|
61
|
+
const configArgument = n.node.arguments[0] as
|
|
62
|
+
| recast.types.namedTypes.ObjectExpression
|
|
63
|
+
| undefined;
|
|
64
|
+
|
|
65
|
+
const platformPropery = b.objectProperty.from({
|
|
66
|
+
key: b.identifier("platform"),
|
|
67
|
+
value: b.identifier("platform"),
|
|
68
|
+
shorthand: true,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (!configArgument) {
|
|
72
|
+
n.node.arguments = [b.objectExpression([platformPropery])];
|
|
73
|
+
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (configArgument.type !== "ObjectExpression") {
|
|
78
|
+
crash("Failed to update `vite.config.ts`");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Add the `platform` object to the object
|
|
82
|
+
configArgument.properties.push(platformPropery);
|
|
55
83
|
|
|
56
84
|
return false;
|
|
57
85
|
},
|
|
58
86
|
});
|
|
59
87
|
|
|
60
|
-
|
|
61
|
-
|
|
88
|
+
s.stop(`${brandColor("updated")} \`vite.config.ts\``);
|
|
89
|
+
};
|
|
62
90
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
91
|
+
const populateCloudflareEnv = () => {
|
|
92
|
+
const entrypointPath = "src/entry.cloudflare-pages.tsx";
|
|
93
|
+
|
|
94
|
+
const s = spinner();
|
|
95
|
+
s.start(`Updating \`${entrypointPath}\``);
|
|
96
|
+
|
|
97
|
+
transformFile(entrypointPath, {
|
|
98
|
+
visitTSInterfaceDeclaration: function (n) {
|
|
99
|
+
const b = recast.types.builders;
|
|
100
|
+
const id = n.node.id as recast.types.namedTypes.Identifier;
|
|
101
|
+
if (id.name !== "QwikCityPlatform") {
|
|
102
|
+
this.traverse(n);
|
|
68
103
|
}
|
|
69
104
|
|
|
70
|
-
|
|
105
|
+
const newBody = [
|
|
106
|
+
["env", "Env"],
|
|
107
|
+
// Qwik doesn't supply `cf` to the platform object. Should they do so, uncomment this
|
|
108
|
+
// ["cf", "CfProperties"],
|
|
109
|
+
].map(([varName, type]) =>
|
|
110
|
+
b.tsPropertySignature(
|
|
111
|
+
b.identifier(varName),
|
|
112
|
+
b.tsTypeAnnotation(b.tsTypeReference(b.identifier(type)))
|
|
113
|
+
)
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
n.node.body.body = newBody;
|
|
117
|
+
|
|
118
|
+
return false;
|
|
71
119
|
},
|
|
72
120
|
});
|
|
73
121
|
|
|
74
|
-
s.stop(`${brandColor("updated")}
|
|
122
|
+
s.stop(`${brandColor("updated")} \`${entrypointPath}\``);
|
|
75
123
|
};
|
|
76
124
|
|
|
77
125
|
const config: TemplateConfig = {
|
|
@@ -89,6 +137,8 @@ const config: TemplateConfig = {
|
|
|
89
137
|
transformPackageJson: async () => ({
|
|
90
138
|
scripts: {
|
|
91
139
|
deploy: `${npm} run build && wrangler pages deploy ./dist`,
|
|
140
|
+
preview: `${npm} run build && wrangler pages dev ./dist`,
|
|
141
|
+
"build-cf-types": `wrangler types`,
|
|
92
142
|
},
|
|
93
143
|
}),
|
|
94
144
|
};
|