Skip to content

babel-plugin-minify-dead-code-elimination removes function names #188

Closed
@mpeyper

Description

@mpeyper
  • kcd-scripts version: 7.5.3
  • node version: 12.20.0
  • npm (or yarn) version: 6.14.8

Relevant code or config

export function createFunc1() {
  function func() {
  }
  return func
}

export function createFunc2() {
  function func() {
  }
  const name = func.name // not returned, just used
  return func
}

What you did:

Run kcd-scripts build

What happened:

The output is

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createFunc1 = createFunc1;
exports.createFunc2 = createFunc2;

function createFunc1() {
  return function () {};
}

function createFunc2() {
  function func() {}

  func.name; // not returned, just used

  return func;
}

Reproduction repository:

Babel REPL

Problem description:

The name of the function returned in createFunc1 gets removed thanks to babel-plugin-minify-dead-code-elimination. This is causing me problems in a node package (specifically a testing library) where the name gets removed and consequently does not appear in stack traces which hurts debugability when things go wrong.

I can understand the desire to remove this when building for browsers (reduced bundle size), but I don't personally see many advantages to this for a node/test package.

Suggested solution:

There is an option to keep function names in the plugin, so we could set that the be true and keep the rest of the functionality. We could use the isTest flag to conditionally set the option for tests only.

Similarly, we could use the isTest flag to remove the plugin all together for tests.

Either solution would suffice for my issue. I'm also happy to submit the PR for this change if you agree with it and can advise me which approach is your preference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions