Skip to content

Commit 2026cce

Browse files
authored
Add standard URL polyfill (#5478)
1 parent 5212154 commit 2026cce

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/wrangler/src/deployment-bundle/bundle.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { getEntryPointFromMetafile } from "./entry-point-from-metafile";
1515
import { cloudflareInternalPlugin } from "./esbuild-plugins/cloudflare-internal";
1616
import { configProviderPlugin } from "./esbuild-plugins/config-provider";
1717
import { nodejsCompatPlugin } from "./esbuild-plugins/nodejs-compat";
18+
import { standardURLPlugin } from "./esbuild-plugins/standard-url";
1819
import { writeAdditionalModules } from "./find-additional-modules";
1920
import { noopModuleCollector } from "./module-collection";
2021
import type { Config } from "../config";
@@ -342,7 +343,11 @@ export async function bundleWorker(
342343
plugins: [
343344
moduleCollector.plugin,
344345
...(legacyNodeCompat
345-
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
346+
? [
347+
NodeGlobalsPolyfills({ buffer: true }),
348+
standardURLPlugin(),
349+
NodeModulesPolyfills(),
350+
]
346351
: []),
347352
nodejsCompatPlugin(!!nodejsCompat),
348353
cloudflareInternalPlugin,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { Plugin } from "esbuild";
2+
3+
/**
4+
* An esbuild plugin that will export URL from the url polyfill
5+
*/
6+
export const standardURLPlugin: () => Plugin = () => ({
7+
name: "standard URL plugin",
8+
setup(pluginBuild) {
9+
pluginBuild.onResolve({ filter: /^url$/ }, ({ importer }) => {
10+
if (importer === "standard-url-plugin") return;
11+
return {
12+
path: "wrangler-url-polyfill",
13+
namespace: "wrangler-url",
14+
};
15+
});
16+
pluginBuild.onResolve(
17+
{ filter: /^wrangler:url$/ },
18+
async ({ kind, resolveDir }) => {
19+
const result = await pluginBuild.resolve("url", {
20+
kind,
21+
resolveDir,
22+
importer: "standard-url-plugin",
23+
});
24+
25+
return result;
26+
}
27+
);
28+
pluginBuild.onLoad(
29+
{ filter: /^wrangler-url-polyfill$/, namespace: "wrangler-url" },
30+
() => {
31+
return {
32+
loader: "js",
33+
contents: `export * from "wrangler:url"; export const URL = globalThis.URL`,
34+
};
35+
}
36+
);
37+
},
38+
});

0 commit comments

Comments
 (0)