Skip to content

Commit 62ca405

Browse files
dario-piotrowiczpenalosa
authored andcommitted
feat: add config.keep_names option (#8771)
--------- Co-authored-by: Somhairle MacLeòid <[email protected]>
1 parent 8ac1436 commit 62ca405

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

65786656
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
@@ -329,6 +329,14 @@ interface EnvironmentInheritable {
329329
*/
330330
minify: boolean | undefined;
331331

332+
/**
333+
* Keep function names after javascript transpilations.
334+
*
335+
* @default {true}
336+
* @inheritable
337+
*/
338+
keep_names: boolean | undefined;
339+
332340
/**
333341
* Designates this Worker as an internal-only "first-party" Worker.
334342
*

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
@@ -559,6 +559,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
559559
jsxFragment,
560560
tsconfig: props.tsconfig ?? config.tsconfig,
561561
minify,
562+
keepNames: config.keep_names ?? true,
562563
sourcemap: uploadSourceMaps,
563564
nodejsCompatMode,
564565
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)