Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/long-mugs-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"wrangler": minor
---

feat: add `config.keep_names` option

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`

This is something developers should not usually need to care about, but sometimes
`keep_names` can create issues, and in such cases they will be now able to opt-out.

Example `wrangler.jsonc`:

```json
{
"name": "my-worker",
"main": "src/worker.ts",
"keep_names": false
}
```
78 changes: 78 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6571,6 +6571,84 @@ addEventListener('fetch', event => {});`
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should apply esbuild's keep-names functionality by default", async () => {
writeWranglerConfig({
main: "./index.js",
legacy_env: false,
env: {
testEnv: {},
},
});
fs.writeFileSync(
"./index.js",
`
export
default {
fetch() {
function sayHello() {
return "Hello World with keep_names";
}
return new Response(sayHello());
}
}
`
);

const underscoreUnderscoreNameRegex = /__name\(.*?\)/;

mockUploadWorkerRequest({
env: "testEnv",
expectedType: "esm",
legacyEnv: false,
expectedEntry: (str) => {
expect(str).toMatch(underscoreUnderscoreNameRegex);
},
});

mockSubDomainRequest();
await runWrangler("deploy -e testEnv index.js");
});

it("should apply esbuild's keep-names functionality unless keep_names is set to false", async () => {
writeWranglerConfig({
main: "./index.js",
legacy_env: false,
env: {
testEnv: {
keep_names: false,
},
},
});
fs.writeFileSync(
"./index.js",
`
export
default {
fetch() {
function sayHello() {
return "Hello World without keep_names";
}
return new Response(sayHello());
}
}
`
);

const underscoreUnderscoreNameRegex = /__name\(.*?\)/;

mockUploadWorkerRequest({
env: "testEnv",
expectedType: "esm",
legacyEnv: false,
expectedEntry: (str) => {
expect(str).not.toMatch(underscoreUnderscoreNameRegex);
},
});

mockSubDomainRequest();
await runWrangler("deploy -e testEnv index.js");
});
});

describe("durable object migrations", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
jsxFragment: config.build.jsxFactory,
tsconfig: config.build.tsconfig,
minify: config.build.minify,
keepNames: config.build.keepNames ?? true,
nodejsCompatMode: config.build.nodejsCompatMode,
define: config.build.define,
checkFetch: shouldCheckFetch(
Expand Down Expand Up @@ -246,6 +247,7 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
rules: config.build.moduleRules,
tsconfig: config.build?.tsconfig,
minify: config.build?.minify,
keepNames: config.build?.keepNames ?? true,
nodejsCompatMode: config.build.nodejsCompatMode,
define: config.build.define,
alias: config.build.alias,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ async function resolveConfig(
moduleRules: input.build?.moduleRules ?? getRules(config),

minify: input.build?.minify ?? config.minify,
keepNames: input.build?.keepNames ?? config.keep_names,
define: { ...config.define, ...input.build?.define },
custom: {
command: input.build?.custom?.command ?? config.build?.command,
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/api/startDevWorker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export interface StartDevWorkerInput {
alias?: Record<string, string>;
/** Whether the bundled worker is minified. Only takes effect if bundle: true. */
minify?: boolean;
/** Whether to keep function names after JavaScript transpilations. */
keepNames?: boolean;
/** Options controlling a custom build step. */
custom?: {
/** Custom shell command to run before bundling. Runs even if bundle. */
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ export const defaultWranglerConfig: Config = {
build: { command: undefined, watch_dir: "./src", cwd: undefined },
no_bundle: undefined,
minify: undefined,
keep_names: undefined,
dispatch_namespaces: [],
first_party_worker: undefined,
logfwdr: { bindings: [] },
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ interface EnvironmentInheritable {
*/
minify: boolean | undefined;

/**
* Keep function names after javascript transpilations.
*
* @default {true}
* @inheritable
*/
keep_names: boolean | undefined;

/**
* Designates this Worker as an internal-only "first-party" Worker.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,14 @@ function normalizeAndValidateEnvironment(
isBoolean,
undefined
),
keep_names: inheritable(
diagnostics,
topLevelEnv,
rawEnv,
"keep_names",
isBoolean,
undefined
),
first_party_worker: inheritable(
diagnostics,
topLevelEnv,
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
jsxFragment,
tsconfig: props.tsconfig ?? config.tsconfig,
minify,
keepNames: config.keep_names ?? true,
sourcemap: uploadSourceMaps,
nodejsCompatMode,
define: { ...config.define, ...props.defines },
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/deployment-bundle/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export type BundleOptions = {
watch: boolean | undefined;
tsconfig: string | undefined;
minify: boolean | undefined;
keepNames: boolean;
nodejsCompatMode: NodeJSCompatMode | undefined;
define: Config["define"];
alias: Config["alias"];
Expand Down Expand Up @@ -154,6 +155,7 @@ export async function bundleWorker(
watch,
tsconfig,
minify,
keepNames,
nodejsCompatMode,
alias,
define,
Expand Down Expand Up @@ -353,7 +355,7 @@ export async function bundleWorker(
bundle,
absWorkingDir: entry.projectRoot,
outdir: destination,
keepNames: true,
keepNames,
entryNames: entryName || path.parse(entryFile).name,
...(isOutfile
? {
Expand Down
3 changes: 3 additions & 0 deletions packages/wrangler/src/dev/use-esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function runBuild(
rules,
tsconfig,
minify,
keepNames,
nodejsCompatMode,
define,
alias,
Expand All @@ -68,6 +69,7 @@ export function runBuild(
alias: Config["alias"];
tsconfig: string | undefined;
minify: boolean | undefined;
keepNames: boolean;
nodejsCompatMode: NodeJSCompatMode | undefined;
noBundle: boolean;
findAdditionalModules: boolean | undefined;
Expand Down Expand Up @@ -148,6 +150,7 @@ export function runBuild(
watch: true,
tsconfig,
minify,
keepNames,
nodejsCompatMode,
doBindings: durableObjects.bindings,
workflowBindings: workflows,
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/pages/functions/buildPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function buildPluginFromFunctions({
routesModule,
outdir,
minify = false,
keepNames = true,
sourcemap = false,
watch = false,
onEnd = () => {},
Expand Down Expand Up @@ -45,6 +46,7 @@ export function buildPluginFromFunctions({
inject: [routesModule],
entryName: "index",
minify,
keepNames,
sourcemap,
watch,
// We don't currently have a mechanism for Plugins 'requiring' a specific compat date/flag,
Expand Down
6 changes: 6 additions & 0 deletions packages/wrangler/src/pages/functions/buildWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Options = {
outfile?: string;
outdir?: string;
minify?: boolean;
keepNames?: boolean;
sourcemap?: boolean;
fallbackService?: string;
watch?: boolean;
Expand All @@ -41,6 +42,7 @@ export function buildWorkerFromFunctions({
outfile = join(getPagesTmpDir(), `./functionsWorker-${Math.random()}.js`),
outdir,
minify = false,
keepNames = true,
sourcemap = false,
fallbackService = "ASSETS",
watch = false,
Expand Down Expand Up @@ -72,6 +74,7 @@ export function buildWorkerFromFunctions({
inject: [routesModule],
...(outdir ? { entryName: "index" } : { entryName: undefined }),
minify,
keepNames,
sourcemap,
watch,
nodejsCompatMode,
Expand Down Expand Up @@ -105,6 +108,7 @@ export type RawOptions = {
bundle?: boolean;
externalModules?: string[];
minify?: boolean;
keepNames?: boolean;
sourcemap?: boolean;
watch?: boolean;
plugins?: Plugin[];
Expand Down Expand Up @@ -133,6 +137,7 @@ export function buildRawWorker({
bundle = true,
externalModules,
minify = false,
keepNames = true,
sourcemap = false,
watch = false,
plugins = [],
Expand Down Expand Up @@ -160,6 +165,7 @@ export function buildRawWorker({
moduleCollector,
additionalModules,
minify,
keepNames,
sourcemap,
watch,
nodejsCompatMode,
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
jsxFragment,
tsconfig: props.tsconfig ?? config.tsconfig,
minify,
keepNames: config.keep_names ?? true,
sourcemap: uploadSourceMaps,
nodejsCompatMode,
define: { ...config.define, ...props.defines },
Expand Down
Loading