question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

transform-arrow-functions being ignored for external packages?

See original GitHub issue

Hi!

I’m trying to babelify a file to transpile arrow functions (because those don’t work in IE). Here’s my setup:

.babelrc

The targets includes IE11, which requires arrow function transpiling.

{
  "presets": [
    ["@babel/preset-env",{
      "targets": "> 0.25%, not dead",
      "debug": true
    }],
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

index.js

// The arrow functions in here do not get transpiled
import queryString from "query-string";

const a = {
  one: 1,
  five: 5
};

// this gets transpiled
const printQs = () => {
  console.log("qs", queryString.stringify(a));
};

printQs();

Browserify command

$ browserify index.js -o output.js -t babelify

Inside the minimal index.js file, I’ve included the query-string package, because I know that the package exports code that contains arrow functions: https://github.com/sindresorhus/query-string/blob/master/index.js#L8

This file is bundled correctly and the console.log has the correct output. The problem is that the output file contains arrow functions from the query-string package.

The debug output is including transform-arrow-functions. It seems to be running on the application code, because printQs from index.js is transpiled. However, output.js has the line return (key, value, index) => { from https://github.com/sindresorhus/query-string/blob/master/index.js#L8 which contains an arrow function.

Is this expected behavior? How can I transpile the exported code from query-string in my bundle?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lambert-velircommented, Jan 8, 2019

As a follow up, it seems that babelify doesn’t automatically load .babelrc anymore when you pass options as the 2nd argument to the transform like this:

browserify()
  .transform(babelify, {
    global: true
  });

As a work around, I used the find-babel-config package like this:

const findBabelConfig = require("find-babel-config");

const babelConfig = findBabelConfig.sync(__dirname);

browserify()
  .transform(babelify, {
    ...babelConfig.config,
    global: true
  });

cc: @goto-bus-stop

1reaction
goto-bus-stopcommented, Oct 6, 2018

Yes, Browserify runs transforms on application code only by default. There are some pointers for solving this use case in the readme here https://github.com/babel/babelify/blob/master/README.md#why-arent-files-in-node_modules-being-transformed and in #268

Read more comments on GitHub >

github_iconTop Results From Across the Web

Arrow Function syntax not working with webpack?
In my application, this issue was caused by less-loader mistakenly added as dependent package instead of devDependency.
Read more >
@babel/plugin-transform-arrow-functions - Package Manager
Fast, reliable, and secure dependency management.
Read more >
babel/plugin-transform-arrow-functions
Wrap the generated function in .bind(this) and keeps uses of this inside the function as-is, instead of using a renamed this .
Read more >
babel/plugin-transform-arrow-functions not working in webpack
@babel/plugin-transform-arrow-functions is not transforming arrow functions when used with webpack (babel-loader). This causes the output JS ...
Read more >
Arrow Functions - Manual - PHP
Arrow functions use by-value variable binding. This is roughly equivalent to performing a use($x) for every variable $x used inside the arrow function....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found