wrangler 2.0.28 → 2.0.29

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.
@@ -0,0 +1,17 @@
1
+ import * as path from "node:path";
2
+ import { getBasePath } from "../paths";
3
+
4
+ describe("paths", () => {
5
+ describe("getBasePath()", () => {
6
+ it("should return the path to the wrangler package", () => {
7
+ expect(getBasePath()).toMatch(/packages[/\\]wrangler$/);
8
+ });
9
+
10
+ it("should use the __RELATIVE_PACKAGE_PATH__ as defined on the global context to compute the base path", () => {
11
+ (
12
+ global as unknown as { __RELATIVE_PACKAGE_PATH__: string }
13
+ ).__RELATIVE_PACKAGE_PATH__ = "/foo/bar";
14
+ expect(getBasePath()).toEqual(path.resolve("/foo/bar"));
15
+ });
16
+ });
17
+ });
package/src/api/dev.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { startDev } from "../dev";
1
+ import { startApiDev, startDev } from "../dev";
2
2
  import { logger } from "../logger";
3
3
 
4
4
  import type { EnablePagesAssetsServiceBindingOptions } from "../miniflare-cli";
5
5
  import type { RequestInit, Response } from "undici";
6
6
 
7
7
  interface DevOptions {
8
+ config?: string;
8
9
  env?: string;
9
10
  ip?: string;
10
11
  port?: number;
@@ -20,7 +21,7 @@ interface DevOptions {
20
21
  experimentalEnableLocalPersistence?: boolean;
21
22
  liveReload?: boolean;
22
23
  watch?: boolean;
23
- vars: {
24
+ vars?: {
24
25
  [key: string]: unknown;
25
26
  };
26
27
  kv?: {
@@ -48,46 +49,91 @@ interface DevOptions {
48
49
  _?: (string | number)[]; //yargs wants this
49
50
  $0?: string; //yargs wants this
50
51
  }
52
+
53
+ interface DevApiOptions {
54
+ testMode?: boolean;
55
+ disableExperimentalWarning?: boolean;
56
+ }
57
+
58
+ interface UnstableDev {
59
+ stop: () => Promise<void>;
60
+ fetch: (init?: RequestInit) => Promise<Response | undefined>;
61
+ waitUntilExit: () => Promise<void>;
62
+ }
51
63
  /**
52
64
  * unstable_dev starts a wrangler dev server, and returns a promise that resolves with utility functions to interact with it.
53
65
  * @param {string} script
54
66
  * @param {DevOptions} options
67
+ * @param {DevApiOptions} apiOptions
68
+ * @returns {Promise<UnstableDev>}
55
69
  */
56
70
  export async function unstable_dev(
57
71
  script: string,
58
- options: DevOptions,
59
- disableExperimentalWarning?: boolean
72
+ options?: DevOptions,
73
+ apiOptions?: DevApiOptions
60
74
  ) {
75
+ const { testMode = true, disableExperimentalWarning = false } =
76
+ apiOptions || {};
61
77
  if (!disableExperimentalWarning) {
62
78
  logger.warn(
63
79
  `unstable_dev() is experimental\nunstable_dev()'s behaviour will likely change in future releases`
64
80
  );
65
81
  }
66
-
67
- return new Promise<{
68
- stop: () => void;
69
- fetch: (init?: RequestInit) => Promise<Response | undefined>;
70
- waitUntilExit: () => Promise<void>;
71
- }>((resolve) => {
72
- //lmao
73
- return new Promise<Awaited<ReturnType<typeof startDev>>>((ready) => {
74
- const devServer = startDev({
75
- script: script,
76
- inspect: false,
77
- logLevel: "none",
78
- showInteractiveDevSession: false,
79
- _: [],
80
- $0: "",
81
- ...options,
82
- local: true,
83
- onReady: () => ready(devServer),
82
+ //due to Pages adoption of unstable_dev, we can't *just* disable rebuilds and watching. instead, we'll have two versions of startDev, which will converge.
83
+ if (testMode) {
84
+ //in testMode, we can run multiple wranglers in parallel, but rebuilds might not work out of the box
85
+ return new Promise<UnstableDev>((resolve) => {
86
+ //lmao
87
+ return new Promise<Awaited<ReturnType<typeof startApiDev>>>((ready) => {
88
+ // once the devServer is ready for requests, we resolve the inner promise
89
+ // (where we've named the resolve function "ready")
90
+ const devServer = startApiDev({
91
+ script: script,
92
+ inspect: false,
93
+ logLevel: "none",
94
+ showInteractiveDevSession: false,
95
+ _: [],
96
+ $0: "",
97
+ ...options,
98
+ local: true,
99
+ onReady: () => ready(devServer),
100
+ });
101
+ }).then((devServer) => {
102
+ // now that the inner promise has resolved, we can resolve the outer promise
103
+ // with an object that lets you fetch and stop the dev server
104
+ resolve({
105
+ stop: devServer.stop,
106
+ fetch: devServer.fetch,
107
+ //no-op, does nothing in tests
108
+ waitUntilExit: async () => {
109
+ return;
110
+ },
111
+ });
84
112
  });
85
- }).then((devServer) => {
86
- resolve({
87
- stop: devServer.stop,
88
- fetch: devServer.fetch,
89
- waitUntilExit: devServer.devReactElement.waitUntilExit,
113
+ });
114
+ } else {
115
+ //outside of test mode, rebuilds work fine, but only one instance of wrangler will work at a time
116
+ return new Promise<UnstableDev>((resolve) => {
117
+ //lmao
118
+ return new Promise<Awaited<ReturnType<typeof startDev>>>((ready) => {
119
+ const devServer = startDev({
120
+ script: script,
121
+ inspect: false,
122
+ logLevel: "none",
123
+ showInteractiveDevSession: false,
124
+ _: [],
125
+ $0: "",
126
+ ...options,
127
+ local: true,
128
+ onReady: () => ready(devServer),
129
+ });
130
+ }).then((devServer) => {
131
+ resolve({
132
+ stop: devServer.stop,
133
+ fetch: devServer.fetch,
134
+ waitUntilExit: devServer.devReactElement.waitUntilExit,
135
+ });
90
136
  });
91
137
  });
92
- });
138
+ }
93
139
  }
package/src/bundle.ts CHANGED
@@ -7,6 +7,7 @@ import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
7
7
  import * as esbuild from "esbuild";
8
8
  import tmp from "tmp-promise";
9
9
  import createModuleCollector from "./module-collection";
10
+ import { getBasePath } from "./paths";
10
11
  import type { Config } from "./config";
11
12
  import type { WorkerRegistry } from "./dev-registry";
12
13
  import type { Entry } from "./entry";
@@ -71,7 +72,7 @@ export async function bundleWorker(
71
72
  nodeCompat: boolean | undefined;
72
73
  define: Config["define"];
73
74
  checkFetch: boolean;
74
- services: Config["services"];
75
+ services: Config["services"] | undefined;
75
76
  workerDefinitions: WorkerRegistry | undefined;
76
77
  firstPartyWorkerDevFacade: boolean | undefined;
77
78
  }
@@ -129,7 +130,7 @@ export async function bundleWorker(
129
130
  });
130
131
  fs.writeFileSync(
131
132
  checkedFetchFileToInject,
132
- fs.readFileSync(path.resolve(__dirname, "../templates/checked-fetch.js"))
133
+ fs.readFileSync(path.resolve(getBasePath(), "templates/checked-fetch.js"))
133
134
  );
134
135
  }
135
136
 
@@ -302,7 +303,9 @@ async function applyFormatDevErrorsFacade(
302
303
  ): Promise<Entry> {
303
304
  const targetPath = path.join(tmpDirPath, "format-dev-errors.entry.js");
304
305
  await esbuild.build({
305
- entryPoints: [path.resolve(__dirname, "../templates/format-dev-errors.ts")],
306
+ entryPoints: [
307
+ path.resolve(getBasePath(), "templates/format-dev-errors.ts"),
308
+ ],
306
309
  bundle: true,
307
310
  sourcemap: true,
308
311
  format: "esm",
@@ -334,7 +337,7 @@ async function applyStaticAssetFacade(
334
337
 
335
338
  await esbuild.build({
336
339
  entryPoints: [
337
- path.resolve(__dirname, "../templates/serve-static-assets.ts"),
340
+ path.resolve(getBasePath(), "templates/serve-static-assets.ts"),
338
341
  ],
339
342
  bundle: true,
340
343
  format: "esm",
@@ -342,7 +345,7 @@ async function applyStaticAssetFacade(
342
345
  plugins: [
343
346
  esbuildAliasExternalPlugin({
344
347
  __ENTRY_POINT__: entry.file,
345
- __KV_ASSET_HANDLER__: path.join(__dirname, "../kv-asset-handler.js"),
348
+ __KV_ASSET_HANDLER__: path.join(getBasePath(), "kv-asset-handler.js"),
346
349
  __STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
347
350
  }),
348
351
  ],
@@ -391,10 +394,10 @@ async function applyMultiWorkerDevFacade(
391
394
  await esbuild.build({
392
395
  entryPoints: [
393
396
  path.join(
394
- __dirname,
397
+ getBasePath(),
395
398
  entry.format === "modules"
396
- ? "../templates/service-bindings-module-facade.js"
397
- : "../templates/service-bindings-sw-facade.js"
399
+ ? "templates/service-bindings-module-facade.js"
400
+ : "templates/service-bindings-sw-facade.js"
398
401
  ),
399
402
  ],
400
403
  bundle: true,
@@ -440,8 +443,8 @@ async function applyFirstPartyWorkerDevFacade(
440
443
  await esbuild.build({
441
444
  entryPoints: [
442
445
  path.resolve(
443
- __dirname,
444
- "../templates/first-party-worker-module-facade.ts"
446
+ getBasePath(),
447
+ "templates/first-party-worker-module-facade.ts"
445
448
  ),
446
449
  ],
447
450
  bundle: true,
package/src/cli.ts CHANGED
@@ -24,4 +24,4 @@ if (typeof jest === "undefined" && require.main) {
24
24
  * It makes it possible to import wrangler from 'wrangler',
25
25
  * and call wrangler.unstable_dev().
26
26
  */
27
- export default { unstable_dev };
27
+ export { unstable_dev };