Skip to content

Commit 0cfcfe0

Browse files
feat: add config.keep_names option (#8771)
--------- Co-authored-by: Somhairle MacLeòid <[email protected]>
1 parent d16f1c6 commit 0cfcfe0

File tree

14 files changed

+136
-1
lines changed

14 files changed

+136
-1
lines changed

.changeset/long-mugs-pull.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: add `config.keep_names` option
6+
7+
Adds a new option to Wrangler to allow developers to opt out of esbuild's `keep_names` option (https://esbuild.github.io/api/#keep-names). By default, Wrangler sets this to `true`
8+
9+
This is something developers should not usually need to care about, but sometimes
10+
`keep_names` can create issues, and in such cases they will be now able to opt-out.
11+
12+
Example `wrangler.jsonc`:
13+
14+
```json
15+
{
16+
"name": "my-worker",
17+
"main": "src/worker.ts",
18+
"keep_names": false
19+
}
20+
```

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6571,6 +6571,84 @@ addEventListener('fetch', event => {});`
65716571
`);
65726572
expect(std.err).toMatchInlineSnapshot(`""`);
65736573
});
6574+
6575+
it("should apply esbuild's keep-names functionality by default", async () => {
6576+
writeWranglerConfig({
6577+
main: "./index.js",
6578+
legacy_env: false,
6579+
env: {
6580+
testEnv: {},
6581+
},
6582+
});
6583+
fs.writeFileSync(
6584+
"./index.js",
6585+
`
6586+
export
6587+
default {
6588+
fetch() {
6589+
function sayHello() {
6590+
return "Hello World with keep_names";
6591+
}
6592+
return new Response(sayHello());
6593+
}
6594+
}
6595+
`
6596+
);
6597+
6598+
const underscoreUnderscoreNameRegex = /__name\(.*?\)/;
6599+
6600+
mockUploadWorkerRequest({
6601+
env: "testEnv",
6602+
expectedType: "esm",
6603+
legacyEnv: false,
6604+
expectedEntry: (str) => {
6605+
expect(str).toMatch(underscoreUnderscoreNameRegex);
6606+
},
6607+
});
6608+
6609+
mockSubDomainRequest();
6610+
await runWrangler("deploy -e testEnv index.js");
6611+
});
6612+
6613+
it("should apply esbuild's keep-names functionality unless keep_names is set to false", async () => {
6614+
writeWranglerConfig({
6615+
main: "./index.js",
6616+
legacy_env: false,
6617+
env: {
6618+
testEnv: {
6619+
keep_names: false,
6620+
},
6621+
},
6622+
});
6623+
fs.writeFileSync(
6624+
"./index.js",
6625+
`
6626+
export
6627+
default {
6628+
fetch() {
6629+
function sayHello() {
6630+
return "Hello World without keep_names";
6631+
}
6632+
return new Response(sayHello());
6633+
}
6634+
}
6635+
`
6636+
);
6637+
6638+
const underscoreUnderscoreNameRegex = /__name\(.*?\)/;
6639+
6640+
mockUploadWorkerRequest({
6641+
env: "testEnv",
6642+
expectedType: "esm",
6643+
legacyEnv: false,
6644+
expectedEntry: (str) => {
6645+
expect(str).not.toMatch(underscoreUnderscoreNameRegex);
6646+
},
6647+
});
6648+
6649+
mockSubDomainRequest();
6650+
await runWrangler("deploy -e testEnv index.js");
6651+
});
65746652
});
65756653

65766654
describe("durable object migrations", () => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
117117
jsxFragment: config.build.jsxFactory,
118118
tsconfig: config.build.tsconfig,
119119
minify: config.build.minify,
120+
keepNames: config.build.keepNames ?? true,
120121
nodejsCompatMode: config.build.nodejsCompatMode,
121122
define: config.build.define,
122123
checkFetch: shouldCheckFetch(
@@ -246,6 +247,7 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
246247
rules: config.build.moduleRules,
247248
tsconfig: config.build?.tsconfig,
248249
minify: config.build?.minify,
250+
keepNames: config.build?.keepNames ?? true,
249251
nodejsCompatMode: config.build.nodejsCompatMode,
250252
define: config.build.define,
251253
alias: config.build.alias,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ async function resolveConfig(
289289
moduleRules: input.build?.moduleRules ?? getRules(config),
290290

291291
minify: input.build?.minify ?? config.minify,
292+
keepNames: input.build?.keepNames ?? config.keep_names,
292293
define: { ...config.define, ...input.build?.define },
293294
custom: {
294295
command: input.build?.custom?.command ?? config.build?.command,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ export interface StartDevWorkerInput {
108108
alias?: Record<string, string>;
109109
/** Whether the bundled worker is minified. Only takes effect if bundle: true. */
110110
minify?: boolean;
111+
/** Whether to keep function names after JavaScript transpilations. */
112+
keepNames?: boolean;
111113
/** Options controlling a custom build step. */
112114
custom?: {
113115
/** Custom shell command to run before bundling. Runs even if bundle. */

packages/wrangler/src/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ export const defaultWranglerConfig: Config = {
332332
build: { command: undefined, watch_dir: "./src", cwd: undefined },
333333
no_bundle: undefined,
334334
minify: undefined,
335+
keep_names: undefined,
335336
dispatch_namespaces: [],
336337
first_party_worker: undefined,
337338
logfwdr: { bindings: [] },

packages/wrangler/src/config/environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ interface EnvironmentInheritable {
306306
*/
307307
minify: boolean | undefined;
308308

309+
/**
310+
* Keep function names after javascript transpilations.
311+
*
312+
* @default {true}
313+
* @inheritable
314+
*/
315+
keep_names: boolean | undefined;
316+
309317
/**
310318
* Designates this Worker as an internal-only "first-party" Worker.
311319
*

packages/wrangler/src/config/validation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,14 @@ function normalizeAndValidateEnvironment(
14171417
isBoolean,
14181418
undefined
14191419
),
1420+
keep_names: inheritable(
1421+
diagnostics,
1422+
topLevelEnv,
1423+
rawEnv,
1424+
"keep_names",
1425+
isBoolean,
1426+
undefined
1427+
),
14201428
first_party_worker: inheritable(
14211429
diagnostics,
14221430
topLevelEnv,

packages/wrangler/src/deploy/deploy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
558558
jsxFragment,
559559
tsconfig: props.tsconfig ?? config.tsconfig,
560560
minify,
561+
keepNames: config.keep_names ?? true,
561562
sourcemap: uploadSourceMaps,
562563
nodejsCompatMode,
563564
define: { ...config.define, ...props.defines },

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export type BundleOptions = {
120120
watch: boolean | undefined;
121121
tsconfig: string | undefined;
122122
minify: boolean | undefined;
123+
keepNames: boolean;
123124
nodejsCompatMode: NodeJSCompatMode | undefined;
124125
define: Config["define"];
125126
alias: Config["alias"];
@@ -154,6 +155,7 @@ export async function bundleWorker(
154155
watch,
155156
tsconfig,
156157
minify,
158+
keepNames,
157159
nodejsCompatMode,
158160
alias,
159161
define,
@@ -353,7 +355,7 @@ export async function bundleWorker(
353355
bundle,
354356
absWorkingDir: entry.projectRoot,
355357
outdir: destination,
356-
keepNames: true,
358+
keepNames,
357359
entryNames: entryName || path.parse(entryFile).name,
358360
...(isOutfile
359361
? {

0 commit comments

Comments
 (0)