Build and use the Babel plugin without making it an npm module

In my project I use Babel 6 with the required hook. I need to download the custom babel plugin that I wrote. But do I really need to publish my plugin using npm first and then include the plugin name in my main .babelrc project?

Is there a way to directly load the plugin code? In other words, can I just download the following directly?

 export default function({types: t }) { return { visitor: { ... } }; } 
+11
source share
2 answers

When you specify your plugins in your .babelrc, specify the path to the plugin instead of the standard published plugin name.

 "plugins": ["transform-react-jsx", "./your/plugin/location"] 

When exporting your plugin function, you probably have to use module.exports = instead of export default , as ES2015 modules have not yet been fully implemented in Node.

+19
source

This is the entire babel.config.js file babel.config.js .

 module.exports = function (api) { api.cache(true); const presets = ["@babel/preset-env", "@babel/preset-react"]; const plugins = [ ["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }], "c:\\projects\\my-babel-plugin" ]; return { presets, plugins }; } 

The first element in the array of plugins is a plugin with options in the form of an array. The second point is my own local plugin.

Inside my-babel-plugin should be package.json with the entry "main", usually "main": "lib/index.js" or "main": "src/index.js" .

0
source

Source: https://habr.com/ru/post/1246282/


All Articles