What may a minifier rename or remove in a production web build, and why does a check like `err.constructor.name === 'FareError'` start failing?
answer
- locals yes, contracts no
- names read by string survive
- Function.prototype.name follows the rename
- keep-names costs bytes
- console calls go only on request
basics
~20 sBy default a minifier renames local bindings, including function and class names, and strips whitespace, comments and usually debugger statements; it keeps property names and exported names. Renaming a class changes its name property, so name-based checks fail.
solid answer
~40 sBy default a minifier shortens names it can prove nothing outside reads: local variables, parameters, and function and class bindings inside a module. It leaves alone the names other code reaches by string or across a boundary: property names, a module's exported names, and globals in a classic script. Because `class FareError` may become `class e`, `err.constructor.name` turns into `'e'` and any check, registry or log keyed on the name breaks. Keep-names settings fix that either by not renaming functions and classes or by restoring `name` with a small helper, and both cost bytes; the sturdier fix is `instanceof` or an explicit `code` field. Renaming properties is a separate, very unsafe opt-in that breaks string-keyed access. Removing `debugger` statements is a common default, while dropping `console` calls is opt-in and removes their arguments too.
code
javascript · 16 lines// fares.js as written
export class FareError extends Error {}
export function describe(err) {
if (err.constructor.name === 'FareError') return 'fare problem';
return 'unknown';
}
// what a minifier could emit: local names shortened, export names kept
// class e extends Error {}
// function t(n) { return n.constructor.name === 'FareError' ? 'fare problem' : 'unknown'; }
// export { e as FareError, t as describe };
// a check that survives renaming
export function describeSafely(err) {
return err instanceof FareError ? 'fare problem' : 'unknown';
}go deeper
Remember that a minifier shortens local names but not properties or exports, and that a renamed class reports a different name at run time.
Explain why only names with fully visible uses are renamed, why constructor.name checks break, the two ways keep-names works, and what dropping console calls removes.
Show you would replace name-based checks with instanceof or explicit codes, keep side effects out of logs, and refuse property renaming unless a convention and a single minify pass make it safe.
Set the rule for shared build configuration: which renaming and dropping settings every app inherits, and how teams prove their code does not depend on names surviving the build.
## What a minifier does **Minification** removes data a program does not need in order to run: whitespace, comments and long identifiers. Shortening identifiers is called **mangling** or renaming. A minifier must not change what the program does, so it renames only what it can rename *consistently* and removes only what it was told is safe to remove. Removing unreachable or unused code is a related step, dead-code elimination, with its own rules; this answer is about renaming and dropping statements. ## What it renames, and what it keeps A minifier can rename a name only when it can see **every** use of it. That draws a clear line: | Name | Renamed by default? | Why | |---|---|---| | Local variables and parameters | yes | every use is inside one visible scope | | Function and class names declared inside a module or function | yes | same reason; their `name` property changes too | | Top-level names of an ES module | usually, in module mode | module scope is private to the file | | Top-level names of a classic script | no, unless asked | they are globals other scripts can read | | A module's exported names | no | importers depend on them | | Property names and object keys | no | they can be read by string, from JSON or by other code | | Names in a scope using `eval` or `with` | no, by default | the scope can be reached dynamically | The last row is a language rule rather than a build rule: `eval` and `with` make identifier resolution dynamic, so a minifier usually leaves those scopes alone. ## Why name checks break Every function and class has a `name` property, `Function.prototype.name`, taken from its declared name. Rename the binding and the property follows: - `class FareError extends Error {}` may be emitted as `class e extends Error {}`, so `new FareError().constructor.name` is `'e'`. - A check such as `err.constructor.name === 'FareError'` becomes false, and the error falls into the wrong branch. - Registries keyed by class name, serializers that write the class name, and logs that print `fn.name` all change the same way. There are two ways a build can keep names: 1. **Do not rename** functions and classes at all, keeping their full identifiers in the output. 2. **Rename them, then restore `name`** by calling a small helper that sets the property after each definition. Both cost bytes, so keep-names settings are usually off. The sturdier fix is in the code: test with `err instanceof FareError`, or give errors an explicit `code` property, so nothing depends on an identifier surviving the build. ## Property renaming: the dangerous opt-in Renaming **properties** saves more bytes but is off by default and described as very unsafe. It breaks whenever a property is reached by a string the minifier cannot see: - bracket access with a string, such as `obj['fare']`; - `hasOwnProperty('fare')`, `Object.defineProperty` and other reflection; - JSON received from or sent to a server; - code minified separately, such as another bundle or a third-party script. Where it is used at all, it is restricted to names matching a convention you control, such as a trailing underscore for internal fields, and to code minified together. ## What it may drop Beyond whitespace and comments, a minifier can remove some statements: - **Comments**: most are removed, while license comments are often kept by a convention such as a `/*!` prefix or an `@license` tag. - **`debugger` statements**: commonly removed by default in production builds. - **`console` calls**: removed only when you opt in, and sometimes only for chosen methods such as `log` and `info`. The **whole call** goes, arguments included, so `console.log(queue.shift())` stops removing an item from the queue. ## Safe habits 1. Never let behaviour depend on `fn.name`, `constructor.name` or a property surviving renaming. 2. Keep side effects out of logging calls. 3. Turn on keep-names only for the classes a framework or registry truly needs, and measure the cost. 4. Leave property renaming off unless every affected name follows a convention and all its code is minified together. Finally, test the **minified** build, not only the development one. Renaming and dropping happen only in production output, so a name-based check or a side effect inside a log call passes every development test and fails only after deployment. Running the end-to-end suite against the production bundle is the cheapest way to catch it.
- Why is property renaming far riskier than renaming locals?A local's every use is visible in one scope, so the minifier can rename them all consistently. A property can be reached through a string, such as `obj['fare']`, a JSON payload, `hasOwnProperty('fare')` or `Object.defineProperty`, or by code minified separately. Renaming it in one place and not another breaks silently, which is why it is opt-in and usually limited to a naming pattern you control.
- What does dropping console calls remove besides the log output?The whole call expression goes, arguments included, so anything evaluated inside the call disappears with it: `console.log(queue.shift())` stops removing an item in production. Keep side effects out of logging calls, or drop only specific methods such as `log` and `info` while keeping `warn` and `error`.
- A minifier renames object properties by default to save bytes.
- Minifying a library renames its exported functions for its consumers.
- Function.prototype.name is unaffected by minification.
- Keep-names settings are free, so always turn them on.
- Dropping console calls removes only the output; the arguments still run.