nameless
nameless

Reputation: 2375

How to get option of babel.transformFile in a babel plugin

I want to write a babel plugin, and there is a transfromFileSync call in my plugin. I need to get the options of transformFileSync. How to do it?

// run transformFile
var babel = require('babel-core');
var path = require('path');
var options = {
  plugins: [path.resolve('./plugin.js')],
  presets: ['es2015']
};
babel.transformFile('./test.js', options);
// plugin.js
module.exports = function (babel) {
  return {
    Program: function () {
      // how to get babel options here

    }
  }
}

Upvotes: 2

Views: 602

Answers (1)

黄宗权
黄宗权

Reputation: 21

You can't get the babel options. But you can get your plugin options in the second param of visitor like:

export default function({ types: t }) {
  return {
    visitor: {
      FunctionDeclaration(path, state) {
        console.log(state.opts);
      }
    }
  }
}

Upvotes: 1

Related Questions