Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mpeyper opened this issue Jan 8, 2021 · 2 comments · Fixed by #189
Closed

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

mpeyper opened this issue Jan 8, 2021 · 2 comments · Fixed by #189
Labels

Comments

@mpeyper
Copy link
Contributor

mpeyper commented Jan 8, 2021

  • 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.

@kentcdodds
Copy link
Owner

I'm happy with your suggestion of enabling that option during tests 👍

mpeyper added a commit to mpeyper/kcd-scripts that referenced this issue Jan 9, 2021
kentcdodds pushed a commit that referenced this issue Jan 10, 2021
* fix(babel): keep function names to improve stack traces in tests

Fixes #188

* fix(babel): remove minify babel plugin if minify env variable is false
@github-actions
Copy link

🎉 This issue has been resolved in version 7.5.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants