Skip to content

Commit d8db7da

Browse files
committed
fixup! restrict context
1 parent 34dc093 commit d8db7da

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { build, Plugin } from "esbuild";
1010
import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js";
1111
import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js";
1212
import * as patches from "./patches/index.js";
13+
import { patchLoadInstrumentation } from "./patches/plugins/load-instrumentation.js";
1314
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
1415
import { fixRequire } from "./patches/plugins/require.js";
1516
import { inlineRequirePagePlugin } from "./patches/plugins/require-page.js";
@@ -82,6 +83,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
8283
setWranglerExternal(),
8384
fixRequire(),
8485
handleOptionalDependencies(optionalDependencies),
86+
patchLoadInstrumentation(),
8587
],
8688
external: ["./middleware/handler.mjs"],
8789
alias: {
@@ -192,7 +194,6 @@ export async function updateWorkerBundledCode(
192194
(code) => patches.inlineMiddlewareManifestRequire(code, buildOpts),
193195
],
194196
["exception bubbling", patches.patchExceptionBubbling],
195-
["`loadInstrumentationModule` function", patches.patchLoadInstrumentationModule],
196197
[
197198
"`patchAsyncStorage` call",
198199
(code) =>

packages/cloudflare/src/cli/build/patches/ast/webpack-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function buildInlineChunksRule(chunks: number[]) {
2727
return `
2828
rule:
2929
pattern: ($CHUNK_ID, $_PROMISES) => { $$$ }
30+
inside: {pattern: $_.$_.require = $$$_, stopBy: end}
3031
all:
3132
- has: {pattern: $INSTALL(require("./chunks/" + $$$)), stopBy: end}
3233
- has: {pattern: $SELF_ID != $CHUNK_ID, stopBy: end}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { readFile } from "node:fs/promises";
2+
3+
import type { PluginBuild } from "esbuild";
4+
5+
import { patchCode } from "../ast/util.js";
6+
7+
export const instrumentationRule = `
8+
rule:
9+
kind: method_definition
10+
all:
11+
- has: {field: name, regex: ^loadInstrumentationModule$}
12+
- has: {pattern: dynamicRequire, stopBy: end}
13+
14+
fix: async loadInstrumentation() { }
15+
`;
16+
17+
export function patchLoadInstrumentation() {
18+
console.error("#############################");
19+
return {
20+
name: "patch-load-instrumentation",
21+
22+
setup: async (build: PluginBuild) => {
23+
build.onLoad({ filter: /.*/ }, async ({ path }) => {
24+
const code = await readFile(path, "utf-8");
25+
console.error({ path });
26+
if (/async loadInstrumentationModule\(/.test(code)) {
27+
console.error({ path });
28+
return { contents: patchCode(code, instrumentationRule) };
29+
}
30+
});
31+
},
32+
};
33+
}

packages/cloudflare/src/cli/build/patches/to-investigate/patch-load-instrumentation-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function patchLoadInstrumentationModule(code: string) {
3232
});
3333

3434
loadInstrumentationModuleDeclarations.forEach((loadInstrumentationModuleDeclaration) => {
35-
loadInstrumentationModuleDeclaration.setBodyText("");
35+
loadInstrumentationModuleDeclaration.setBodyText("/** emptied */");
3636
});
3737
return file.print();
3838
}

0 commit comments

Comments
 (0)