babel / babelify

Browserify transform for Babel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

If I don't specify any Babel plugins or presets, what exactly should happen?

cagross opened this issue · comments

What exactly will happen if I run Babelify with no presets or plugins specified, e.g. this command:

node_modules/.bin/browserify file-a.js file-b.js -t [ babelify ] --outfile outfile.js

The documentation seems to say that Babelify will 'not do anything.' I say that because

  • The Babelify GitHub repo says, "For babelify to be useful, you must also include some presets and/or plugins."
  • The Babel plugins page says, "Now, out of the box Babel doesn't do anything."

The reason I ask is because in my case, when I run the above Browserify command, it seems to do something. I will explain below.

I created two test JS files: file-a.js and file-b.js. Here are the contents of each:

  • file-a.js contents

const testFile = require('../www/js/functions.js')

  • file-b.js contents

import 'regenerator-runtime/runtime'

From the command line, if I execute:

node file-a.js

it fails with:

import 'regenerator-runtime/runtime'
^^^^^^
SyntaxError: Cannot use import statement outside a module

Then, I bundle both files with the following Browserify command with a Babelify transform (note there are no plugins/presets explicitly specified):

node_modules/.bin/browserify file-a.js file-b.js -t [ babelify ] --outfile outfile.js

After, I run the command:

node outfile.js

it now completes without error. So bundling both files with Browserify, with a Babelify transform, and omitting plugins/presets, seemed to resolve the issue.

So what is happening in my case? If I omit plugins/presets, are there default plugins that are used? Or am I misunderstanding something?


Note

--If I remove the Babelify transform from the Browserify command, then run node outfile.js it fails with:

C:\Users\snarl\Orbis\osatest\www\js\functions.js:3

import 'regenerator-runtime/runtime'
^

ParseError: 'import' and 'export' may appear only with 'sourceType: module'*

--I am using Browserify v16.5.1 and Babelify v10.0.0.

Thanks.

It sounds like there's a babel config file somewhere. Plugins specified on the command line are combined with the plugins specified in the nearest babel config file.

OK thanks for that info. Here's what I found in terms of Babel config files:

  • In the root of my project is a file babel.config.json, which contains only:
{
    "presets": [
        "@babel/preset-env"
    ]
}

So is this to say that in my case, when Babel carries out its transform, it uses at least this particular preset? It sounds like 'yes' :-)

  • In my Windows home directory for this particular user is the file .babel.json. It is rather long, and there is a plugins key which appears twice in the file. But I'm not sure how to interpret the value of this key. Here is a pastebin . Is it relevant in this case? If so, what is it telling us about the Babel transform? In the same file, the presets key has a blank array as a value.

  • In my project's package.json there are no babel keys.

~/.babel.json I think is some kind of cache file. babel.config.json is the one that will be picked up by babelify or any other babel tool. https://babeljs.io/docs/en/config-files

OK thanks. So in babel.config.json let's say I remove that presets key. What should then occur if I execute the same browserify command?

The files are still passed through babel, but without any plugins, so it will just output the same code as you put in (save for formatting changes).

OK thanks for all the info--I appreciate it. We can consider this resolved.