https://github.com/bahmutov/node-hook
Run source transform function on node require call
https://github.com/bahmutov/node-hook
hook nodejs require
Last synced: 7 months ago
JSON representation
Run source transform function on node require call
- Host: GitHub
- URL: https://github.com/bahmutov/node-hook
- Owner: bahmutov
- License: other
- Created: 2014-01-11T22:20:10.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-07-24T16:29:45.000Z (almost 9 years ago)
- Last Synced: 2024-12-27T20:43:35.968Z (over 1 year ago)
- Topics: hook, nodejs, require
- Language: JavaScript
- Size: 21.5 KB
- Stars: 66
- Watchers: 2
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: MIT-license.md
Awesome Lists containing this project
README
# node-hook
> Run source transform function on Node require
[![NPM][node-hook-icon]][node-hook-url]
[![Build status][node-hook-ci-image]][node-hook-ci-url]
[![dependencies][node-hook-dependencies-image]][node-hook-dependencies-url]
[![devdependencies][node-hook-devdependencies-image]][node-hook-devdependencies-url]
[![semantic-release][semantic-image] ][semantic-url]
## Install and use
```sh
npm install --save node-hook
```
Before loading desired *.js* files, install hook
```js
var hook = require('node-hook');
function logLoadedFilename(source, filename) {
return 'console.log("' + filename + '");\n' + source;
}
hook.hook('.js', logLoadedFilename);
require('./dummy');
// prints fulle dummy.js filename, runs dummy.js
hook.unhook('.js'); // removes your own transform
```
**remember:** Nodejs caches compiled modules, so if the transform is not
working, you might need to delete the cached entry in `require.cache`,
then call `require(filename)` again to force reload.
Related: Node require replacement [really-need](https://github.com/bahmutov/really-need).
You can hook several transformers thanks to the [code](https://github.com/bahmutov/node-hook/pull/2)
submitted by [djulien](https://github.com/djulien)
## Existing transform
You can get the current transform and run any source through it. For example
to see how the current source looks when loaded but before evaluated
```js
const filename = resolve('./call-foo.js')
const transform = Module._extensions['.js']
const fakeModule = {
_compile: source => {
console.log('transformed code')
console.log(source)
}
}
transform(fakeModule, filename)
```
## Small print
Author: Gleb Bahmutov © 2013
* [Changelog](History.md)
* [@bahmutov](https://twitter.com/bahmutov)
* [glebbahmutov.com](http://glebbahmutov.com)
* [blog](http://glebbahmutov.com/blog/)
License: [MIT](MIT-license.md) - do anything with the code,
but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
[node-hook-icon]: https://nodei.co/npm/node-hook.svg?downloads=true
[node-hook-url]: https://npmjs.org/package/node-hook
[node-hook-ci-image]: https://travis-ci.org/bahmutov/node-hook.svg?branch=master
[node-hook-ci-url]: https://travis-ci.org/bahmutov/node-hook
[node-hook-dependencies-image]: https://david-dm.org/bahmutov/node-hook.svg
[node-hook-dependencies-url]: https://david-dm.org/bahmutov/node-hook
[node-hook-devdependencies-image]: https://david-dm.org/bahmutov/node-hook/dev-status.svg
[node-hook-devdependencies-url]: https://david-dm.org/bahmutov/node-hook#info=devDependencies
[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-url]: https://github.com/semantic-release/semantic-release