Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow intercept of transitive require calls files. #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@

var Module = module.constructor;
var path = require('path');
var assert = require('assert');

module.exports = function requireFromString(code, filename, opts) {
module.exports = requireFromString;
module.exports.load = load;

function requireFromString(code, filename, opts) {
if (typeof filename === 'object') {
opts = filename;
filename = undefined;
}

if (typeof code !== 'string') {
throw new Error('code must be a string, not ' + typeof code);
}

var m = load(filename, opts);

m._compile(code, filename);

return m.exports;
}

function load(filename, opts) {
if (typeof filename === 'object') {
opts = filename;
filename = undefined;
Expand All @@ -14,16 +35,20 @@ module.exports = function requireFromString(code, filename, opts) {
opts.appendPaths = opts.appendPaths || [];
opts.prependPaths = opts.prependPaths || [];

if (typeof code !== 'string') {
throw new Error('code must be a string, not ' + typeof code);
}

var paths = Module._nodeModulePaths(path.dirname(filename));

var m = new Module(filename, module.parent);

var requireHook = opts.require;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require option should be described in readme.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, for sure.

Still a work in progress. (which I now realize I did not mention).


if (requireHook) {
m.require = function (path) {
assert(typeof path === 'string', 'path must be a string');
assert(path, 'missing path');
return requireHook.call(this, path);
}
}
m.filename = filename;
m.paths = [].concat(opts.prependPaths).concat(paths).concat(opts.appendPaths);
m._compile(code, filename);

return m.exports;
};
return m;
}
2 changes: 2 additions & 0 deletions test/fixture/depth0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = require('./depth1');
1 change: 1 addition & 0 deletions test/fixture/depth1/depth2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'FOO - depth2';
1 change: 1 addition & 0 deletions test/fixture/depth1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./depth2');
3 changes: 3 additions & 0 deletions test/fixture/greet-james.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function() {
return 'Hello ' + require('./james');
};
1 change: 1 addition & 0 deletions test/fixture/james.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'James';
55 changes: 49 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ var fs = require('fs');
var path = require('path');
var requireFromString = require('../');

function getFixture(file) {
file = path.join(__dirname, 'fixture', file);
var code = fs.readFileSync(file, 'utf8');

return {file: file, code: code};
}

it('should accept only string as code', function () {
assert.throws(function () {
requireFromString();
Expand All @@ -24,18 +31,16 @@ it('should accept filename', function () {
});

it('should work with relative require in file', function () {
var file = path.join(__dirname, '/fixture/module.js');
var code = fs.readFileSync(file, 'utf8');
var result = requireFromString(code, file);
var fixture = getFixture('module.js');
var result = requireFromString(fixture.code, fixture.file);

assert.ok(result);
assert.ok(module === result.parent.parent);
});

it('should have appended and preppended paths', function () {
var file = path.join(__dirname, '/fixture/submodule.js');
var code = fs.readFileSync(file, 'utf8');
var result = requireFromString(code, file, {
var fixture = getFixture('submodule.js');
var result = requireFromString(fixture.code, fixture.file, {
appendPaths: ['append'],
prependPaths: ['prepend']
});
Expand All @@ -44,3 +49,41 @@ it('should have appended and preppended paths', function () {
assert.equal(result.paths.indexOf('append'), result.paths.length - 1);
assert.equal(result.paths.indexOf('prepend'), 0);
});

it('should allow modification of other required modules via callback', function () {
var fixture = getFixture('greet-james.js');
var Module = module.constructor;

function transform(code) {
return code.replace('James', 'Jim');
}

function requireHook(path) {
var file = Module._resolveFilename(path, this);
var code = fs.readFileSync(file, 'utf8');
return requireFromString(transform(code), file, {require: requireHook});
}

var result = requireFromString(fixture.code, fixture.file, {require: requireHook});

assert.equal(result(), 'Hello Jim');
});

it('transforms should be doable even as relative directory changes', function () {
var fixture = getFixture('depth0.js');
var Module = module.constructor;

function transform(code) {
return code.replace('FOO', 'BAR');
}

function requireHook(path) {
var file = Module._resolveFilename(path, this);
var code = fs.readFileSync(file, 'utf8');
return requireFromString(transform(code), file, {require: requireHook});
}

var result = requireFromString(fixture.code, fixture.file, {require: requireHook});

assert.equal(result, 'BAR - depth2');
});