wrangler 2.20.0 → 3.0.0
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/README.md +4 -4
- package/bin/wrangler.js +9 -75
- package/package.json +5 -13
- package/templates/__tests__/tsconfig.tsbuildinfo +1 -1
- package/templates/checked-fetch.js +1 -1
- package/templates/first-party-worker-module-facade.ts +2 -2
- package/templates/middleware/common.ts +9 -4
- package/templates/middleware/loader-sw.ts +2 -7
- package/templates/new-worker-scheduled.ts +1 -1
- package/templates/new-worker.ts +1 -1
- package/templates/pages-dev-util.ts +4 -1
- package/templates/pages-shim.ts +0 -3
- package/templates/tsconfig.tsbuildinfo +1 -1
- package/wrangler-dist/cli.d.ts +149 -75
- package/wrangler-dist/cli.js +60062 -64338
- package/import_meta_url.js +0 -3
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +0 -1
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/miniflare-dist/index.mjs +0 -6442
- package/src/__tests__/access.test.ts +0 -25
- package/src/__tests__/api-dev.test.ts +0 -238
- package/src/__tests__/api-devregistry.test.ts +0 -121
- package/src/__tests__/api.test.ts +0 -102
- package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
- package/src/__tests__/config-cache.test.ts +0 -42
- package/src/__tests__/configuration.test.ts +0 -4517
- package/src/__tests__/constellation.test.ts +0 -371
- package/src/__tests__/d1/d1.test.ts +0 -82
- package/src/__tests__/d1/execute.test.ts +0 -66
- package/src/__tests__/d1/migrate.test.ts +0 -257
- package/src/__tests__/d1/splitter.test.ts +0 -255
- package/src/__tests__/delete.test.ts +0 -272
- package/src/__tests__/deployments.test.ts +0 -369
- package/src/__tests__/dev.test.tsx +0 -1617
- package/src/__tests__/generate.test.ts +0 -237
- package/src/__tests__/get-host-from-url.test.ts +0 -16
- package/src/__tests__/guess-worker-format.test.ts +0 -120
- package/src/__tests__/helpers/clipboardy-mock.js +0 -4
- package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
- package/src/__tests__/helpers/end-event-loop.ts +0 -6
- package/src/__tests__/helpers/mock-account-id.ts +0 -48
- package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
- package/src/__tests__/helpers/mock-bin.ts +0 -36
- package/src/__tests__/helpers/mock-console.ts +0 -112
- package/src/__tests__/helpers/mock-dialogs.ts +0 -139
- package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
- package/src/__tests__/helpers/mock-http-server.ts +0 -46
- package/src/__tests__/helpers/mock-istty.ts +0 -74
- package/src/__tests__/helpers/mock-known-routes.ts +0 -12
- package/src/__tests__/helpers/mock-kv.ts +0 -46
- package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
- package/src/__tests__/helpers/mock-process.ts +0 -34
- package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
- package/src/__tests__/helpers/mock-stdin.ts +0 -108
- package/src/__tests__/helpers/mock-web-socket.ts +0 -29
- package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
- package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
- package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -160
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
- package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
- package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
- package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
- package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
- package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
- package/src/__tests__/helpers/msw/index.ts +0 -52
- package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
- package/src/__tests__/helpers/run-in-tmp.ts +0 -38
- package/src/__tests__/helpers/run-wrangler.ts +0 -16
- package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
- package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
- package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
- package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
- package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -11
- package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
- package/src/__tests__/helpers/write-worker-source.ts +0 -31
- package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
- package/src/__tests__/https-options.test.ts +0 -163
- package/src/__tests__/index.test.ts +0 -282
- package/src/__tests__/init.test.ts +0 -3196
- package/src/__tests__/jest.setup.ts +0 -179
- package/src/__tests__/kv.test.ts +0 -1799
- package/src/__tests__/logger.test.ts +0 -207
- package/src/__tests__/logout.test.ts +0 -47
- package/src/__tests__/metrics.test.ts +0 -493
- package/src/__tests__/middleware.scheduled.test.ts +0 -145
- package/src/__tests__/middleware.test.ts +0 -816
- package/src/__tests__/mtls-certificates.test.ts +0 -589
- package/src/__tests__/package-manager.test.ts +0 -353
- package/src/__tests__/pages/deployment-list.test.ts +0 -80
- package/src/__tests__/pages/functions-build.test.ts +0 -528
- package/src/__tests__/pages/pages.test.ts +0 -81
- package/src/__tests__/pages/project-create.test.ts +0 -63
- package/src/__tests__/pages/project-list.test.ts +0 -110
- package/src/__tests__/pages/project-upload.test.ts +0 -500
- package/src/__tests__/pages/publish.test.ts +0 -2864
- package/src/__tests__/pages-deployment-tail.test.ts +0 -957
- package/src/__tests__/parse.test.ts +0 -436
- package/src/__tests__/paths.test.ts +0 -39
- package/src/__tests__/publish.test.ts +0 -8849
- package/src/__tests__/pubsub.test.ts +0 -496
- package/src/__tests__/queues.test.ts +0 -532
- package/src/__tests__/r2.test.ts +0 -374
- package/src/__tests__/route.test.ts +0 -45
- package/src/__tests__/secret.test.ts +0 -693
- package/src/__tests__/tail.test.ts +0 -989
- package/src/__tests__/test-old-node-version.js +0 -31
- package/src/__tests__/traverse-module-graph.test.ts +0 -220
- package/src/__tests__/tsconfig-sanity.ts +0 -12
- package/src/__tests__/tsconfig.json +0 -8
- package/src/__tests__/tsconfig.tsbuildinfo +0 -1
- package/src/__tests__/type-generation.test.ts +0 -234
- package/src/__tests__/user.test.ts +0 -118
- package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
- package/src/__tests__/validate-dev-props.test.ts +0 -56
- package/src/__tests__/version.test.ts +0 -35
- package/src/__tests__/whoami.test.tsx +0 -172
- package/src/__tests__/worker-namespace.test.ts +0 -340
- package/src/abort.d.ts +0 -3
- package/src/api/dev.ts +0 -321
- package/src/api/index.ts +0 -11
- package/src/api/mtls-certificate.ts +0 -148
- package/src/api/pages/create-worker-bundle-contents.ts +0 -77
- package/src/api/pages/index.ts +0 -5
- package/src/api/pages/publish.tsx +0 -371
- package/src/bundle-reporter.ts +0 -68
- package/src/bundle.ts +0 -929
- package/src/cfetch/index.ts +0 -158
- package/src/cfetch/internal.ts +0 -258
- package/src/cli.ts +0 -28
- package/src/config/README.md +0 -107
- package/src/config/config.ts +0 -282
- package/src/config/diagnostics.ts +0 -80
- package/src/config/environment.ts +0 -625
- package/src/config/index.ts +0 -403
- package/src/config/validation-helpers.ts +0 -597
- package/src/config/validation.ts +0 -2369
- package/src/config-cache.ts +0 -85
- package/src/constellation/createProject.tsx +0 -51
- package/src/constellation/deleteProject.ts +0 -51
- package/src/constellation/deleteProjectModel.ts +0 -68
- package/src/constellation/index.ts +0 -75
- package/src/constellation/listCatalog.tsx +0 -35
- package/src/constellation/listModel.tsx +0 -41
- package/src/constellation/listProject.tsx +0 -28
- package/src/constellation/listRuntime.tsx +0 -28
- package/src/constellation/options.ts +0 -17
- package/src/constellation/types.ts +0 -17
- package/src/constellation/uploadModel.tsx +0 -64
- package/src/constellation/utils.ts +0 -90
- package/src/create-worker-preview.ts +0 -293
- package/src/create-worker-upload-form.ts +0 -363
- package/src/d1/backups.tsx +0 -219
- package/src/d1/constants.ts +0 -2
- package/src/d1/create.tsx +0 -70
- package/src/d1/delete.ts +0 -53
- package/src/d1/execute.tsx +0 -357
- package/src/d1/formatTimeAgo.ts +0 -14
- package/src/d1/index.ts +0 -100
- package/src/d1/list.tsx +0 -62
- package/src/d1/migrations/apply.tsx +0 -212
- package/src/d1/migrations/create.tsx +0 -79
- package/src/d1/migrations/helpers.ts +0 -169
- package/src/d1/migrations/index.ts +0 -3
- package/src/d1/migrations/list.tsx +0 -95
- package/src/d1/migrations/options.ts +0 -23
- package/src/d1/options.ts +0 -22
- package/src/d1/splitter.ts +0 -161
- package/src/d1/types.ts +0 -25
- package/src/d1/utils.ts +0 -49
- package/src/delete.ts +0 -100
- package/src/deployments.ts +0 -368
- package/src/deprecated/index.ts +0 -144
- package/src/dev/dev-vars.ts +0 -39
- package/src/dev/dev.tsx +0 -605
- package/src/dev/get-local-persistence-path.ts +0 -31
- package/src/dev/local.tsx +0 -952
- package/src/dev/remote.tsx +0 -635
- package/src/dev/start-server.ts +0 -545
- package/src/dev/use-esbuild.ts +0 -215
- package/src/dev/validate-dev-props.ts +0 -40
- package/src/dev-registry.ts +0 -202
- package/src/dev.tsx +0 -934
- package/src/dialogs.ts +0 -136
- package/src/dispatch-namespace.ts +0 -211
- package/src/docs/helpers.ts +0 -50
- package/src/docs/index.ts +0 -54
- package/src/durable.ts +0 -102
- package/src/entry.ts +0 -344
- package/src/environment-variables/factory.ts +0 -89
- package/src/environment-variables/misc-variables.ts +0 -30
- package/src/errors.ts +0 -11
- package/src/generate/index.ts +0 -298
- package/src/git-client.ts +0 -135
- package/src/global-wrangler-config-path.ts +0 -26
- package/src/https-options.ts +0 -127
- package/src/index.ts +0 -768
- package/src/init.ts +0 -1037
- package/src/inspect.ts +0 -883
- package/src/intl-polyfill.d.ts +0 -139
- package/src/is-ci.ts +0 -14
- package/src/is-interactive.ts +0 -16
- package/src/jest.d.ts +0 -4
- package/src/kv/helpers.ts +0 -433
- package/src/kv/index.ts +0 -594
- package/src/logger.ts +0 -123
- package/src/metrics/index.ts +0 -5
- package/src/metrics/metrics-config.ts +0 -239
- package/src/metrics/metrics-dispatcher.ts +0 -96
- package/src/metrics/metrics-usage-headers.ts +0 -24
- package/src/metrics/send-event.ts +0 -99
- package/src/miniflare-cli/README.md +0 -30
- package/src/miniflare-cli/assets.ts +0 -251
- package/src/miniflare-cli/index.ts +0 -210
- package/src/miniflare-cli/request-context.ts +0 -40
- package/src/miniflare-cli/tsconfig.json +0 -9
- package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
- package/src/miniflare-cli/types.ts +0 -11
- package/src/module-collection.ts +0 -333
- package/src/mtls-certificate/cli.ts +0 -155
- package/src/open-in-browser.ts +0 -17
- package/src/package-manager.ts +0 -219
- package/src/pages/build.ts +0 -423
- package/src/pages/buildFunctions.ts +0 -140
- package/src/pages/constants.ts +0 -18
- package/src/pages/deployment-tails.ts +0 -281
- package/src/pages/deployments.tsx +0 -84
- package/src/pages/dev.ts +0 -734
- package/src/pages/errors.ts +0 -67
- package/src/pages/functions/buildPlugin.ts +0 -114
- package/src/pages/functions/buildWorker.ts +0 -350
- package/src/pages/functions/filepath-routing.test.ts +0 -234
- package/src/pages/functions/filepath-routing.ts +0 -189
- package/src/pages/functions/identifiers.ts +0 -78
- package/src/pages/functions/routes-consolidation.test.ts +0 -250
- package/src/pages/functions/routes-consolidation.ts +0 -73
- package/src/pages/functions/routes-transformation.test.ts +0 -282
- package/src/pages/functions/routes-transformation.ts +0 -115
- package/src/pages/functions/routes-validation.test.ts +0 -403
- package/src/pages/functions/routes-validation.ts +0 -202
- package/src/pages/functions/routes.ts +0 -151
- package/src/pages/functions/tsconfig.json +0 -8
- package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
- package/src/pages/functions.ts +0 -86
- package/src/pages/hash.ts +0 -13
- package/src/pages/index.ts +0 -102
- package/src/pages/projects.tsx +0 -159
- package/src/pages/prompt-select-project.tsx +0 -31
- package/src/pages/publish.tsx +0 -267
- package/src/pages/types.ts +0 -46
- package/src/pages/upload.tsx +0 -469
- package/src/pages/utils.ts +0 -23
- package/src/parse.ts +0 -308
- package/src/paths.ts +0 -71
- package/src/proxy.ts +0 -694
- package/src/publish/index.ts +0 -274
- package/src/publish/publish.ts +0 -1065
- package/src/pubsub/index.ts +0 -286
- package/src/pubsub/pubsub-commands.ts +0 -623
- package/src/queues/cli/commands/consumer/add.ts +0 -71
- package/src/queues/cli/commands/consumer/index.ts +0 -19
- package/src/queues/cli/commands/consumer/remove.ts +0 -31
- package/src/queues/cli/commands/create.ts +0 -25
- package/src/queues/cli/commands/delete.ts +0 -26
- package/src/queues/cli/commands/index.ts +0 -35
- package/src/queues/cli/commands/list.ts +0 -25
- package/src/queues/client.ts +0 -136
- package/src/queues/utils.ts +0 -18
- package/src/r2/constants.ts +0 -4
- package/src/r2/helpers.ts +0 -132
- package/src/r2/index.ts +0 -289
- package/src/routes.ts +0 -140
- package/src/secret/index.ts +0 -377
- package/src/selfsigned.d.ts +0 -29
- package/src/sites.ts +0 -484
- package/src/tail/createTail.ts +0 -415
- package/src/tail/filters.ts +0 -277
- package/src/tail/index.ts +0 -211
- package/src/tail/printing.ts +0 -132
- package/src/traverse-module-graph.ts +0 -54
- package/src/tsconfig-sanity.ts +0 -16
- package/src/type-generation.ts +0 -181
- package/src/update-check.ts +0 -19
- package/src/user/access.ts +0 -68
- package/src/user/auth-variables.ts +0 -113
- package/src/user/choose-account.tsx +0 -39
- package/src/user/generate-auth-url.ts +0 -33
- package/src/user/generate-random-state.ts +0 -16
- package/src/user/index.ts +0 -2
- package/src/user/user.ts +0 -1234
- package/src/utils/collectKeyValues.ts +0 -14
- package/src/utils/render.ts +0 -93
- package/src/whoami.ts +0 -135
- package/src/worker.ts +0 -279
- package/src/yargs-types.ts +0 -37
- package/src/zones.ts +0 -191
package/src/bundle.ts
DELETED
|
@@ -1,929 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert";
|
|
2
|
-
import * as fs from "node:fs";
|
|
3
|
-
import { builtinModules } from "node:module";
|
|
4
|
-
import * as path from "node:path";
|
|
5
|
-
import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
|
|
6
|
-
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
|
|
7
|
-
import * as esbuild from "esbuild";
|
|
8
|
-
import tmp from "tmp-promise";
|
|
9
|
-
import createModuleCollector from "./module-collection";
|
|
10
|
-
import { getBasePath } from "./paths";
|
|
11
|
-
import type { Config } from "./config";
|
|
12
|
-
import type { DurableObjectBindings } from "./config/environment";
|
|
13
|
-
import type { WorkerRegistry } from "./dev-registry";
|
|
14
|
-
import type { Entry } from "./entry";
|
|
15
|
-
import type { SourceMapMetadata } from "./inspect";
|
|
16
|
-
import type { CfModule } from "./worker";
|
|
17
|
-
|
|
18
|
-
export const COMMON_ESBUILD_OPTIONS = {
|
|
19
|
-
// Our workerd runtime uses the same V8 version as recent Chrome, which is highly ES2022 compliant: https://kangax.github.io/compat-table/es2016plus/
|
|
20
|
-
target: "es2022",
|
|
21
|
-
loader: { ".js": "jsx", ".mjs": "jsx", ".cjs": "jsx" },
|
|
22
|
-
} as const;
|
|
23
|
-
|
|
24
|
-
export type BundleResult = {
|
|
25
|
-
modules: CfModule[];
|
|
26
|
-
dependencies: esbuild.Metafile["outputs"][string]["inputs"];
|
|
27
|
-
resolvedEntryPointPath: string;
|
|
28
|
-
bundleType: "esm" | "commonjs";
|
|
29
|
-
stop: (() => void) | undefined;
|
|
30
|
-
sourceMapPath?: string | undefined;
|
|
31
|
-
sourceMapMetadata?: SourceMapMetadata | undefined;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
type StaticAssetsConfig =
|
|
35
|
-
| (Config["assets"] & {
|
|
36
|
-
bypassCache: boolean | undefined;
|
|
37
|
-
})
|
|
38
|
-
| undefined;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* When applying the middleware facade for service workers, we need to inject
|
|
42
|
-
* some code at the top of the final output bundle. Applying an inject too early
|
|
43
|
-
* will allow esbuild to reorder the code. Additionally, we need to make sure
|
|
44
|
-
* user code is bundled in the final esbuild step with `watch` correctly
|
|
45
|
-
* configured, so code changes are detected.
|
|
46
|
-
*
|
|
47
|
-
* This type is used as the return type for the `MiddlewareFn` type representing
|
|
48
|
-
* a facade-applying function. Returned injects should be injected with the
|
|
49
|
-
* final esbuild step.
|
|
50
|
-
*/
|
|
51
|
-
type EntryWithInject = Entry & { inject?: string[] };
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* RegExp matching against esbuild's error text when it is unable to resolve
|
|
55
|
-
* a Node built-in module. If we detect this when node_compat is disabled,
|
|
56
|
-
* we'll rewrite the error to suggest enabling it.
|
|
57
|
-
*/
|
|
58
|
-
const nodeBuiltinResolveErrorText = new RegExp(
|
|
59
|
-
'^Could not resolve "(' +
|
|
60
|
-
builtinModules.join("|") +
|
|
61
|
-
"|" +
|
|
62
|
-
builtinModules.map((module) => "node:" + module).join("|") +
|
|
63
|
-
')"$'
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Returns true if the passed value looks like an esbuild BuildFailure object
|
|
68
|
-
*/
|
|
69
|
-
export function isBuildFailure(err: unknown): err is esbuild.BuildFailure {
|
|
70
|
-
return (
|
|
71
|
-
typeof err === "object" &&
|
|
72
|
-
err !== null &&
|
|
73
|
-
"errors" in err &&
|
|
74
|
-
"warnings" in err
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Rewrites esbuild BuildFailures for failing to resolve Node built-in modules
|
|
80
|
-
* to suggest enabling Node compat as opposed to `platform: "node"`.
|
|
81
|
-
*/
|
|
82
|
-
export function rewriteNodeCompatBuildFailure(
|
|
83
|
-
err: esbuild.BuildFailure,
|
|
84
|
-
forPages = false
|
|
85
|
-
) {
|
|
86
|
-
for (const error of err.errors) {
|
|
87
|
-
const match = nodeBuiltinResolveErrorText.exec(error.text);
|
|
88
|
-
if (match !== null) {
|
|
89
|
-
const issue = `The package "${match[1]}" wasn't found on the file system but is built into node.`;
|
|
90
|
-
|
|
91
|
-
const instructionForUser = `${
|
|
92
|
-
forPages
|
|
93
|
-
? 'Add the "nodejs_compat" compatibility flag to your Pages project'
|
|
94
|
-
: 'Add "node_compat = true" to your wrangler.toml file'
|
|
95
|
-
} to enable Node.js compatibility.`;
|
|
96
|
-
|
|
97
|
-
error.notes = [
|
|
98
|
-
{
|
|
99
|
-
location: null,
|
|
100
|
-
text: `${issue}\n${instructionForUser}`,
|
|
101
|
-
},
|
|
102
|
-
];
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const nodejsCompatPlugin: esbuild.Plugin = {
|
|
108
|
-
name: "nodejs_compat Plugin",
|
|
109
|
-
setup(pluginBuild) {
|
|
110
|
-
pluginBuild.onResolve({ filter: /node:.*/ }, () => {
|
|
111
|
-
return { external: true };
|
|
112
|
-
});
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const cloudflareJsPlugin: esbuild.Plugin = {
|
|
117
|
-
name: "cloudflare javascript Plugin",
|
|
118
|
-
setup(pluginBuild) {
|
|
119
|
-
pluginBuild.onResolve({ filter: /^cloudflare:.*/ }, () => {
|
|
120
|
-
return { external: true };
|
|
121
|
-
});
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Generate a bundle for the worker identified by the arguments passed in.
|
|
127
|
-
*/
|
|
128
|
-
export async function bundleWorker(
|
|
129
|
-
entry: Entry,
|
|
130
|
-
destination: string,
|
|
131
|
-
options: {
|
|
132
|
-
// When `bundle` is set to false, we apply shims to the Worker, but won't pull in any imports
|
|
133
|
-
bundle?: boolean;
|
|
134
|
-
serveAssetsFromWorker: boolean;
|
|
135
|
-
assets?: StaticAssetsConfig;
|
|
136
|
-
betaD1Shims?: string[];
|
|
137
|
-
doBindings: DurableObjectBindings;
|
|
138
|
-
jsxFactory?: string;
|
|
139
|
-
jsxFragment?: string;
|
|
140
|
-
entryName?: string;
|
|
141
|
-
rules: Config["rules"];
|
|
142
|
-
watch?: esbuild.WatchMode | boolean;
|
|
143
|
-
tsconfig?: string;
|
|
144
|
-
minify?: boolean;
|
|
145
|
-
legacyNodeCompat?: boolean;
|
|
146
|
-
nodejsCompat?: boolean;
|
|
147
|
-
define: Config["define"];
|
|
148
|
-
checkFetch: boolean;
|
|
149
|
-
services?: Config["services"];
|
|
150
|
-
workerDefinitions?: WorkerRegistry;
|
|
151
|
-
firstPartyWorkerDevFacade?: boolean;
|
|
152
|
-
targetConsumer: "dev" | "publish";
|
|
153
|
-
local: boolean;
|
|
154
|
-
testScheduled?: boolean;
|
|
155
|
-
experimentalLocal?: boolean;
|
|
156
|
-
inject?: string[];
|
|
157
|
-
loader?: Record<string, string>;
|
|
158
|
-
sourcemap?: esbuild.CommonOptions["sourcemap"];
|
|
159
|
-
plugins?: esbuild.Plugin[];
|
|
160
|
-
// TODO: Rip these out https://github.com/cloudflare/workers-sdk/issues/2153
|
|
161
|
-
disableModuleCollection?: boolean;
|
|
162
|
-
isOutfile?: boolean;
|
|
163
|
-
forPages?: boolean;
|
|
164
|
-
}
|
|
165
|
-
): Promise<BundleResult> {
|
|
166
|
-
const {
|
|
167
|
-
bundle = true,
|
|
168
|
-
serveAssetsFromWorker,
|
|
169
|
-
betaD1Shims,
|
|
170
|
-
doBindings,
|
|
171
|
-
jsxFactory,
|
|
172
|
-
jsxFragment,
|
|
173
|
-
entryName,
|
|
174
|
-
rules,
|
|
175
|
-
watch,
|
|
176
|
-
tsconfig,
|
|
177
|
-
minify,
|
|
178
|
-
legacyNodeCompat,
|
|
179
|
-
nodejsCompat,
|
|
180
|
-
checkFetch,
|
|
181
|
-
local,
|
|
182
|
-
assets,
|
|
183
|
-
workerDefinitions,
|
|
184
|
-
services,
|
|
185
|
-
firstPartyWorkerDevFacade,
|
|
186
|
-
targetConsumer,
|
|
187
|
-
testScheduled,
|
|
188
|
-
experimentalLocal,
|
|
189
|
-
inject: injectOption,
|
|
190
|
-
loader,
|
|
191
|
-
sourcemap,
|
|
192
|
-
plugins,
|
|
193
|
-
disableModuleCollection,
|
|
194
|
-
isOutfile,
|
|
195
|
-
forPages,
|
|
196
|
-
} = options;
|
|
197
|
-
|
|
198
|
-
// We create a temporary directory for any oneoff files we
|
|
199
|
-
// need to create. This is separate from the main build
|
|
200
|
-
// directory (`destination`).
|
|
201
|
-
const tmpDir = await tmp.dir({ unsafeCleanup: true });
|
|
202
|
-
|
|
203
|
-
const entryDirectory = path.dirname(entry.file);
|
|
204
|
-
let moduleCollector = createModuleCollector({
|
|
205
|
-
wrangler1xlegacyModuleReferences: {
|
|
206
|
-
rootDirectory: entryDirectory,
|
|
207
|
-
fileNames: new Set(
|
|
208
|
-
fs
|
|
209
|
-
.readdirSync(entryDirectory, { withFileTypes: true })
|
|
210
|
-
.filter(
|
|
211
|
-
(dirEntry) =>
|
|
212
|
-
dirEntry.isFile() && dirEntry.name !== path.basename(entry.file)
|
|
213
|
-
)
|
|
214
|
-
.map((dirEnt) => dirEnt.name)
|
|
215
|
-
),
|
|
216
|
-
},
|
|
217
|
-
format: entry.format,
|
|
218
|
-
rules,
|
|
219
|
-
});
|
|
220
|
-
if (disableModuleCollection) {
|
|
221
|
-
moduleCollector = {
|
|
222
|
-
modules: [],
|
|
223
|
-
plugin: {
|
|
224
|
-
name: moduleCollector.plugin.name,
|
|
225
|
-
setup: () => {},
|
|
226
|
-
},
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// In dev, we want to patch `fetch()` with a special version that looks
|
|
231
|
-
// for bad usages and can warn the user about them; so we inject
|
|
232
|
-
// `checked-fetch.js` to do so. However, with yarn 3 style pnp,
|
|
233
|
-
// we need to extract that file to an accessible place before injecting
|
|
234
|
-
// it in, hence this code here.
|
|
235
|
-
|
|
236
|
-
const checkedFetchFileToInject = path.join(tmpDir.path, "checked-fetch.js");
|
|
237
|
-
|
|
238
|
-
if (checkFetch && !fs.existsSync(checkedFetchFileToInject)) {
|
|
239
|
-
fs.mkdirSync(tmpDir.path, {
|
|
240
|
-
recursive: true,
|
|
241
|
-
});
|
|
242
|
-
fs.writeFileSync(
|
|
243
|
-
checkedFetchFileToInject,
|
|
244
|
-
fs.readFileSync(path.resolve(getBasePath(), "templates/checked-fetch.js"))
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// At this point, we take the opportunity to "wrap" any input workers
|
|
249
|
-
// with any extra functionality we may want to add. This is done by
|
|
250
|
-
// passing the entry point through a pipeline of functions that return
|
|
251
|
-
// a new entry point, that we call "middleware" or "facades".
|
|
252
|
-
// Look at implementations of these functions to learn more.
|
|
253
|
-
|
|
254
|
-
// We also have middleware that uses a more "traditional" middleware stack,
|
|
255
|
-
// which is all loaded as one in a stack.
|
|
256
|
-
const middlewareToLoad: MiddlewareLoader[] = [];
|
|
257
|
-
|
|
258
|
-
if (testScheduled) {
|
|
259
|
-
middlewareToLoad.push({
|
|
260
|
-
path: "templates/middleware/middleware-scheduled.ts",
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
if (experimentalLocal) {
|
|
264
|
-
// In Miniflare 3, we bind the user's worker as a service binding in a
|
|
265
|
-
// special entry worker that handles things like injecting `Request.cf`,
|
|
266
|
-
// live-reload, and the pretty-error page.
|
|
267
|
-
//
|
|
268
|
-
// Unfortunately, due to a bug in `workerd`, errors thrown asynchronously by
|
|
269
|
-
// native APIs don't have `stack`s. This means Miniflare can't extract the
|
|
270
|
-
// `stack` trace from dispatching to the user worker service binding by
|
|
271
|
-
// `try/catch`.
|
|
272
|
-
//
|
|
273
|
-
// As a stop-gap solution, if the `MF-Experimental-Error-Stack` header is
|
|
274
|
-
// truthy on responses, the body will be interpreted as a JSON-error of the
|
|
275
|
-
// form `{ message?: string, name?: string, stack?: string }`.
|
|
276
|
-
//
|
|
277
|
-
// This middleware wraps the user's worker in a `try/catch`, and rewrites
|
|
278
|
-
// errors in this format so a pretty-error page can be shown.
|
|
279
|
-
middlewareToLoad.push({
|
|
280
|
-
path: "templates/middleware/middleware-miniflare3-json-error.ts",
|
|
281
|
-
dev: true,
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
type MiddlewareFn = (currentEntry: Entry) => Promise<EntryWithInject>;
|
|
286
|
-
const middleware: (false | undefined | MiddlewareFn)[] = [
|
|
287
|
-
// serve static assets
|
|
288
|
-
serveAssetsFromWorker &&
|
|
289
|
-
((currentEntry: Entry) => {
|
|
290
|
-
return applyStaticAssetFacade(currentEntry, tmpDir.path, assets);
|
|
291
|
-
}),
|
|
292
|
-
// format errors nicely
|
|
293
|
-
// We use an env var here because we don't actually
|
|
294
|
-
// want to expose this to the user. It's only used internally to
|
|
295
|
-
// experiment with middleware as a teaching exercise.
|
|
296
|
-
process.env.FORMAT_WRANGLER_ERRORS === "true" &&
|
|
297
|
-
((currentEntry: Entry) => {
|
|
298
|
-
return applyFormatDevErrorsFacade(currentEntry, tmpDir.path);
|
|
299
|
-
}),
|
|
300
|
-
// bind to other dev instances/service bindings
|
|
301
|
-
workerDefinitions &&
|
|
302
|
-
Object.keys(workerDefinitions).length > 0 &&
|
|
303
|
-
services &&
|
|
304
|
-
services.length > 0 &&
|
|
305
|
-
((currentEntry: Entry) => {
|
|
306
|
-
return applyMultiWorkerDevFacade(
|
|
307
|
-
currentEntry,
|
|
308
|
-
tmpDir.path,
|
|
309
|
-
services,
|
|
310
|
-
workerDefinitions
|
|
311
|
-
);
|
|
312
|
-
}),
|
|
313
|
-
// Simulate internal environment when using first party workers in dev
|
|
314
|
-
firstPartyWorkerDevFacade === true &&
|
|
315
|
-
((currentEntry: Entry) => {
|
|
316
|
-
return applyFirstPartyWorkerDevFacade(currentEntry, tmpDir.path);
|
|
317
|
-
}),
|
|
318
|
-
|
|
319
|
-
Array.isArray(betaD1Shims) &&
|
|
320
|
-
betaD1Shims.length > 0 &&
|
|
321
|
-
((currentEntry: Entry) => {
|
|
322
|
-
return applyD1BetaFacade(
|
|
323
|
-
currentEntry,
|
|
324
|
-
tmpDir.path,
|
|
325
|
-
betaD1Shims,
|
|
326
|
-
local && !experimentalLocal,
|
|
327
|
-
doBindings
|
|
328
|
-
);
|
|
329
|
-
}),
|
|
330
|
-
|
|
331
|
-
// Middleware loader: to add middleware, we add the path to the middleware
|
|
332
|
-
// Currently for demonstration purposes we have two example middlewares
|
|
333
|
-
// Middlewares are togglable by changing the `publish` (default=false) and `dev` (default=true) options
|
|
334
|
-
// As we are not yet supporting user created middlewares yet, if no wrangler applied middleware
|
|
335
|
-
// are found, we will not load any middleware. We also need to check if there are middlewares compatible with
|
|
336
|
-
// the target consumer (dev / publish).
|
|
337
|
-
(middlewareToLoad.filter(
|
|
338
|
-
(m) =>
|
|
339
|
-
(m.publish && targetConsumer === "publish") ||
|
|
340
|
-
(m.dev !== false && targetConsumer === "dev")
|
|
341
|
-
).length > 0 ||
|
|
342
|
-
process.env.EXPERIMENTAL_MIDDLEWARE === "true") &&
|
|
343
|
-
((currentEntry: Entry) => {
|
|
344
|
-
return applyMiddlewareLoaderFacade(
|
|
345
|
-
currentEntry,
|
|
346
|
-
tmpDir.path,
|
|
347
|
-
middlewareToLoad.filter(
|
|
348
|
-
// We dynamically filter the middleware depending on where we are bundling for
|
|
349
|
-
(m) =>
|
|
350
|
-
(targetConsumer === "dev" && m.dev !== false) ||
|
|
351
|
-
(m.publish && targetConsumer === "publish")
|
|
352
|
-
)
|
|
353
|
-
);
|
|
354
|
-
}),
|
|
355
|
-
].filter(Boolean);
|
|
356
|
-
|
|
357
|
-
const inject: string[] = injectOption ?? [];
|
|
358
|
-
if (checkFetch) inject.push(checkedFetchFileToInject);
|
|
359
|
-
|
|
360
|
-
let inputEntry: EntryWithInject = entry;
|
|
361
|
-
for (const middlewareFn of middleware as MiddlewareFn[]) {
|
|
362
|
-
inputEntry = await middlewareFn(inputEntry);
|
|
363
|
-
if (inputEntry.inject !== undefined) inject.push(...inputEntry.inject);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
// At this point, inputEntry points to the entry point we want to build.
|
|
367
|
-
|
|
368
|
-
const buildOptions: esbuild.BuildOptions & { metafile: true } = {
|
|
369
|
-
entryPoints: [inputEntry.file],
|
|
370
|
-
bundle,
|
|
371
|
-
absWorkingDir: entry.directory,
|
|
372
|
-
outdir: destination,
|
|
373
|
-
entryNames: entryName || path.parse(entry.file).name,
|
|
374
|
-
...(isOutfile
|
|
375
|
-
? {
|
|
376
|
-
outdir: undefined,
|
|
377
|
-
outfile: destination,
|
|
378
|
-
entryNames: undefined,
|
|
379
|
-
}
|
|
380
|
-
: {}),
|
|
381
|
-
inject,
|
|
382
|
-
external: bundle ? ["__STATIC_CONTENT_MANIFEST"] : undefined,
|
|
383
|
-
format: entry.format === "modules" ? "esm" : "iife",
|
|
384
|
-
target: COMMON_ESBUILD_OPTIONS.target,
|
|
385
|
-
sourcemap: sourcemap ?? true,
|
|
386
|
-
// Include a reference to the output folder in the sourcemap.
|
|
387
|
-
// This is omitted by default, but we need it to properly resolve source paths in error output.
|
|
388
|
-
sourceRoot: destination,
|
|
389
|
-
minify,
|
|
390
|
-
metafile: true,
|
|
391
|
-
conditions: ["workerd", "worker", "browser"],
|
|
392
|
-
...(process.env.NODE_ENV && {
|
|
393
|
-
define: {
|
|
394
|
-
// use process.env["NODE_ENV" + ""] so that esbuild doesn't replace it
|
|
395
|
-
// when we do a build of wrangler. (re: https://github.com/cloudflare/workers-sdk/issues/1477)
|
|
396
|
-
"process.env.NODE_ENV": `"${process.env["NODE_ENV" + ""]}"`,
|
|
397
|
-
...(legacyNodeCompat ? { global: "globalThis" } : {}),
|
|
398
|
-
...(checkFetch ? { fetch: "checkedFetch" } : {}),
|
|
399
|
-
...options.define,
|
|
400
|
-
},
|
|
401
|
-
}),
|
|
402
|
-
loader: {
|
|
403
|
-
...COMMON_ESBUILD_OPTIONS.loader,
|
|
404
|
-
...(loader || {}),
|
|
405
|
-
},
|
|
406
|
-
plugins: [
|
|
407
|
-
moduleCollector.plugin,
|
|
408
|
-
...(legacyNodeCompat
|
|
409
|
-
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
|
|
410
|
-
: []),
|
|
411
|
-
...(nodejsCompat ? [nodejsCompatPlugin] : []),
|
|
412
|
-
...[cloudflareJsPlugin],
|
|
413
|
-
...(plugins || []),
|
|
414
|
-
],
|
|
415
|
-
...(jsxFactory && { jsxFactory }),
|
|
416
|
-
...(jsxFragment && { jsxFragment }),
|
|
417
|
-
...(tsconfig && { tsconfig }),
|
|
418
|
-
watch,
|
|
419
|
-
// The default logLevel is "warning". So that we can rewrite errors before
|
|
420
|
-
// logging, we disable esbuild's default logging, and log build failures
|
|
421
|
-
// ourselves.
|
|
422
|
-
logLevel: "silent",
|
|
423
|
-
};
|
|
424
|
-
let result;
|
|
425
|
-
try {
|
|
426
|
-
result = await esbuild.build(buildOptions);
|
|
427
|
-
} catch (e) {
|
|
428
|
-
if (!legacyNodeCompat && isBuildFailure(e))
|
|
429
|
-
rewriteNodeCompatBuildFailure(e, forPages);
|
|
430
|
-
throw e;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
const entryPointOutputs = Object.entries(result.metafile.outputs).filter(
|
|
434
|
-
([_path, output]) => output.entryPoint !== undefined
|
|
435
|
-
);
|
|
436
|
-
assert(
|
|
437
|
-
entryPointOutputs.length > 0,
|
|
438
|
-
`Cannot find entry-point "${entry.file}" in generated bundle.` +
|
|
439
|
-
listEntryPoints(entryPointOutputs)
|
|
440
|
-
);
|
|
441
|
-
assert(
|
|
442
|
-
entryPointOutputs.length < 2,
|
|
443
|
-
"More than one entry-point found for generated bundle." +
|
|
444
|
-
listEntryPoints(entryPointOutputs)
|
|
445
|
-
);
|
|
446
|
-
|
|
447
|
-
const { exports: entryPointExports, inputs: dependencies } =
|
|
448
|
-
entryPointOutputs[0][1];
|
|
449
|
-
const bundleType = entryPointExports.length > 0 ? "esm" : "commonjs";
|
|
450
|
-
|
|
451
|
-
const sourceMapPath = Object.keys(result.metafile.outputs).filter((_path) =>
|
|
452
|
-
_path.includes(".map")
|
|
453
|
-
)[0];
|
|
454
|
-
|
|
455
|
-
const resolvedEntryPointPath = path.resolve(
|
|
456
|
-
entry.directory,
|
|
457
|
-
entryPointOutputs[0][0]
|
|
458
|
-
);
|
|
459
|
-
|
|
460
|
-
// copy all referenced modules into the output bundle directory
|
|
461
|
-
for (const module of moduleCollector.modules) {
|
|
462
|
-
fs.writeFileSync(
|
|
463
|
-
path.join(path.dirname(resolvedEntryPointPath), module.name),
|
|
464
|
-
module.content
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
return {
|
|
469
|
-
modules: moduleCollector.modules,
|
|
470
|
-
dependencies,
|
|
471
|
-
resolvedEntryPointPath,
|
|
472
|
-
bundleType,
|
|
473
|
-
stop: result.stop,
|
|
474
|
-
sourceMapPath,
|
|
475
|
-
sourceMapMetadata: {
|
|
476
|
-
tmpDir: tmpDir.path,
|
|
477
|
-
entryDirectory: entry.directory,
|
|
478
|
-
},
|
|
479
|
-
};
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
/**
|
|
483
|
-
* A simple plugin to alias modules and mark them as external
|
|
484
|
-
*/
|
|
485
|
-
export function esbuildAliasExternalPlugin(
|
|
486
|
-
aliases: Record<string, string>
|
|
487
|
-
): esbuild.Plugin {
|
|
488
|
-
return {
|
|
489
|
-
name: "alias",
|
|
490
|
-
setup(build) {
|
|
491
|
-
build.onResolve({ filter: /.*/g }, (args) => {
|
|
492
|
-
// If it's the entrypoint, let it be as is
|
|
493
|
-
if (args.kind === "entry-point") {
|
|
494
|
-
return {
|
|
495
|
-
path: args.path,
|
|
496
|
-
};
|
|
497
|
-
}
|
|
498
|
-
// If it's not a recognised alias, then throw an error
|
|
499
|
-
if (!Object.keys(aliases).includes(args.path)) {
|
|
500
|
-
throw new Error("unrecognized module: " + args.path);
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
// Otherwise, return the alias
|
|
504
|
-
return {
|
|
505
|
-
path: aliases[args.path as keyof typeof aliases],
|
|
506
|
-
external: true,
|
|
507
|
-
};
|
|
508
|
-
});
|
|
509
|
-
},
|
|
510
|
-
};
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
/**
|
|
514
|
-
* A middleware that catches any thrown errors, and instead formats
|
|
515
|
-
* them to be rendered in a browser. This middleware is for demonstration
|
|
516
|
-
* purposes only, and is not intended to be used in production (or even dev!)
|
|
517
|
-
*/
|
|
518
|
-
async function applyFormatDevErrorsFacade(
|
|
519
|
-
entry: Entry,
|
|
520
|
-
tmpDirPath: string
|
|
521
|
-
): Promise<Entry> {
|
|
522
|
-
const targetPath = path.join(tmpDirPath, "format-dev-errors.entry.js");
|
|
523
|
-
await esbuild.build({
|
|
524
|
-
entryPoints: [
|
|
525
|
-
path.resolve(getBasePath(), "templates/format-dev-errors.ts"),
|
|
526
|
-
],
|
|
527
|
-
bundle: true,
|
|
528
|
-
sourcemap: true,
|
|
529
|
-
format: "esm",
|
|
530
|
-
plugins: [
|
|
531
|
-
esbuildAliasExternalPlugin({
|
|
532
|
-
__ENTRY_POINT__: entry.file,
|
|
533
|
-
}),
|
|
534
|
-
],
|
|
535
|
-
outfile: targetPath,
|
|
536
|
-
});
|
|
537
|
-
|
|
538
|
-
return {
|
|
539
|
-
...entry,
|
|
540
|
-
file: targetPath,
|
|
541
|
-
};
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
/**
|
|
545
|
-
* A facade that acts as a "middleware loader".
|
|
546
|
-
* Instead of needing to apply a facade for each individual middleware, this allows
|
|
547
|
-
* middleware to be written in a more traditional manner and then be applied all
|
|
548
|
-
* at once, requiring just two esbuild steps, rather than 1 per middleware.
|
|
549
|
-
*/
|
|
550
|
-
|
|
551
|
-
interface MiddlewareLoader {
|
|
552
|
-
path: string;
|
|
553
|
-
// By default all middleware will run on dev, but will not be run when published
|
|
554
|
-
publish?: boolean;
|
|
555
|
-
dev?: boolean;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
async function applyMiddlewareLoaderFacade(
|
|
559
|
-
entry: Entry,
|
|
560
|
-
tmpDirPath: string,
|
|
561
|
-
middleware: MiddlewareLoader[] // a list of paths to middleware files
|
|
562
|
-
): Promise<EntryWithInject> {
|
|
563
|
-
// Firstly we need to insert the middleware array into the project,
|
|
564
|
-
// and then we load the middleware - this insertion and loading is
|
|
565
|
-
// different for each format.
|
|
566
|
-
|
|
567
|
-
// Make sure we resolve all files relative to the actual temporary directory,
|
|
568
|
-
// otherwise we'll have issues with source maps
|
|
569
|
-
tmpDirPath = fs.realpathSync(tmpDirPath);
|
|
570
|
-
|
|
571
|
-
const targetPathInsertion = path.join(
|
|
572
|
-
tmpDirPath,
|
|
573
|
-
"middleware-insertion.entry.js"
|
|
574
|
-
);
|
|
575
|
-
|
|
576
|
-
// We need to import each of the middlewares, so we need to generate a
|
|
577
|
-
// random, unique identifier that we can use for the import.
|
|
578
|
-
// Middlewares are required to be default exports so we can import to any name.
|
|
579
|
-
const middlewareIdentifiers = middleware.map(
|
|
580
|
-
(_, index) => `__MIDDLEWARE_${index}__`
|
|
581
|
-
);
|
|
582
|
-
|
|
583
|
-
const dynamicFacadePath = path.join(
|
|
584
|
-
tmpDirPath,
|
|
585
|
-
"middleware-insertion-facade.js"
|
|
586
|
-
);
|
|
587
|
-
|
|
588
|
-
if (entry.format === "modules") {
|
|
589
|
-
// We use a facade to expose the required middleware alongside any user defined
|
|
590
|
-
// middleware on the worker object
|
|
591
|
-
|
|
592
|
-
const imports = middlewareIdentifiers
|
|
593
|
-
.map((m) => `import ${m} from "${m}";`)
|
|
594
|
-
.join("\n");
|
|
595
|
-
|
|
596
|
-
// write a file with all of the imports required
|
|
597
|
-
fs.writeFileSync(
|
|
598
|
-
dynamicFacadePath,
|
|
599
|
-
`import worker from "__ENTRY_POINT__";
|
|
600
|
-
${imports}
|
|
601
|
-
const facade = {
|
|
602
|
-
...worker,
|
|
603
|
-
middleware: [
|
|
604
|
-
${middlewareIdentifiers.join(",")}${middlewareIdentifiers.length > 0 ? "," : ""}
|
|
605
|
-
...(worker.middleware ? worker.middleware : []),
|
|
606
|
-
]
|
|
607
|
-
}
|
|
608
|
-
export * from "__ENTRY_POINT__";
|
|
609
|
-
export default facade;`
|
|
610
|
-
);
|
|
611
|
-
|
|
612
|
-
await esbuild.build({
|
|
613
|
-
entryPoints: [dynamicFacadePath],
|
|
614
|
-
bundle: true,
|
|
615
|
-
sourcemap: true,
|
|
616
|
-
format: "esm",
|
|
617
|
-
plugins: [
|
|
618
|
-
esbuildAliasExternalPlugin({
|
|
619
|
-
__ENTRY_POINT__: entry.file,
|
|
620
|
-
...Object.fromEntries(
|
|
621
|
-
middleware.map((val, index) => [
|
|
622
|
-
middlewareIdentifiers[index],
|
|
623
|
-
path.resolve(getBasePath(), val.path),
|
|
624
|
-
])
|
|
625
|
-
),
|
|
626
|
-
}),
|
|
627
|
-
],
|
|
628
|
-
outfile: targetPathInsertion,
|
|
629
|
-
});
|
|
630
|
-
const targetPathLoader = path.join(
|
|
631
|
-
tmpDirPath,
|
|
632
|
-
"middleware-loader.entry.js"
|
|
633
|
-
);
|
|
634
|
-
const loaderPath = path.resolve(
|
|
635
|
-
getBasePath(),
|
|
636
|
-
"templates/middleware/loader-modules.ts"
|
|
637
|
-
);
|
|
638
|
-
await esbuild.build({
|
|
639
|
-
entryPoints: [loaderPath],
|
|
640
|
-
bundle: true,
|
|
641
|
-
sourcemap: true,
|
|
642
|
-
format: "esm",
|
|
643
|
-
plugins: [
|
|
644
|
-
esbuildAliasExternalPlugin({
|
|
645
|
-
__ENTRY_POINT__: targetPathInsertion,
|
|
646
|
-
"./common": path.resolve(
|
|
647
|
-
getBasePath(),
|
|
648
|
-
"templates/middleware/common.ts"
|
|
649
|
-
),
|
|
650
|
-
}),
|
|
651
|
-
],
|
|
652
|
-
outfile: targetPathLoader,
|
|
653
|
-
});
|
|
654
|
-
return {
|
|
655
|
-
...entry,
|
|
656
|
-
file: targetPathLoader,
|
|
657
|
-
};
|
|
658
|
-
} else {
|
|
659
|
-
const imports = middlewareIdentifiers
|
|
660
|
-
.map((m) => `import ${m} from "${m}";`)
|
|
661
|
-
.join("\n");
|
|
662
|
-
const contents = `import { __facade_registerInternal__ } from "__LOADER__";
|
|
663
|
-
${imports}
|
|
664
|
-
__facade_registerInternal__([${middlewareIdentifiers.join(",")}]);`;
|
|
665
|
-
fs.writeFileSync(dynamicFacadePath, contents);
|
|
666
|
-
|
|
667
|
-
await esbuild.build({
|
|
668
|
-
entryPoints: [dynamicFacadePath],
|
|
669
|
-
bundle: true,
|
|
670
|
-
sourcemap: true,
|
|
671
|
-
format: "iife",
|
|
672
|
-
plugins: [
|
|
673
|
-
{
|
|
674
|
-
name: "dynamic-facade-imports",
|
|
675
|
-
setup(build) {
|
|
676
|
-
build.onResolve({ filter: /^__LOADER__$/ }, () => {
|
|
677
|
-
const loaderPath = path.resolve(
|
|
678
|
-
getBasePath(),
|
|
679
|
-
"templates/middleware/loader-sw.ts"
|
|
680
|
-
);
|
|
681
|
-
return { path: loaderPath };
|
|
682
|
-
});
|
|
683
|
-
const middlewareFilter = /^__MIDDLEWARE_(\d+)__$/;
|
|
684
|
-
build.onResolve({ filter: middlewareFilter }, (args) => {
|
|
685
|
-
const match = middlewareFilter.exec(args.path);
|
|
686
|
-
assert(match !== null);
|
|
687
|
-
const middlewareIndex = parseInt(match[1]);
|
|
688
|
-
return {
|
|
689
|
-
path: path.resolve(
|
|
690
|
-
getBasePath(),
|
|
691
|
-
middleware[middlewareIndex].path
|
|
692
|
-
),
|
|
693
|
-
};
|
|
694
|
-
});
|
|
695
|
-
},
|
|
696
|
-
},
|
|
697
|
-
],
|
|
698
|
-
outfile: targetPathInsertion,
|
|
699
|
-
});
|
|
700
|
-
return {
|
|
701
|
-
...entry,
|
|
702
|
-
inject: [targetPathInsertion],
|
|
703
|
-
};
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* A middleware that serves static assets from a worker.
|
|
709
|
-
* This powers --assets / config.assets
|
|
710
|
-
*/
|
|
711
|
-
|
|
712
|
-
async function applyStaticAssetFacade(
|
|
713
|
-
entry: Entry,
|
|
714
|
-
tmpDirPath: string,
|
|
715
|
-
assets: StaticAssetsConfig
|
|
716
|
-
): Promise<Entry> {
|
|
717
|
-
const targetPath = path.join(tmpDirPath, "serve-static-assets.entry.js");
|
|
718
|
-
|
|
719
|
-
await esbuild.build({
|
|
720
|
-
entryPoints: [
|
|
721
|
-
path.resolve(getBasePath(), "templates/serve-static-assets.ts"),
|
|
722
|
-
],
|
|
723
|
-
bundle: true,
|
|
724
|
-
format: "esm",
|
|
725
|
-
sourcemap: true,
|
|
726
|
-
plugins: [
|
|
727
|
-
esbuildAliasExternalPlugin({
|
|
728
|
-
__ENTRY_POINT__: entry.file,
|
|
729
|
-
__KV_ASSET_HANDLER__: path.join(getBasePath(), "kv-asset-handler.js"),
|
|
730
|
-
__STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
|
|
731
|
-
}),
|
|
732
|
-
],
|
|
733
|
-
define: {
|
|
734
|
-
__CACHE_CONTROL_OPTIONS__: JSON.stringify(
|
|
735
|
-
typeof assets === "object"
|
|
736
|
-
? {
|
|
737
|
-
browserTTL:
|
|
738
|
-
assets.browser_TTL || 172800 /* 2 days: 2* 60 * 60 * 24 */,
|
|
739
|
-
bypassCache: assets.bypassCache,
|
|
740
|
-
}
|
|
741
|
-
: {}
|
|
742
|
-
),
|
|
743
|
-
__SERVE_SINGLE_PAGE_APP__: JSON.stringify(
|
|
744
|
-
typeof assets === "object" ? assets.serve_single_page_app : false
|
|
745
|
-
),
|
|
746
|
-
},
|
|
747
|
-
outfile: targetPath,
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
return {
|
|
751
|
-
...entry,
|
|
752
|
-
file: targetPath,
|
|
753
|
-
};
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
/**
|
|
757
|
-
* A middleware that enables service bindings to be used in dev,
|
|
758
|
-
* binding to other love wrangler dev instances
|
|
759
|
-
*/
|
|
760
|
-
|
|
761
|
-
async function applyMultiWorkerDevFacade(
|
|
762
|
-
entry: Entry,
|
|
763
|
-
tmpDirPath: string,
|
|
764
|
-
services: Config["services"],
|
|
765
|
-
workerDefinitions: WorkerRegistry
|
|
766
|
-
) {
|
|
767
|
-
const targetPath = path.join(tmpDirPath, "multiworker-dev-facade.entry.js");
|
|
768
|
-
const serviceMap = Object.fromEntries(
|
|
769
|
-
(services || []).map((serviceBinding) => [
|
|
770
|
-
serviceBinding.binding,
|
|
771
|
-
workerDefinitions[serviceBinding.service] || null,
|
|
772
|
-
])
|
|
773
|
-
);
|
|
774
|
-
|
|
775
|
-
await esbuild.build({
|
|
776
|
-
entryPoints: [
|
|
777
|
-
path.join(
|
|
778
|
-
getBasePath(),
|
|
779
|
-
entry.format === "modules"
|
|
780
|
-
? "templates/service-bindings-module-facade.js"
|
|
781
|
-
: "templates/service-bindings-sw-facade.js"
|
|
782
|
-
),
|
|
783
|
-
],
|
|
784
|
-
bundle: true,
|
|
785
|
-
sourcemap: true,
|
|
786
|
-
format: "esm",
|
|
787
|
-
plugins: [
|
|
788
|
-
esbuildAliasExternalPlugin({
|
|
789
|
-
__ENTRY_POINT__: entry.file,
|
|
790
|
-
}),
|
|
791
|
-
],
|
|
792
|
-
define: {
|
|
793
|
-
__WORKERS__: JSON.stringify(serviceMap),
|
|
794
|
-
},
|
|
795
|
-
outfile: targetPath,
|
|
796
|
-
});
|
|
797
|
-
|
|
798
|
-
return {
|
|
799
|
-
...entry,
|
|
800
|
-
file: targetPath,
|
|
801
|
-
};
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
/**
|
|
805
|
-
* A middleware that makes first party workers "work" in
|
|
806
|
-
* our dev environments. Is applied during wrangler dev
|
|
807
|
-
* when config.first_party_worker is true
|
|
808
|
-
*/
|
|
809
|
-
async function applyFirstPartyWorkerDevFacade(
|
|
810
|
-
entry: Entry,
|
|
811
|
-
tmpDirPath: string
|
|
812
|
-
) {
|
|
813
|
-
if (entry.format !== "modules") {
|
|
814
|
-
throw new Error(
|
|
815
|
-
"First party workers must be in the modules format. See https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
|
|
816
|
-
);
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
const targetPath = path.join(
|
|
820
|
-
tmpDirPath,
|
|
821
|
-
"first-party-worker-module-facade.entry.js"
|
|
822
|
-
);
|
|
823
|
-
|
|
824
|
-
await esbuild.build({
|
|
825
|
-
entryPoints: [
|
|
826
|
-
path.resolve(
|
|
827
|
-
getBasePath(),
|
|
828
|
-
"templates/first-party-worker-module-facade.ts"
|
|
829
|
-
),
|
|
830
|
-
],
|
|
831
|
-
bundle: true,
|
|
832
|
-
format: "esm",
|
|
833
|
-
sourcemap: true,
|
|
834
|
-
plugins: [
|
|
835
|
-
esbuildAliasExternalPlugin({
|
|
836
|
-
__ENTRY_POINT__: entry.file,
|
|
837
|
-
}),
|
|
838
|
-
],
|
|
839
|
-
outfile: targetPath,
|
|
840
|
-
});
|
|
841
|
-
|
|
842
|
-
return {
|
|
843
|
-
...entry,
|
|
844
|
-
file: targetPath,
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
/**
|
|
849
|
-
* A middleware that injects the beta D1 API in JS.
|
|
850
|
-
*
|
|
851
|
-
* This code be removed from here when the API is in Workers core,
|
|
852
|
-
* but moved inside Miniflare for simulating D1.
|
|
853
|
-
*/
|
|
854
|
-
async function applyD1BetaFacade(
|
|
855
|
-
entry: Entry,
|
|
856
|
-
tmpDirPath: string,
|
|
857
|
-
betaD1Shims: string[],
|
|
858
|
-
miniflare2: boolean,
|
|
859
|
-
doBindings: DurableObjectBindings
|
|
860
|
-
): Promise<Entry> {
|
|
861
|
-
let entrypointPath = path.resolve(
|
|
862
|
-
getBasePath(),
|
|
863
|
-
"templates/d1-beta-facade.js"
|
|
864
|
-
);
|
|
865
|
-
if (Array.isArray(doBindings) && doBindings.length > 0) {
|
|
866
|
-
//we have DO bindings, so we need to shim them
|
|
867
|
-
const maskedDoBindings = doBindings
|
|
868
|
-
// Don't shim anything not local to this worker
|
|
869
|
-
.filter((b) => !b.script_name)
|
|
870
|
-
// Reexport the DO classnames
|
|
871
|
-
.map(
|
|
872
|
-
(b) =>
|
|
873
|
-
`export const ${b.class_name} = maskDurableObjectDefinition(OTHER_EXPORTS.${b.class_name});`
|
|
874
|
-
)
|
|
875
|
-
.join("\n");
|
|
876
|
-
const baseFile = fs.readFileSync(
|
|
877
|
-
path.resolve(getBasePath(), "templates/d1-beta-facade.js"),
|
|
878
|
-
"utf8"
|
|
879
|
-
);
|
|
880
|
-
//getMaskedEnv is already used to shim regular Workers
|
|
881
|
-
const contents = `
|
|
882
|
-
${baseFile}
|
|
883
|
-
|
|
884
|
-
var maskDurableObjectDefinition = (cls) =>
|
|
885
|
-
class extends cls {
|
|
886
|
-
constructor(state, env) {
|
|
887
|
-
super(state, getMaskedEnv(env));
|
|
888
|
-
}
|
|
889
|
-
};
|
|
890
|
-
${maskedDoBindings}`;
|
|
891
|
-
const doD1FacadePath = path.join(tmpDirPath, "d1-do-facade.js");
|
|
892
|
-
//write our shim so we can build it
|
|
893
|
-
fs.writeFileSync(doD1FacadePath, contents);
|
|
894
|
-
entrypointPath = doD1FacadePath;
|
|
895
|
-
}
|
|
896
|
-
const targetPath = path.join(tmpDirPath, "d1-beta-facade.entry.js");
|
|
897
|
-
await esbuild.build({
|
|
898
|
-
entryPoints: [entrypointPath],
|
|
899
|
-
bundle: true,
|
|
900
|
-
format: "esm",
|
|
901
|
-
sourcemap: true,
|
|
902
|
-
plugins: [
|
|
903
|
-
esbuildAliasExternalPlugin({
|
|
904
|
-
__ENTRY_POINT__: entry.file,
|
|
905
|
-
}),
|
|
906
|
-
],
|
|
907
|
-
define: {
|
|
908
|
-
__D1_IMPORTS__: JSON.stringify(betaD1Shims),
|
|
909
|
-
__LOCAL_MODE__: JSON.stringify(miniflare2),
|
|
910
|
-
},
|
|
911
|
-
outfile: targetPath,
|
|
912
|
-
});
|
|
913
|
-
|
|
914
|
-
return {
|
|
915
|
-
...entry,
|
|
916
|
-
file: targetPath,
|
|
917
|
-
};
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
/**
|
|
921
|
-
* Generate a string that describes the entry-points that were identified by esbuild.
|
|
922
|
-
*/
|
|
923
|
-
function listEntryPoints(
|
|
924
|
-
outputs: [string, ValueOf<esbuild.Metafile["outputs"]>][]
|
|
925
|
-
): string {
|
|
926
|
-
return outputs.map(([_input, output]) => output.entryPoint).join("\n");
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
type ValueOf<T> = T[keyof T];
|