wrangler 0.0.0-e14e589 → 0.0.0-e928f94

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/src/publish.ts CHANGED
@@ -8,6 +8,7 @@ import { readFile } from "fs/promises";
8
8
  import cfetch from "./cfetch";
9
9
  import assert from "node:assert";
10
10
  import { syncAssets } from "./sites";
11
+ import makeModuleCollector from "./module-collection";
11
12
 
12
13
  type CfScriptFormat = void | "modules" | "service-worker";
13
14
 
@@ -22,6 +23,8 @@ type Props = {
22
23
  triggers?: (string | number)[];
23
24
  routes?: (string | number)[];
24
25
  legacyEnv?: boolean;
26
+ jsxFactory: void | string;
27
+ jsxFragment: void | string;
25
28
  };
26
29
 
27
30
  function sleep(ms: number) {
@@ -47,19 +50,25 @@ export default async function publish(props: Props): Promise<void> {
47
50
  const triggers = props.triggers || config.triggers?.crons;
48
51
  const routes = props.routes || config.routes;
49
52
 
53
+ const jsxFactory = props.jsxFactory || config.jsx_factory;
54
+ const jsxFragment = props.jsxFragment || config.jsx_fragment;
55
+
50
56
  assert(config.account_id, "missing account id");
51
57
 
58
+ let scriptName = props.name || config.name;
59
+ assert(
60
+ scriptName,
61
+ 'You need to provide a name when publishing a worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`'
62
+ );
63
+
52
64
  let file: string;
53
65
  if (props.script) {
54
66
  file = props.script;
55
- assert(props.name, "name is required when using script");
56
67
  } else {
57
68
  assert(build?.upload?.main, "missing main file");
58
- assert(config.name, "missing name");
59
69
  file = path.join(path.dirname(__path__), build.upload.main);
60
70
  }
61
71
 
62
- let scriptName = props.script ? props.name : config.name;
63
72
  if (props.legacyEnv) {
64
73
  scriptName += props.env ? `-${props.env}` : "";
65
74
  }
@@ -67,6 +76,7 @@ export default async function publish(props: Props): Promise<void> {
67
76
 
68
77
  const destination = await tmp.dir({ unsafeCleanup: true });
69
78
 
79
+ const moduleCollector = makeModuleCollector();
70
80
  const result = await esbuild.build({
71
81
  ...(props.public
72
82
  ? {
@@ -89,6 +99,13 @@ export default async function publish(props: Props): Promise<void> {
89
99
  format: "esm",
90
100
  sourcemap: true,
91
101
  metafile: true,
102
+ conditions: ["worker", "browser"],
103
+ loader: {
104
+ ".js": "jsx",
105
+ },
106
+ plugins: [moduleCollector.plugin],
107
+ ...(jsxFactory && { jsxFactory }),
108
+ ...(jsxFragment && { jsxFragment }),
92
109
  });
93
110
 
94
111
  const chunks = Object.entries(result.metafile.outputs).find(
@@ -121,6 +138,46 @@ export default async function publish(props: Props): Promise<void> {
121
138
 
122
139
  const content = await readFile(chunks[0], { encoding: "utf-8" });
123
140
  destination.cleanup();
141
+
142
+ // if config.migrations
143
+ // get current migration tag
144
+ let migrations;
145
+ if ("migrations" in config) {
146
+ const scripts = await cfetch<{ id: string; migration_tag: string }[]>(
147
+ `/accounts/${accountId}/workers/scripts`
148
+ );
149
+ const script = scripts.find((script) => script.id === scriptName);
150
+ if (script?.migration_tag) {
151
+ // was already published once
152
+ const foundIndex = config.migrations.findIndex(
153
+ (migration) => migration.tag === script.migration_tag
154
+ );
155
+ if (foundIndex === -1) {
156
+ console.warn(
157
+ `The published script ${scriptName} has a migration tag "${script.migration_tag}, which was not found in wrangler.toml. You may have already delated it. Applying all available migrations to the script...`
158
+ );
159
+ migrations = {
160
+ old_tag: script.migration_tag,
161
+ new_tag: config.migrations[config.migrations.length - 1].tag,
162
+ steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
163
+ };
164
+ } else {
165
+ migrations = {
166
+ old_tag: script.migration_tag,
167
+ new_tag: config.migrations[config.migrations.length - 1].tag,
168
+ steps: config.migrations
169
+ .slice(foundIndex + 1)
170
+ .map(({ tag: _tag, ...rest }) => rest),
171
+ };
172
+ }
173
+ } else {
174
+ migrations = {
175
+ new_tag: config.migrations[config.migrations.length - 1].tag,
176
+ steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
177
+ };
178
+ }
179
+ }
180
+
124
181
  const assets =
125
182
  props.public || props.site || props.config.site?.bucket // TODO: allow both
126
183
  ? await syncAssets(
@@ -131,42 +188,53 @@ export default async function publish(props: Props): Promise<void> {
131
188
  )
132
189
  : { manifest: undefined, namespace: undefined };
133
190
 
134
- const envRootObj = props.env ? config[`env.${props.env}`] : config;
191
+ const envRootObj = props.env ? config.env[props.env] || {} : config;
135
192
 
136
193
  const worker: CfWorkerInit = {
194
+ name: scriptName,
137
195
  main: {
138
- name: scriptName,
196
+ name: path.basename(chunks[0]),
139
197
  content: content,
140
- type:
141
- (bundle.type === "esm" ? "modules" : "service-worker") === "modules"
142
- ? "esm"
143
- : "commonjs",
198
+ type: bundle.type === "esm" ? "esm" : "commonjs",
144
199
  },
145
200
  variables: {
146
201
  ...(envRootObj?.vars || {}),
147
202
  ...(envRootObj?.kv_namespaces || []).reduce(
148
- (obj, { binding, preview_id, id }) => {
203
+ (obj, { binding, preview_id: _preview_id, id }) => {
149
204
  return { ...obj, [binding]: { namespaceId: id } };
150
205
  },
151
206
  {}
152
207
  ),
208
+ ...(envRootObj?.durable_objects?.bindings || []).reduce(
209
+ (obj, { name, class_name, script_name }) => {
210
+ return {
211
+ ...obj,
212
+ [name]: { class_name, ...(script_name && { script_name }) },
213
+ };
214
+ },
215
+ {}
216
+ ),
153
217
  ...(assets.namespace
154
218
  ? { __STATIC_CONTENT: { namespaceId: assets.namespace } }
155
219
  : {}),
156
220
  },
221
+ ...(migrations && { migrations }),
157
222
  modules: assets.manifest
158
- ? [].concat({
223
+ ? moduleCollector.modules.concat({
159
224
  name: "__STATIC_CONTENT_MANIFEST",
160
225
  content: JSON.stringify(assets.manifest),
161
226
  type: "text",
162
227
  })
163
- : [],
228
+ : moduleCollector.modules,
229
+ compatibility_date: config.compatibility_date,
230
+ compatibility_flags: config.compatibility_flags,
231
+ usage_model: config.usage_model,
164
232
  };
165
233
 
166
234
  const start = Date.now();
167
- const formatTime = (duration) => {
235
+ function formatTime(duration: number) {
168
236
  return `(${(duration / 1000).toFixed(2)} sec)`;
169
- };
237
+ }
170
238
 
171
239
  const notProd = !props.legacyEnv && props.env;
172
240
  const workerName = notProd ? `${scriptName} (${envName})` : scriptName;
package/src/sites.tsx CHANGED
@@ -76,7 +76,7 @@ export async function syncAssets(
76
76
  scriptName: string,
77
77
  dirPath: string,
78
78
  preview: boolean,
79
- env?: string
79
+ _env?: string
80
80
  ) {
81
81
  const title = `__${scriptName}_sites_assets${preview ? "_preview" : ""}`;
82
82
  const { id: namespace } = await createKVNamespaceIfNotAlreadyExisting(
package/src/tail.tsx CHANGED
@@ -33,7 +33,7 @@ async function createTailButDontConnect(
33
33
  export async function createTail(
34
34
  accountId: string,
35
35
  workerName: string,
36
- filters: Filters
36
+ _filters: Filters
37
37
  ): Promise<{
38
38
  tail: WebSocket;
39
39
  expiration: Date;