Support for opting out of minification for specific var names
Hey! I saw a few issues (e.g. https://github.com/evanw/esbuild/issues/818) related to this, but they all appear to be related to the .name property - I have a use case where I'm replacing a known variable name at runtime, e.g.
const createAPI = (injectedValues) => {
// ...
}
const MY_VALUE = {};
createAPI(MY_VALUE);
Then, in a service worker, when this script is requested, I swap out MY_VALUE for specific injected values.
I'm looking for a way to tell minify that MY_VALUE should not be renamed, but I do want everything else renamed normally.
I could use minifySyntax & minifyWhitespace to preserve all identifiers, but I'd rather just preserve the one if possible.
I see that esbuild supports /* @__PURE__ */ statements, perhaps something similar could be used to mark identifiers as non-minifiable?
I recommend using a global reference instead of a local identifier:
const createAPI = (injectedValues) => {
// ...
}
const myValue = MY_VALUE;
createAPI(myValue);
Global references are automatically preserved during minification without the need for anything custom like this.
I'm actually injecting these at runtime (via a service worker), not during the build - otherwise I would use a global! But in this case, I'm loading the (post-build) script as text, and doing a string substitution on the value.
I'm not sure if my use case of this potential feature could be relevant, but here it is.
I have a base class for many others in the app which uses constructor.name like the following:
export class BaseController {
constructor() {
this.logger = createLogger(`[${this.constructor.name}]`);
}
// ...
}
so every inheritor already has a branded this.logger instance.
Recently during output bundle optimization all the inheritors classes declarations along with their usages turned out to be in a common chunk and their class names got minified, logs became useless.
Disregarding obvious workarounds in my case, I'd appreciate some kind of directive for marking some specific symbols as such whose names will be preserved after minification.
Thanks!
Interested in this too. I want to extract a function's body via its .toString() method but preserve the identifiers for its parameters, so they can be declared as local variables in an enclosing scope.
const add_one = (id: number) => id + 1;
const body = add_one.toString().replace(/^.+?=>/, ""); // remove parameter list and arrow
const double_add = Function("arg", `const id = arg * 2; return ${body}`); // use `id`
But when minified, the runtime identifiers don't match the hard-coded string's variables.
EDIT: Just read bullet point 4, guess this isn't planned.