Run source transform function on Node require
npm install 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]
``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,require(filename)
then call again to force reload.
Related: Node require replacement really-need.
You can hook several transformers thanks to the code
submitted by djulien
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)
Author: Gleb Bahmutov © 2013
* Changelog
* @bahmutov
* glebbahmutov.com
* blog
License: MIT - 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