GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - bahmutov/node-hook: Run source transform function on node require call

BranchesTags
Open more actions menu

Latest commit

 

History

43 Commits
43 Commits

Folders and files

NameName
Last commit message
Last commit date
test
test
 
 
.gitignore
.gitignore
 
 
.npmrc
.npmrc
 
 
.travis.yml
.travis.yml
 
 
DockerTemplate
DockerTemplate
 
 
MIT-license.md
MIT-license.md
 
 
README.md
README.md
 
 
index.js
index.js
 
 
package.json
package.json
 
 

Repository files navigation

node-hook

Run source transform function on Node require

NPM

Build status dependencies devdependencies semantic-release

Install and use

npm install --save node-hook

Before loading desired .js files, install hook

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.

You can hook several transformers thanks to the code submitted by 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

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
  • @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

About

Run source transform function on node require call

Topics

hooknodejsrequire

Resources

Readme
License
Activity

Stars

66 stars

Watchers

1 watching

Forks

7 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.