wrangler 2.7.1 → 2.8.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/package.json +1 -1
- package/src/__tests__/d1/d1.test.ts +12 -8
- package/src/__tests__/deployments.test.ts +4 -4
- package/src/__tests__/helpers/mock-dialogs.ts +2 -0
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -5
- package/src/__tests__/helpers/mock-known-routes.ts +7 -2
- package/src/__tests__/helpers/mock-kv.ts +29 -16
- package/src/__tests__/helpers/mock-oauth-flow.ts +90 -98
- package/src/__tests__/helpers/msw/handlers/deployments.ts +20 -10
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +18 -41
- package/src/__tests__/helpers/msw/handlers/r2.ts +14 -34
- package/src/__tests__/helpers/msw/handlers/script.ts +9 -28
- package/src/__tests__/helpers/msw/handlers/user.ts +13 -24
- package/src/__tests__/helpers/msw/handlers/zones.ts +6 -8
- package/src/__tests__/helpers/msw/index.ts +30 -1
- package/src/__tests__/init.test.ts +3 -14
- package/src/__tests__/jest.setup.ts +0 -23
- package/src/__tests__/pages-deployment-tail.test.ts +72 -1
- package/src/__tests__/pages.test.ts +52 -53
- package/src/__tests__/publish.test.ts +870 -522
- package/src/__tests__/r2.test.ts +11 -35
- package/src/__tests__/secret.test.ts +1 -9
- package/src/__tests__/tail.test.ts +72 -19
- package/src/__tests__/tsconfig.tsbuildinfo +1 -1
- package/src/__tests__/user.test.ts +5 -16
- package/src/__tests__/whoami.test.tsx +6 -17
- package/src/__tests__/worker-namespace.test.ts +56 -48
- package/src/api/index.ts +1 -0
- package/src/api/pages/index.ts +5 -0
- package/src/api/pages/publish.tsx +321 -0
- package/src/bundle.ts +62 -10
- package/src/cfetch/internal.ts +0 -3
- package/src/cli.ts +2 -2
- package/src/config/environment.ts +12 -10
- package/src/d1/backups.tsx +1 -5
- package/src/d1/utils.ts +1 -1
- package/src/deployments.ts +16 -6
- package/src/dev/local.tsx +1 -10
- package/src/dev/remote.tsx +2 -0
- package/src/dev/start-server.ts +5 -10
- package/src/dev/use-esbuild.ts +1 -0
- package/src/docs/index.ts +2 -1
- package/src/entry.ts +1 -2
- package/src/index.ts +1 -1
- package/src/init.ts +1 -1
- package/src/metrics/send-event.ts +2 -1
- package/src/pages/build.ts +4 -124
- package/src/pages/buildFunctions.ts +129 -0
- package/src/pages/dev.ts +68 -63
- package/src/pages/functions/buildPlugin.ts +3 -20
- package/src/pages/functions/buildWorker.ts +143 -21
- package/src/pages/functions/tsconfig.tsbuildinfo +1 -1
- package/src/pages/publish.tsx +21 -220
- package/src/publish/publish.ts +30 -4
- package/src/tail/createTail.ts +28 -1
- package/src/tail/printing.ts +15 -0
- package/templates/checked-fetch.js +1 -3
- package/templates/d1-beta-facade.js +1 -1
- package/templates/middleware/loader-modules.ts +2 -0
- package/templates/tsconfig.tsbuildinfo +1 -1
- package/wrangler-dist/cli.d.ts +132 -10
- package/wrangler-dist/cli.js +3532 -3330
- package/src/__tests__/helpers/mock-cfetch.ts +0 -278
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { toUrlPath } from "../paths";
|
|
5
|
+
import { FunctionsNoRoutesError } from "./errors";
|
|
6
|
+
import { buildPlugin } from "./functions/buildPlugin";
|
|
7
|
+
import { buildWorker } from "./functions/buildWorker";
|
|
8
|
+
import { generateConfigFromFileTree } from "./functions/filepath-routing";
|
|
9
|
+
import { writeRoutesModule } from "./functions/routes";
|
|
10
|
+
import { convertRoutesToRoutesJSONSpec } from "./functions/routes-transformation";
|
|
11
|
+
import { RUNNING_BUILDERS } from "./utils";
|
|
12
|
+
import type { PagesBuildArgs } from "./build";
|
|
13
|
+
import type { Config } from "./functions/routes";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Builds a Functions worker based on the functions directory, with filepath and handler based routing.
|
|
17
|
+
* @throws FunctionsNoRoutesError when there are no routes found in the functions directory
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export async function buildFunctions({
|
|
21
|
+
outfile,
|
|
22
|
+
outputConfigPath,
|
|
23
|
+
functionsDirectory,
|
|
24
|
+
minify = false,
|
|
25
|
+
sourcemap = false,
|
|
26
|
+
fallbackService = "ASSETS",
|
|
27
|
+
watch = false,
|
|
28
|
+
onEnd,
|
|
29
|
+
plugin = false,
|
|
30
|
+
buildOutputDirectory,
|
|
31
|
+
routesOutputPath,
|
|
32
|
+
nodeCompat,
|
|
33
|
+
local,
|
|
34
|
+
d1Databases,
|
|
35
|
+
experimentalWorkerBundle = false,
|
|
36
|
+
}: Partial<
|
|
37
|
+
Pick<
|
|
38
|
+
PagesBuildArgs,
|
|
39
|
+
| "outputConfigPath"
|
|
40
|
+
| "minify"
|
|
41
|
+
| "sourcemap"
|
|
42
|
+
| "fallbackService"
|
|
43
|
+
| "watch"
|
|
44
|
+
| "plugin"
|
|
45
|
+
| "buildOutputDirectory"
|
|
46
|
+
| "nodeCompat"
|
|
47
|
+
>
|
|
48
|
+
> & {
|
|
49
|
+
functionsDirectory: string;
|
|
50
|
+
onEnd?: () => void;
|
|
51
|
+
outfile: Required<PagesBuildArgs>["outfile"];
|
|
52
|
+
routesOutputPath?: PagesBuildArgs["outputRoutesPath"];
|
|
53
|
+
local: boolean;
|
|
54
|
+
d1Databases?: string[];
|
|
55
|
+
experimentalWorkerBundle?: boolean;
|
|
56
|
+
}) {
|
|
57
|
+
RUNNING_BUILDERS.forEach(
|
|
58
|
+
(runningBuilder) => runningBuilder.stop && runningBuilder.stop()
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const routesModule = join(tmpdir(), `./functionsRoutes-${Math.random()}.mjs`);
|
|
62
|
+
const baseURL = toUrlPath("/");
|
|
63
|
+
|
|
64
|
+
const config: Config = await generateConfigFromFileTree({
|
|
65
|
+
baseDir: functionsDirectory,
|
|
66
|
+
baseURL,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (!config.routes || config.routes.length === 0) {
|
|
70
|
+
throw new FunctionsNoRoutesError(
|
|
71
|
+
`Failed to find any routes while compiling Functions in: ${functionsDirectory}`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (routesOutputPath) {
|
|
76
|
+
const routesJSON = convertRoutesToRoutesJSONSpec(config.routes);
|
|
77
|
+
writeFileSync(routesOutputPath, JSON.stringify(routesJSON, null, 2));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (outputConfigPath) {
|
|
81
|
+
writeFileSync(
|
|
82
|
+
outputConfigPath,
|
|
83
|
+
JSON.stringify({ ...config, baseURL }, null, 2)
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
await writeRoutesModule({
|
|
88
|
+
config,
|
|
89
|
+
srcDir: functionsDirectory,
|
|
90
|
+
outfile: routesModule,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const absoluteFunctionsDirectory = resolve(functionsDirectory);
|
|
94
|
+
|
|
95
|
+
if (plugin) {
|
|
96
|
+
RUNNING_BUILDERS.push(
|
|
97
|
+
await buildPlugin({
|
|
98
|
+
routesModule,
|
|
99
|
+
outfile,
|
|
100
|
+
minify,
|
|
101
|
+
sourcemap,
|
|
102
|
+
watch,
|
|
103
|
+
nodeCompat,
|
|
104
|
+
functionsDirectory: absoluteFunctionsDirectory,
|
|
105
|
+
local,
|
|
106
|
+
betaD1Shims: d1Databases,
|
|
107
|
+
onEnd,
|
|
108
|
+
})
|
|
109
|
+
);
|
|
110
|
+
} else {
|
|
111
|
+
RUNNING_BUILDERS.push(
|
|
112
|
+
await buildWorker({
|
|
113
|
+
routesModule,
|
|
114
|
+
outfile,
|
|
115
|
+
minify,
|
|
116
|
+
sourcemap,
|
|
117
|
+
fallbackService,
|
|
118
|
+
watch,
|
|
119
|
+
functionsDirectory: absoluteFunctionsDirectory,
|
|
120
|
+
local,
|
|
121
|
+
betaD1Shims: d1Databases,
|
|
122
|
+
onEnd,
|
|
123
|
+
buildOutputDirectory,
|
|
124
|
+
nodeCompat,
|
|
125
|
+
experimentalWorkerBundle,
|
|
126
|
+
})
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
package/src/pages/dev.ts
CHANGED
|
@@ -10,9 +10,10 @@ import { FatalError } from "../errors";
|
|
|
10
10
|
import { logger } from "../logger";
|
|
11
11
|
import * as metrics from "../metrics";
|
|
12
12
|
import { getBasePath } from "../paths";
|
|
13
|
-
import { buildFunctions } from "./
|
|
13
|
+
import { buildFunctions } from "./buildFunctions";
|
|
14
14
|
import { ROUTES_SPEC_VERSION, SECONDS_TO_WAIT_FOR_PROXY } from "./constants";
|
|
15
15
|
import { FunctionsNoRoutesError, getFunctionsNoRoutesWarning } from "./errors";
|
|
16
|
+
import { buildRawWorker, checkRawWorker } from "./functions/buildWorker";
|
|
16
17
|
import { validateRoutes } from "./functions/routes-validation";
|
|
17
18
|
import { CLEANUP, CLEANUP_CALLBACKS, pagesBetaWarning } from "./utils";
|
|
18
19
|
import type { AdditionalDevProps } from "../dev";
|
|
@@ -78,6 +79,23 @@ export function Options(yargs: Argv) {
|
|
|
78
79
|
description:
|
|
79
80
|
"The location of the single Worker script if not using functions",
|
|
80
81
|
},
|
|
82
|
+
bundle: {
|
|
83
|
+
type: "boolean",
|
|
84
|
+
default: false,
|
|
85
|
+
hidden: true,
|
|
86
|
+
},
|
|
87
|
+
"no-bundle": {
|
|
88
|
+
type: "boolean",
|
|
89
|
+
default: true,
|
|
90
|
+
description: "Whether to run bundling on `_worker.js`",
|
|
91
|
+
},
|
|
92
|
+
"experimental-worker-bundle": {
|
|
93
|
+
type: "boolean",
|
|
94
|
+
default: false,
|
|
95
|
+
hidden: true,
|
|
96
|
+
description:
|
|
97
|
+
"Whether to process non-JS module imports or not, such as wasm/text/binary, when we run bundling on `functions` or `_worker.js`",
|
|
98
|
+
},
|
|
81
99
|
binding: {
|
|
82
100
|
type: "array",
|
|
83
101
|
description: "Bind variable/secret (KEY=VALUE)",
|
|
@@ -162,6 +180,9 @@ export const Handler = async ({
|
|
|
162
180
|
"inspector-port": inspectorPort,
|
|
163
181
|
proxy: requestedProxyPort,
|
|
164
182
|
"script-path": singleWorkerScriptPath,
|
|
183
|
+
bundle,
|
|
184
|
+
noBundle,
|
|
185
|
+
experimentalWorkerBundle,
|
|
165
186
|
binding: bindings = [],
|
|
166
187
|
kv: kvs = [],
|
|
167
188
|
do: durableObjects = [],
|
|
@@ -252,23 +273,35 @@ export const Handler = async ({
|
|
|
252
273
|
let scriptPath = "";
|
|
253
274
|
|
|
254
275
|
if (usingWorkerScript) {
|
|
255
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
256
|
-
scriptReadyResolve!();
|
|
257
|
-
|
|
258
276
|
scriptPath = workerScriptPath;
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
try {
|
|
262
|
-
await esbuild.build({
|
|
263
|
-
entryPoints: [scriptPath],
|
|
264
|
-
write: false,
|
|
265
|
-
// we need it to be bundled so that any imports that are used are affected by the blocker plugin
|
|
266
|
-
bundle: true,
|
|
267
|
-
plugins: [blockWorkerJsImports],
|
|
268
|
-
});
|
|
269
|
-
} catch {}
|
|
277
|
+
let runBuild = async () => {
|
|
278
|
+
await checkRawWorker(workerScriptPath, () => scriptReadyResolve());
|
|
270
279
|
};
|
|
271
280
|
|
|
281
|
+
const enableBundling = bundle || !noBundle || experimentalWorkerBundle;
|
|
282
|
+
if (enableBundling) {
|
|
283
|
+
// We want to actually run the `_worker.js` script through the bundler
|
|
284
|
+
// So update the final path to the script that will be uploaded and
|
|
285
|
+
// change the `runBuild()` function to bundle the `_worker.js`.
|
|
286
|
+
scriptPath = join(tmpdir(), `./bundledWorker-${Math.random()}.mjs`);
|
|
287
|
+
runBuild = async () => {
|
|
288
|
+
try {
|
|
289
|
+
await buildRawWorker({
|
|
290
|
+
workerScriptPath,
|
|
291
|
+
outfile: scriptPath,
|
|
292
|
+
directory: directory ?? ".",
|
|
293
|
+
local: true,
|
|
294
|
+
sourcemap: true,
|
|
295
|
+
watch: true,
|
|
296
|
+
onEnd: () => scriptReadyResolve(),
|
|
297
|
+
experimentalWorkerBundle,
|
|
298
|
+
});
|
|
299
|
+
} catch (e: unknown) {
|
|
300
|
+
logger.warn("Failed to bundle _worker.js.", e);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
272
305
|
await runBuild();
|
|
273
306
|
watch([scriptPath], {
|
|
274
307
|
persistent: true,
|
|
@@ -278,8 +311,7 @@ export const Handler = async ({
|
|
|
278
311
|
});
|
|
279
312
|
} else if (usingFunctions) {
|
|
280
313
|
// Try to use Functions
|
|
281
|
-
|
|
282
|
-
scriptPath = outfile;
|
|
314
|
+
scriptPath = join(tmpdir(), `./functionsWorker-${Math.random()}.mjs`);
|
|
283
315
|
|
|
284
316
|
if (nodeCompat) {
|
|
285
317
|
console.warn(
|
|
@@ -287,38 +319,32 @@ export const Handler = async ({
|
|
|
287
319
|
);
|
|
288
320
|
}
|
|
289
321
|
|
|
290
|
-
logger.log(`Compiling worker to "${
|
|
322
|
+
logger.log(`Compiling worker to "${scriptPath}"...`);
|
|
291
323
|
const onEnd = () => scriptReadyResolve();
|
|
292
324
|
try {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
325
|
+
const buildFn = async () => {
|
|
326
|
+
await buildFunctions({
|
|
327
|
+
outfile: scriptPath,
|
|
328
|
+
functionsDirectory,
|
|
329
|
+
sourcemap: true,
|
|
330
|
+
watch: true,
|
|
331
|
+
onEnd,
|
|
332
|
+
buildOutputDirectory: directory,
|
|
333
|
+
nodeCompat,
|
|
334
|
+
local: true,
|
|
335
|
+
experimentalWorkerBundle,
|
|
336
|
+
});
|
|
337
|
+
await metrics.sendMetricsEvent("build pages functions");
|
|
338
|
+
};
|
|
304
339
|
|
|
340
|
+
await buildFn();
|
|
305
341
|
// If Functions found routes, continue using Functions
|
|
306
342
|
watch([functionsDirectory], {
|
|
307
343
|
persistent: true,
|
|
308
344
|
ignoreInitial: true,
|
|
309
345
|
}).on("all", async () => {
|
|
310
346
|
try {
|
|
311
|
-
await
|
|
312
|
-
outfile,
|
|
313
|
-
functionsDirectory,
|
|
314
|
-
sourcemap: true,
|
|
315
|
-
watch: true,
|
|
316
|
-
onEnd,
|
|
317
|
-
buildOutputDirectory: directory,
|
|
318
|
-
nodeCompat,
|
|
319
|
-
local: true,
|
|
320
|
-
});
|
|
321
|
-
await metrics.sendMetricsEvent("build pages functions");
|
|
347
|
+
await buildFn();
|
|
322
348
|
} catch (e) {
|
|
323
349
|
if (e instanceof FunctionsNoRoutesError) {
|
|
324
350
|
logger.warn(
|
|
@@ -359,7 +385,7 @@ export const Handler = async ({
|
|
|
359
385
|
|
|
360
386
|
if (scriptPath === "") {
|
|
361
387
|
// Failed to get a script with or without Functions,
|
|
362
|
-
// something really bad must have
|
|
388
|
+
// something really bad must have happened.
|
|
363
389
|
throw new FatalError(
|
|
364
390
|
"Failed to start wrangler pages dev due to an unknown error",
|
|
365
391
|
1
|
|
@@ -441,7 +467,7 @@ export const Handler = async ({
|
|
|
441
467
|
* If _routes.json is invalid, don't exit but instead fallback to a sensible default
|
|
442
468
|
* and continue to serve the assets. At the same time make sure we warn users that we
|
|
443
469
|
* we detected an invalid file and that we'll be using a default.
|
|
444
|
-
* This basically
|
|
470
|
+
* This basically equates to serving a Functions or _worker.js project as is,
|
|
445
471
|
* without applying any additional routing rules on top.
|
|
446
472
|
*/
|
|
447
473
|
const error =
|
|
@@ -688,24 +714,3 @@ async function spawnProxyProcess({
|
|
|
688
714
|
|
|
689
715
|
return port;
|
|
690
716
|
}
|
|
691
|
-
|
|
692
|
-
// TODO: Kill this once we have https://github.com/cloudflare/wrangler2/issues/2153
|
|
693
|
-
const blockWorkerJsImports: esbuild.Plugin = {
|
|
694
|
-
name: "block-worker-js-imports",
|
|
695
|
-
setup(build) {
|
|
696
|
-
build.onResolve({ filter: /.*/g }, (args) => {
|
|
697
|
-
// If it's the entrypoint, let it be as is
|
|
698
|
-
if (args.kind === "entry-point") {
|
|
699
|
-
return {
|
|
700
|
-
path: args.path,
|
|
701
|
-
};
|
|
702
|
-
}
|
|
703
|
-
// Otherwise, block any imports that the file is requesting
|
|
704
|
-
logger.error(
|
|
705
|
-
`_worker.js is importing from another file. This will throw an error if deployed.\nYou should bundle your Worker or remove the import if it is unused.`
|
|
706
|
-
);
|
|
707
|
-
// Miniflare will error with this briefly down the line -- there's no point in continuing.
|
|
708
|
-
process.exit(1);
|
|
709
|
-
});
|
|
710
|
-
},
|
|
711
|
-
};
|
|
@@ -3,6 +3,7 @@ import { dirname, relative, resolve } from "node:path";
|
|
|
3
3
|
import { bundleWorker } from "../../bundle";
|
|
4
4
|
import { getBasePath } from "../../paths";
|
|
5
5
|
import { D1_BETA_PREFIX } from "../../worker";
|
|
6
|
+
import { buildNotifierPlugin } from "./buildWorker";
|
|
6
7
|
import type { Options as WorkerOptions } from "./buildWorker";
|
|
7
8
|
|
|
8
9
|
type Options = Omit<WorkerOptions, "fallbackService" | "buildOutputDirectory">;
|
|
@@ -36,27 +37,9 @@ export function buildPlugin({
|
|
|
36
37
|
betaD1Shims: (betaD1Shims || []).map(
|
|
37
38
|
(binding) => `${D1_BETA_PREFIX}${binding}`
|
|
38
39
|
),
|
|
40
|
+
doBindings: [], // Pages functions don't support internal Durable Objects
|
|
39
41
|
plugins: [
|
|
40
|
-
|
|
41
|
-
name: "wrangler notifier and monitor",
|
|
42
|
-
setup(pluginBuild) {
|
|
43
|
-
pluginBuild.onEnd((result) => {
|
|
44
|
-
if (result.errors.length > 0) {
|
|
45
|
-
console.error(
|
|
46
|
-
`${result.errors.length} error(s) and ${result.warnings.length} warning(s) when compiling Worker.`
|
|
47
|
-
);
|
|
48
|
-
} else if (result.warnings.length > 0) {
|
|
49
|
-
console.warn(
|
|
50
|
-
`${result.warnings.length} warning(s) when compiling Worker.`
|
|
51
|
-
);
|
|
52
|
-
onEnd();
|
|
53
|
-
} else {
|
|
54
|
-
console.log("Compiled Worker successfully.");
|
|
55
|
-
onEnd();
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
},
|
|
59
|
-
},
|
|
42
|
+
buildNotifierPlugin(onEnd),
|
|
60
43
|
{
|
|
61
44
|
name: "Assets",
|
|
62
45
|
setup(pluginBuild) {
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { access, cp, lstat, rm } from "node:fs/promises";
|
|
2
2
|
import { join, resolve } from "node:path";
|
|
3
|
+
import { build as esBuild } from "esbuild";
|
|
3
4
|
import { nanoid } from "nanoid";
|
|
4
5
|
import { bundleWorker } from "../../bundle";
|
|
6
|
+
import { logger } from "../../logger";
|
|
5
7
|
import { getBasePath } from "../../paths";
|
|
6
8
|
import { D1_BETA_PREFIX } from "../../worker";
|
|
9
|
+
import type { Plugin } from "esbuild";
|
|
7
10
|
|
|
8
11
|
export type Options = {
|
|
9
12
|
routesModule: string;
|
|
@@ -18,6 +21,7 @@ export type Options = {
|
|
|
18
21
|
functionsDirectory: string;
|
|
19
22
|
local: boolean;
|
|
20
23
|
betaD1Shims?: string[];
|
|
24
|
+
experimentalWorkerBundle?: boolean;
|
|
21
25
|
};
|
|
22
26
|
|
|
23
27
|
export function buildWorker({
|
|
@@ -33,6 +37,7 @@ export function buildWorker({
|
|
|
33
37
|
functionsDirectory,
|
|
34
38
|
local,
|
|
35
39
|
betaD1Shims,
|
|
40
|
+
experimentalWorkerBundle = false,
|
|
36
41
|
}: Options) {
|
|
37
42
|
return bundleWorker(
|
|
38
43
|
{
|
|
@@ -57,27 +62,9 @@ export function buildWorker({
|
|
|
57
62
|
betaD1Shims: (betaD1Shims || []).map(
|
|
58
63
|
(binding) => `${D1_BETA_PREFIX}${binding}`
|
|
59
64
|
),
|
|
65
|
+
doBindings: [], // Pages functions don't support internal Durable Objects
|
|
60
66
|
plugins: [
|
|
61
|
-
|
|
62
|
-
name: "wrangler notifier and monitor",
|
|
63
|
-
setup(pluginBuild) {
|
|
64
|
-
pluginBuild.onEnd((result) => {
|
|
65
|
-
if (result.errors.length > 0) {
|
|
66
|
-
console.error(
|
|
67
|
-
`${result.errors.length} error(s) and ${result.warnings.length} warning(s) when compiling Worker.`
|
|
68
|
-
);
|
|
69
|
-
} else if (result.warnings.length > 0) {
|
|
70
|
-
console.warn(
|
|
71
|
-
`${result.warnings.length} warning(s) when compiling Worker.`
|
|
72
|
-
);
|
|
73
|
-
onEnd();
|
|
74
|
-
} else {
|
|
75
|
-
console.log("Compiled Worker successfully.");
|
|
76
|
-
onEnd();
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
},
|
|
80
|
-
},
|
|
67
|
+
buildNotifierPlugin(onEnd),
|
|
81
68
|
{
|
|
82
69
|
name: "Assets",
|
|
83
70
|
setup(pluginBuild) {
|
|
@@ -155,7 +142,78 @@ export function buildWorker({
|
|
|
155
142
|
],
|
|
156
143
|
isOutfile: true,
|
|
157
144
|
serveAssetsFromWorker: false,
|
|
158
|
-
disableModuleCollection: true,
|
|
145
|
+
disableModuleCollection: experimentalWorkerBundle ? false : true,
|
|
146
|
+
rules: [],
|
|
147
|
+
checkFetch: local,
|
|
148
|
+
targetConsumer: local ? "dev" : "publish",
|
|
149
|
+
local,
|
|
150
|
+
experimentalLocal: false,
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export type RawOptions = {
|
|
156
|
+
workerScriptPath: string;
|
|
157
|
+
outfile: string;
|
|
158
|
+
directory: string;
|
|
159
|
+
minify?: boolean;
|
|
160
|
+
sourcemap?: boolean;
|
|
161
|
+
watch?: boolean;
|
|
162
|
+
plugins?: Plugin[];
|
|
163
|
+
onEnd?: () => void;
|
|
164
|
+
buildOutputDirectory?: string;
|
|
165
|
+
nodeCompat?: boolean;
|
|
166
|
+
local: boolean;
|
|
167
|
+
betaD1Shims?: string[];
|
|
168
|
+
experimentalWorkerBundle?: boolean;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* This function bundles a raw `_worker.js` Pages file
|
|
173
|
+
* before it gets deployed.
|
|
174
|
+
*
|
|
175
|
+
* This allows Wrangler to add shims and other wrappers
|
|
176
|
+
* around the handlers, which is useful to support beta features.
|
|
177
|
+
*/
|
|
178
|
+
export function buildRawWorker({
|
|
179
|
+
workerScriptPath,
|
|
180
|
+
outfile,
|
|
181
|
+
directory,
|
|
182
|
+
minify = false,
|
|
183
|
+
sourcemap = false,
|
|
184
|
+
watch = false,
|
|
185
|
+
plugins = [],
|
|
186
|
+
onEnd = () => {},
|
|
187
|
+
nodeCompat,
|
|
188
|
+
local,
|
|
189
|
+
betaD1Shims,
|
|
190
|
+
experimentalWorkerBundle = false,
|
|
191
|
+
}: RawOptions) {
|
|
192
|
+
return bundleWorker(
|
|
193
|
+
{
|
|
194
|
+
file: workerScriptPath,
|
|
195
|
+
directory: resolve(directory),
|
|
196
|
+
format: "modules",
|
|
197
|
+
},
|
|
198
|
+
resolve(outfile),
|
|
199
|
+
{
|
|
200
|
+
minify,
|
|
201
|
+
sourcemap,
|
|
202
|
+
watch,
|
|
203
|
+
nodeCompat,
|
|
204
|
+
loader: {
|
|
205
|
+
".txt": "text",
|
|
206
|
+
".html": "text",
|
|
207
|
+
},
|
|
208
|
+
define: {},
|
|
209
|
+
betaD1Shims: (betaD1Shims || []).map(
|
|
210
|
+
(binding) => `${D1_BETA_PREFIX}${binding}`
|
|
211
|
+
),
|
|
212
|
+
doBindings: [], // Pages functions don't support internal Durable Objects
|
|
213
|
+
plugins: [...plugins, buildNotifierPlugin(onEnd)],
|
|
214
|
+
isOutfile: true,
|
|
215
|
+
serveAssetsFromWorker: false,
|
|
216
|
+
disableModuleCollection: experimentalWorkerBundle ? false : true,
|
|
159
217
|
rules: [],
|
|
160
218
|
checkFetch: local,
|
|
161
219
|
targetConsumer: local ? "dev" : "publish",
|
|
@@ -164,3 +222,67 @@ export function buildWorker({
|
|
|
164
222
|
}
|
|
165
223
|
);
|
|
166
224
|
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Creates an esbuild plugin that can notify Wrangler (via the `onEnd()`)
|
|
228
|
+
* when the build completes.
|
|
229
|
+
*/
|
|
230
|
+
export function buildNotifierPlugin(onEnd: () => void): Plugin {
|
|
231
|
+
return {
|
|
232
|
+
name: "wrangler notifier and monitor",
|
|
233
|
+
setup(pluginBuild) {
|
|
234
|
+
pluginBuild.onEnd((result) => {
|
|
235
|
+
if (result.errors.length > 0) {
|
|
236
|
+
logger.error(
|
|
237
|
+
`${result.errors.length} error(s) and ${result.warnings.length} warning(s) when compiling Worker.`
|
|
238
|
+
);
|
|
239
|
+
} else if (result.warnings.length > 0) {
|
|
240
|
+
logger.warn(
|
|
241
|
+
`${result.warnings.length} warning(s) when compiling Worker.`
|
|
242
|
+
);
|
|
243
|
+
onEnd();
|
|
244
|
+
} else {
|
|
245
|
+
logger.log("✨ Compiled Worker successfully");
|
|
246
|
+
onEnd();
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Runs the script through a simple esbuild bundle step to check for unwanted imports.
|
|
255
|
+
*
|
|
256
|
+
* This is useful when the user chooses not to bundle the `_worker.js` file by setting
|
|
257
|
+
* `--no-bundle` at the command line.
|
|
258
|
+
*/
|
|
259
|
+
export async function checkRawWorker(scriptPath: string, onEnd: () => void) {
|
|
260
|
+
await esBuild({
|
|
261
|
+
entryPoints: [scriptPath],
|
|
262
|
+
write: false,
|
|
263
|
+
// we need it to be bundled so that any imports that are used are affected by the blocker plugin
|
|
264
|
+
bundle: true,
|
|
265
|
+
plugins: [blockWorkerJsImports, buildNotifierPlugin(onEnd)],
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const blockWorkerJsImports: Plugin = {
|
|
270
|
+
name: "block-worker-js-imports",
|
|
271
|
+
setup(build) {
|
|
272
|
+
build.onResolve({ filter: /.*/g }, (args) => {
|
|
273
|
+
// If it's the entrypoint, let it be as is
|
|
274
|
+
if (args.kind === "entry-point") {
|
|
275
|
+
return {
|
|
276
|
+
path: args.path,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
// Otherwise, block any imports that the file is requesting
|
|
280
|
+
logger.error(
|
|
281
|
+
"_worker.js is not being bundled by Wrangler but it is importing from another file.\n" +
|
|
282
|
+
"This will throw an error if deployed.\n" +
|
|
283
|
+
"You should bundle the Worker in a pre-build step, remove the import if it is unused, or ask Wrangler to bundle it by setting `--bundle`."
|
|
284
|
+
);
|
|
285
|
+
process.exit(1);
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
};
|