Skip to content

Commit 63a6504

Browse files
ItsWendellvicbemily-shen
authored
refactor(wrangler): add metafile flag, pass to build functions, adjus… (#8716)
* refactor(wrangler): generate bundle meta file * fixup! add test * refactor(wrangler): add metafile flag, pass to build functions, adjust tests --------- Co-authored-by: Victor Berchet <[email protected]> Co-authored-by: emily-shen <[email protected]>
1 parent 37af035 commit 63a6504

File tree

13 files changed

+114
-3
lines changed

13 files changed

+114
-3
lines changed

.changeset/soft-bears-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
add --metafile flag to generate esbuild metadata file during build

packages/wrangler/src/__tests__/deploy.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12646,6 +12646,34 @@ export default{
1264612646
});
1264712647
});
1264812648

12649+
describe("--metafile", () => {
12650+
it("should output a metafile when --metafile is set", async () => {
12651+
writeWranglerConfig();
12652+
writeWorkerSource();
12653+
await runWrangler("deploy index.js --metafile --dry-run --outdir=dist");
12654+
12655+
// Check if file exists
12656+
const metafilePath = path.join(process.cwd(), "dist", "bundle-meta.json");
12657+
expect(fs.existsSync(metafilePath)).toBe(true);
12658+
const metafile = JSON.parse(fs.readFileSync(metafilePath, "utf8"));
12659+
expect(metafile.inputs).toBeDefined();
12660+
expect(metafile.outputs).toBeDefined();
12661+
});
12662+
12663+
it("should output a metafile when --metafile=./meta.json is set", async () => {
12664+
writeWranglerConfig();
12665+
writeWorkerSource();
12666+
await runWrangler("deploy index.js --metafile=./meta.json --dry-run");
12667+
12668+
// Check if file exists
12669+
const metafilePath = path.join(process.cwd(), "meta.json");
12670+
expect(fs.existsSync(metafilePath)).toBe(true);
12671+
const metafile = JSON.parse(fs.readFileSync(metafilePath, "utf8"));
12672+
expect(metafile.inputs).toBeDefined();
12673+
expect(metafile.outputs).toBeDefined();
12674+
});
12675+
});
12676+
1264912677
describe("--dispatch-namespace", () => {
1265012678
it("should upload to dispatch namespace", async () => {
1265112679
writeWranglerConfig();

packages/wrangler/src/__tests__/pages/functions-build.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ describe("pages functions build", () => {
168168
`);
169169
});
170170

171+
it("should output a metafile when --metafile is set", async () => {
172+
// Setup a basic pages function
173+
mkdirSync("functions");
174+
writeFileSync(
175+
"functions/hello.js",
176+
`export function onRequest() { return new Response("Hello from Pages Functions"); }`
177+
);
178+
179+
// Run the build command
180+
await runWrangler(`pages functions build --outdir=dist --metafile`);
181+
182+
// Check if file exists
183+
expect(existsSync("dist/bundle-meta.json")).toBe(true);
184+
185+
// Structure checks for the metafile
186+
const meta = JSON.parse(
187+
readFileSync("dist/bundle-meta.json", { encoding: "utf8" })
188+
);
189+
190+
expect(meta.inputs).toBeDefined();
191+
expect(meta.outputs).toBeDefined();
192+
});
193+
171194
it("should build _worker.js", async () => {
172195
/* ---------------------------- */
173196
/* Set up js files */

packages/wrangler/src/api/startDevWorker/BundlerController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
149149

150150
// sourcemap defaults to true in dev
151151
sourcemap: undefined,
152+
153+
metafile: undefined,
152154
});
153155
if (buildAborter.signal.aborted) {
154156
return;

packages/wrangler/src/deploy/deploy.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type Props = {
111111
projectRoot: string | undefined;
112112
dispatchNamespace: string | undefined;
113113
experimentalAutoCreate: boolean;
114+
metafile: string | boolean | undefined;
114115
};
115116

116117
export type RouteObject = ZoneIdRoute | ZoneNameRoute | CustomDomainRoute;
@@ -550,6 +551,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
550551
props.entry,
551552
typeof destination === "string" ? destination : destination.path,
552553
{
554+
metafile: props.metafile,
553555
bundle: true,
554556
additionalModules: [],
555557
moduleCollector,

packages/wrangler/src/deploy/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ export const deployCommand = createCommand({
168168
describe: "Don't actually deploy",
169169
type: "boolean",
170170
},
171+
metafile: {
172+
describe:
173+
"Path to output build metadata from esbuild. If flag is used without a path, defaults to 'bundle-meta.json' inside the directory specified by --outdir.",
174+
type: "string",
175+
coerce: (v: string) => (!v ? true : v),
176+
},
171177
"keep-vars": {
172178
describe:
173179
"Stop Wrangler from deleting vars that are not present in the Wrangler configuration file\nBy default Wrangler will remove all vars and replace them with those found in the Wrangler configuration.\nIf your development approach is to modify vars after deployment via the dashboard you may wish to set this flag.",
@@ -310,6 +316,7 @@ export const deployCommand = createCommand({
310316
outDir: args.outdir,
311317
outFile: args.outfile,
312318
dryRun: args.dryRun,
319+
metafile: args.metafile,
313320
noBundle: !(args.bundle ?? !config.no_bundle),
314321
keepVars: args.keepVars,
315322
logpush: args.logpush,

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export type BundleOptions = {
135135
projectRoot: string | undefined;
136136
defineNavigatorUserAgent: boolean;
137137
external: string[] | undefined;
138+
metafile: string | boolean | undefined;
138139
};
139140

140141
/**
@@ -170,6 +171,7 @@ export async function bundleWorker(
170171
projectRoot,
171172
defineNavigatorUserAgent,
172173
external,
174+
metafile,
173175
}: BundleOptions
174176
): Promise<BundleResult> {
175177
// We create a temporary directory for any one-off files we
@@ -354,16 +356,18 @@ export async function bundleWorker(
354356
entryPoints: [entry.file],
355357
bundle,
356358
absWorkingDir: entry.projectRoot,
357-
outdir: destination,
358359
keepNames,
359-
entryNames: entryName || path.parse(entryFile).name,
360360
...(isOutfile
361361
? {
362362
outdir: undefined,
363363
outfile: destination,
364364
entryNames: undefined,
365365
}
366-
: {}),
366+
: {
367+
outdir: destination,
368+
outfile: undefined,
369+
entryNames: entryName || path.parse(entryFile).name,
370+
}),
367371
inject,
368372
external: bundle
369373
? ["__STATIC_CONTENT_MANIFEST", ...(external ? external : [])]
@@ -438,6 +442,23 @@ export async function bundleWorker(
438442
};
439443
} else {
440444
result = await esbuild.build(buildOptions);
445+
446+
// Write the bundle metafile to disk.
447+
if (metafile && result.metafile) {
448+
let metaFilePath: string;
449+
450+
if (typeof metafile === "string") {
451+
metaFilePath = path.resolve(metafile);
452+
} else if (isOutfile) {
453+
metaFilePath = `${destination}.bundle-meta.json`;
454+
} else {
455+
metaFilePath = path.join(destination, "bundle-meta.json");
456+
}
457+
458+
const metaJson = JSON.stringify(result.metafile, null, 2);
459+
fs.writeFileSync(metaFilePath, metaJson);
460+
}
461+
441462
// Even when we're not watching, we still want some way of cleaning up the
442463
// temporary directory when we don't need it anymore
443464
stop = async function () {

packages/wrangler/src/dev/use-esbuild.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ export function runBuild(
172172
// sourcemap defaults to true in dev
173173
sourcemap: undefined,
174174
checkFetch,
175+
176+
metafile: undefined,
175177
})
176178
: undefined;
177179

packages/wrangler/src/pages/build.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ export const pagesFunctionsBuildCommand = createCommand({
136136
type: "string",
137137
array: true,
138138
},
139+
metafile: {
140+
describe:
141+
"Path to output build metadata from esbuild. If flag is used without a path, defaults to 'bundle-meta.json' inside the directory specified by --outdir.",
142+
type: "string",
143+
coerce: (v: string) => (!v ? true : v),
144+
},
139145
},
140146
positionalArgs: ["directory"],
141147
async handler(args) {
@@ -159,6 +165,7 @@ export const pagesFunctionsBuildCommand = createCommand({
159165
defineNavigatorUserAgent,
160166
checkFetch,
161167
external,
168+
metafile,
162169
} = validatedArgs;
163170

164171
try {
@@ -186,6 +193,7 @@ export const pagesFunctionsBuildCommand = createCommand({
186193
defineNavigatorUserAgent,
187194
checkFetch,
188195
external,
196+
metafile,
189197
});
190198
} catch (e) {
191199
if (e instanceof FunctionsNoRoutesError) {
@@ -228,6 +236,7 @@ export const pagesFunctionsBuildCommand = createCommand({
228236
defineNavigatorUserAgent,
229237
checkFetch,
230238
external,
239+
metafile,
231240
} = validatedArgs;
232241

233242
/**
@@ -289,6 +298,7 @@ export const pagesFunctionsBuildCommand = createCommand({
289298
defineNavigatorUserAgent,
290299
checkFetch,
291300
external,
301+
metafile,
292302
});
293303
} catch (e) {
294304
if (e instanceof FunctionsNoRoutesError) {

packages/wrangler/src/pages/buildFunctions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export async function buildFunctions({
4141
defineNavigatorUserAgent,
4242
checkFetch,
4343
external,
44+
metafile,
4445
}: Partial<
4546
Pick<
4647
typeof pagesFunctionsBuildCommand.args,
@@ -66,6 +67,7 @@ export async function buildFunctions({
6667
routesModule?: string;
6768
defineNavigatorUserAgent: boolean;
6869
checkFetch: boolean;
70+
metafile?: string | boolean;
6971
}) {
7072
RUNNING_BUILDERS.forEach(
7173
(runningBuilder) => runningBuilder.stop && runningBuilder.stop()
@@ -143,6 +145,7 @@ export async function buildFunctions({
143145
defineNavigatorUserAgent,
144146
checkFetch,
145147
external,
148+
metafile,
146149
});
147150
}
148151

0 commit comments

Comments
 (0)