3.0.0 • Published 5 years ago

require-modify v3.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

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 = function () {
	console.log('hello');
};
var requireModify = require('require-modify');

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

greet();
//=> yo

Example 2

Expose a local variable.

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

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

greet();
//=> hello

API

requireModify(moduleId, callback)

moduleId

Required
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