3
Codemod with babel
source link: https://lihautan.com/codemod-with-babel/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Codemod with babel
A general template that I used:
const babel = require('@babel/core');
const { promisify } = require('util');
const { writeFile } = promisify(require('fs').writeFile);
(async function() {
const { code } = await babel.transformFileAsync(filename, {
plugins: [
function() {
return {
manipulateOptions(opts, parserOpts) {
/*
add to parserOpts.plugins to enable the syntax
eg:
jsx, flow, typescript, objectRestSpread, pipelineOperator,
throwExpressions, optionalChaining, nullishCoalescingOperator,
exportDefaultFrom, dynamicImport, ...
*/
parserOpts.plugins.push(
'classProperties',
'classPrivateProperties'
);
},
visitor: {
// fill in a transformer here
},
};
},
],
});
await writeFile(filename, code, 'utf-8');
})();
[Updated on 2019-09-13]
I have written a step-by-step guide on how to write a babel transformation plugin.
The only difference in this template than the guide is that there's this manipulateOptions
where you can add additional parser options to enable ES2015+ syntax.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK