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)