Answer the question
In order to leave comments, you need to log in
How to connect/use self-written babel-plugin?
I wrote a babel plugin that assigns the number 777
to all variables named test .
const babel = require('@babel/core');
const code = `const test = 'text'`;
const output = babel.transformSync(code, {
plugins: [
function myCustomPlugin() {
return {
visitor: {
Identifier(path) {
if (path.isIdentifier({ name: 'test' })) {
path.parentPath.get('init').node.value = 777
}
},
},
};
},
],
});
Answer the question
In order to leave comments, you need to log in
First of all. you need to issue it as a plugin:
https://github.com/jamiebuilds/babel-handbook/blob...
export default function({ types: t }) {
return {
visitor: {
Identifier(path) {
if(path.isIdentifier({ name: 'test' })) {
path.parentPath.get('init').node.value = 777
}
}
}
};
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question