Modify the source of a required module
JavaScript
Latest commit 444f0f1 Oct 5, 2015 @sindresorhus tweaks
Permalink
Failed to load latest commit information.
.editorconfig tweaks Oct 5, 2015
.gitattributes init Jan 1, 2014
.gitignore init Jan 1, 2014
.jshintrc tweaks Aug 13, 2014
.travis.yml tweaks Oct 5, 2015
fixture.js init Jan 1, 2014
index.js init Jan 1, 2014
license tweaks Aug 13, 2014
package.json tweaks Oct 5, 2015
readme.md tweaks Oct 5, 2015
test.js tweaks Aug 13, 2014

readme.md

require-modify Build Status

Modify the source of a required module

Install

$ npm install --save require-modify

Usage

Replace some text.

// greet.js
module.exports = () => {
    console.log('hello');
};
const requireModify = require('require-modify');

const greet = requireModify('./greet', source => {
    return source.replace('hello', 'yo');
});

greet();
//=> 'yo'

Example 2

Expose a local variable.

// greet.js
const greet = () => {
    console.log('hello');
};
const requireModify = require('require-modify');

const greet = requireModify('./greet', source => {
    return source + ';module.exports = greet;';
    // the leading semicolon is in case the user has
    // forgotten a semicolon on the last statement
});

greet();
//=> 'hello'

API

requireModify(moduleId, [callback])

moduleId

Type: string

Same as you would use in require().

callback(source)

Type: function

Callback where you can modify the source and return the new one.

License

MIT © Sindre Sorhus