vuejs / vueify

Browserify transform for single-file Vue components
MIT License
1.17k stars 152 forks source link

Incorrect options object passed to babel from browserify cli #233

Open nolimitdev opened 6 years ago

nolimitdev commented 6 years ago

Vueify transform passes incorrect format of options object to babel...

$ browserify -t [ vueify --babel [ --presets [ env ] ] ] -e main.js -o build.js

ReferenceError: [BABEL] item.vue: Unknown option: foreign._. Check out http://babeljs.io/docs/usage/options/ for more information about options. A common cause of this error is the presence of a configuration options object without the corresponding preset name.

Vueify in vueify/lib/compilers/babel.js passes options to babel.transform(raw, options) in wrong format presets: { _: [ 'env' ] } which is created by minimist via subarg in browserify instead of correct format presets: [ 'env' ].

For example babelify transform works well with the similar command: $ browserify -t [ babelify --presets [ env ] ] -e main.js -o build.js ... this syntax comes directly from babelify doc at npmjs.com

In babelify it is working thanks to following code in babelify/index.js in Babelify.configure():

// browserify cli options
delete opts._;
if (opts.presets && opts.presets._) opts.presets = opts.presets._;

This issue can be fixed by adding following code in vueify/lib/compilers/babel.js:

// add this after line "`var babel = require('babel-core')"
var opts = compiler.options.babel
if (opts) {
  delete opts._
  // "--opt [ a b ]" and "--opt a --opt b" are allowed:
  if (opts.ignore && opts.ignore._) opts.ignore = opts.ignore._
  if (opts.only && opts.only._) opts.only = opts.only._
  if (opts.plugins && opts.plugins._) opts.plugins = opts.plugins._
  if (opts.presets && opts.presets._) opts.presets = opts.presets._
}

PR: https://github.com/vuejs/vueify/pull/235