babelify icon indicating copy to clipboard operation
babelify copied to clipboard

usage of babel/polyfills

Open br4nnigan opened this issue 6 years ago • 4 comments

so if I use browserify like this:

browserify src/test2.js -o dist/js/test.js -t [ babelify --presets [ @babel/preset-env ] ]

it is completely ignored that I want the polyfill to be used.

here's my .babelrc

{
  "presets": [
    ["@babel/preset-env", {
      "useBuiltIns": "usage",
      "loose": true,
      "modules": false,
      "targets": {
        "ie": "8"
      }
    }]
  ]
}

If I ommit the --presets from cli then @babel/preset-env will not be pulled from the .babelrc which I find strange, so maybe babelify ignores that file completely? But how to pass options via the cli then?

now if I just use babel directly like this:

node_modules\.bin\babel src/test2.js --out-file dist/js/test.js

the output file will actually kind of try to use the polyfill but use import statements like this, which makes it hardly usable:

import "core-js/modules/es6.string.includes";

I just need both, conversion if es6 imports AND injection of needed polyfills

br4nnigan avatar Feb 19 '19 21:02 br4nnigan

nasty workaround: I run both, first the babel command, then the browserify

node_modules\\.bin\\babel src/js/script.js --out-file src/js/script_babel.js & browserify src/js/script_babel.js -o dist/js/script.js -t [ babelify --presets [ @babel/preset-env ] ] -p [ tinyify ] & del src\\js\\script_babel.js

br4nnigan avatar Mar 11 '19 08:03 br4nnigan

if you set "modules": "commonjs", babel should transform the import statements to commonjs require.

goto-bus-stop avatar Mar 11 '19 09:03 goto-bus-stop

Well the babel command does indeed do that, but it doesn't solve the problem. I'd still have to browserify the bundle afterwards. And if I'm not completely mistaken, babelify should do exactly that: include babel in the browserify process.

And when I run browserify anyway I wouldn't even need the commonjs setting in babelrc. It works with the es6 modules too.

As I said, the browserify command with babelify transform seems to ignore .babelrc because the polyfills needed for my specified target are not added. This would also explain why I cannot ommit the "preset" parameter in the command which could/should be pulled from the .babelrc

Thanks for the input though.

br4nnigan avatar Mar 11 '19 11:03 br4nnigan

@br4nnigan, I had a similar problem.

The babel transform via babelify worked, i.e. arrow functions and templates were replaced, but promises were still in the output. (I am not using a .babelrc file.)

Eventually, this SO answer solved my problem. I had to add the transform-es2015-classes plugin on top of the presets. My final browserify line (in package.json) looks like this: "browserify": "browserify src/js/main.js -t [ babelify --presets [ @babel/preset-env ] --plugins [ transform-es2015-classes ] ] -o dist/js/main.min.js"

I also had to install core-js and add a require to my main.js, in my case require('core-js/modules/es.promise');

helzich avatar Nov 01 '19 08:11 helzich