0.0.1 • Published 6 years ago

babel-plugin-require-hook v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

babel-plugin-require-hook

Build Status

A babel plugin to call custom function when babel requires a module.

for more things about writing babel plugin see babel-handbook

Usage

$ npm install --save-dev @babel/core babel-plugin-require-hook

Specify the plugin in your .babelrc with the file that exports the hook function.

{
  "plugins": [["require-hook", { "hook": "hook.js" }]]
}
// or configure babel with javascript is ok
// babel.config.js
module.exports = api => {
  return {
    plugins: [['require-hook', { hook(src, file, state) {} }]]
  };
};

The hook function will be called when mets require.

// hook.js
module.exports = function(src, file, state) {
  if (src === 'react') {
    return 'preact';
  }
};
// require('react') will replace to be preact;
const react = require('react');
// const react = require('preact');

These followings will be processd too.

require.resolve('react');
import('react');
export * from 'react';

License

MIT

0.0.1

6 years ago