Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How to add default options to babel plugin's visitor?

I have been following the babel-handbook to update a plugin for a library. Using the handbook's code as an example, my current plugin follows this template:

export default function({ types: t }) {
  return {
    visitor: {
      FunctionDeclaration(path, state) {
        const color = state.opts.color
        // Do something
      }
    }
  }
}

However To prevent the changes from being breaking, I need to set some default value for the state options. Does Babel have a way of doing that? Or would I need to resort to something like below?

export default function({ types: t }) {
  return {
    visitor: {
      FunctionDeclaration(path, state) {
        const color = state.opts.color || "Green"
        // Do something
      }
    }
  }
}

Edit: Ideally neither of these would result in an error.

import { transform } from '@babel/core'

const result = transform(code, {
  plugins: ['@babel/plugin-syntax-jsx', myCustomPlugin]
})

console.log(result.code)

const result = transform(code, {
  plugins: ['@babel/plugin-syntax-jsx', [myCustomPlugin, {
    color: 'red'
  }]]
})

console.log(result.code)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!